Fix incoherent 'UnknownVariable' being returned in type-check

I initially removed the 'UnkownTypeConstructor' since it wasn't used anywhere and was in fact dead-code. On second thoughts however, it is nicer to provide a slightly better error message when a constructor is missing as well as some valid suggestion. Prior to that commit, we would simply return a 'UnknownVariable' and the hint might suggest lowercase identifiers; which is wrong.
This commit is contained in:
KtorZ 2023-10-21 14:09:07 +02:00
parent 5986163ba7
commit f6eff7ec58
No known key found for this signature in database
GPG Key ID: 33173CB6F77F4277
2 changed files with 17 additions and 6 deletions

View File

@ -309,11 +309,14 @@ impl<'a> Environment<'a> {
location: Span,
) -> Result<&ValueConstructor, Error> {
match module {
None => self.scope.get(name).ok_or_else(|| Error::UnknownVariable {
location,
name: name.to_string(),
variables: self.local_value_names(),
}),
None => self
.scope
.get(name)
.ok_or_else(|| Error::UnknownTypeConstructor {
location,
name: name.to_string(),
constructors: self.local_constructor_names(),
}),
Some(m) => {
let (_, module) =
@ -577,6 +580,14 @@ impl<'a> Environment<'a> {
.collect()
}
pub fn local_constructor_names(&self) -> Vec<String> {
self.scope
.keys()
.filter(|&t| t.chars().next().unwrap_or_default().is_uppercase())
.map(|t| t.to_string())
.collect()
}
fn make_type_vars(
&mut self,
args: &[String],

View File

@ -799,7 +799,7 @@ Perhaps, try the following:
suggest_neighbor(name, constructors.iter(), "Did you forget to import it?")
))]
UnknownTypeConstructor {
#[label]
#[label("unknown constructor")]
location: Span,
name: String,
constructors: Vec<String>,