correct misleading error for modules (#300)

This commit is contained in:
Mark Thom
2020-04-12 12:51:59 -06:00
parent 2ce4f602c0
commit 28099b9812
6 changed files with 106 additions and 22 deletions

View File

@@ -377,6 +377,19 @@ pub enum ModuleSource {
File(ClauseName),
}
impl ModuleSource {
pub fn as_functor_stub(&self) -> MachineStub {
match self {
ModuleSource::Library(ref name) => {
functor!("library", [clause_name(name.clone())])
}
ModuleSource::File(ref name) => {
functor!(clause_name(name.clone()))
}
}
}
}
pub type ScopedPredicateKey = (ClauseName, PredicateKey); // module name, predicate indicator.
#[derive(Clone)]

View File

@@ -607,7 +607,13 @@ fn load_library(
&listing_src,
)
}
None => Err(SessionError::ModuleNotFound)
None => {
let err = ExistenceError::SourceSink(ModuleSource::Library(
name.clone()
));
Err(SessionError::ExistenceError(err))
}
}
}
@@ -693,7 +699,11 @@ impl ListingCompiler {
Ok(wam_indices.insert_module(submodule))
} else {
Err(SessionError::ModuleNotFound)
let err = ExistenceError::SourceSink(ModuleSource::File(
module_name,
));
Err(SessionError::ExistenceError(err))
}
}
@@ -727,7 +737,11 @@ impl ListingCompiler {
Ok(wam_indices.insert_module(submodule))
} else {
Err(SessionError::ModuleNotFound)
let err = ExistenceError::SourceSink(ModuleSource::File(
module_name
));
Err(SessionError::ExistenceError(err))
}
}
@@ -1057,7 +1071,11 @@ impl ListingCompiler {
insert_or_refresh_term_dir_quantum(term_dir, key, term_dirs);
}
None => {
return Err(SessionError::ModuleNotFound);
let err = ExistenceError::SourceSink(ModuleSource::File(
module_name,
));
return Err(SessionError::ExistenceError(err));
}
}
}
@@ -1392,14 +1410,18 @@ pub(super) fn setup_indices(
module: ClauseName,
indices: &mut IndexStore,
) -> Result<(), SessionError> {
if let Some(module) = wam.indices.take_module(module) {
if let Some(module) = wam.indices.take_module(module.clone()) {
let flags = wam.machine_flags();
let result = indices.use_module(&mut wam.code_repo, flags, &module);
wam.indices.insert_module(module);
result
} else {
Err(SessionError::ModuleNotFound)
let err = ExistenceError::SourceSink(ModuleSource::Library(
module
));
Err(SessionError::ExistenceError(err))
}
}

View File

@@ -1,6 +1,6 @@
use prolog_parser::ast::*;
use crate::prolog::forms::{Number, PredicateKey};
use crate::prolog::forms::{ModuleSource, Number, PredicateKey};
use crate::prolog::machine::heap::*;
use crate::prolog::machine::machine_indices::*;
use crate::prolog::machine::machine_state::*;
@@ -207,7 +207,7 @@ impl MachineError {
ExistenceError::Module(name) => {
let stub = functor!(
"existence_error",
[atom("module"), clause_name(name)]
[atom("source_sink"), clause_name(name)]
);
MachineError {
@@ -235,6 +235,21 @@ impl MachineError {
from: ErrorProvenance::Constructed,
}
}
ExistenceError::SourceSink(source) => {
let source_stub = source.as_functor_stub();
let stub = functor!(
"existence_error",
[atom("source_sink"), aux(h, 0)],
[source_stub]
);
MachineError {
stub,
location: None,
from: ErrorProvenance::Constructed,
}
}
ExistenceError::Stream(culprit) => {
let stub = functor!(
"existence_error",
@@ -301,8 +316,8 @@ impl MachineError {
pub(super)
fn session_error(h: usize, err: SessionError) -> Self {
match err {
SessionError::CannotOverwriteBuiltIn(pred_str)
| SessionError::CannotOverwriteImport(pred_str) => {
SessionError::CannotOverwriteBuiltIn(pred_str) |
SessionError::CannotOverwriteImport(pred_str) => {
Self::permission_error(
h,
Permission::Modify,
@@ -310,6 +325,9 @@ impl MachineError {
functor!(clause_name(pred_str)),
)
}
SessionError::ExistenceError(err) => {
Self::existence_error(h, err)
}
SessionError::InvalidFileName(filename) => {
Self::existence_error(h, ExistenceError::Module(filename))
}
@@ -321,14 +339,6 @@ impl MachineError {
functor!("module_does_not_contain_claimed_export"),
)
}
SessionError::ModuleNotFound => {
Self::permission_error(
h,
Permission::Access,
"private_procedure",
functor!("module_does_not_exist"),
)
}
SessionError::NamelessEntry => {
Self::permission_error(
h,
@@ -685,15 +695,16 @@ impl MachineState {
pub enum ExistenceError {
Module(ClauseName),
Procedure(ClauseName, usize),
SourceSink(ModuleSource),
Stream(Addr),
}
pub enum SessionError {
CannotOverwriteBuiltIn(ClauseName),
CannotOverwriteImport(ClauseName),
ExistenceError(ExistenceError),
InvalidFileName(ClauseName),
ModuleDoesNotContainExport(ClauseName, PredicateKey),
ModuleNotFound,
NamelessEntry,
OpIsInfixAndPostFix(ClauseName),
QueryCannotBePostedAsGoal,

View File

@@ -288,7 +288,11 @@ impl Machine {
Ok(self.indices.insert_module(module))
} else {
Err(SessionError::ModuleNotFound)
let err = ExistenceError::SourceSink(ModuleSource::File(
clause_name!("$toplevel"),
));
Err(SessionError::ExistenceError(err))
}
}

View File

@@ -295,7 +295,7 @@ use_module(Module) :-
use_module(Module, QualifiedExports) :-
( nonvar(Module) ->
( list_si(QualifiedExports) ->
maplist('$module_export'(use_module/2), QualifiedExports) ->
maplist('$module_export'(use_module/2), QualifiedExports) ->
( Module = library(Filename) ->
'$use_qualified_module'(Filename, QualifiedExports)
; atom(Module) ->

View File

@@ -289,6 +289,9 @@ impl fmt::Display for IndexingInstruction {
impl fmt::Display for SessionError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&SessionError::ExistenceError(ref err) => {
write!(f, "{}", err)
}
&SessionError::CannotOverwriteBuiltIn(ref msg) => {
write!(f, "cannot overwrite {}", msg)
}
@@ -298,7 +301,6 @@ impl fmt::Display for SessionError {
&SessionError::InvalidFileName(ref filename) => {
write!(f, "filename {} is invalid", filename)
}
&SessionError::ModuleNotFound => write!(f, "module not found."),
&SessionError::ModuleDoesNotContainExport(ref module, ref key) => {
write!(
f,
@@ -324,6 +326,38 @@ impl fmt::Display for SessionError {
}
}
impl fmt::Display for ExistenceError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&ExistenceError::Module(ref module_name) => {
write!(f, "the module {} does not exist", module_name)
}
&ExistenceError::Procedure(ref name, arity) => {
write!(f, "the procedure {}/{} does not exist", name, arity)
}
&ExistenceError::SourceSink(ref module_source) => {
write!(f, "the source/sink {} does not exist", module_source)
}
&ExistenceError::Stream(ref addr) => {
write!(f, "the stream at {} does not exist", addr)
}
}
}
}
impl fmt::Display for ModuleSource {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&ModuleSource::File(ref file) => {
write!(f, "at the file {}", file)
}
&ModuleSource::Library(ref library) => {
write!(f, "at library({})", library)
}
}
}
}
impl fmt::Display for Line {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {