Skip to content

Error message’s hint pointing entirely at the wrong place. #77677

Closed
@steffahn

Description

@steffahn

Reduced example:

trait TraitF {
    fn f();
}
trait TraitG {
    fn g();
}

struct Foo<T>(T);

impl<T> Foo<T> {
    fn foo() {}
    fn bar() {
        struct Bar<F, T>(F, T);
        impl<F, T> TraitF for Bar<F, T>
        where
            F: FnOnce(T),
        {
            fn f() {}
        }
        impl<F> TraitG for Bar<F>
        where
            F: FnOnce(T),
        {
            fn g() {}
        }
    }
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0401]: can't use generic parameters from outer function
  --> src/lib.rs:22:23
   |
10 | impl<T> Foo<T> {
   |      - type parameter from outer function
...
18 |             fn f() {}
   |                - try adding a local generic parameter in this method instead
...
22 |             F: FnOnce(T),
   |                       ^ use of generic parameter from outer function

error[E0107]: wrong number of type arguments: expected 2, found 1
  --> src/lib.rs:20:28
   |
20 |         impl<F> TraitG for Bar<F>
   |                            ^^^^^^ expected 2 type arguments

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0107, E0401.
For more information about an error, try `rustc --explain E0107`.
error: could not compile `playground`.

To learn more, run the command again with --verbose.

This came up during refactoring, while I was adding the parameter T to Bar (intentionally named the same as the outer T because they are going to be instantiated to the same type anyways). This kind of error message (the first one, E0401) had me puzzled for some time before figuring out that it was nonsensical. I’m referring to the try adding a local generic parameter in this method instead part. The real code of course was quite a bit longer, making the situation actually confusing, in particular:

  • The try adding ... hint points to the wrong place entirely. It should suggest adding a parameter to the impl TraitG (even though such a suggestion might not always make sense, idk).
  • The type parameter from outer function pointer is also somewhat confusing, since when foo’s body is a few lines and bar is super long and nested, it is quite nontrivial to even figure out that “outer function” refers to bar(). (As in: it is easy to forget that you’re even inside of a function body at all if you’ve been focusing on this Bar struct and it’s impls for a while.) Also... what is the distinction between the words function and method here? (My original code has bar(self) but still called it “outer function”.)

@rustbot modify labels: A-diagnostics, T-compiler, D-confusing, C-bug.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.D-confusingDiagnostics: Confusing error or lint that should be reworked.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions