You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Check for type aliases when implementing traits (#6875)
## Description
Fixes#6329 .
During insertion of `impl`s into the trait map we used to forget to
check for alias types, which meant that it was possible to have multiple
implementations of a trait for the same type, one for the original type
and one for the alias.
#6626 fixed this bug, but the error message was unhelpful in that it
reported the error on the original type without mentioning the alias.
This PR improves the error message, and also improves the error message
for aliased types when they contain multiple declarations of the same
member.
## Checklist
- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
---------
Co-authored-by: IGI-111 <igi-111@protonmail.com>
Co-authored-by: Joshua Batty <joshpbatty@gmail.com>
Copy file name to clipboardexpand all lines: sway-error/src/error.rs
+47-7
Original file line number
Diff line number
Diff line change
@@ -625,6 +625,7 @@ pub enum CompileError {
625
625
ConflictingImplsForTraitAndType{
626
626
trait_name:String,
627
627
type_implementing_for:String,
628
+
type_implementing_for_unaliased:String,
628
629
existing_impl_span:Span,
629
630
second_impl_span:Span,
630
631
},
@@ -640,7 +641,9 @@ pub enum CompileError {
640
641
decl_kind:String,
641
642
decl_name:String,
642
643
type_implementing_for:String,
643
-
span:Span,
644
+
type_implementing_for_unaliased:String,
645
+
existing_impl_span:Span,
646
+
second_impl_span:Span,
644
647
},
645
648
#[error("The function \"{fn_name}\" in {interface_name} is defined with {num_parameters} parameters, but the provided implementation has {provided_parameters} parameters.")]
reason:Some(Reason::new(code(1),"Trait is already implemented for type".to_string())),
2322
2327
issue:Issue::error(
2323
2328
source_engine,
2324
2329
second_impl_span.clone(),
2325
-
format!("Trait \"{trait_name}\" is already implemented for type \"{type_implementing_for}\".")
2330
+
if type_implementing_for == type_implementing_for_unaliased {
2331
+
format!("Trait \"{trait_name}\" is already implemented for type \"{type_implementing_for}\".")
2332
+
}else{
2333
+
format!("Trait \"{trait_name}\" is already implemented for type \"{type_implementing_for}\" (which is an alias for \"{type_implementing_for_unaliased}\").")
2334
+
}
2326
2335
),
2327
2336
hints:vec![
2328
2337
Hint::info(
2329
2338
source_engine,
2330
2339
existing_impl_span.clone(),
2331
-
format!("This is the already existing implementation of \"{}\" for \"{type_implementing_for}\".",
2332
-
call_path_suffix_with_args(trait_name)
2333
-
)
2340
+
if type_implementing_for == type_implementing_for_unaliased {
2341
+
format!("This is the already existing implementation of \"{}\" for \"{type_implementing_for}\".",
2342
+
call_path_suffix_with_args(trait_name)
2343
+
)
2344
+
} else {
2345
+
format!("This is the already existing implementation of \"{}\" for \"{type_implementing_for}\" (which is an alias for \"{type_implementing_for_unaliased}\").",
2346
+
call_path_suffix_with_args(trait_name)
2347
+
)
2348
+
}
2334
2349
),
2335
2350
],
2336
2351
help:vec![
2337
2352
"In Sway, there can be at most one implementation of a trait for any given type.".to_string(),
2338
2353
"This property is called \"trait coherence\".".to_string(),
if type_implementing_for == type_implementing_for_unaliased {
2364
+
format!("{decl_kind_snake_case} \"{decl_name}\" already declared in type \"{type_implementing_for}\".")
2365
+
}else{
2366
+
format!("{decl_kind_snake_case} \"{decl_name}\" already declared in type \"{type_implementing_for}\" (which is an alias for \"{type_implementing_for_unaliased}\").")
2367
+
}
2368
+
),
2369
+
hints:vec![
2370
+
Hint::info(
2371
+
source_engine,
2372
+
existing_impl_span.clone(),
2373
+
format!("\"{decl_name}\" previously defined here.")
2374
+
)
2375
+
],
2376
+
help:vec![
2377
+
"A type may not contain two or more declarations of the same name".to_string(),
0 commit comments