From 07b1912acc54949b4077138b6f1adac376b94e92 Mon Sep 17 00:00:00 2001 From: Eric Mark Martin Date: Sun, 2 Jul 2023 18:44:26 -0400 Subject: [PATCH] refactor --- compiler/rustc_mir_build/src/thir/cx/expr.rs | 36 +++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_mir_build/src/thir/cx/expr.rs b/compiler/rustc_mir_build/src/thir/cx/expr.rs index 26192c8e3c6..dc1c0966fe7 100644 --- a/compiler/rustc_mir_build/src/thir/cx/expr.rs +++ b/compiler/rustc_mir_build/src/thir/cx/expr.rs @@ -208,17 +208,18 @@ impl<'tcx> Cx<'tcx> { // so we wouldn't have to compute and store the actual value let hir::ExprKind::Path(ref qpath) = source.kind else { - return ExprKind::Cast { source: self.mirror_expr(source)}; + return ExprKind::Cast { source: self.mirror_expr(source) }; }; let res = self.typeck_results().qpath_res(qpath, source.hir_id); let ty = self.typeck_results().node_type(source.hir_id); let ty::Adt(adt_def, substs) = ty.kind() else { - return ExprKind::Cast { source: self.mirror_expr(source)}; + return ExprKind::Cast { source: self.mirror_expr(source) }; }; - let Res::Def(DefKind::Ctor(CtorOf::Variant, CtorKind::Const), variant_ctor_id) = res else { - return ExprKind::Cast { source: self.mirror_expr(source)}; + let Res::Def(DefKind::Ctor(CtorOf::Variant, CtorKind::Const), variant_ctor_id) = res + else { + return ExprKind::Cast { source: self.mirror_expr(source) }; }; let idx = adt_def.variant_index_with_ctor_id(variant_ctor_id); @@ -351,28 +352,29 @@ impl<'tcx> Cx<'tcx> { }); } } - let adt_data = if let hir::ExprKind::Path(qpath) = fun.kind { + + // Tuple-like ADTs are represented as ExprKind::Call. We convert them here. + let adt_data = if let hir::ExprKind::Path(ref qpath) = fun.kind + && let Some(adt_def) = expr_ty.ty_adt_def() { match qpath { - // Tuple-like ADTs are represented as ExprKind::Call. We convert them here. hir::QPath::Resolved(_, ref path) => { - expr_ty.ty_adt_def().and_then(|adt_def| match path.res { + match path.res { Res::Def(DefKind::Ctor(_, CtorKind::Fn), ctor_id) => { Some((adt_def, adt_def.variant_index_with_ctor_id(ctor_id))) } Res::SelfCtor(..) => Some((adt_def, FIRST_VARIANT)), _ => None, - }) + } } hir::QPath::TypeRelative(_ty, _) => { - expr_ty.ty_adt_def().and_then(|adt_def| { - if let Some((DefKind::Ctor(_, CtorKind::Fn), ctor_id)) = - self.typeck_results().type_dependent_def(fun.hir_id) - { - Some((adt_def, adt_def.variant_index_with_ctor_id(ctor_id))) - } else { - None - } - }) + if let Some((DefKind::Ctor(_, CtorKind::Fn), ctor_id)) = + self.typeck_results().type_dependent_def(fun.hir_id) + { + Some((adt_def, adt_def.variant_index_with_ctor_id(ctor_id))) + } else { + None + } + } _ => None, }