Skip to content

Wrong error emitted with multiple traits in return position of trait function #122193

Open

Description

I tried this code:

pub struct Bar<T: Foo> {}

impl<T> Bar<T>
where
    T: Foo,
{
    pub fn dumb(&self) {}
}

pub trait Foo {
    fn qux(bar: &Bar<impl Foo>) -> impl Send + Sync;
}

pub struct Baz;

impl Foo for Baz {
    fn qux(bar: &Bar<impl Foo>) -> impl Send {
        bar.dumb();
    }
}

fn main() {}

I expected to see this happen:

error[E0392]: type parameter `T` is never used
 --> src/main.rs:1:16
  |
1 | pub struct Bar<T: Foo> {}
  |                ^ unused type parameter
  |
  = help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`

Instead, this happened:

error[E0283]: type annotations needed
  --> src/main.rs:18:13
   |
18 |         bar.dumb();
   |             ^^^^ cannot infer type for type parameter `T`
   |
   = note: cannot satisfy `_: Foo`
   = help: the trait `Foo` is implemented for `Baz`
note: required by a bound in `Bar::<T>::dumb`
  --> src/main.rs:5:8
   |
5  |     T: Foo,
   |        ^^^ required by this bound in `Bar::<T>::dumb`
6  | {
7  |     pub fn dumb(&self) {}
   |            ---- required by a bound in this associated function

on nightly I get both errors but only the expected error (E0392) should appear. On stable I only get the latter (E0283).
E0283 is incorrect since the type of bar is annotated in the function signature.
If E0392 is fixed by adding PhantomData both errors disappear as expected.

Meta

rustc --version --verbose (stable):

rustc 1.76.0 (07dca489a 2024-02-04)
binary: rustc
commit-hash: 07dca489ac2d933c78d3c5158e3f43beefeb02ce
commit-date: 2024-02-04
host: x86_64-unknown-linux-gnu
release: 1.76.0
LLVM version: 17.0.6

rustc --version --verbose (nightly):

rustc 1.78.0-nightly (9c3ad802d 2024-03-07)
binary: rustc
commit-hash: 9c3ad802d9b9633d60d3a74668eb1be819212d34
commit-date: 2024-03-07
host: x86_64-unknown-linux-gnu
release: 1.78.0-nightly
LLVM version: 18.1.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.D-verboseDiagnostics: Too much output caused by a single piece of incorrect code.S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueT-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