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:
parent
2dca0c4185
commit
6de1d91104
|
@ -91,19 +91,35 @@ impl<'a> Environment<'a> {
|
||||||
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("/");
|
||||||
|
|
||||||
if name == ast::ENV_MODULE {
|
let is_env = name == ast::ENV_MODULE;
|
||||||
|
|
||||||
|
if is_env {
|
||||||
name = self
|
name = self
|
||||||
.target_env
|
.target_env
|
||||||
.unwrap_or(ast::DEFAULT_ENV_MODULE)
|
.unwrap_or(ast::DEFAULT_ENV_MODULE)
|
||||||
.to_string()
|
.to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
self.importable_modules
|
self.importable_modules.get(&name).ok_or_else(|| {
|
||||||
.get(&name)
|
if is_env {
|
||||||
.ok_or_else(|| Error::UnknownModule {
|
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,
|
location,
|
||||||
name,
|
name,
|
||||||
imported_modules: self.imported_modules.keys().cloned().collect(),
|
known_modules: self.importable_modules.keys().cloned().collect(),
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -373,7 +389,7 @@ impl<'a> Environment<'a> {
|
||||||
.ok_or_else(|| Error::UnknownModule {
|
.ok_or_else(|| Error::UnknownModule {
|
||||||
location,
|
location,
|
||||||
name: name.to_string(),
|
name: name.to_string(),
|
||||||
imported_modules: self
|
known_modules: self
|
||||||
.importable_modules
|
.importable_modules
|
||||||
.keys()
|
.keys()
|
||||||
.map(|t| t.to_string())
|
.map(|t| t.to_string())
|
||||||
|
@ -419,7 +435,7 @@ impl<'a> Environment<'a> {
|
||||||
.get(m)
|
.get(m)
|
||||||
.ok_or_else(|| Error::UnknownModule {
|
.ok_or_else(|| Error::UnknownModule {
|
||||||
name: m.to_string(),
|
name: m.to_string(),
|
||||||
imported_modules: self
|
known_modules: self
|
||||||
.importable_modules
|
.importable_modules
|
||||||
.keys()
|
.keys()
|
||||||
.map(|t| t.to_string())
|
.map(|t| t.to_string())
|
||||||
|
@ -1726,7 +1742,7 @@ impl<'a> Environment<'a> {
|
||||||
.ok_or_else(|| Error::UnknownModule {
|
.ok_or_else(|| Error::UnknownModule {
|
||||||
location,
|
location,
|
||||||
name: name.to_string(),
|
name: name.to_string(),
|
||||||
imported_modules: self
|
known_modules: self
|
||||||
.importable_modules
|
.importable_modules
|
||||||
.keys()
|
.keys()
|
||||||
.map(|t| t.to_string())
|
.map(|t| t.to_string())
|
||||||
|
|
|
@ -751,13 +751,39 @@ Perhaps, try the following:
|
||||||
#[diagnostic(code("unknown::module"))]
|
#[diagnostic(code("unknown::module"))]
|
||||||
#[diagnostic(help(
|
#[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 {
|
UnknownModule {
|
||||||
#[label]
|
#[label]
|
||||||
location: Span,
|
location: Span,
|
||||||
name: String,
|
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(
|
#[error(
|
||||||
|
@ -1066,6 +1092,7 @@ impl ExtraData for Error {
|
||||||
| Error::UnknownModuleType { .. }
|
| Error::UnknownModuleType { .. }
|
||||||
| Error::UnknownModuleValue { .. }
|
| Error::UnknownModuleValue { .. }
|
||||||
| Error::UnknownRecordField { .. }
|
| Error::UnknownRecordField { .. }
|
||||||
|
| Error::UnknownEnvironment { .. }
|
||||||
| Error::UnnecessarySpreadOperator { .. }
|
| Error::UnnecessarySpreadOperator { .. }
|
||||||
| Error::UpdateMultiConstructorType { .. }
|
| Error::UpdateMultiConstructorType { .. }
|
||||||
| Error::ValidatorImported { .. }
|
| Error::ValidatorImported { .. }
|
||||||
|
|
|
@ -956,9 +956,9 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
|
||||||
.ok_or_else(|| Error::UnknownModule {
|
.ok_or_else(|| Error::UnknownModule {
|
||||||
name: module_alias.to_string(),
|
name: module_alias.to_string(),
|
||||||
location: *module_location,
|
location: *module_location,
|
||||||
imported_modules: self
|
known_modules: self
|
||||||
.environment
|
.environment
|
||||||
.imported_modules
|
.importable_modules
|
||||||
.keys()
|
.keys()
|
||||||
.map(|t| t.to_string())
|
.map(|t| t.to_string())
|
||||||
.collect(),
|
.collect(),
|
||||||
|
@ -2327,9 +2327,9 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
|
||||||
.ok_or_else(|| Error::UnknownModule {
|
.ok_or_else(|| Error::UnknownModule {
|
||||||
location: *location,
|
location: *location,
|
||||||
name: module_name.to_string(),
|
name: module_name.to_string(),
|
||||||
imported_modules: self
|
known_modules: self
|
||||||
.environment
|
.environment
|
||||||
.imported_modules
|
.importable_modules
|
||||||
.keys()
|
.keys()
|
||||||
.map(|t| t.to_string())
|
.map(|t| t.to_string())
|
||||||
.collect(),
|
.collect(),
|
||||||
|
|
Loading…
Reference in New Issue