]> Repositorios git - scryer-prolog.git/commitdiff
correct misleading error for modules (#300)
authorMark Thom <[email protected]>
Sun, 12 Apr 2020 18:51:59 +0000 (12:51 -0600)
committerMark Thom <[email protected]>
Sun, 12 Apr 2020 18:51:59 +0000 (12:51 -0600)
src/prolog/forms.rs
src/prolog/machine/compile.rs
src/prolog/machine/machine_errors.rs
src/prolog/machine/mod.rs
src/prolog/toplevel.pl
src/prolog/write.rs

index ece57a04a401789a4b71ded339cbc6c8c653a720..95af1e8ea4d3d57dff1a4a0df86b16be024b2df4 100644 (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)]
index 03359b58680fd9a4ba66c84eedd623bede187c95..aeaff98b7cb176b8fa696856f07fa19e987e6745 100644 (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))
     }
 }
 
index 1e09b2bb246326761e09721a9205eafc52739e43..85bde23da12b8172185b336e96af17c276ded3c8 100644 (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,
index 06d80ba1fe8fcd9e768a0bd6d50cf01779f68c37..e176229e12101482ddbe7668f2d649116ab5f0ce 100644 (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))
         }
     }
 
index 4f037c9a7c8767b579dd1fdffb56b8919227fc80..55b665c90f55f6bd53dd22c7bff67082fe9ae0c8 100644 (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) ->
index d83b848d6bb0b463f4dad69a72ccf34b14f946d0..129d480cae1303b8544cb64d51de3defec042c1e 100644 (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 {