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 => { None => {
retraction_info.push_record(RetractionRecord::AddedUserOp(op_decl.clone())); retraction_info.push_record(RetractionRecord::AddedUserOp(op_decl.clone()));
module_op_exports.push((op_decl.clone(), None)); module_op_exports.push((op_decl.clone(), None));
} }
} }
@@ -133,7 +132,7 @@ pub(super) fn import_module_exports(
code_dir: &mut CodeDir, code_dir: &mut CodeDir,
op_dir: &mut OpDir, op_dir: &mut OpDir,
meta_predicates: &mut MetaPredicateDir, meta_predicates: &mut MetaPredicateDir,
) { ) -> Result<(), SessionError> {
for export in imported_module.module_decl.exports.iter() { for export in imported_module.module_decl.exports.iter() {
match export { match export {
ModuleExport::PredicateKey((ref name, arity)) => { ModuleExport::PredicateKey((ref name, arity)) => {
@@ -157,7 +156,10 @@ pub(super) fn import_module_exports(
src_code_index.get(), src_code_index.get(),
); );
} else { } else {
unreachable!() return Err(SessionError::ModuleDoesNotContainExport(
imported_module.module_decl.name.clone(),
(name.clone(), *arity),
));
} }
} }
ModuleExport::OpDecl(ref op_decl) => { ModuleExport::OpDecl(ref op_decl) => {
@@ -165,6 +167,8 @@ pub(super) fn import_module_exports(
} }
} }
} }
Ok(())
} }
fn import_module_exports_into_module( fn import_module_exports_into_module(
@@ -176,7 +180,7 @@ fn import_module_exports_into_module(
meta_predicates: &mut MetaPredicateDir, meta_predicates: &mut MetaPredicateDir,
wam_op_dir: &mut OpDir, wam_op_dir: &mut OpDir,
module_op_exports: &mut ModuleOpExports, module_op_exports: &mut ModuleOpExports,
) { ) -> Result<(), SessionError> {
for export in imported_module.module_decl.exports.iter() { for export in imported_module.module_decl.exports.iter() {
match export { match export {
ModuleExport::PredicateKey((ref name, arity)) => { ModuleExport::PredicateKey((ref name, arity)) => {
@@ -200,7 +204,10 @@ fn import_module_exports_into_module(
src_code_index.get(), src_code_index.get(),
); );
} else { } else {
unreachable!() return Err(SessionError::ModuleDoesNotContainExport(
imported_module.module_decl.name.clone(),
(name.clone(), *arity),
));
} }
} }
ModuleExport::OpDecl(ref op_decl) => { ModuleExport::OpDecl(ref op_decl) => {
@@ -215,6 +222,8 @@ fn import_module_exports_into_module(
} }
} }
} }
Ok(())
} }
fn import_qualified_module_exports( fn import_qualified_module_exports(
@@ -224,7 +233,7 @@ fn import_qualified_module_exports(
exports: &IndexSet<ModuleExport>, exports: &IndexSet<ModuleExport>,
code_dir: &mut CodeDir, code_dir: &mut CodeDir,
op_dir: &mut OpDir, op_dir: &mut OpDir,
) { ) -> Result<(), SessionError> {
for export in imported_module.module_decl.exports.iter() { for export in imported_module.module_decl.exports.iter() {
if !exports.contains(export) { if !exports.contains(export) {
continue; continue;
@@ -248,7 +257,10 @@ fn import_qualified_module_exports(
src_code_index.get(), src_code_index.get(),
); );
} else { } else {
unreachable!() return Err(SessionError::ModuleDoesNotContainExport(
imported_module.module_decl.name.clone(),
(name.clone(), *arity),
));
} }
} }
ModuleExport::OpDecl(ref op_decl) => { ModuleExport::OpDecl(ref op_decl) => {
@@ -256,6 +268,8 @@ fn import_qualified_module_exports(
} }
} }
} }
Ok(())
} }
fn import_qualified_module_exports_into_module( fn import_qualified_module_exports_into_module(
@@ -267,7 +281,7 @@ fn import_qualified_module_exports_into_module(
op_dir: &mut OpDir, op_dir: &mut OpDir,
wam_op_dir: &mut OpDir, wam_op_dir: &mut OpDir,
module_op_exports: &mut ModuleOpExports, module_op_exports: &mut ModuleOpExports,
) { ) -> Result<(), SessionError> {
for export in imported_module.module_decl.exports.iter() { for export in imported_module.module_decl.exports.iter() {
if !exports.contains(export) { if !exports.contains(export) {
continue; continue;
@@ -291,7 +305,10 @@ fn import_qualified_module_exports_into_module(
src_code_index.get(), src_code_index.get(),
); );
} else { } else {
unreachable!() return Err(SessionError::ModuleDoesNotContainExport(
imported_module.module_decl.name.clone(),
(name.clone(), *arity),
));
} }
} }
ModuleExport::OpDecl(ref op_decl) => { ModuleExport::OpDecl(ref op_decl) => {
@@ -306,6 +323,8 @@ fn import_qualified_module_exports_into_module(
} }
} }
} }
Ok(())
} }
impl<'a> LoadState<'a> { impl<'a> LoadState<'a> {
@@ -608,7 +627,7 @@ impl<'a> LoadState<'a> {
code_dir, code_dir,
op_dir, op_dir,
meta_predicates, meta_predicates,
); ).unwrap();
} }
} }
@@ -673,7 +692,7 @@ impl<'a> LoadState<'a> {
&mut self.wam.indices.code_dir, &mut self.wam.indices.code_dir,
&mut self.wam.indices.op_dir, &mut self.wam.indices.op_dir,
&mut self.wam.indices.meta_predicates, &mut self.wam.indices.meta_predicates,
); )?;
} }
CompilationTarget::Module(ref defining_module_name) => { CompilationTarget::Module(ref defining_module_name) => {
match self.wam.indices.modules.get_mut(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 target_module.meta_predicates,
&mut self.wam.indices.op_dir, &mut self.wam.indices.op_dir,
&mut self.module_op_exports, &mut self.module_op_exports,
); )?;
} }
None => { None => {
// we find ourselves here because we're trying to import // we find ourselves here because we're trying to import
@@ -723,7 +742,7 @@ impl<'a> LoadState<'a> {
&exports, &exports,
&mut self.wam.indices.code_dir, &mut self.wam.indices.code_dir,
&mut self.wam.indices.op_dir, &mut self.wam.indices.op_dir,
); )?;
} }
CompilationTarget::Module(ref defining_module_name) => { CompilationTarget::Module(ref defining_module_name) => {
match self.wam.indices.modules.get_mut(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 target_module.op_dir,
&mut self.wam.indices.op_dir, &mut self.wam.indices.op_dir,
&mut self.module_op_exports, &mut self.module_op_exports,
); )?;
} }
None => { None => {
// we find ourselves here because we're trying to import // we find ourselves here because we're trying to import

View File

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

View File

@@ -2,6 +2,7 @@ use prolog_parser::ast::*;
use prolog_parser::{clause_name, temp_v}; use prolog_parser::{clause_name, temp_v};
use crate::forms::{ModuleSource, Number}; //, PredicateKey}; use crate::forms::{ModuleSource, Number}; //, PredicateKey};
use crate::machine::PredicateKey;
use crate::machine::heap::*; use crate::machine::heap::*;
use crate::machine::machine_indices::*; use crate::machine::machine_indices::*;
use crate::machine::machine_state::*; use crate::machine::machine_state::*;
@@ -319,7 +320,6 @@ impl MachineError {
// SessionError::InvalidFileName(filename) => { // SessionError::InvalidFileName(filename) => {
// Self::existence_error(h, ExistenceError::Module(filename)) // Self::existence_error(h, ExistenceError::Module(filename))
// } // }
/*
SessionError::ModuleDoesNotContainExport(..) => { SessionError::ModuleDoesNotContainExport(..) => {
Self::permission_error( Self::permission_error(
h, h,
@@ -328,7 +328,6 @@ impl MachineError {
functor!("module_does_not_contain_claimed_export"), functor!("module_does_not_contain_claimed_export"),
) )
} }
*/
SessionError::ModuleCannotImportSelf(module_name) => Self::permission_error( SessionError::ModuleCannotImportSelf(module_name) => Self::permission_error(
h, h,
Permission::Modify, Permission::Modify,
@@ -479,7 +478,7 @@ impl CompilationError {
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum Permission { pub enum Permission {
// Access, Access,
Create, Create,
InputStream, InputStream,
Modify, Modify,
@@ -492,7 +491,7 @@ impl Permission {
#[inline] #[inline]
pub fn as_str(self) -> &'static str { pub fn as_str(self) -> &'static str {
match self { match self {
// Permission::Access => "access", Permission::Access => "access",
Permission::Create => "create", Permission::Create => "create",
Permission::InputStream => "input", Permission::InputStream => "input",
Permission::Modify => "modify", Permission::Modify => "modify",
@@ -805,7 +804,7 @@ pub enum SessionError {
// CannotOverwriteImport(ClauseName), // CannotOverwriteImport(ClauseName),
ExistenceError(ExistenceError), ExistenceError(ExistenceError),
// InvalidFileName(ClauseName), // InvalidFileName(ClauseName),
// ModuleDoesNotContainExport(ClauseName, PredicateKey), ModuleDoesNotContainExport(ClauseName, PredicateKey),
ModuleCannotImportSelf(ClauseName), ModuleCannotImportSelf(ClauseName),
NamelessEntry, NamelessEntry,
OpIsInfixAndPostFix(ClauseName), OpIsInfixAndPostFix(ClauseName),

View File

@@ -377,15 +377,15 @@ impl fmt::Display for SessionError {
// &SessionError::InvalidFileName(ref filename) => { // &SessionError::InvalidFileName(ref filename) => {
// write!(f, "filename {} is invalid", filename) // write!(f, "filename {} is invalid", filename)
// } // }
// &SessionError::ModuleDoesNotContainExport(ref module, ref key) => { &SessionError::ModuleDoesNotContainExport(ref module, ref key) => {
// write!( write!(
// f, f,
// "module {} does not contain claimed export {}/{}", "module {} does not contain claimed export {}/{}",
// module, module,
// key.0, key.0,
// key.1, key.1,
// ) )
// } }
&SessionError::OpIsInfixAndPostFix(_) => { &SessionError::OpIsInfixAndPostFix(_) => {
write!(f, "cannot define an op to be both postfix and infix.") write!(f, "cannot define an op to be both postfix and infix.")
} }