rust-lang / rust-lang/rust

False compile error on associated type bounds

Open
#125,845 1 comment 0 reactions 1 assignee View on GitHub

@compiler-errors is already working on this.

Since Jun 1, 2024.

C-bug F-associated_type_bounds fixed-by-next-solver T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code with rustc 1.79.0-beta.7 (playground):

use std::ops::Add;

trait R {
    type S;
}

trait Trait {
    type I: R<S: Add<Output: Add>>;
}

fn main() {}

I expected a successful compilation for this code as Associated Type Bounds has been stabilized (https://github.com/rust-lang/rust/pull/122055). However, I got the following compile error:

   Compiling playground v0.0.1 (/playground)
error[E0277]: cannot add `<<Self as Trait>::I as R>::S` to `<<Self as Trait>::I as R>::S`
 --> src/main.rs:8:18
  |
8 |     type I: R<S: Add<Output: Add>>;
  |                  ^^^^^^^^^^^^^^^^ no implementation for `<<Self as Trait>::I as R>::S + <<Self as Trait>::I as R>::S`
  |
  = help: the trait `Add` is not implemented for `<<Self as Trait>::I as R>::S`
help: consider further restricting the associated type
  |
7 | trait Trait where <<Self as Trait>::I as R>::S: Add {
  |             +++++++++++++++++++++++++++++++++++++++

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` (bin "playground") due to 1 previous error

The code compiles if Trait is replaced with one of the following:

trait Trait {
    type I: Add<Output: Add>;
}
fn f<T: R<S: Add<Output: Add>>>(){}
trait Trait: R<S: Add<Output: Add>> {}
trait Trait {
    type I: R<S = Self::S_Desugar>;
    type S_Desugar: Add<Output: Add>;
}

or Add is replaced with user-defined Op:

trait Op<Rhs = Self> {
    type Output;
}
Meta

rustc +beta --version --verbose:

rustc 1.79.0-beta.7 (d9e85b56e 2024-05-25)
binary: rustc
commit-hash: d9e85b56e7f85f7fdabff71f217b7bb2cee0ef68
commit-date: 2024-05-25
host: aarch64-apple-darwin
release: 1.79.0-beta.7
LLVM version: 18.1.6

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.