rust-lang / rust-lang/rust

Suggestion creates invalid syntax with a repeated associated type bound

Open
#142,121 1 comment 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
trait Num: std::ops::Add + std::ops::Div + Sized + Copy {}

struct Something<T: Num> {
    data: T,
}

impl<T: Num> Something<T> {
    fn add(&mut self, val: T) {
        self.data = self.data + val;
    }
}
Current output
error[E0308]: mismatched types
  --> src/stats.rs:40:21
   |
38 | impl<T: Num> Something<T> {
   |      - expected this type parameter
39 |     fn add(&mut self, val: T) {
40 |         self.data = self.data + val;
   |         ---------   ^^^^^^^^^^^^^^^ expected type parameter `T`, found associated type
   |         |
   |         expected due to the type of this binding
   |
   = note: expected type parameter `T`
             found associated type `<T as std::ops::Add>::Output`
help: consider further restricting this bound
   |
38 | impl<T: Num<Output = T>> Something<T> {
   |            ++++++++++++
Desired output

Don't know what it should say

Rationale and extra context

Upon applying the suggestion, turning the impl line to

impl<T: Num<Output = T>> Something<T> {

The next suggestion tells me to add the same section again, which upon adding just gets the compiler to throw syntax errors.

Invalid suggestion:

error[E0222]: ambiguous associated type `Output` in bounds of `stats::Num`
  --> src/stats.rs:38:13
   |
38 | impl<T: Num<Output = T>> Something<T> {
   |             ^^^^^^^^^^ ambiguous associated type `Output`
   |
   = note: associated type `Output` could derive from `Div`
   = note: associated type `Output` could derive from `std::ops::Add`

error[E0308]: mismatched types
  --> src/stats.rs:40:21
   |
38 | impl<T: Num<Output = T>> Something<T> {
   |      - expected this type parameter
39 |     fn add(&mut self, val: T) {
40 |         self.data = self.data + val;
   |         ---------   ^^^^^^^^^^^^^^^ expected type parameter `T`, found associated type
   |         |
   |         expected due to the type of this binding
   |
   = note: expected type parameter `T`
             found associated type `<T as std::ops::Add>::Output`
help: consider further restricting this bound
   |
38 | impl<T: Num<Output = T><Output = T>> Something<T> {
   |                        ++++++++++++

Upon changing impl line to

impl<T: Num<Output = T><Output = T>> Something<T> {

The error turns to gibberish

error: expected one of `+`, `,`, `::`, `=`, or `>`, found `<`
  --> src/stats.rs:38:24
   |
38 | impl<T: Num<Output = T><Output = T>> Something<T> {
   |                        ^ expected one of `+`, `,`, `::`, `=`, or `>`
   |
help: you might have meant to end the type parameters here
   |
38 | impl<T: Num<Output = T>><Output = T>> Something<T> {
   |                        +
Rust Version
> rustc --version --verbose
rustc 1.85.1 (4eb161250 2025-03-15)
binary: rustc
commit-hash: 4eb161250e340c8f48f66e2b929ef4a5bed7c181
commit-date: 2025-03-15
host: aarch64-apple-darwin
release: 1.85.1
LLVM version: 19.1.7

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

Reproduce the issue with the provided Rust trait and impl snippet using rustc 1.85.1, then trace the compiler diagnostic that suggests the associated type bound. Done means the help suggestion remains syntactically valid and does not repeat an ambiguous bound or produce follow-up syntax errors.

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
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.