Skip to content

suggest binding site when issuing "try using a local type parameter instead" err msg #14844

Closed
@pnkfelix

Description

@pnkfelix

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 the impl

or

error: can't use type parameters from outer function in the substitution of foo::S<T>; try using a local type parameter like impl<T> Bar for ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.WG-diagnosticsWorking group: Diagnostics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions