]> Repositorios git - scryer-prolog.git/commitdiff
throw exceptions when modules do not contain claimed exports
authorMark Thom <[email protected]>
Tue, 9 Feb 2021 19:17:34 +0000 (12:17 -0700)
committerMark Thom <[email protected]>
Tue, 9 Feb 2021 19:17:34 +0000 (12:17 -0700)
src/machine/load_state.rs
src/machine/loader.rs
src/machine/machine_errors.rs
src/write.rs

index ebca8518fc91eb63532027aa407ec6a40eeac25f..8ed61cefdeee8211d1cbe58e81f7a9b6a0b1b8fc 100644 (file)
@@ -80,7 +80,6 @@ fn add_op_decl_as_module_export(
         }
         None => {
             retraction_info.push_record(RetractionRecord::AddedUserOp(op_decl.clone()));
-
             module_op_exports.push((op_decl.clone(), None));
         }
     }
@@ -133,7 +132,7 @@ pub(super) fn import_module_exports(
     code_dir: &mut CodeDir,
     op_dir: &mut OpDir,
     meta_predicates: &mut MetaPredicateDir,
-) {
+) -> Result<(), SessionError> {
     for export in imported_module.module_decl.exports.iter() {
         match export {
             ModuleExport::PredicateKey((ref name, arity)) => {
@@ -157,7 +156,10 @@ pub(super) fn import_module_exports(
                         src_code_index.get(),
                     );
                 } else {
-                    unreachable!()
+                    return Err(SessionError::ModuleDoesNotContainExport(
+                        imported_module.module_decl.name.clone(),
+                        (name.clone(), *arity),
+                    ));
                 }
             }
             ModuleExport::OpDecl(ref op_decl) => {
@@ -165,6 +167,8 @@ pub(super) fn import_module_exports(
             }
         }
     }
+
+    Ok(())
 }
 
 fn import_module_exports_into_module(
@@ -176,7 +180,7 @@ fn import_module_exports_into_module(
     meta_predicates: &mut MetaPredicateDir,
     wam_op_dir: &mut OpDir,
     module_op_exports: &mut ModuleOpExports,
-) {
+) -> Result<(), SessionError> {
     for export in imported_module.module_decl.exports.iter() {
         match export {
             ModuleExport::PredicateKey((ref name, arity)) => {
@@ -200,7 +204,10 @@ fn import_module_exports_into_module(
                         src_code_index.get(),
                     );
                 } else {
-                    unreachable!()
+                    return Err(SessionError::ModuleDoesNotContainExport(
+                        imported_module.module_decl.name.clone(),
+                        (name.clone(), *arity),
+                    ));
                 }
             }
             ModuleExport::OpDecl(ref op_decl) => {
@@ -215,6 +222,8 @@ fn import_module_exports_into_module(
             }
         }
     }
+
+    Ok(())
 }
 
 fn import_qualified_module_exports(
@@ -224,7 +233,7 @@ fn import_qualified_module_exports(
     exports: &IndexSet<ModuleExport>,
     code_dir: &mut CodeDir,
     op_dir: &mut OpDir,
-) {
+) -> Result<(), SessionError> {
     for export in imported_module.module_decl.exports.iter() {
         if !exports.contains(export) {
             continue;
@@ -248,7 +257,10 @@ fn import_qualified_module_exports(
                         src_code_index.get(),
                     );
                 } else {
-                    unreachable!()
+                    return Err(SessionError::ModuleDoesNotContainExport(
+                        imported_module.module_decl.name.clone(),
+                        (name.clone(), *arity),
+                    ));
                 }
             }
             ModuleExport::OpDecl(ref op_decl) => {
@@ -256,6 +268,8 @@ fn import_qualified_module_exports(
             }
         }
     }
+
+    Ok(())
 }
 
 fn import_qualified_module_exports_into_module(
@@ -267,7 +281,7 @@ fn import_qualified_module_exports_into_module(
     op_dir: &mut OpDir,
     wam_op_dir: &mut OpDir,
     module_op_exports: &mut ModuleOpExports,
-) {
+) -> Result<(), SessionError> {
     for export in imported_module.module_decl.exports.iter() {
         if !exports.contains(export) {
             continue;
@@ -291,7 +305,10 @@ fn import_qualified_module_exports_into_module(
                         src_code_index.get(),
                     );
                 } else {
-                    unreachable!()
+                    return Err(SessionError::ModuleDoesNotContainExport(
+                        imported_module.module_decl.name.clone(),
+                        (name.clone(), *arity),
+                    ));
                 }
             }
             ModuleExport::OpDecl(ref op_decl) => {
@@ -306,6 +323,8 @@ fn import_qualified_module_exports_into_module(
             }
         }
     }
+
+    Ok(())
 }
 
 impl<'a> LoadState<'a> {
@@ -608,7 +627,7 @@ impl<'a> LoadState<'a> {
                 code_dir,
                 op_dir,
                 meta_predicates,
-            );
+            ).unwrap();
         }
     }
 
