Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False compile error on associated type bounds #125845

Open
taiheioki opened this issue Jun 1, 2024 · 1 comment
Open

False compile error on associated type bounds #125845

taiheioki opened this issue Jun 1, 2024 · 1 comment
Assignees
Labels
C-bug Category: This is a bug. F-associated_type_bounds `#![feature(associated_type_bounds)]` fixed-by-next-solver Fixed by the next-generation trait solver, `-Znext-solver`. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@taiheioki
Copy link

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
@taiheioki taiheioki added the C-bug Category: This is a bug. label Jun 1, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jun 1, 2024
@workingjubilee workingjubilee added the T-types Relevant to the types team, which will review and decide on the PR/issue. label Jun 1, 2024
@compiler-errors compiler-errors self-assigned this Jun 1, 2024
@compiler-errors compiler-errors added fixed-by-next-solver Fixed by the next-generation trait solver, `-Znext-solver`. F-associated_type_bounds `#![feature(associated_type_bounds)]` labels Jun 1, 2024
@compiler-errors
Copy link
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.

@fmease fmease removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jun 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. F-associated_type_bounds `#![feature(associated_type_bounds)]` fixed-by-next-solver Fixed by the next-generation trait solver, `-Znext-solver`. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants