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

Implicit Trait Constraints #6377

Closed
IGI-111 opened this issue Aug 7, 2024 · 0 comments · Fixed by #6517
Closed

Implicit Trait Constraints #6377

IGI-111 opened this issue Aug 7, 2024 · 0 comments · Fixed by #6517
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-011

In Sway, traits can define supertraits. This means that, to implement such a trait for a type, one must
implement its supertrait as well. For example, T2 is a supertrait of T1:

script;
trait T2 {}
trait T1: T2 {
 fn new() -> Self;
}
struct S {}
impl T2 for S {}
impl T1 for S {
 fn new() -> Self {
 S {}
 }
}
fn bar<T>() -> T
where
 T: T1,
{
 T::new()
}
fn foo<T>() -> T
where
 T: T2,
{
 bar()
}
fn main() {}

In the above example, let's focus on foo(). foo() is a generic function, whose type parameter T just
has to implement T2. In its implementation, foo() calls bar() and returns its result: type inference
therefore implies that the same type T should also parametrise the call to bar(). However, foo()
additionally requires that its type parameter implement T1: this constraint is not automatically satisfied for
all types T that implement T2, so compilation should fail. Instead, the program is accepted by the
compiler.
Taking this a step further, if one adds let s: S = foo(); to the main(), so as to actually trigger a
call to a non-existent function, the compiler runs into an ICE:
Internal compiler error: Method new_3 is a trait method dummy and was not properly replaced.
Please file an issue on the repository and include the code that triggered this error.
For reference, Rust prohibits the above snippet and requires to add an explicit trait bound T1 for foo().

@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 6, 2024
This PR adds test that proves implicit trait constraint already returns
 an error. The method dummy ICE reported in #6377 was fixed by #6490.

Closes #6377.
esdrubal added a commit that referenced this issue Sep 16, 2024
## Description

This PR adds a test that proves implicit trait constraint already
returns
 an error. The method dummy ICE reported in #6377 was fixed by #6490.

Closes #6377.

## Checklist

- [x] 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: Sophie Dankel <47993817+sdankel@users.noreply.github.com>
Co-authored-by: Joshua Batty <joshpbatty@gmail.com>
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
Development

Successfully merging a pull request may close this issue.

2 participants