Skip to content

Commit 4f8b62a

Browse files
committed
added error for closures case in impl
1 parent 42d009c commit 4f8b62a

File tree

6 files changed

+56
-9
lines changed

6 files changed

+56
-9
lines changed

compiler/rustc_hir_analysis/messages.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ hir_analysis_cannot_capture_late_bound_lifetime =
6565
cannot capture late-bound lifetime in {$what}
6666
.label = lifetime defined here
6767
68+
hir_analysis_monomorphization_inherent_impl_for_closure =
69+
cannot define inherent `impl` for closure types
70+
6871
hir_analysis_cannot_capture_late_bound_ty =
6972
cannot capture late-bound type parameter in {$what}
7073
.label = parameter defined here

compiler/rustc_hir_analysis/src/coherence/inherent_impls.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,10 @@ impl<'tcx> InherentCollect<'tcx> {
195195
| ty::Closure(..)
196196
| ty::CoroutineClosure(..)
197197
| ty::Coroutine(..)
198-
| ty::CoroutineWitness(..)
199-
| ty::Alias(ty::Free, _)
200-
| ty::Bound(..)
201-
| ty::Placeholder(_)
202-
| ty::Infer(_) => {
198+
| ty::CoroutineWitness(..) => {
199+
Err(self.tcx.dcx().emit_err(errors::InherentImplForClosure { span: item_span }))
200+
}
201+
ty::Alias(ty::Free, _) | ty::Bound(..) | ty::Placeholder(_) | ty::Infer(_) => {
203202
bug!("unexpected impl self type of impl: {:?} {:?}", id, self_ty);
204203
}
205204
// We could bail out here, but that will silence other useful errors.

compiler/rustc_hir_analysis/src/coherence/orphan.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,12 @@ pub(crate) fn orphan_check_impl(
230230
ty::Closure(..)
231231
| ty::CoroutineClosure(..)
232232
| ty::Coroutine(..)
233-
| ty::CoroutineWitness(..)
234-
| ty::Bound(..)
235-
| ty::Placeholder(..)
236-
| ty::Infer(..) => {
233+
| ty::CoroutineWitness(..) => {
234+
return Err(tcx
235+
.dcx()
236+
.emit_err(errors::InherentImplForClosure { span: tcx.def_span(impl_def_id) }));
237+
}
238+
ty::Bound(..) | ty::Placeholder(..) | ty::Infer(..) => {
237239
let sp = tcx.def_span(impl_def_id);
238240
span_bug!(sp, "weird self type for autotrait impl")
239241
}

compiler/rustc_hir_analysis/src/errors.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ pub(crate) struct AssocKindMismatchWrapInBracesSugg {
5959
pub hi: Span,
6060
}
6161

62+
#[derive(Diagnostic)]
63+
#[diag(hir_analysis_monomorphization_inherent_impl_for_closure)]
64+
pub struct InherentImplForClosure {
65+
#[primary_span]
66+
pub span: Span,
67+
}
68+
6269
#[derive(Diagnostic)]
6370
#[diag(hir_analysis_assoc_item_is_private, code = E0624)]
6471
pub(crate) struct AssocItemIsPrivate {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
impl typeof(|| {}) {}
2+
//~^ ERROR `typeof` is a reserved keyword but unimplemented
3+
//~| ERROR cannot define inherent `impl` for closure types
4+
5+
unsafe impl Send for typeof(|| {}) {}
6+
//~^ ERROR `typeof` is a reserved keyword but unimplemented
7+
//~| ERROR cannot define inherent `impl` for closure types
8+
9+
fn main() {}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error[E0516]: `typeof` is a reserved keyword but unimplemented
2+
--> $DIR/impl-closure-147146.rs:1:6
3+
|
4+
LL | impl typeof(|| {}) {}
5+
| ^^^^^^^^^^^^^ reserved keyword
6+
7+
error[E0516]: `typeof` is a reserved keyword but unimplemented
8+
--> $DIR/impl-closure-147146.rs:5:22
9+
|
10+
LL | unsafe impl Send for typeof(|| {}) {}
11+
| ^^^^^^^^^^^^^ reserved keyword
12+
13+
error: cannot define inherent `impl` for closure types
14+
--> $DIR/impl-closure-147146.rs:5:1
15+
|
16+
LL | unsafe impl Send for typeof(|| {}) {}
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18+
19+
error: cannot define inherent `impl` for closure types
20+
--> $DIR/impl-closure-147146.rs:1:1
21+
|
22+
LL | impl typeof(|| {}) {}
23+
| ^^^^^^^^^^^^^^^^^^
24+
25+
error: aborting due to 4 previous errors
26+
27+
For more information about this error, try `rustc --explain E0516`.

0 commit comments

Comments
 (0)