Skip to content

Commit 367efa8

Browse files
committed
Don't allow implementing trait directly on type-alias-impl-trait
This is specifically disallowed by the RFC, but we never added a check for it. Fixes #76202
1 parent 59fb88d commit 367efa8

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

compiler/rustc_typeck/src/coherence/orphan.rs

+8
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,14 @@ impl ItemLikeVisitor<'v> for OrphanChecker<'tcx> {
230230
return;
231231
}
232232
}
233+
234+
if let ty::Opaque(def_id, _) = *trait_ref.self_ty().kind() {
235+
self.tcx
236+
.sess
237+
.struct_span_err(sp, "cannot implement trait on type alias impl trait")
238+
.span_note(self.tcx.def_span(def_id), "type alias impl trait defined here")
239+
.emit();
240+
}
233241
}
234242
}
235243

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Regression test for issue #76202
2+
// Tests that we don't ICE when we have a trait impl on a TAIT.
3+
4+
#![feature(type_alias_impl_trait)]
5+
6+
trait Dummy {}
7+
impl Dummy for () {}
8+
9+
type F = impl Dummy;
10+
fn f() -> F {}
11+
12+
trait Test {
13+
fn test(self);
14+
}
15+
16+
impl Test for F { //~ ERROR cannot implement trait
17+
fn test(self) {}
18+
}
19+
20+
fn main() {
21+
let x: F = f();
22+
x.test();
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: cannot implement trait on type alias impl trait
2+
--> $DIR/issue-76202-trait-impl-for-tait.rs:16:1
3+
|
4+
LL | impl Test for F {
5+
| ^^^^^^^^^^^^^^^
6+
|
7+
note: type alias impl trait defined here
8+
--> $DIR/issue-76202-trait-impl-for-tait.rs:9:10
9+
|
10+
LL | type F = impl Dummy;
11+
| ^^^^^^^^^^
12+
13+
error: aborting due to previous error
14+

0 commit comments

Comments
 (0)