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 Constraints Are Not Respected #6380

Closed
IGI-111 opened this issue Aug 7, 2024 · 1 comment
Closed

Trait Constraints Are Not Respected #6380

IGI-111 opened this issue Aug 7, 2024 · 1 comment
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-014

In Sway similarly to Rust, one can define function over a generic type which can be further constrained by
a trait. An example can be found in the following snippet:

trait MyTrait1{
 fn foo();
}
fn bar<T>() -> Option<T> where T: MyTrait1{
 None
}

Here bar, returns a generic Option T where T should implement MyTrait. When we force the result to
monomorphize with a type ascription, the compiler should check if the concrete type satisfies the trait
bounds. However, this doesn't happen therefore the example below successfully compiles.

fn main(){
 let x: Option<u32> = bar();
}

Note, however, that the following, seemingly equivalent snippet, will be rejected by the compiler:

fn main(){
 let x = bar::<Option<u32>>();
}
@IGI-111 IGI-111 added bug Something isn't working P: medium audit-report Related to the audit report labels Aug 7, 2024
@ironcev
Copy link
Member

ironcev commented Sep 4, 2024

#6490 fixes this issue.

We still do have a separate bug in this example. The type mentioned in the trait not implemented error is not always fully correct. This will be solved as a separate issue #6496.

@ironcev ironcev closed this as completed Sep 4, 2024
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

No branches or pull requests

2 participants