Skip to content

False compile error on associated type bounds #125845

Open
@taiheioki

Description

@taiheioki

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 (#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

Metadata

Metadata

Labels

C-bugCategory: This is a bug.F-associated_type_bounds`#![feature(associated_type_bounds)]`T-typesRelevant to the types team, which will review and decide on the PR/issue.fixed-by-next-solverFixed by the next-generation trait solver, `-Znext-solver`.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions