about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorManish Goregaokar <manishsmail@gmail.com>2020-07-14 13:19:37 -0700
committerGitHub <noreply@github.com>2020-07-14 13:19:37 -0700
commitc4fcf5a7a4e70073e2229641fedfd57a63f3dfed (patch)
tree150cf5fc747ac145b72e6bf3c17c83a2585d61ff /src
parentaedb7c37b8dff924cbb569601fa2bc3219a209bb (diff)
parentd9485be595ee1655391697104b72ca005b0a9c80 (diff)
downloadrust-c4fcf5a7a4e70073e2229641fedfd57a63f3dfed.tar.gz
rust-c4fcf5a7a4e70073e2229641fedfd57a63f3dfed.zip
Rollup merge of #74336 - davidtwco:issue-73112-cross-crate-packed-type-diagnostic, r=estebank
typeck: use `item_name` in cross-crate packed diag

Fixes #73112.

This PR replaces the use of `expect_local` and `hir().get` to fetch the identifier for a ADT with `item_name` - which works across crates.
Diffstat (limited to 'src')
-rw-r--r--src/librustc_typeck/check/mod.rs47
-rw-r--r--src/test/ui/issues/auxiliary/issue-73112.rs10
-rw-r--r--src/test/ui/issues/issue-73112.rs13
-rw-r--r--src/test/ui/issues/issue-73112.stderr20
4 files changed, 65 insertions, 25 deletions
diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs
index aa768f1d251..bc01da324b6 100644
--- a/src/librustc_typeck/check/mod.rs
+++ b/src/librustc_typeck/check/mod.rs
@@ -103,7 +103,7 @@ use rustc_hir::itemlikevisit::ItemLikeVisitor;
 use rustc_hir::lang_items::{
     FutureTraitLangItem, PinTypeLangItem, SizedTraitLangItem, VaListTypeLangItem,
 };
-use rustc_hir::{ExprKind, GenericArg, HirIdMap, Item, ItemKind, Node, PatKind, QPath};
+use rustc_hir::{ExprKind, GenericArg, HirIdMap, ItemKind, Node, PatKind, QPath};
 use rustc_index::bit_set::BitSet;
 use rustc_index::vec::Idx;
 use rustc_infer::infer;
@@ -2625,34 +2625,31 @@ fn check_packed(tcx: TyCtxt<'_>, sp: Span, def: &ty::AdtDef) {
                     "packed type cannot transitively contain a `#[repr(align)]` type"
                 );
 
-                let hir = tcx.hir();
-                let hir_id = hir.as_local_hir_id(def_spans[0].0.expect_local());
-                if let Node::Item(Item { ident, .. }) = hir.get(hir_id) {
-                    err.span_note(
-                        tcx.def_span(def_spans[0].0),
-                        &format!("`{}` has a `#[repr(align)]` attribute", ident),
-                    );
-                }
+                err.span_note(
+                    tcx.def_span(def_spans[0].0),
+                    &format!(
+                        "`{}` has a `#[repr(align)]` attribute",
+                        tcx.item_name(def_spans[0].0)
+                    ),
+                );
 
                 if def_spans.len() > 2 {
                     let mut first = true;
                     for (adt_def, span) in def_spans.iter().skip(1).rev() {
-                        let hir_id = hir.as_local_hir_id(adt_def.expect_local());
-                        if let Node::Item(Item { ident, .. }) = hir.get(hir_id) {
-                            err.span_note(
-                                *span,
-                                &if first {
-                                    format!(
-                                        "`{}` contains a field of type `{}`",
-                                        tcx.type_of(def.did),
-                                        ident
-                                    )
-                                } else {
-                                    format!("...which contains a field of type `{}`", ident)
-                                },
-                            );
-                            first = false;
-                        }
+                        let ident = tcx.item_name(*adt_def);
+                        err.span_note(
+                            *span,
+                            &if first {
+                                format!(
+                                    "`{}` contains a field of type `{}`",
+                                    tcx.type_of(def.did),
+                                    ident
+                                )
+                            } else {
+                                format!("...which contains a field of type `{}`", ident)
+                            },
+                        );
+                        first = false;
                     }
                 }
 
diff --git a/src/test/ui/issues/auxiliary/issue-73112.rs b/src/test/ui/issues/auxiliary/issue-73112.rs
new file mode 100644
index 00000000000..6210c29bbdc
--- /dev/null
+++ b/src/test/ui/issues/auxiliary/issue-73112.rs
@@ -0,0 +1,10 @@
+#[repr(transparent)]
+pub struct PageTableEntry {
+    entry: u64,
+}
+
+#[repr(align(4096))]
+#[repr(C)]
+pub struct PageTable {
+    entries: [PageTableEntry; 512],
+}
diff --git a/src/test/ui/issues/issue-73112.rs b/src/test/ui/issues/issue-73112.rs
new file mode 100644
index 00000000000..cc7be9c95ae
--- /dev/null
+++ b/src/test/ui/issues/issue-73112.rs
@@ -0,0 +1,13 @@
+// aux-build:issue-73112.rs
+
+extern crate issue_73112;
+
+fn main() {
+    use issue_73112::PageTable;
+
+    #[repr(C, packed)]
+    struct SomeStruct {
+    //~^ ERROR packed type cannot transitively contain a `#[repr(align)]` type [E0588]
+        page_table: PageTable,
+    }
+}
diff --git a/src/test/ui/issues/issue-73112.stderr b/src/test/ui/issues/issue-73112.stderr
new file mode 100644
index 00000000000..5a548378c26
--- /dev/null
+++ b/src/test/ui/issues/issue-73112.stderr
@@ -0,0 +1,20 @@
+error[E0588]: packed type cannot transitively contain a `#[repr(align)]` type
+  --> $DIR/issue-73112.rs:9:5
+   |
+LL | /     struct SomeStruct {
+LL | |
+LL | |         page_table: PageTable,
+LL | |     }
+   | |_____^
+   |
+note: `PageTable` has a `#[repr(align)]` attribute
+  --> $DIR/auxiliary/issue-73112.rs:8:1
+   |
+LL | / pub struct PageTable {
+LL | |     entries: [PageTableEntry; 512],
+LL | | }
+   | |_^
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0588`.