throw exceptions when modules do not contain claimed exports

This commit is contained in:
Mark Thom
2021-02-09 12:17:34 -07:00
parent cc7e21170f
commit 195273f01d
4 changed files with 47 additions and 29 deletions

View 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

View File

@@ -1868,5 +1868,5 @@ pub(super) fn load_module(
code_dir,
op_dir,
meta_predicate_dir,
);
).unwrap();
}

View 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),

View 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.")
}