adjust spans & tweak errors for maximum UX

Signed-off-by: KtorZ <matthias.benkort@gmail.com>
This commit is contained in:
KtorZ 2025-03-15 22:28:49 +01:00
parent b9052949f7
commit 660ff7fa31
No known key found for this signature in database
GPG Key ID: 33173CB6F77F4277
4 changed files with 35 additions and 13 deletions

View File

@ -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))),
)
}

View File

@ -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,
@ -1530,13 +1530,14 @@ fn suggest_import_constructor() -> String {
{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_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())
}
}

View File

@ -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<TypedExpr, Error> {
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(),

View File

@ -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(),