rust-lang / rust-lang/rust

Incorrect error when specifying `impl Trait` explicitly

Open
#139,140 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
struct S;
struct F;
impl F {
    fn foo(self, _x: impl Sized) {}
}

fn test() {
    F.foo::<u32>(S);
}
Current output
error[E0107]: method takes 0 generic arguments but 1 generic argument was supplied
 --> src/lib.rs:8:7
  |
8 |     F.foo::<u32>(S);
  |       ^^^------- help: remove the unnecessary generics
  |       |
  |       expected 0 generic arguments
  |
note: method defined here, with 0 generic parameters
 --> src/lib.rs:4:8
  |
4 |     fn foo(self, _x: impl Sized) {}
  |        ^^^
  = note: `impl Trait` cannot be explicitly specified as a generic argument

error[E0308]: mismatched types
 --> src/lib.rs:8:18
  |
8 |     F.foo::<u32>(S);
  |       ---------- ^ expected `u32`, found `S`
  |       |
  |       arguments to this method are incorrect
  |
note: method defined here
 --> src/lib.rs:4:8
  |
4 |     fn foo(self, _x: impl Sized) {}
  |        ^^^       --------------
Desired output
error[E0107]: method takes 0 generic arguments but 1 generic argument was supplied
 --> src/lib.rs:8:7
  |
8 |     F.foo::<u32>(S);
  |       ^^^------- help: remove the unnecessary generics
  |       |
  |       expected 0 generic arguments
  |
note: method defined here, with 0 generic parameters
 --> src/lib.rs:4:8
  |
4 |     fn foo(self, _x: impl Sized) {}
  |        ^^^
  = note: `impl Trait` cannot be explicitly specified as a generic argument
Rationale and extra context

The second error is confusing, since rustc considers it as if we could specify the value of impl Trait, which we cannot. It would be better if the impl Trait type would be inferred.

Other cases

Rust Version
rustc 1.85.0 (4d91de4e4 2025-02-17)
Anything else?

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Rust reproducer in the issue and run it with the reported rustc 1.85.0 version to observe the E0107 and E0308 diagnostics. Trace the compiler path that reports the method-call type mismatch; done means the explicit impl Trait case reports only the E0107 diagnostic shown in the desired output.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.