Rollup merge of #147219 - Kivooeo:typeof-is-imposter, r=jdonszelmann

Add proper error handling for closure in impl

Fixes https://github.com/rust-lang/rust/issues/147146
Fixes https://github.com/rust-lang/rust/issues/146620

Not sure if it can cause any regressions or anything, as for test also have no idea where to store this one

cc ```@theemathas```

r? compiler
This commit is contained in:
Matthias Krüger
2025-10-01 18:42:37 +02:00
committed by GitHub
4 changed files with 32 additions and 9 deletions

View File

@@ -195,11 +195,10 @@ impl<'tcx> InherentCollect<'tcx> {
| ty::Closure(..)
| ty::CoroutineClosure(..)
| ty::Coroutine(..)
| ty::CoroutineWitness(..)
| ty::Alias(ty::Free, _)
| ty::Bound(..)
| ty::Placeholder(_)
| ty::Infer(_) => {
| ty::CoroutineWitness(..) => {
Err(self.tcx.dcx().delayed_bug("cannot define inherent `impl` for closure types"))
}
ty::Alias(ty::Free, _) | ty::Bound(..) | ty::Placeholder(_) | ty::Infer(_) => {
bug!("unexpected impl self type of impl: {:?} {:?}", id, self_ty);
}
// We could bail out here, but that will silence other useful errors.

View File

@@ -230,10 +230,12 @@ pub(crate) fn orphan_check_impl(
ty::Closure(..)
| ty::CoroutineClosure(..)
| ty::Coroutine(..)
| ty::CoroutineWitness(..)
| ty::Bound(..)
| ty::Placeholder(..)
| ty::Infer(..) => {
| ty::CoroutineWitness(..) => {
return Err(tcx
.dcx()
.delayed_bug("cannot define inherent `impl` for closure types"));
}
ty::Bound(..) | ty::Placeholder(..) | ty::Infer(..) => {
let sp = tcx.def_span(impl_def_id);
span_bug!(sp, "weird self type for autotrait impl")
}

View File

@@ -0,0 +1,7 @@
impl typeof(|| {}) {}
//~^ ERROR `typeof` is a reserved keyword but unimplemented
unsafe impl Send for typeof(|| {}) {}
//~^ ERROR `typeof` is a reserved keyword but unimplemented
fn main() {}

View File

@@ -0,0 +1,15 @@
error[E0516]: `typeof` is a reserved keyword but unimplemented
--> $DIR/impl-closure-147146.rs:1:6
|
LL | impl typeof(|| {}) {}
| ^^^^^^^^^^^^^ reserved keyword
error[E0516]: `typeof` is a reserved keyword but unimplemented
--> $DIR/impl-closure-147146.rs:4:22
|
LL | unsafe impl Send for typeof(|| {}) {}
| ^^^^^^^^^^^^^ reserved keyword
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0516`.