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

Activity

added
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Jun 1, 2024
added
T-typesRelevant to the types team, which will review and decide on the PR/issue.
on Jun 1, 2024
compiler-errors

compiler-errors commented on Jun 1, 2024

@compiler-errors
Member

Not certain this is possible to fix easily. This is definitely due to the double-add bound causing the (old) trait solver to be confused.

removed
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Jun 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

      Participants

      @taiheioki@compiler-errors@fmease@workingjubilee@rustbot

      Issue actions

        False compile error on associated type bounds · Issue #125845 · rust-lang/rust