From 21fbd48b8d87452860bdebe22fb3db4861e31bf7 Mon Sep 17 00:00:00 2001 From: KtorZ Date: Fri, 10 Feb 2023 17:07:08 +0100 Subject: [PATCH] Improve error on duplicate imports. --- crates/aiken-lang/src/tipo/environment.rs | 2 ++ crates/aiken-lang/src/tipo/error.rs | 19 +++++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/crates/aiken-lang/src/tipo/environment.rs b/crates/aiken-lang/src/tipo/environment.rs index 9fb16b41..8b2f265a 100644 --- a/crates/aiken-lang/src/tipo/environment.rs +++ b/crates/aiken-lang/src/tipo/environment.rs @@ -710,6 +710,7 @@ impl<'a> Environment<'a> { location: *location, previous_location: *previous, name: name.to_string(), + module: module.clone(), }); } @@ -800,6 +801,7 @@ impl<'a> Environment<'a> { location: *location, previous_location: *previous_location, name: module_name, + module: module.clone(), }); } diff --git a/crates/aiken-lang/src/tipo/error.rs b/crates/aiken-lang/src/tipo/error.rs index fba72b68..28f61c45 100644 --- a/crates/aiken-lang/src/tipo/error.rs +++ b/crates/aiken-lang/src/tipo/error.rs @@ -140,13 +140,24 @@ For example: #[error("I noticed you were importing '{}' twice.\n", name.purple())] #[diagnostic(code("duplicate::import"))] - #[diagnostic(help("The best thing to do from here is to remove one of them."))] + #[diagnostic(help(r#"If you're trying to import two modules with identical names but from different packages, you'll need to use a named import. +For example: + +╰─▶ {keyword_use} {import} {keyword_as} {named} + +Otherwise, just remove the redundant import."# + , keyword_use = "use".bright_blue() + , keyword_as = "as".bright_blue() + , import = module.iter().map(|x| x.purple().bold().to_string()).collect::>().join("/".bold().to_string().as_ref()) + , named = module.join("_") + ))] DuplicateImport { - #[label] + #[label("also imported here as '{name}'")] location: Span, - #[label] - previous_location: Span, name: String, + module: Vec, + #[label("imported here as '{name}'")] + previous_location: Span, }, #[error("I discovered two top-level objects referred to as '{}'.\n", name.purple())]