factor out common logic for creating UnknownModule error in a cute little helper.

Signed-off-by: KtorZ <matthias.benkort@gmail.com>
This commit is contained in:
KtorZ 2025-03-16 00:58:50 +01:00
parent d7af418a63
commit 9d05011001
No known key found for this signature in database
GPG Key ID: 33173CB6F77F4277
2 changed files with 39 additions and 98 deletions

View File

@ -91,6 +91,19 @@ pub struct Environment<'a> {
} }
impl<'a> Environment<'a> { impl<'a> Environment<'a> {
#[allow(clippy::result_large_err)]
pub fn err_unknown_module(&self, name: String, location: Span) -> Error {
Error::UnknownModule {
name,
location,
known_modules: self
.importable_modules
.keys()
.map(|t| t.to_string())
.collect(),
}
}
#[allow(clippy::result_large_err)] #[allow(clippy::result_large_err)]
pub fn find_module(&self, fragments: &[String], location: Span) -> Result<&'a TypeInfo, Error> { pub fn find_module(&self, fragments: &[String], location: Span) -> Result<&'a TypeInfo, Error> {
let mut name = fragments.join("/"); let mut name = fragments.join("/");
@ -118,11 +131,7 @@ impl<'a> Environment<'a> {
.collect(), .collect(),
} }
} else { } else {
Error::UnknownModule { self.err_unknown_module(name, location)
location,
name,
known_modules: self.importable_modules.keys().cloned().collect(),
}
} }
}) })
} }
@ -386,15 +395,7 @@ impl<'a> Environment<'a> {
} }
}) })
.next() .next()
.ok_or_else(|| Error::UnknownModule { .ok_or_else(|| self.err_unknown_module(name.to_string(), location))
location,
name: name.to_string(),
known_modules: self
.importable_modules
.keys()
.map(|t| t.to_string())
.collect(),
})
} }
#[allow(clippy::result_large_err)] #[allow(clippy::result_large_err)]
@ -404,18 +405,10 @@ impl<'a> Environment<'a> {
(type_name, type_location): (&str, Span), (type_name, type_location): (&str, Span),
(value, value_location): (&str, Span), (value, value_location): (&str, Span),
) -> Result<&ValueConstructor, Error> { ) -> Result<&ValueConstructor, Error> {
let module = let module = self
self.importable_modules .importable_modules
.get(module_name) .get(module_name)
.ok_or_else(|| Error::UnknownModule { .ok_or_else(|| self.err_unknown_module(module_name.to_string(), module_location))?;
location: module_location,
name: module_name.to_string(),
known_modules: self
.importable_modules
.keys()
.map(|t| t.to_string())
.collect(),
})?;
let constructors = let constructors =
module module
@ -483,18 +476,10 @@ impl<'a> Environment<'a> {
}), }),
Some(m) => { Some(m) => {
let (_, module) = let (_, module) = self
self.imported_modules .imported_modules
.get(m) .get(m)
.ok_or_else(|| Error::UnknownModule { .ok_or_else(|| self.err_unknown_module(name.to_string(), location))?;
location,
name: name.to_string(),
known_modules: self
.importable_modules
.keys()
.map(|t| t.to_string())
.collect(),
})?;
self.unused_modules.remove(m); self.unused_modules.remove(m);
@ -534,18 +519,9 @@ impl<'a> Environment<'a> {
// Lookup the module using the declared name (which may have been rebind with // Lookup the module using the declared name (which may have been rebind with
// 'as'), to obtain its _full unambiguous name_. // 'as'), to obtain its _full unambiguous name_.
let (_, module) = let (_, module) = self.imported_modules.get(module_name).ok_or_else(|| {
self.imported_modules self.err_unknown_module(module_name.to_string(), module_location)
.get(module_name) })?;
.ok_or_else(|| Error::UnknownModule {
location: module_location,
name: module_name.to_string(),
known_modules: self
.importable_modules
.keys()
.map(|t| t.to_string())
.collect(),
})?;
let type_location = Span::create(module_location.end + 1, t.len()); let type_location = Span::create(module_location.end + 1, t.len());
@ -583,18 +559,9 @@ impl<'a> Environment<'a> {
} }
Some(Namespace::Module(m)) => { Some(Namespace::Module(m)) => {
let (_, module) = let (_, module) = self.imported_modules.get(m).ok_or_else(|| {
self.imported_modules self.err_unknown_module(m.to_string(), Span::create(location.start, m.len()))
.get(m) })?;
.ok_or_else(|| Error::UnknownModule {
name: m.to_string(),
known_modules: self
.importable_modules
.keys()
.map(|t| t.to_string())
.collect(),
location: Span::create(location.start, m.len()),
})?;
self.unused_modules.remove(m); self.unused_modules.remove(m);
@ -1985,15 +1952,7 @@ impl<'a> Environment<'a> {
let module = self let module = self
.importable_modules .importable_modules
.get(full_module_name) .get(full_module_name)
.ok_or_else(|| Error::UnknownModule { .ok_or_else(|| self.err_unknown_module(name.to_string(), location))?;
location,
name: name.to_string(),
known_modules: self
.importable_modules
.keys()
.map(|t| t.to_string())
.collect(),
})?;
self.unused_modules.remove(full_module_name); self.unused_modules.remove(full_module_name);

View File

@ -1107,15 +1107,9 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
.environment .environment
.imported_modules .imported_modules
.get(module_name) .get(module_name)
.ok_or_else(|| Error::UnknownModule { .ok_or_else(|| {
location: *module_location, self.environment
name: module_name.to_string(), .err_unknown_module(module_name.to_string(), *module_location)
known_modules: self
.environment
.importable_modules
.keys()
.map(|t| t.to_string())
.collect(),
})?; })?;
return self.infer_inner_type_constructor_access( return self.infer_inner_type_constructor_access(
@ -1175,15 +1169,9 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
.environment .environment
.imported_modules .imported_modules
.get(module_alias) .get(module_alias)
.ok_or_else(|| Error::UnknownModule { .ok_or_else(|| {
name: module_alias.to_string(), self.environment
location: *module_location, .err_unknown_module(module_alias.to_string(), *module_location)
known_modules: self
.environment
.importable_modules
.keys()
.map(|t| t.to_string())
.collect(),
})?; })?;
let constructor = let constructor =
@ -2683,15 +2671,9 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
.environment .environment
.imported_modules .imported_modules
.get(module_name) .get(module_name)
.ok_or_else(|| Error::UnknownModule { .ok_or_else(|| {
location: *location, self.environment
name: module_name.to_string(), .err_unknown_module(module_name.to_string(), *location)
known_modules: self
.environment
.importable_modules
.keys()
.map(|t| t.to_string())
.collect(),
})?; })?;
module module