#6496·sway

Trait constraint error does not always mention the exact constrained type

Author: ironcevCreated Sep 4, 2024Updated Sep 17, 2026
Labelscompilercompiler: frontendcompiler: uiteam:compiler

In the following example, compiler will properly reject the code in the main function saying that "Trait "MyTrait1" is not implemented for type". However, in the first case, the type will be the expected "u32", but in the second the whole "Option".

sway
script;

trait MyTrait1 {
    fn foo();
}

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

fn main(){
    let x: Option<u32> = bar();
    //                   ^^^ Trait "MyTrait1" is not implemented for type "u32".    <<<--- OK: u32.
    let x = bar::<Option<u32>>();
    //      ^^^^^^^^^^^^^^^^^^ Trait "MyTrait1" is not implemented for type "Option<u32>".   <<<--- WRONG: Option<u32>.
}