Description
UPDATE: Mentoring instructions below.
--
Here is some code:
pub trait Bar { fn m(&self); }
pub fn foo<T>() {
struct S<T>;
impl Bar for S<T> {
fn m(&self) { println!("Hello World"); }
}
let s = S::<T>;
s.m();
}
pub fn main() {
foo::<int>();
}
Here is a transcript of a compilation attempt:
% rustc /tmp/bar.rs
error: can't use type parameters from outer function in the substitution of `foo::S<T>`; try using a local type parameter instead
/tmp/bar.rs:9:7: 9:10 error: type `foo::S<T>` does not implement any method in scope named `m`
/tmp/bar.rs:9 s.m();
^~~
error: can't use type parameters from outer function in the substitution of `foo::S<T>`; try using a local type parameter instead
error: can't use type parameters from outer function in the substitution of `fn(&foo::S<T>)`; try using a local type parameter instead
error: can't use type parameters from outer function in the substitution of `fn(&foo::S<T>)`; try using a local type parameter instead
error: can't use type parameters from outer function in the substitution of `foo::S<T>`; try using a local type parameter instead
error: can't use type parameters from outer function in the substitution of `foo::S<T>`; try using a local type parameter instead
error: can't use type parameters from outer function in the substitution of `foo::S<T>`; try using a local type parameter instead
error: can't use type parameters from outer function in the substitution of `foo::S<T>`; try using a local type parameter instead
error: can't use type parameters from outer function in the substitution of `foo::S<T>`; try using a local type parameter instead
error: can't use type parameters from outer function in the substitution of `foo::S<T>`; try using a local type parameter instead
error: aborting due to 11 previous errors
%
First off, maybe we should be aborting the compilation a little sooner rather than emitting so many seemingly redundant error messages.
Second, I was scratching my head for a little while because I was saying to myself "T
is a local type parameter". Of course, it is not local enough; one needs to change the impl Bar for S<T>
to impl<T> Bar for S<T>
.
So, I think it would be good if the error message suggested this; e.g.:
error: can't use type parameters from outer function in the substitution of
foo::S<T>
; try adding a local type parameter to theimpl
or
error: can't use type parameters from outer function in the substitution of
foo::S<T>
; try using a local type parameter likeimpl<T> Bar for ...