From 660ff7fa314fe39aeeb20eafe4d6785f5217118c Mon Sep 17 00:00:00 2001 From: KtorZ Date: Sat, 15 Mar 2025 22:28:49 +0100 Subject: [PATCH] adjust spans & tweak errors for maximum UX Signed-off-by: KtorZ --- crates/aiken-lang/src/tipo/environment.rs | 8 +++++--- crates/aiken-lang/src/tipo/error.rs | 16 ++++++++++------ crates/aiken-lang/src/tipo/expr.rs | 22 +++++++++++++++++++--- crates/aiken-project/src/error.rs | 2 +- 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/crates/aiken-lang/src/tipo/environment.rs b/crates/aiken-lang/src/tipo/environment.rs index 3a2cd2f2..ab5fce6c 100644 --- a/crates/aiken-lang/src/tipo/environment.rs +++ b/crates/aiken-lang/src/tipo/environment.rs @@ -505,8 +505,10 @@ impl<'a> Environment<'a> { }), Some(Namespace::Type(t)) => { + let type_location = location.map(|start, _| (start, start + t.len())); + let parent_type = self.module_types.get(t).ok_or_else(|| Error::UnknownType { - location, + location: type_location, name: t.to_string(), types: self.known_type_names(), })?; @@ -514,8 +516,8 @@ impl<'a> Environment<'a> { self.unused_modules.remove(&parent_type.module); self.get_fully_qualified_value_constructor( - (parent_type.module.as_str(), location), - (t, location), + (parent_type.module.as_str(), type_location), + (t, type_location), (name, location.map(|start, end| (start + t.len() + 1, end))), ) } diff --git a/crates/aiken-lang/src/tipo/error.rs b/crates/aiken-lang/src/tipo/error.rs index 769b05df..dd7aea8d 100644 --- a/crates/aiken-lang/src/tipo/error.rs +++ b/crates/aiken-lang/src/tipo/error.rs @@ -808,7 +808,7 @@ Perhaps, try the following: }, #[error( - "I looked for '{}' in '{}' but couldn't find it.\n", + "I looked for '{}' in module '{}' but couldn't find it.\n", name.if_supports_color(Stdout, |s| s.purple()), module_name.if_supports_color(Stdout, |s| s.purple()) )] @@ -822,7 +822,7 @@ Perhaps, try the following: ) ))] UnknownModuleType { - #[label("unknown import")] + #[label("not exported?")] location: Span, name: String, module_name: String, @@ -1529,14 +1529,15 @@ fn suggest_import_constructor() -> String { ┍━ aiken/pet.ak ━ ==> ┍━ foo.ak ━━━━━━━━━━━━━━━━ │ {keyword_pub} {keyword_type} {type_Pet} {{ │ {keyword_use} aiken/pet.{{{type_Pet}, {variant_Dog}}} │ {variant_Cat} │ - │ {variant_Dog} │ {keyword_fn} foo(pet : {type_Pet}) {{ - │ }} │ {keyword_when} pet {keyword_is} {{ - │ pet.{variant_Cat} -> // ... + │ {variant_Dog} │ {keyword_fn} foo(pet: {type_Pet}) {{ + │ {variant_Fox} │ {keyword_when} pet {keyword_is} {{ + │ }} │ pet.{variant_Cat} -> // ... │ {variant_Dog} -> // ... + │ {type_Pet}.{variant_Fox} -> // ... │ }} │ }} - You must import its constructors explicitly to use them, or prefix them with the module's name. + You must import its constructors explicitly to use them, or prefix them with the module or type's name. "# , keyword_fn = "fn".if_supports_color(Stdout, |s| s.yellow()) , keyword_is = "is".if_supports_color(Stdout, |s| s.yellow()) @@ -1553,6 +1554,9 @@ fn suggest_import_constructor() -> String { , variant_Dog = "Dog" .if_supports_color(Stdout, |s| s.bright_blue()) .if_supports_color(Stdout, |s| s.bold()) + , variant_Fox = "Fox" + .if_supports_color(Stdout, |s| s.bright_blue()) + .if_supports_color(Stdout, |s| s.bold()) } } diff --git a/crates/aiken-lang/src/tipo/expr.rs b/crates/aiken-lang/src/tipo/expr.rs index 483a9839..8fa90958 100644 --- a/crates/aiken-lang/src/tipo/expr.rs +++ b/crates/aiken-lang/src/tipo/expr.rs @@ -1078,7 +1078,10 @@ impl<'a, 'b> ExprTyper<'a, 'b> { } if TypeConstructor::might_be(type_name) => { return self.infer_type_constructor_access( (type_name, type_location), - (&label, access_location), + ( + &label, + access_location.map(|start, end| (start + type_name.len() + 1, end)), + ), ); } @@ -1094,8 +1097,16 @@ impl<'a, 'b> ExprTyper<'a, 'b> { { return self.infer_inner_type_constructor_access( (module_name, *module_location), - (type_name, type_location), - (&label, access_location), + ( + type_name, + type_location.map(|start, end| (start + module_name.len() + 1, end)), + ), + ( + &label, + access_location.map(|start, end| { + (start + module_name.len() + type_name.len() + 2, end) + }), + ), ); } } @@ -1203,6 +1214,8 @@ impl<'a, 'b> ExprTyper<'a, 'b> { (type_name, type_location): (&str, Span), (label, label_location): (&str, Span), ) -> Result { + self.environment.increment_usage(type_name); + let parent_type = self .environment .module_types @@ -1234,6 +1247,9 @@ impl<'a, 'b> ExprTyper<'a, 'b> { (type_name, type_location), (label, label_location), )?; + + self.environment.unused_modules.remove(module_name); + self.infer_module_access( module_name, label.to_string(), diff --git a/crates/aiken-project/src/error.rs b/crates/aiken-project/src/error.rs index b5048195..0180e831 100644 --- a/crates/aiken-project/src/error.rs +++ b/crates/aiken-project/src/error.rs @@ -731,7 +731,7 @@ impl Warning { impl Debug for Warning { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - default_miette_handler(0) + default_miette_handler(1) .debug( &DisplayWarning { title: &self.to_string(),