Skip to content

[E0403] False positive generated for implicit future type #129265

New issue

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

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

Already on GitHub? # to your account

Closed
hchataing opened this issue Aug 19, 2024 · 3 comments · Fixed by #129270
Closed

[E0403] False positive generated for implicit future type #129265

hchataing opened this issue Aug 19, 2024 · 3 comments · Fixed by #129270
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@hchataing
Copy link

Code

pub async fn test() {
    let a = async { loop { } };
    let a = std::pin::pin!(a);
    
    use std::future::Future;
    use std::pin::Pin;
    use std::task::{Poll, Context};
    
    #[allow(non_camel_case_types)]
    struct Foo<'a, a> {
        a: Pin<&'a mut a>,
    }
    
    #[allow(non_camel_case_types)]
    impl<'a, a: Future<Output = ()>> Future for Foo<'a, a> {
        type Output = ();
        fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
            self.a.as_mut().poll(cx)
        }
    }
    
    Foo { a }.await
}

Current output

Compiling playground v0.0.1 (/playground)
error[E0403]: the name `a` is already used for a generic parameter in this item's generic parameters
  --> src/lib.rs:11:20
   |
3  |     let a = async { loop { } };
   |         - first use of `a`
...
11 |     struct Foo<'a, a> {
   |                    ^ already used

error[E0403]: the name `a` is already used for a generic parameter in this item's generic parameters
  --> src/lib.rs:16:14
   |
3  |     let a = async { loop { } };
   |         - first use of `a`
...
16 |     impl<'a, a: Future<Output = ()>> Future for Foo<'a, a> {
   |              ^ already used

error[E0277]: `Foo<'_, _>` is not a future
  --> src/lib.rs:23:15
   |
23 |     Foo { a }.await
   |              -^^^^^
   |              ||
   |              |`Foo<'_, _>` is not a future
   |              help: remove the `.await`
   |
   = help: the trait `Future` is not implemented for `Foo<'_, _>`, which is required by `Foo<'_, _>: IntoFuture`
   = note: Foo<'_, _> must be a future or must implement `IntoFuture` to be awaited
   = note: required for `Foo<'_, _>` to implement `IntoFuture`

Some errors have detailed explanations: E0277, E0403.
For more information about an error, try `rustc --explain E0277`.
error: could not compile `playground` (lib) due to 3 previous errors

Desired output

No error code raised

Rationale and extra context

This code snippet uses neither of the two error code conditions presented in https://doc.rust-lang.org/error_codes/E0403.html.
It seems that the variable name a for the assignment let a = async { loop { test::a().await; } }; is used implicitly in the type name, which would cause the struct declaration to shadow the first declaration.

Other cases

No response

Rust Version

Nightly channel
Build using the Nightly version: 1.82.0-nightly
(2024-08-18 6de928dce9472b864f4e)

Anything else?

No response

@hchataing hchataing added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 19, 2024
@compiler-errors
Copy link
Member

compiler-errors commented Aug 19, 2024

Oof -- I think I caused this regression in #128357.

@compiler-errors compiler-errors self-assigned this Aug 19, 2024
@compiler-errors
Copy link
Member

Minimal:

pub fn foo() {
    let a = 1;
    struct Foo<a> { a: a, };
}

@hchataing
Copy link
Author

That was fast :) thank you for your help

GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Aug 20, 2024
…dowing, r=petrochenkov

Don't consider locals to shadow inner items' generics

We don't want to consider the bindings from a `RibKind::Module` itself, because for an inner item that module will contain the local bindings from the function body or wherever else the inner item is being defined.

Fixes rust-lang#129265

r? petrochenkov
@bors bors closed this as completed in d502b1c Aug 21, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Aug 21, 2024
Rollup merge of rust-lang#129270 - compiler-errors:inner-generics-shadowing, r=petrochenkov

Don't consider locals to shadow inner items' generics

We don't want to consider the bindings from a `RibKind::Module` itself, because for an inner item that module will contain the local bindings from the function body or wherever else the inner item is being defined.

Fixes rust-lang#129265

r? petrochenkov
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants