Skip to content

Commit c4fcf5a

Browse files
authored
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.
2 parents aedb7c3 + d9485be commit c4fcf5a

File tree

4 files changed

+65
-25
lines changed

4 files changed

+65
-25
lines changed

src/librustc_typeck/check/mod.rs

+22-25
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ use rustc_hir::itemlikevisit::ItemLikeVisitor;
103103
use rustc_hir::lang_items::{
104104
FutureTraitLangItem, PinTypeLangItem, SizedTraitLangItem, VaListTypeLangItem,
105105
};
106-
use rustc_hir::{ExprKind, GenericArg, HirIdMap, Item, ItemKind, Node, PatKind, QPath};
106+
use rustc_hir::{ExprKind, GenericArg, HirIdMap, ItemKind, Node, PatKind, QPath};
107107
use rustc_index::bit_set::BitSet;
108108
use rustc_index::vec::Idx;
109109
use rustc_infer::infer;
@@ -2625,34 +2625,31 @@ fn check_packed(tcx: TyCtxt<'_>, sp: Span, def: &ty::AdtDef) {
26252625
"packed type cannot transitively contain a `#[repr(align)]` type"
26262626
);
26272627

2628-
let hir = tcx.hir();
2629-
let hir_id = hir.as_local_hir_id(def_spans[0].0.expect_local());
2630-
if let Node::Item(Item { ident, .. }) = hir.get(hir_id) {
2631-
err.span_note(
2632-
tcx.def_span(def_spans[0].0),
2633-
&format!("`{}` has a `#[repr(align)]` attribute", ident),
2634-
);
2635-
}
2628+
err.span_note(
2629+
tcx.def_span(def_spans[0].0),
2630+
&format!(
2631+
"`{}` has a `#[repr(align)]` attribute",
2632+
tcx.item_name(def_spans[0].0)
2633+
),
2634+
);
26362635

26372636
if def_spans.len() > 2 {
26382637
let mut first = true;
26392638
for (adt_def, span) in def_spans.iter().skip(1).rev() {
2640-
let hir_id = hir.as_local_hir_id(adt_def.expect_local());
2641-
if let Node::Item(Item { ident, .. }) = hir.get(hir_id) {
2642-
err.span_note(
2643-
*span,
2644-
&if first {
2645-
format!(
2646-
"`{}` contains a field of type `{}`",
2647-
tcx.type_of(def.did),
2648-
ident
2649-
)
2650-
} else {
2651-
format!("...which contains a field of type `{}`", ident)
2652-
},
2653-
);
2654-
first = false;
2655-
}
2639+
let ident = tcx.item_name(*adt_def);
2640+
err.span_note(
2641+
*span,
2642+
&if first {
2643+
format!(
2644+
"`{}` contains a field of type `{}`",
2645+
tcx.type_of(def.did),
2646+
ident
2647+
)
2648+
} else {
2649+
format!("...which contains a field of type `{}`", ident)
2650+
},
2651+
);
2652+
first = false;
26562653
}
26572654
}
26582655

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#[repr(transparent)]
2+
pub struct PageTableEntry {
3+
entry: u64,
4+
}
5+
6+
#[repr(align(4096))]
7+
#[repr(C)]
8+
pub struct PageTable {
9+
entries: [PageTableEntry; 512],
10+
}

src/test/ui/issues/issue-73112.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// aux-build:issue-73112.rs
2+
3+
extern crate issue_73112;
4+
5+
fn main() {
6+
use issue_73112::PageTable;
7+
8+
#[repr(C, packed)]
9+
struct SomeStruct {
10+
//~^ ERROR packed type cannot transitively contain a `#[repr(align)]` type [E0588]
11+
page_table: PageTable,
12+
}
13+
}

src/test/ui/issues/issue-73112.stderr

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error[E0588]: packed type cannot transitively contain a `#[repr(align)]` type
2+
--> $DIR/issue-73112.rs:9:5
3+
|
4+
LL | / struct SomeStruct {
5+
LL | |
6+
LL | | page_table: PageTable,
7+
LL | | }
8+
| |_____^
9+
|
10+
note: `PageTable` has a `#[repr(align)]` attribute
11+
--> $DIR/auxiliary/issue-73112.rs:8:1
12+
|
13+
LL | / pub struct PageTable {
14+
LL | | entries: [PageTableEntry; 512],
15+
LL | | }
16+
| |_^
17+
18+
error: aborting due to previous error
19+
20+
For more information about this error, try `rustc --explain E0588`.

0 commit comments

Comments
 (0)