Skip to content

Projecting <Type as Trait>::N should check that N is part of Trait #22731

Closed
@pnkfelix

Description

@pnkfelix

spawned off of #21259

test case

use std::default::Default;

trait Impled { fn f(&self) {} }
trait Vr: Impled {
    type Y: Default + Par<<Self as Impled>::Noexist>; fn g(&self) {}
}
trait Par<X:Default> {
    fn h(&self) -> (X, String) { (Default::default(), format!("Par::h")) }
}

struct S1;
impl Impled for S1 {}
impl Vr for S1 { type Y = (); }
impl Par<()> for () { }

fn main() {
    let y: <S1 as Vr>::Y = Default::default();
    let (_, s) = Par::h(&y);
    println!("{}", s);
}

The above code should be rejected by the compiler, because <Self as Impled>::Noexist is not a well-formed projection of an associated item. Instead, it currently runs.


(This bug is not quite as bad as it might seem, since one easily runs into problems if one actually attempts to program generically with this trait and associated item, as shown in the below modification to the last three lines above. This new program is correctly rejected by the compiler, though perhaps with messages that are ... subpar:)

fn foo<V:Vr>(v: V) {
    let y: <V as Vr>::Y = Default::default();
    let (_, s) = Par::h(&y);
    println!("{}", s);
}
fn main() {
    foo(S1);
}

Transcript of error output:

../src/test/compile-fail/issue-21259-e.rs:18:18: 18:24 error: the trait `core::marker::Sized` is not implemented for the type `<V as Impled>::Noexist` [E0277]
../src/test/compile-fail/issue-21259-e.rs:18     let (_, s) = Par::h(&y);
                                                              ^~~~~~
../src/test/compile-fail/issue-21259-e.rs:18:18: 18:24 note: `<V as Impled>::Noexist` does not have a constant size known at compile-time
../src/test/compile-fail/issue-21259-e.rs:18     let (_, s) = Par::h(&y);
                                                              ^~~~~~
../src/test/compile-fail/issue-21259-e.rs:18:18: 18:24 error: the trait `core::default::Default` is not implemented for the type `<V as Impled>::Noexist` [E0277]
../src/test/compile-fail/issue-21259-e.rs:18     let (_, s) = Par::h(&y);
                                                              ^~~~~~
error: aborting due to 2 previous errors

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions