Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trait Functions With the Same Name Get Disambiguated Arbitrarily #6381

Open
IGI-111 opened this issue Aug 7, 2024 · 0 comments · Fixed by #6493 · May be fixed by #6744
Open

Trait Functions With the Same Name Get Disambiguated Arbitrarily #6381

IGI-111 opened this issue Aug 7, 2024 · 0 comments · Fixed by #6493 · May be fixed by #6744
Assignees
Labels
audit-report Related to the audit report bug Something isn't working P: medium

Comments

@IGI-111
Copy link
Contributor

IGI-111 commented Aug 7, 2024

CS-FSSA-015

A type can implement two different traits that define the same function. A UFCS-style call syntax is then
needed to explicitly disambiguate because calls like x.foo() will raise a compiler error.
Such an error, however, is not raised if the type is wrapped in a generic struct that has two generic impl
blocks, one bound to the first trait, one to the other, as in the following code snippet:

script;
trait Cat {
 fn speak(self) -> u64;
}
trait Dog {
 fn speak(self) -> u64;
}
struct S<T> {
 x: T,
}
impl<T> S<T>
where
 T: Cat,
{
 fn foo(self) -> u64 {
 self.x.speak()
 }
}
impl<T> S<T>
where
 T: Dog,
{
 fn foo(self) -> u64 {
 self.x.speak()
 }
}
impl Dog for u64 {
 fn speak(self) -> u64 {
 log("2");
 2
 }
}
impl Cat for u64 {
 fn speak(self) -> u64 {
 log("1");
 1
 }
}
fn main() {
 let s = S::<u64> { x: 1 };
 s.foo();
}

The compilation succeeds and the execution output shows that it is the first generic impl (the one bound
to Cat) that prevails.
To the contrary, Rust refuses the compilation of an analogous snippet.

@IGI-111 IGI-111 added bug Something isn't working P: medium audit-report Related to the audit report labels Aug 7, 2024
esdrubal added a commit that referenced this issue Sep 3, 2024
While inserting trait implementations into the trait map we now compare
the implementing type_id without matching UnkownGenerics trait
constraints. Matching the trait constraints was causing a missing error.

Fixes #6381
@esdrubal esdrubal self-assigned this Sep 3, 2024
esdrubal added a commit that referenced this issue Sep 3, 2024
## Description

While inserting trait implementations into the trait map we now compare
the implementing type_id without matching UnkownGenerics trait
constraints. Matching the trait constraints was causing a missing error.

Fixes #6381

## Checklist

- [ ] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [ ] 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 <[email protected]>
@IGI-111 IGI-111 reopened this Feb 17, 2025
@IGI-111 IGI-111 linked a pull request Feb 17, 2025 that will close this issue
8 tasks
@linear linear bot closed this as completed Feb 17, 2025
@IGI-111 IGI-111 reopened this Feb 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
audit-report Related to the audit report bug Something isn't working P: medium
Projects
None yet
3 participants