@@ -673,7 +692,7 @@ impl<'a> LoadState<'a> {
                         &mut self.wam.indices.code_dir,
                         &mut self.wam.indices.op_dir,
                         &mut self.wam.indices.meta_predicates,
-                    );
+                    )?;
                 }
                 CompilationTarget::Module(ref defining_module_name) => {
                     match self.wam.indices.modules.get_mut(defining_module_name) {
@@ -687,7 +706,7 @@ impl<'a> LoadState<'a> {
                                 &mut target_module.meta_predicates,
                                 &mut self.wam.indices.op_dir,
                                 &mut self.module_op_exports,
-                            );
+                            )?;
                         }
                         None => {
                             // we find ourselves here because we're trying to import
@@ -723,7 +742,7 @@ impl<'a> LoadState<'a> {
                         &exports,
                         &mut self.wam.indices.code_dir,
                         &mut self.wam.indices.op_dir,
-                    );
+                    )?;
                 }
                 CompilationTarget::Module(ref defining_module_name) => {
                     match self.wam.indices.modules.get_mut(defining_module_name) {
@@ -737,7 +756,7 @@ impl<'a> LoadState<'a> {
                                 &mut target_module.op_dir,
                                 &mut self.wam.indices.op_dir,
                                 &mut self.module_op_exports,
-                            );
+                            )?;
                         }
                         None => {
                             // we find ourselves here because we're trying to import
index 90364ce76426b84f45478f0dd1a9d7142a912597..95a1f13ab3d483307813fbd68bc8b8c082e432ee 100644 (file)
@@ -1868,5 +1868,5 @@ pub(super) fn load_module(
         code_dir,
         op_dir,
         meta_predicate_dir,
-    );
+    ).unwrap();
 }
index eab527703813d4712936b019db74a29b09f477b4..2e3b5e91e2ee27d50746bd346a3f89a51792df65 100644 (file)
@@ -2,6 +2,7 @@ use prolog_parser::ast::*;
 use prolog_parser::{clause_name, temp_v};
 
 use crate::forms::{ModuleSource, Number}; //, PredicateKey};
+use crate::machine::PredicateKey;
 use crate::machine::heap::*;
 use crate::machine::machine_indices::*;
 use crate::machine::machine_state::*;
@@ -319,7 +320,6 @@ impl MachineError {
             // SessionError::InvalidFileName(filename) => {
             //     Self::existence_error(h, ExistenceError::Module(filename))
             // }
-            /*
             SessionError::ModuleDoesNotContainExport(..) => {
                 Self::permission_error(
                     h,
@@ -328,7 +328,6 @@ impl MachineError {
                     functor!("module_does_not_contain_claimed_export"),
                 )
             }
-            */
             SessionError::ModuleCannotImportSelf(module_name) => Self::permission_error(
                 h,
                 Permission::Modify,
@@ -479,7 +478,7 @@ impl CompilationError {
 
 #[derive(Debug, Clone, Copy)]
 pub enum Permission {
-    // Access,
+    Access,
     Create,
     InputStream,
     Modify,
@@ -492,7 +491,7 @@ impl Permission {
     #[inline]
     pub fn as_str(self) -> &'static str {
         match self {
-            // Permission::Access => "access",
+            Permission::Access => "access",
             Permission::Create => "create",
             Permission::InputStream => "input",
             Permission::Modify => "modify",
@@ -805,7 +804,7 @@ pub enum SessionError {
     // CannotOverwriteImport(ClauseName),
     ExistenceError(ExistenceError),
     // InvalidFileName(ClauseName),
-    // ModuleDoesNotContainExport(ClauseName, PredicateKey),
+    ModuleDoesNotContainExport(ClauseName, PredicateKey),
     ModuleCannotImportSelf(ClauseName),
     NamelessEntry,
     OpIsInfixAndPostFix(ClauseName),
index 00538abb9fca80f155db7b4f19837c14204acfa0..4137cf05b2491b48c39ecc1a271563d938260034 100644 (file)
@@ -377,15 +377,15 @@ impl fmt::Display for SessionError {
             // &SessionError::InvalidFileName(ref filename) => {
             //     write!(f, "filename {} is invalid", filename)
             // }
-            // &SessionError::ModuleDoesNotContainExport(ref module, ref key) => {
-            //     write!(
-            //         f,
-            //         "module {} does not contain claimed export {}/{}",
-            //         module,
-            //         key.0,
-            //         key.1,
-            //     )
-            // }
+            &SessionError::ModuleDoesNotContainExport(ref module, ref key) => {
+                write!(
+                    f,
+                    "module {} does not contain claimed export {}/{}",
+                    module,
+                    key.0,
+                    key.1,
+                )
+            }
             &SessionError::OpIsInfixAndPostFix(_) => {
                 write!(f, "cannot define an op to be both postfix and infix.")
             }