about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBastian Kauschke <bastian_kauschke@hotmail.de>2020-05-17 17:02:57 +0200
committerBastian Kauschke <bastian_kauschke@hotmail.de>2020-05-30 12:22:29 +0200
commit49b1b4c438a87f54ebbae90b9b21ccce7622f8e7 (patch)
treea18c12b5e3e10736c3d293fffc5aa62d43a1d536
parent0e9e4083100aa3ebf09b8f1ace0348cb37475eb9 (diff)
downloadrust-49b1b4c438a87f54ebbae90b9b21ccce7622f8e7.tar.gz
rust-49b1b4c438a87f54ebbae90b9b21ccce7622f8e7.zip
more `LocalDefId`s
-rw-r--r--src/librustc_interface/passes.rs2
-rw-r--r--src/librustc_middle/query/mod.rs10
-rw-r--r--src/librustc_mir/transform/check_unsafety.rs17
3 files changed, 18 insertions, 11 deletions
diff --git a/src/librustc_interface/passes.rs b/src/librustc_interface/passes.rs
index 214701db724..c06fd91133b 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 85451bf6538..59b6f5e529b 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 1f01bc0e195..e32bccc85ee 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<DefId> {
     }
 }
 
-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,