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
Rust allows impl blocks to be generic, even if the type is not generic. However, any methods contained in such a generic impl block must use every generic parameter, or the functions become uncallable (because the other types cannot be specified). Rust will not find any errors or warnings in such a defintion, only at the call site.
Rust should at least complain at the declaration site if all generic parameters are not used by every method. Alternatively, we could disallow generics on impl blocks that are not used as type parameters on the implementing type.
Example Code
structFoo;impl<A: std::fmt::Show,B>Foo{fnfunc(&mutself,a:A){println!("{}", a);}}fnmain(){letmut f = Foo;// uncomment the following line, program fails to compile.//f.func(42i); // error: cannot determine a type for this expression: unconstrained type}
The text was updated successfully, but these errors were encountered:
Relevant issues: this will become callable with rust-lang/rfcs#132 (not really true, since the param is only in the impl); #11658 a similar case: warning when generics are shadowed.
hello.rs:2:6: 2:7 error: the type parameter `A` is not constrained by the impl trait, self type, or predicates [E0207]
hello.rs:2 impl<A: std::fmt::Display, B> Foo {
^
hello.rs:2:28: 2:29 error: the type parameter `B` is not constrained by the impl trait, self type, or predicates [E0207]
hello.rs:2 impl<A: std::fmt::Display, B> Foo {
^
This would seem to be ' Alternatively, we could disallow generics on impl blocks that are not used as type parameters on the implementing type.'
Rust allows impl blocks to be generic, even if the type is not generic. However, any methods contained in such a generic impl block must use every generic parameter, or the functions become uncallable (because the other types cannot be specified). Rust will not find any errors or warnings in such a defintion, only at the call site.
Rust should at least complain at the declaration site if all generic parameters are not used by every method. Alternatively, we could disallow generics on impl blocks that are not used as type parameters on the implementing type.
Example Code
The text was updated successfully, but these errors were encountered: