Skip to content

Suggestion creates invalid syntax with a repeated associated type bound #142121

Open
@tbillington

Description

@tbillington

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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