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,10 +309,13 @@ impl<'a> Environment<'a> {
location: Span, location: Span,
) -> Result<&ValueConstructor, Error> { ) -> Result<&ValueConstructor, Error> {
match module { match module {
None => self.scope.get(name).ok_or_else(|| Error::UnknownVariable { None => self
.scope
.get(name)
.ok_or_else(|| Error::UnknownTypeConstructor {
location, location,
name: name.to_string(), name: name.to_string(),
variables: self.local_value_names(), constructors: self.local_constructor_names(),
}), }),
Some(m) => { Some(m) => {
@ -577,6 +580,14 @@ impl<'a> Environment<'a> {
.collect() .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( fn make_type_vars(
&mut self, &mut self,
args: &[String], args: &[String],

View File

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