From: Mark Thom Date: Sun, 12 Apr 2020 18:51:59 +0000 (-0600) Subject: correct misleading error for modules (#300) X-Git-Tag: v0.8.119~9 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=28099b9812933f059086c0502bc864852ce8fe88;p=scryer-prolog.git correct misleading error for modules (#300) --- diff --git a/src/prolog/forms.rs b/src/prolog/forms.rs index ece57a04..95af1e8e 100644 --- a/src/prolog/forms.rs +++ b/src/prolog/forms.rs @@ -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)] diff --git a/src/prolog/machine/compile.rs b/src/prolog/machine/compile.rs index 03359b58..aeaff98b 100644 --- a/src/prolog/machine/compile.rs +++ b/src/prolog/machine/compile.rs @@ -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)) } } diff --git a/src/prolog/machine/machine_errors.rs b/src/prolog/machine/machine_errors.rs index 1e09b2bb..85bde23d 100644 --- a/src/prolog/machine/machine_errors.rs +++ b/src/prolog/machine/machine_errors.rs @@ -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, diff --git a/src/prolog/machine/mod.rs b/src/prolog/machine/mod.rs index 06d80ba1..e176229e 100644 --- a/src/prolog/machine/mod.rs +++ b/src/prolog/machine/mod.rs @@ -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)) } } diff --git a/src/prolog/toplevel.pl b/src/prolog/toplevel.pl index 4f037c9a..55b665c9 100644 --- a/src/prolog/toplevel.pl +++ b/src/prolog/toplevel.pl @@ -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) -> diff --git a/src/prolog/write.rs b/src/prolog/write.rs index d83b848d..129d480c 100644 --- a/src/prolog/write.rs +++ b/src/prolog/write.rs @@ -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 {