Create dedicated error when environment isn't found.

This is less confusing that getting an 'UnknownModule' error reporting
  even a different module name than the one actually being important
  ('env').

  Also, this commit fixes a few errors found in the type-checker
  when reporting 'UnknownModule' errors. About half the time, we would
  actually attached _imported modules_ instead of _importable modules_
  to the error, making the neighboring suggestion quite worse (nay
  useless).
This commit is contained in:
KtorZ 2024-08-04 11:02:15 +02:00
parent 2dca0c4185
commit 6de1d91104
No known key found for this signature in database
GPG Key ID: 33173CB6F77F4277
3 changed files with 60 additions and 17 deletions

View File

@ -91,20 +91,36 @@ impl<'a> Environment<'a> {
pub fn find_module(&self, fragments: &[String], location: Span) -> Result<&'a TypeInfo, Error> {
let mut name = fragments.join("/");
if name == ast::ENV_MODULE {
let is_env = name == ast::ENV_MODULE;
if is_env {
name = self
.target_env
.unwrap_or(ast::DEFAULT_ENV_MODULE)
.to_string()
}
self.importable_modules
.get(&name)
.ok_or_else(|| Error::UnknownModule {
location,
name,
imported_modules: self.imported_modules.keys().cloned().collect(),
})
self.importable_modules.get(&name).ok_or_else(|| {
if is_env {
Error::UnknownEnvironment {
name,
known_environments: self
.importable_modules
.values()
.filter_map(|m| match m.kind {
ModuleKind::Env => Some(m.name.clone()),
ModuleKind::Lib | ModuleKind::Validator => None,
})
.collect(),
}
} else {
Error::UnknownModule {
location,
name,
known_modules: self.importable_modules.keys().cloned().collect(),
}
}
})
}
pub fn close_scope(&mut self, data: ScopeResetData) {
@ -373,7 +389,7 @@ impl<'a> Environment<'a> {
.ok_or_else(|| Error::UnknownModule {
location,
name: name.to_string(),
imported_modules: self
known_modules: self
.importable_modules
.keys()
.map(|t| t.to_string())
@ -419,7 +435,7 @@ impl<'a> Environment<'a> {
.get(m)
.ok_or_else(|| Error::UnknownModule {
name: m.to_string(),
imported_modules: self
known_modules: self
.importable_modules
.keys()
.map(|t| t.to_string())
@ -1726,7 +1742,7 @@ impl<'a> Environment<'a> {
.ok_or_else(|| Error::UnknownModule {
location,
name: name.to_string(),
imported_modules: self
known_modules: self
.importable_modules
.keys()
.map(|t| t.to_string())

View File

@ -751,13 +751,39 @@ Perhaps, try the following:
#[diagnostic(code("unknown::module"))]
#[diagnostic(help(
"{}",
suggest_neighbor(name, imported_modules.iter(), "Did you forget to add a package as dependency?")
suggest_neighbor(name, known_modules.iter(), "Did you forget to add a package as dependency?")
))]
UnknownModule {
#[label]
location: Span,
name: String,
imported_modules: Vec<String>,
known_modules: Vec<String>,
},
#[error(
"I couldn't find any module for the environment: '{}'\n",
name.if_supports_color(Stdout, |s| s.purple())
)]
#[diagnostic(code("unknown::environment"))]
#[diagnostic(help(
"{}{}",
if known_environments.is_empty() {
String::new()
} else {
format!(
"I know about the following environments:\n{}\n\n",
known_environments
.iter()
.map(|s| format!("─▶ {}", s.if_supports_color(Stdout, |s| s.purple())))
.collect::<Vec<_>>()
.join("\n")
)
},
suggest_neighbor(name, known_environments.iter(), "Did you forget to define this environment?")
))]
UnknownEnvironment {
name: String,
known_environments: Vec<String>,
},
#[error(
@ -1066,6 +1092,7 @@ impl ExtraData for Error {
| Error::UnknownModuleType { .. }
| Error::UnknownModuleValue { .. }
| Error::UnknownRecordField { .. }
| Error::UnknownEnvironment { .. }
| Error::UnnecessarySpreadOperator { .. }
| Error::UpdateMultiConstructorType { .. }
| Error::ValidatorImported { .. }

View File

@ -956,9 +956,9 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
.ok_or_else(|| Error::UnknownModule {
name: module_alias.to_string(),
location: *module_location,
imported_modules: self
known_modules: self
.environment
.imported_modules
.importable_modules
.keys()
.map(|t| t.to_string())
.collect(),
@ -2327,9 +2327,9 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
.ok_or_else(|| Error::UnknownModule {
location: *location,
name: module_name.to_string(),
imported_modules: self
known_modules: self
.environment
.imported_modules
.importable_modules
.keys()
.map(|t| t.to_string())
.collect(),