Skip to content

Unsound overlaps being accepted, and incr-comp issues from whitespace changes, with #[marker] traits #84955

Closed
@steffahn

Description

@steffahn
#![feature(marker_trait_attr)]

#[marker]
trait A {}

trait B {}

impl<T: A> B for T {}
impl<T: B> A for T {}
impl A for &str {}
impl<T: A + B> A for (T,) {}
// insert another line break around here and it stops compiling on nightly-2021-04-25
trait TraitWithAssoc {
    type Assoc;
}

impl<T: A> TraitWithAssoc for T {
    type Assoc = T;
}
// ^^^ vvv  these two impls should be overlapping
impl TraitWithAssoc for ((&str,),) {
    type Assoc = ((&'static str,),);
}

fn g<T: TraitWithAssoc<Assoc = ((&'static str,),)>>(x: T::Assoc) -> ((&'static str,),) {
    x
}

fn j<T: A + B>(
    x: (T,),
    f: fn(<(T,) as TraitWithAssoc>::Assoc) -> ((&'static str,),),
) -> ((&'static str,),) {
    f(x)
}

fn main() {
    let y;
    {
        let hello = "Hello World!".to_owned();
        y = j::<(&str,)>(((&hello,),), g::<((&str,),)>).0 .0;
    }
    println!("{}", y);
}
�0X�

(playground)

same code with an additional line of whitespace/comments where indicated:

error[E0277]: the trait bound `(&str,): A` is not satisfied
  --> src/main.rs:39:26
   |
31 | fn j<T: A + B>(x: (T,), f: fn(<(T,) as TraitWithAssoc>::Assoc) -> &'static str) -> &'static str {
   |         - required by this bound in `j`
...
39 |         y = j::<(&str,)>(((&hello,),), g::<((&str,),)>);
   |                          ^^^^^^^^^^^^ the trait `A` is not implemented for `(&str,)`
   |
   = help: the following implementations were found:
             <(T,) as A>

however, the playground seems to behave different from my local nightly here

Tested on

$ rustc +nightly-2021-04-25 --version
rustc 1.53.0-nightly (42816d61e 2021-04-24)

@rustbot label T-compiler requires-nightly F-marker_trait_attr A-traits A-typesystem
and someone please add “I-unsound 💥”

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-incr-compArea: Incremental compilationA-trait-systemArea: Trait systemA-type-systemArea: Type systemC-bugCategory: This is a bug.F-marker_trait_attr`#![feature(marker_trait_attr)]`I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions