From 195273f01d09b63d9655b9424b83b450fb24e6ae Mon Sep 17 00:00:00 2001 From: Mark Thom Date: Tue, 9 Feb 2021 12:17:34 -0700 Subject: [PATCH] throw exceptions when modules do not contain claimed exports --- src/machine/load_state.rs | 47 ++++++++++++++++++++++++----------- src/machine/loader.rs | 2 +- src/machine/machine_errors.rs | 9 +++---- src/write.rs | 18 +++++++------- 4 files changed, 47 insertions(+), 29 deletions(-) diff --git a/src/machine/load_state.rs b/src/machine/load_state.rs index ebca8518..8ed61cef 100644 --- a/src/machine/load_state.rs +++ b/src/machine/load_state.rs @@ -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, 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 diff --git a/src/machine/loader.rs b/src/machine/loader.rs index 90364ce7..95a1f13a 100644 --- a/src/machine/loader.rs +++ b/src/machine/loader.rs @@ -1868,5 +1868,5 @@ pub(super) fn load_module( code_dir, op_dir, meta_predicate_dir, - ); + ).unwrap(); } diff --git a/src/machine/machine_errors.rs b/src/machine/machine_errors.rs index eab52770..2e3b5e91 100644 --- a/src/machine/machine_errors.rs +++ b/src/machine/machine_errors.rs @@ -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), diff --git a/src/write.rs b/src/write.rs index 00538abb..4137cf05 100644 --- a/src/write.rs +++ b/src/write.rs @@ -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.") } -- 2.54.0