From 88cd5f12b41146326c3206d7a2b29da6ea228c9b Mon Sep 17 00:00:00 2001 From: bohan Date: Sat, 8 Jul 2023 00:00:39 +0800 Subject: [PATCH] fix: skip cast if `Ctor` has an invalid type --- compiler/rustc_hir_analysis/src/collect/type_of.rs | 13 ++++++++++--- tests/ui/cast/issue-112630.rs | 7 +++++++ tests/ui/cast/issue-112630.stderr | 14 ++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 tests/ui/cast/issue-112630.rs create mode 100644 tests/ui/cast/issue-112630.stderr diff --git a/compiler/rustc_hir_analysis/src/collect/type_of.rs b/compiler/rustc_hir_analysis/src/collect/type_of.rs index 3755342aef5df..9e5a3d41323ff 100644 --- a/compiler/rustc_hir_analysis/src/collect/type_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/type_of.rs @@ -483,9 +483,16 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder { tcx.type_of(tcx.hir().get_parent_item(hir_id)).subst_identity() } - VariantData::Tuple(..) => { - let substs = InternalSubsts::identity_for_item(tcx, def_id); - Ty::new_fn_def(tcx, def_id.to_def_id(), substs) + VariantData::Tuple(fields, ..) => { + if let Some(Err(error)) = fields.iter().find_map(|field| { + let field_ty = tcx.type_of(field.def_id).skip_binder(); + field_ty.references_error().then_some(field_ty.error_reported()) + }) { + Ty::new_error(tcx, error) + } else { + let substs = InternalSubsts::identity_for_item(tcx, def_id); + Ty::new_fn_def(tcx, def_id.to_def_id(), substs) + } } }, diff --git a/tests/ui/cast/issue-112630.rs b/tests/ui/cast/issue-112630.rs new file mode 100644 index 0000000000000..2e7ad50e5124a --- /dev/null +++ b/tests/ui/cast/issue-112630.rs @@ -0,0 +1,7 @@ +enum Foo { + Bar(B) //~ ERROR cannot find type `B` in this scope [E0412] +} + +fn main() { + let _ = [0; Foo::Bar as usize]; +} diff --git a/tests/ui/cast/issue-112630.stderr b/tests/ui/cast/issue-112630.stderr new file mode 100644 index 0000000000000..b25bee9832135 --- /dev/null +++ b/tests/ui/cast/issue-112630.stderr @@ -0,0 +1,14 @@ +error[E0412]: cannot find type `B` in this scope + --> $DIR/issue-112630.rs:2:9 + | +LL | Bar(B) + | ^ not found in this scope + | +help: you might be missing a type parameter + | +LL | enum Foo { + | +++ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0412`.