From f6eff7ec589caaa399ef57139ba4d1f126e76a22 Mon Sep 17 00:00:00 2001 From: KtorZ Date: Sat, 21 Oct 2023 14:09:07 +0200 Subject: [PATCH] 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. --- crates/aiken-lang/src/tipo/environment.rs | 21 ++++++++++++++++----- crates/aiken-lang/src/tipo/error.rs | 2 +- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/crates/aiken-lang/src/tipo/environment.rs b/crates/aiken-lang/src/tipo/environment.rs index b95a5a0b..954ebfdc 100644 --- a/crates/aiken-lang/src/tipo/environment.rs +++ b/crates/aiken-lang/src/tipo/environment.rs @@ -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 { + 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], diff --git a/crates/aiken-lang/src/tipo/error.rs b/crates/aiken-lang/src/tipo/error.rs index 93706a8c..2ce8b768 100644 --- a/crates/aiken-lang/src/tipo/error.rs +++ b/crates/aiken-lang/src/tipo/error.rs @@ -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,