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:
parent
5986163ba7
commit
f6eff7ec58
|
@ -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],
|
||||||
|
|
|
@ -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>,
|
||||||
|
|
Loading…
Reference in New Issue