From 20d6941be7039eb7fad9a4f893792a77d65bc792 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Sat, 4 Jul 2020 14:02:41 +0200 Subject: [PATCH 1/3] ConstCx to LocalDefId --- src/librustc_mir/const_eval/eval_queries.rs | 8 ++++---- src/librustc_mir/transform/check_consts/mod.rs | 4 ++-- .../check_consts/post_drop_elaboration.rs | 8 +------- .../transform/check_consts/qualifs.rs | 2 +- .../transform/check_consts/validation.rs | 18 +++++++++--------- src/librustc_mir/transform/mod.rs | 5 +++-- src/librustc_mir/transform/promote_consts.rs | 9 +++++---- 7 files changed, 25 insertions(+), 29 deletions(-) diff --git a/src/librustc_mir/const_eval/eval_queries.rs b/src/librustc_mir/const_eval/eval_queries.rs index d62300b3f5541..75067ffa4b338 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 e4aa88e3c20a7..81c1b0b5bd49f 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 1fd907f89fe18..33fe2758c28fe 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 e2893e81a2ce6..3dddd9c1c1766 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 f00fc96e5915a..5cb161ebcfb5e 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 8ca240d2c7da7..14c3093e1e9a1 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 bd7ebaa01f81b..5aa67227994d9 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, }; From dbcabc248c0e035f916a5f4bb64b714640a5c338 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Sat, 4 Jul 2020 14:14:41 +0200 Subject: [PATCH 2/3] instantiate_opaque_types LocalDefId --- .../borrow_check/type_check/input_output.rs | 4 ++-- src/librustc_mir/borrow_check/type_check/mod.rs | 8 ++++---- src/librustc_trait_selection/opaque_types.rs | 17 ++++++----------- src/librustc_typeck/check/mod.rs | 6 +++--- 4 files changed, 15 insertions(+), 20 deletions(-) diff --git a/src/librustc_mir/borrow_check/type_check/input_output.rs b/src/librustc_mir/borrow_check/type_check/input_output.rs index edd2dc3c2de55..8cebd3679345f 100644 --- a/src/librustc_mir/borrow_check/type_check/input_output.rs +++ b/src/librustc_mir/borrow_check/type_check/input_output.rs @@ -122,7 +122,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { if let Err(terr) = self.eq_opaque_type_and_type( mir_output_ty, normalized_output_ty, - self.mir_def_id.to_def_id(), + self.mir_def_id, Locations::All(output_span), ConstraintCategory::BoringNoLocation, ) { @@ -145,7 +145,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { if let Err(err) = self.eq_opaque_type_and_type( mir_output_ty, user_provided_output_ty, - self.mir_def_id.to_def_id(), + self.mir_def_id, Locations::All(output_span), ConstraintCategory::BoringNoLocation, ) { diff --git a/src/librustc_mir/borrow_check/type_check/mod.rs b/src/librustc_mir/borrow_check/type_check/mod.rs index 0e35cafb9f3e9..3532b6de003ba 100644 --- a/src/librustc_mir/borrow_check/type_check/mod.rs +++ b/src/librustc_mir/borrow_check/type_check/mod.rs @@ -1144,7 +1144,8 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { // When you have `let x: impl Foo = ...` in a closure, // the resulting inferend values are stored with the // def-id of the base function. - let parent_def_id = self.tcx().closure_base_def_id(self.mir_def_id.to_def_id()); + let parent_def_id = + self.tcx().closure_base_def_id(self.mir_def_id.to_def_id()).expect_local(); return self.eq_opaque_type_and_type(sub, sup, parent_def_id, locations, category); } else { return Err(terr); @@ -1208,7 +1209,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { &mut self, revealed_ty: Ty<'tcx>, anon_ty: Ty<'tcx>, - anon_owner_def_id: DefId, + anon_owner_def_id: LocalDefId, locations: Locations, category: ConstraintCategory, ) -> Fallible<()> { @@ -1238,8 +1239,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { let tcx = infcx.tcx; let param_env = self.param_env; let body = self.body; - let concrete_opaque_types = - &tcx.typeck_tables_of(anon_owner_def_id.expect_local()).concrete_opaque_types; + let concrete_opaque_types = &tcx.typeck_tables_of(anon_owner_def_id).concrete_opaque_types; let mut opaque_type_values = Vec::new(); debug!("eq_opaque_type_and_type: mir_def_id={:?}", self.mir_def_id); diff --git a/src/librustc_trait_selection/opaque_types.rs b/src/librustc_trait_selection/opaque_types.rs index ce478de7c755c..b60531833bd41 100644 --- a/src/librustc_trait_selection/opaque_types.rs +++ b/src/librustc_trait_selection/opaque_types.rs @@ -108,7 +108,7 @@ pub enum GenerateMemberConstraints { pub trait InferCtxtExt<'tcx> { fn instantiate_opaque_types>( &self, - parent_def_id: DefId, + parent_def_id: LocalDefId, body_id: hir::HirId, param_env: ty::ParamEnv<'tcx>, value: &T, @@ -184,7 +184,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { /// - `value_span` -- the span where the value came from, used in error reporting fn instantiate_opaque_types>( &self, - parent_def_id: DefId, + parent_def_id: LocalDefId, body_id: hir::HirId, param_env: ty::ParamEnv<'tcx>, value: &T, @@ -986,7 +986,7 @@ impl TypeFolder<'tcx> for ReverseMapper<'tcx> { struct Instantiator<'a, 'tcx> { infcx: &'a InferCtxt<'a, 'tcx>, - parent_def_id: DefId, + parent_def_id: LocalDefId, body_id: hir::HirId, param_env: ty::ParamEnv<'tcx>, value_span: Span, @@ -1043,8 +1043,7 @@ impl<'a, 'tcx> Instantiator<'a, 'tcx> { let parent_def_id = self.parent_def_id; let def_scope_default = || { let opaque_parent_hir_id = tcx.hir().get_parent_item(opaque_hir_id); - parent_def_id - == tcx.hir().local_def_id(opaque_parent_hir_id).to_def_id() + parent_def_id == tcx.hir().local_def_id(opaque_parent_hir_id) }; let (in_definition_scope, origin) = match tcx.hir().find(opaque_hir_id) { Some(Node::Item(item)) => match item.kind { @@ -1053,18 +1052,14 @@ impl<'a, 'tcx> Instantiator<'a, 'tcx> { impl_trait_fn: Some(parent), origin, .. - }) => (parent == self.parent_def_id, origin), + }) => (parent == self.parent_def_id.to_def_id(), origin), // Named `type Foo = impl Bar;` hir::ItemKind::OpaqueTy(hir::OpaqueTy { impl_trait_fn: None, origin, .. }) => ( - may_define_opaque_type( - tcx, - self.parent_def_id.expect_local(), - opaque_hir_id, - ), + may_define_opaque_type(tcx, self.parent_def_id, opaque_hir_id), origin, ), _ => (def_scope_default(), hir::OpaqueTyOrigin::Misc), diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 58fd0f989c678..e564b01b11721 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -1321,8 +1321,8 @@ fn check_fn<'a, 'tcx>( fcx.resume_yield_tys = Some((resume_ty, yield_ty)); } - let outer_def_id = tcx.closure_base_def_id(hir.local_def_id(fn_id).to_def_id()); - let outer_hir_id = hir.as_local_hir_id(outer_def_id.expect_local()); + let outer_def_id = tcx.closure_base_def_id(hir.local_def_id(fn_id).to_def_id()).expect_local(); + let outer_hir_id = hir.as_local_hir_id(outer_def_id); GatherLocalsVisitor { fcx: &fcx, parent_id: outer_hir_id }.visit_body(body); // C-variadic fns also have a `VaList` input that's not listed in `fn_sig` @@ -3427,7 +3427,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let (value, opaque_type_map) = self.register_infer_ok_obligations(self.instantiate_opaque_types( - parent_def_id.to_def_id(), + parent_def_id, self.body_id, self.param_env, value, From f5305c3d16fb8221463ef604a1ea5ac11506ba6d Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Sat, 4 Jul 2020 16:20:27 +0200 Subject: [PATCH 3/3] nit Co-authored-by: varkor --- .../transform/check_consts/post_drop_elaboration.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 33fe2758c28fe..55075b3ab5e99 100644 --- a/src/librustc_mir/transform/check_consts/post_drop_elaboration.rs +++ b/src/librustc_mir/transform/check_consts/post_drop_elaboration.rs @@ -29,7 +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, const_kind, param_env: tcx.param_env(def_id) }; + let ccx = ConstCx { body, tcx, def_id, const_kind, param_env: tcx.param_env(def_id) }; let mut visitor = CheckLiveDrops { ccx: &ccx, qualifs: Qualifs::default() };