From 49b1b4c438a87f54ebbae90b9b21ccce7622f8e7 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Sun, 17 May 2020 17:02:57 +0200 Subject: [PATCH] more `LocalDefId`s --- src/librustc_interface/passes.rs | 2 +- src/librustc_middle/query/mod.rs | 10 ++++++++-- src/librustc_mir/transform/check_unsafety.rs | 17 +++++++++-------- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/librustc_interface/passes.rs b/src/librustc_interface/passes.rs index 214701db724ec..c06fd91133b5c 100644 --- a/src/librustc_interface/passes.rs +++ b/src/librustc_interface/passes.rs @@ -838,7 +838,7 @@ fn analysis(tcx: TyCtxt<'_>, cnum: CrateNum) -> Result<()> { sess.time("MIR_effect_checking", || { for def_id in tcx.body_owners() { - mir::transform::check_unsafety::check_unsafety(tcx, def_id.to_def_id()) + mir::transform::check_unsafety::check_unsafety(tcx, def_id) } }); diff --git a/src/librustc_middle/query/mod.rs b/src/librustc_middle/query/mod.rs index 85451bf6538e4..59b6f5e529baa 100644 --- a/src/librustc_middle/query/mod.rs +++ b/src/librustc_middle/query/mod.rs @@ -386,8 +386,14 @@ rustc_queries! { storage(ArenaCacheSelector<'tcx>) } - /// HACK: when evaluated, this reports a "unsafe derive on repr(packed)" error - query unsafe_derive_on_repr_packed(_: DefId) -> () {} + /// HACK: when evaluated, this reports a "unsafe derive on repr(packed)" error. + /// + /// Unsafety checking is executed for each method separately, but we only want + /// to emit this error once per derive. As there are some impls with multiple + /// methods, we use a query for deduplication. + query unsafe_derive_on_repr_packed(key: LocalDefId) -> () { + desc { |tcx| "processing `{}`", tcx.def_path_str(key.to_def_id()) } + } /// The signature of functions and closures. query fn_sig(_: DefId) -> ty::PolyFnSig<'tcx> {} diff --git a/src/librustc_mir/transform/check_unsafety.rs b/src/librustc_mir/transform/check_unsafety.rs index 1f01bc0e19513..e32bccc85ee6f 100644 --- a/src/librustc_mir/transform/check_unsafety.rs +++ b/src/librustc_mir/transform/check_unsafety.rs @@ -579,8 +579,8 @@ fn unsafety_check_result(tcx: TyCtxt<'_>, def_id: LocalDefId) -> UnsafetyCheckRe } } -fn unsafe_derive_on_repr_packed(tcx: TyCtxt<'_>, def_id: DefId) { - let lint_hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); +fn unsafe_derive_on_repr_packed(tcx: TyCtxt<'_>, def_id: LocalDefId) { + let lint_hir_id = tcx.hir().as_local_hir_id(def_id); tcx.struct_span_lint_hir(SAFE_PACKED_BORROWS, lint_hir_id, tcx.def_span(def_id), |lint| { // FIXME: when we make this a hard error, this should have its @@ -659,16 +659,15 @@ fn builtin_derive_def_id(tcx: TyCtxt<'_>, def_id: DefId) -> Option { } } -pub fn check_unsafety(tcx: TyCtxt<'_>, def_id: DefId) { +pub fn check_unsafety(tcx: TyCtxt<'_>, def_id: LocalDefId) { debug!("check_unsafety({:?})", def_id); // closures are handled by their parent fn. - if tcx.is_closure(def_id) { + if tcx.is_closure(def_id.to_def_id()) { return; } - let UnsafetyCheckResult { violations, unsafe_blocks } = - tcx.unsafety_check_result(def_id.expect_local()); + let UnsafetyCheckResult { violations, unsafe_blocks } = tcx.unsafety_check_result(def_id); for &UnsafetyViolation { source_info, lint_root, description, details, kind } in violations.iter() @@ -693,8 +692,10 @@ pub fn check_unsafety(tcx: TyCtxt<'_>, def_id: DefId) { .emit(); } UnsafetyViolationKind::BorrowPacked => { - if let Some(impl_def_id) = builtin_derive_def_id(tcx, def_id) { - tcx.ensure().unsafe_derive_on_repr_packed(impl_def_id); + if let Some(impl_def_id) = builtin_derive_def_id(tcx, def_id.to_def_id()) { + // If a method is defined in the local crate, + // the impl containing that method should also be. + tcx.ensure().unsafe_derive_on_repr_packed(impl_def_id.expect_local()); } else { tcx.struct_span_lint_hir( SAFE_PACKED_BORROWS,