Improve error on duplicate imports.

This commit is contained in:
KtorZ 2023-02-10 17:07:08 +01:00
parent f747ee0aca
commit 21fbd48b8d
No known key found for this signature in database
GPG Key ID: 33173CB6F77F4277
2 changed files with 17 additions and 4 deletions

View File

@ -710,6 +710,7 @@ impl<'a> Environment<'a> {
location: *location, location: *location,
previous_location: *previous, previous_location: *previous,
name: name.to_string(), name: name.to_string(),
module: module.clone(),
}); });
} }
@ -800,6 +801,7 @@ impl<'a> Environment<'a> {
location: *location, location: *location,
previous_location: *previous_location, previous_location: *previous_location,
name: module_name, name: module_name,
module: module.clone(),
}); });
} }

View File

@ -140,13 +140,24 @@ For example:
#[error("I noticed you were importing '{}' twice.\n", name.purple())] #[error("I noticed you were importing '{}' twice.\n", name.purple())]
#[diagnostic(code("duplicate::import"))] #[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::<Vec<_>>().join("/".bold().to_string().as_ref())
, named = module.join("_")
))]
DuplicateImport { DuplicateImport {
#[label] #[label("also imported here as '{name}'")]
location: Span, location: Span,
#[label]
previous_location: Span,
name: String, name: String,
module: Vec<String>,
#[label("imported here as '{name}'")]
previous_location: Span,
}, },
#[error("I discovered two top-level objects referred to as '{}'.\n", name.purple())] #[error("I discovered two top-level objects referred to as '{}'.\n", name.purple())]