diff --git a/src/librustc_mir/const_eval/eval_queries.rs b/src/librustc_mir/const_eval/eval_queries.rs index d62300b3f554..75067ffa4b33 100644 --- a/src/librustc_mir/const_eval/eval_queries.rs +++ b/src/librustc_mir/const_eval/eval_queries.rs @@ -334,9 +334,9 @@ pub fn const_eval_raw_provider<'tcx>( } v - } else if def_id.is_local() { + } else if let Some(def_id) = def_id.as_local() { // constant defined in this crate, we can figure out a lint level! - match tcx.def_kind(def_id) { + match tcx.def_kind(def_id.to_def_id()) { // constants never produce a hard error at the definition site. Anything else is // a backwards compatibility hazard (and will break old versions of winapi for // sure) @@ -346,7 +346,7 @@ pub fn const_eval_raw_provider<'tcx>( // validation thus preventing such a hard error from being a backwards // compatibility hazard DefKind::Const | DefKind::AssocConst => { - let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); + let hir_id = tcx.hir().as_local_hir_id(def_id); err.report_as_lint( tcx.at(tcx.def_span(def_id)), "any use of this value will cause an error", @@ -369,7 +369,7 @@ pub fn const_eval_raw_provider<'tcx>( err.report_as_lint( tcx.at(span), "reaching this expression at runtime will panic or abort", - tcx.hir().as_local_hir_id(def_id.expect_local()), + tcx.hir().as_local_hir_id(def_id), Some(err.span), ) } diff --git a/src/librustc_mir/transform/check_consts/mod.rs b/src/librustc_mir/transform/check_consts/mod.rs index e4aa88e3c20a..81c1b0b5bd49 100644 --- a/src/librustc_mir/transform/check_consts/mod.rs +++ b/src/librustc_mir/transform/check_consts/mod.rs @@ -22,7 +22,7 @@ pub mod validation; pub struct ConstCx<'mir, 'tcx> { pub body: &'mir mir::Body<'tcx>, pub tcx: TyCtxt<'tcx>, - pub def_id: DefId, + pub def_id: LocalDefId, pub param_env: ty::ParamEnv<'tcx>, pub const_kind: Option, } @@ -40,7 +40,7 @@ impl ConstCx<'mir, 'tcx> { param_env: ty::ParamEnv<'tcx>, ) -> Self { let const_kind = tcx.hir().body_const_context(def_id); - ConstCx { body, tcx, def_id: def_id.to_def_id(), param_env, const_kind } + ConstCx { body, tcx, def_id: def_id, param_env, const_kind } } /// Returns the kind of const context this `Item` represents (`const`, `static`, etc.). diff --git a/src/librustc_mir/transform/check_consts/post_drop_elaboration.rs b/src/librustc_mir/transform/check_consts/post_drop_elaboration.rs index 1fd907f89fe1..33fe2758c28f 100644 --- a/src/librustc_mir/transform/check_consts/post_drop_elaboration.rs +++ b/src/librustc_mir/transform/check_consts/post_drop_elaboration.rs @@ -29,13 +29,7 @@ pub fn check_live_drops(tcx: TyCtxt<'tcx>, def_id: LocalDefId, body: &mir::Body< return; } - let ccx = ConstCx { - body, - tcx, - def_id: def_id.to_def_id(), - const_kind, - param_env: tcx.param_env(def_id), - }; + let ccx = ConstCx { body, tcx, def_id: def_id, const_kind, param_env: tcx.param_env(def_id) }; let mut visitor = CheckLiveDrops { ccx: &ccx, qualifs: Qualifs::default() }; diff --git a/src/librustc_mir/transform/check_consts/qualifs.rs b/src/librustc_mir/transform/check_consts/qualifs.rs index e2893e81a2ce..3dddd9c1c176 100644 --- a/src/librustc_mir/transform/check_consts/qualifs.rs +++ b/src/librustc_mir/transform/check_consts/qualifs.rs @@ -126,7 +126,7 @@ impl Qualif for CustomEq { // because that component may be part of an enum variant (e.g., // `Option::::Some`), in which case some values of this type may be // structural-match (`Option::None`). - let id = cx.tcx.hir().local_def_id_to_hir_id(cx.def_id.as_local().unwrap()); + let id = cx.tcx.hir().local_def_id_to_hir_id(cx.def_id); traits::search_for_structural_match_violation(id, cx.body.span, cx.tcx, ty).is_some() } diff --git a/src/librustc_mir/transform/check_consts/validation.rs b/src/librustc_mir/transform/check_consts/validation.rs index f00fc96e5915..5cb161ebcfb5 100644 --- a/src/librustc_mir/transform/check_consts/validation.rs +++ b/src/librustc_mir/transform/check_consts/validation.rs @@ -56,7 +56,7 @@ impl Qualifs<'mir, 'tcx> { // without breaking stable code? MaybeMutBorrowedLocals::mut_borrows_only(tcx, &body, param_env) .unsound_ignore_borrow_on_drop() - .into_engine(tcx, &body, def_id) + .into_engine(tcx, &body, def_id.to_def_id()) .iterate_to_fixpoint() .into_results_cursor(&body) }); @@ -83,7 +83,7 @@ impl Qualifs<'mir, 'tcx> { let ConstCx { tcx, body, def_id, .. } = *ccx; FlowSensitiveAnalysis::new(NeedsDrop, ccx) - .into_engine(tcx, &body, def_id) + .into_engine(tcx, &body, def_id.to_def_id()) .iterate_to_fixpoint() .into_results_cursor(&body) }); @@ -110,7 +110,7 @@ impl Qualifs<'mir, 'tcx> { let ConstCx { tcx, body, def_id, .. } = *ccx; FlowSensitiveAnalysis::new(HasMutInterior, ccx) - .into_engine(tcx, &body, def_id) + .into_engine(tcx, &body, def_id.to_def_id()) .iterate_to_fixpoint() .into_results_cursor(&body) }); @@ -153,7 +153,7 @@ impl Qualifs<'mir, 'tcx> { hir::ConstContext::Const | hir::ConstContext::Static(_) => { let mut cursor = FlowSensitiveAnalysis::new(CustomEq, ccx) - .into_engine(ccx.tcx, &ccx.body, ccx.def_id) + .into_engine(ccx.tcx, &ccx.body, ccx.def_id.to_def_id()) .iterate_to_fixpoint() .into_results_cursor(&ccx.body); @@ -195,13 +195,13 @@ impl Validator<'mir, 'tcx> { let ConstCx { tcx, body, def_id, const_kind, .. } = *self.ccx; let use_min_const_fn_checks = (const_kind == Some(hir::ConstContext::ConstFn) - && crate::const_eval::is_min_const_fn(tcx, def_id)) + && crate::const_eval::is_min_const_fn(tcx, def_id.to_def_id())) && !tcx.sess.opts.debugging_opts.unleash_the_miri_inside_of_you; if use_min_const_fn_checks { // Enforce `min_const_fn` for stable `const fn`s. use crate::transform::qualify_min_const_fn::is_min_const_fn; - if let Err((span, err)) = is_min_const_fn(tcx, def_id, &body) { + if let Err((span, err)) = is_min_const_fn(tcx, def_id.to_def_id(), &body) { error_min_const_fn_violation(tcx, span, err); return; } @@ -212,10 +212,10 @@ impl Validator<'mir, 'tcx> { // Ensure that the end result is `Sync` in a non-thread local `static`. let should_check_for_sync = const_kind == Some(hir::ConstContext::Static(hir::Mutability::Not)) - && !tcx.is_thread_local_static(def_id); + && !tcx.is_thread_local_static(def_id.to_def_id()); if should_check_for_sync { - let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); + let hir_id = tcx.hir().as_local_hir_id(def_id); check_return_ty_is_sync(tcx, &body, hir_id); } } @@ -535,7 +535,7 @@ impl Visitor<'tcx> for Validator<'mir, 'tcx> { // `#[allow_internal_unstable]`. use crate::transform::qualify_min_const_fn::lib_feature_allowed; if !self.span.allows_unstable(feature) - && !lib_feature_allowed(self.tcx, self.def_id, feature) + && !lib_feature_allowed(self.tcx, self.def_id.to_def_id(), feature) { self.check_op(ops::FnCallUnstable(def_id, feature)); } diff --git a/src/librustc_mir/transform/mod.rs b/src/librustc_mir/transform/mod.rs index 8ca240d2c7da..14c3093e1e9a 100644 --- a/src/librustc_mir/transform/mod.rs +++ b/src/librustc_mir/transform/mod.rs @@ -203,7 +203,8 @@ pub fn run_passes( } fn mir_const_qualif(tcx: TyCtxt<'_>, def_id: DefId) -> ConstQualifs { - let const_kind = tcx.hir().body_const_context(def_id.expect_local()); + let def_id = def_id.expect_local(); + let const_kind = tcx.hir().body_const_context(def_id); // No need to const-check a non-const `fn`. if const_kind.is_none() { @@ -214,7 +215,7 @@ fn mir_const_qualif(tcx: TyCtxt<'_>, def_id: DefId) -> ConstQualifs { // cannot yet be stolen), because `mir_validated()`, which steals // from `mir_const(), forces this query to execute before // performing the steal. - let body = &tcx.mir_const(def_id).borrow(); + let body = &tcx.mir_const(def_id.to_def_id()).borrow(); if body.return_ty().references_error() { tcx.sess.delay_span_bug(body.span, "mir_const_qualif: MIR had errors"); diff --git a/src/librustc_mir/transform/promote_consts.rs b/src/librustc_mir/transform/promote_consts.rs index bd7ebaa01f81..5aa67227994d 100644 --- a/src/librustc_mir/transform/promote_consts.rs +++ b/src/librustc_mir/transform/promote_consts.rs @@ -60,15 +60,16 @@ impl<'tcx> MirPass<'tcx> for PromoteTemps<'tcx> { return; } - let def_id = src.def_id(); + let def_id = src.def_id().expect_local(); let mut rpo = traversal::reverse_postorder(body); - let ccx = ConstCx::new(tcx, def_id.expect_local(), body); + let ccx = ConstCx::new(tcx, def_id, body); let (temps, all_candidates) = collect_temps_and_candidates(&ccx, &mut rpo); let promotable_candidates = validate_candidates(&ccx, &temps, &all_candidates); - let promoted = promote_candidates(def_id, body, tcx, temps, promotable_candidates); + let promoted = + promote_candidates(def_id.to_def_id(), body, tcx, temps, promotable_candidates); self.promoted_fragments.set(promoted); } } @@ -724,7 +725,7 @@ impl<'tcx> Validator<'_, 'tcx> { ty::FnDef(def_id, _) => { is_const_fn(self.tcx, def_id) || is_unstable_const_fn(self.tcx, def_id).is_some() - || is_lang_panic_fn(self.tcx, self.def_id) + || is_lang_panic_fn(self.tcx, self.def_id.to_def_id()) } _ => false, };