Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using type with local type parameters has confusing error #37892

Open
bluss opened this issue Nov 20, 2016 · 7 comments
Open

Using type with local type parameters has confusing error #37892

bluss opened this issue Nov 20, 2016 · 7 comments
Labels
A-associated-items Area: Associated items (types, constants & functions) A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@bluss
Copy link
Member

bluss commented Nov 20, 2016

Using type with local type parameters or Self should have a clearer error.

Reproduce with: (playpen link)

struct Foo;

impl Foo {
    fn method<T>(&self) {
        type X = (Self, T);
    }
}

Current errors have a suggestion ("try..") that doesn't apply.

error[E0401]: can't use type parameters from outer function; try using a local type parameter instead
 --> <anon>:6:19
  |
6 |         type X = (Self, T);
  |                   ^^^^ use of type variable from outer function

error[E0401]: can't use type parameters from outer function; try using a local type parameter instead
 --> <anon>:6:25
  |
6 |         type X = (Self, T);
  |                         ^ use of type variable from outer function
@bluss bluss added the A-diagnostics Area: Messages for errors, warnings, and lints label Nov 20, 2016
@KalitaAlexey
Copy link
Contributor

Why is this disallowed?

@arielb1
Copy link
Contributor

arielb1 commented Dec 5, 2016

@KalitaAlexey

Nested items are independent from their parent item for everything except for privacy and name resolution. This includes type parameters.

The suggestion is in fact correct - this can be fixed by something like

struct Foo;

impl Foo {
    fn method<T>(&self) {
        type X<This, T> = (This, T);
    }
}
 
fn main() { }

@bluss
Copy link
Member Author

bluss commented Dec 5, 2016

I don't think the user will understand This in type X<This> to be a "local type parameter"; I didn't. If that's just the issue, it could call it something else.

@KalitaAlexey
Copy link
Contributor

Hi @arielb1,
I meant that I think that type X = (Self, T) in a function's body can be useful.
You said that nested items are independent. What's a reason?

@steveklabnik steveklabnik added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 9, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 26, 2017
@csmoe
Copy link
Member

csmoe commented Sep 17, 2018

cc #47574
current error message:

error[E0401]: can't use type parameters from outer function
 --> src/main.rs:6:19
  |
4 | impl Foo {
  | ---- `Self` type implicitly declared here, by this `impl`
5 |     fn method<T>(&self) {
6 |         type X = (Self, T);
  |                   ^^^^
  |                   |
  |                   use of type variable from outer function
  |                   use a type here instead

error[E0401]: can't use type parameters from outer function
 --> src/main.rs:6:25
  |
5 |     fn method<T>(&self) {
  |        ------ - type variable from outer function
  |        |
  |        try adding a local type parameter in this method instead
6 |         type X = (Self, T);
  |                         ^ use of type variable from outer function

error: aborting due to 2 previous errors

@estebank estebank added the A-associated-items Area: Associated items (types, constants & functions) label Mar 8, 2019
@estebank estebank added the D-papercut Diagnostics: An error or lint that needs small tweaks. label Oct 11, 2019
@estebank
Copy link
Contributor

It seems to me what this is missing is a note explaining the same thing as #37892 (comment).

@MatrixDev
Copy link

Just had the same issue, just a little different implementation:

enum MyEnum {}

impl MyEnum {
    fn do_something(result: impl FnOnce()) {
        result();
    }

    fn do_something_extra() {
        fn inner() {
            Self::do_something(move || {}); // can't use generic parameters from outer function E0401
            MyEnum::do_something(move || {}); // no error
        }
        inner();
    }
}

It seams that accessing Self from inner functions is not allowed. This doesn't seem logical and error is way too confusing (or probably describing something else completely).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items (types, constants & functions) A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

8 participants