Skip to content

Commit 0d0ad42

Browse files
committed
Auto merge of #115712 - RalfJung:wf, r=compiler-errors
rustc_layout, rustc_abi: make sure the types are well-formed Fixes #115676
2 parents 8ed4537 + 3bd8bcb commit 0d0ad42

File tree

11 files changed

+222
-185
lines changed

11 files changed

+222
-185
lines changed

compiler/rustc_passes/src/abi_test.rs

+21-35
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,31 @@
11
use rustc_ast::Attribute;
22
use rustc_hir::def::DefKind;
3-
use rustc_hir::def_id::DefId;
3+
use rustc_hir::def_id::LocalDefId;
44
use rustc_middle::ty::layout::{FnAbiError, LayoutError};
55
use rustc_middle::ty::{self, GenericArgs, Instance, Ty, TyCtxt};
66
use rustc_span::source_map::Spanned;
77
use rustc_span::symbol::sym;
88
use rustc_target::abi::call::FnAbi;
99

10+
use super::layout_test::ensure_wf;
1011
use crate::errors::{AbiInvalidAttribute, AbiNe, AbiOf, UnrecognizedField};
1112

1213
pub fn test_abi(tcx: TyCtxt<'_>) {
1314
if !tcx.features().rustc_attrs {
1415
// if the `rustc_attrs` feature is not enabled, don't bother testing ABI
1516
return;
1617
}
17-
for id in tcx.hir().items() {
18-
for attr in tcx.get_attrs(id.owner_id, sym::rustc_abi) {
19-
match tcx.def_kind(id.owner_id) {
20-
DefKind::Fn => {
21-
dump_abi_of_fn_item(tcx, id.owner_id.def_id.into(), attr);
18+
for id in tcx.hir_crate_items(()).definitions() {
19+
for attr in tcx.get_attrs(id, sym::rustc_abi) {
20+
match tcx.def_kind(id) {
21+
DefKind::Fn | DefKind::AssocFn => {
22+
dump_abi_of_fn_item(tcx, id, attr);
2223
}
2324
DefKind::TyAlias { .. } => {
24-
dump_abi_of_fn_type(tcx, id.owner_id.def_id.into(), attr);
25+
dump_abi_of_fn_type(tcx, id, attr);
2526
}
2627
_ => {
27-
tcx.sess.emit_err(AbiInvalidAttribute { span: tcx.def_span(id.owner_id) });
28-
}
29-
}
30-
}
31-
if matches!(tcx.def_kind(id.owner_id), DefKind::Impl { .. }) {
32-
// To find associated functions we need to go into the child items here.
33-
for &id in tcx.associated_item_def_ids(id.owner_id) {
34-
for attr in tcx.get_attrs(id, sym::rustc_abi) {
35-
match tcx.def_kind(id) {
36-
DefKind::AssocFn => {
37-
dump_abi_of_fn_item(tcx, id, attr);
38-
}
39-
_ => {
40-
tcx.sess.emit_err(AbiInvalidAttribute { span: tcx.def_span(id) });
41-
}
42-
}
28+
tcx.sess.emit_err(AbiInvalidAttribute { span: tcx.def_span(id) });
4329
}
4430
}
4531
}
@@ -49,7 +35,7 @@ pub fn test_abi(tcx: TyCtxt<'_>) {
4935
fn unwrap_fn_abi<'tcx>(
5036
abi: Result<&'tcx FnAbi<'tcx, Ty<'tcx>>, &'tcx FnAbiError<'tcx>>,
5137
tcx: TyCtxt<'tcx>,
52-
item_def_id: DefId,
38+
item_def_id: LocalDefId,
5339
) -> &'tcx FnAbi<'tcx, Ty<'tcx>> {
5440
match abi {
5541
Ok(abi) => abi,
@@ -71,10 +57,10 @@ fn unwrap_fn_abi<'tcx>(
7157
}
7258
}
7359

74-
fn dump_abi_of_fn_item(tcx: TyCtxt<'_>, item_def_id: DefId, attr: &Attribute) {
60+
fn dump_abi_of_fn_item(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribute) {
7561
let param_env = tcx.param_env(item_def_id);
7662
let args = GenericArgs::identity_for_item(tcx, item_def_id);
77-
let instance = match Instance::resolve(tcx, param_env, item_def_id, args) {
63+
let instance = match Instance::resolve(tcx, param_env, item_def_id.into(), args) {
7864
Ok(Some(instance)) => instance,
7965
Ok(None) => {
8066
// Not sure what to do here, but `LayoutError::Unknown` seems reasonable?
@@ -99,7 +85,7 @@ fn dump_abi_of_fn_item(tcx: TyCtxt<'_>, item_def_id: DefId, attr: &Attribute) {
9985
for meta_item in meta_items {
10086
match meta_item.name_or_empty() {
10187
sym::debug => {
102-
let fn_name = tcx.item_name(item_def_id);
88+
let fn_name = tcx.item_name(item_def_id.into());
10389
tcx.sess.emit_err(AbiOf {
10490
span: tcx.def_span(item_def_id),
10591
fn_name,
@@ -128,9 +114,13 @@ fn test_abi_eq<'tcx>(abi1: &'tcx FnAbi<'tcx, Ty<'tcx>>, abi2: &'tcx FnAbi<'tcx,
128114
&& abi1.args.iter().zip(abi2.args.iter()).all(|(arg1, arg2)| arg1.eq_abi(arg2))
129115
}
130116

131-
fn dump_abi_of_fn_type(tcx: TyCtxt<'_>, item_def_id: DefId, attr: &Attribute) {
117+
fn dump_abi_of_fn_type(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribute) {
132118
let param_env = tcx.param_env(item_def_id);
133119
let ty = tcx.type_of(item_def_id).instantiate_identity();
120+
let span = tcx.def_span(item_def_id);
121+
if !ensure_wf(tcx, param_env, ty, item_def_id, span) {
122+
return;
123+
}
134124
let meta_items = attr.meta_item_list().unwrap_or_default();
135125
for meta_item in meta_items {
136126
match meta_item.name_or_empty() {
@@ -147,12 +137,8 @@ fn dump_abi_of_fn_type(tcx: TyCtxt<'_>, item_def_id: DefId, attr: &Attribute) {
147137
item_def_id,
148138
);
149139

150-
let fn_name = tcx.item_name(item_def_id);
151-
tcx.sess.emit_err(AbiOf {
152-
span: tcx.def_span(item_def_id),
153-
fn_name,
154-
fn_abi: format!("{:#?}", abi),
155-
});
140+
let fn_name = tcx.item_name(item_def_id.into());
141+
tcx.sess.emit_err(AbiOf { span, fn_name, fn_abi: format!("{:#?}", abi) });
156142
}
157143
sym::assert_eq => {
158144
let ty::Tuple(fields) = ty.kind() else {
@@ -196,7 +182,7 @@ fn dump_abi_of_fn_type(tcx: TyCtxt<'_>, item_def_id: DefId, attr: &Attribute) {
196182

197183
if !test_abi_eq(abi1, abi2) {
198184
tcx.sess.emit_err(AbiNe {
199-
span: tcx.def_span(item_def_id),
185+
span,
200186
left: format!("{:#?}", abi1),
201187
right: format!("{:#?}", abi2),
202188
});

compiler/rustc_passes/src/layout_test.rs

+49-33
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ use rustc_ast::Attribute;
22
use rustc_hir::def::DefKind;
33
use rustc_hir::def_id::LocalDefId;
44
use rustc_middle::ty::layout::{HasParamEnv, HasTyCtxt, LayoutError, LayoutOfHelpers, TyAndLayout};
5-
use rustc_middle::ty::{ParamEnv, Ty, TyCtxt};
5+
use rustc_middle::ty::{self, ParamEnv, Ty, TyCtxt};
66
use rustc_span::source_map::Spanned;
77
use rustc_span::symbol::sym;
88
use rustc_span::Span;
99
use rustc_target::abi::{HasDataLayout, TargetDataLayout};
10+
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt;
11+
use rustc_trait_selection::{infer::TyCtxtInferExt, traits};
1012

1113
use crate::errors::{
1214
LayoutAbi, LayoutAlign, LayoutHomogeneousAggregate, LayoutInvalidAttribute, LayoutOf,
@@ -18,31 +20,58 @@ pub fn test_layout(tcx: TyCtxt<'_>) {
1820
// if the `rustc_attrs` feature is not enabled, don't bother testing layout
1921
return;
2022
}
21-
for id in tcx.hir().items() {
22-
for attr in tcx.get_attrs(id.owner_id, sym::rustc_layout) {
23-
match tcx.def_kind(id.owner_id) {
23+
for id in tcx.hir_crate_items(()).definitions() {
24+
for attr in tcx.get_attrs(id, sym::rustc_layout) {
25+
match tcx.def_kind(id) {
2426
DefKind::TyAlias { .. } | DefKind::Enum | DefKind::Struct | DefKind::Union => {
25-
dump_layout_of(tcx, id.owner_id.def_id, attr);
27+
dump_layout_of(tcx, id, attr);
2628
}
2729
_ => {
28-
tcx.sess.emit_err(LayoutInvalidAttribute { span: tcx.def_span(id.owner_id) });
29-
}
30-
}
31-
}
32-
if matches!(tcx.def_kind(id.owner_id), DefKind::Impl { .. }) {
33-
// To find associated functions we need to go into the child items here.
34-
for &id in tcx.associated_item_def_ids(id.owner_id) {
35-
for _attr in tcx.get_attrs(id, sym::rustc_layout) {
3630
tcx.sess.emit_err(LayoutInvalidAttribute { span: tcx.def_span(id) });
3731
}
3832
}
3933
}
4034
}
4135
}
4236

37+
pub fn ensure_wf<'tcx>(
38+
tcx: TyCtxt<'tcx>,
39+
param_env: ParamEnv<'tcx>,
40+
ty: Ty<'tcx>,
41+
def_id: LocalDefId,
42+
span: Span,
43+
) -> bool {
44+
let pred = ty::ClauseKind::WellFormed(ty.into());
45+
let obligation = traits::Obligation::new(
46+
tcx,
47+
traits::ObligationCause::new(
48+
span,
49+
def_id,
50+
traits::ObligationCauseCode::WellFormed(Some(traits::WellFormedLoc::Ty(def_id))),
51+
),
52+
param_env,
53+
pred,
54+
);
55+
let infcx = tcx.infer_ctxt().build();
56+
let ocx = traits::ObligationCtxt::new(&infcx);
57+
ocx.register_obligation(obligation);
58+
let errors = ocx.select_all_or_error();
59+
if !errors.is_empty() {
60+
infcx.err_ctxt().report_fulfillment_errors(&errors);
61+
false
62+
} else {
63+
// looks WF!
64+
true
65+
}
66+
}
67+
4368
fn dump_layout_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribute) {
4469
let param_env = tcx.param_env(item_def_id);
4570
let ty = tcx.type_of(item_def_id).instantiate_identity();
71+
let span = tcx.def_span(item_def_id.to_def_id());
72+
if !ensure_wf(tcx, param_env, ty, item_def_id, span) {
73+
return;
74+
}
4675
match tcx.layout_of(param_env.and(ty)) {
4776
Ok(ty_layout) => {
4877
// Check out the `#[rustc_layout(..)]` attribute to tell what to dump.
@@ -51,29 +80,24 @@ fn dump_layout_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribute) {
5180
for meta_item in meta_items {
5281
match meta_item.name_or_empty() {
5382
sym::abi => {
54-
tcx.sess.emit_err(LayoutAbi {
55-
span: tcx.def_span(item_def_id.to_def_id()),
56-
abi: format!("{:?}", ty_layout.abi),
57-
});
83+
tcx.sess.emit_err(LayoutAbi { span, abi: format!("{:?}", ty_layout.abi) });
5884
}
5985

6086
sym::align => {
6187
tcx.sess.emit_err(LayoutAlign {
62-
span: tcx.def_span(item_def_id.to_def_id()),
88+
span,
6389
align: format!("{:?}", ty_layout.align),
6490
});
6591
}
6692

6793
sym::size => {
68-
tcx.sess.emit_err(LayoutSize {
69-
span: tcx.def_span(item_def_id.to_def_id()),
70-
size: format!("{:?}", ty_layout.size),
71-
});
94+
tcx.sess
95+
.emit_err(LayoutSize { span, size: format!("{:?}", ty_layout.size) });
7296
}
7397

7498
sym::homogeneous_aggregate => {
7599
tcx.sess.emit_err(LayoutHomogeneousAggregate {
76-
span: tcx.def_span(item_def_id.to_def_id()),
100+
span,
77101
homogeneous_aggregate: format!(
78102
"{:?}",
79103
ty_layout.homogeneous_aggregate(&UnwrapLayoutCx { tcx, param_env })
@@ -90,11 +114,7 @@ fn dump_layout_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribute) {
90114
)
91115
);
92116
let ty_layout = format!("{:#?}", *ty_layout);
93-
tcx.sess.emit_err(LayoutOf {
94-
span: tcx.def_span(item_def_id.to_def_id()),
95-
normalized_ty,
96-
ty_layout,
97-
});
117+
tcx.sess.emit_err(LayoutOf { span, normalized_ty, ty_layout });
98118
}
99119

100120
name => {
@@ -105,11 +125,7 @@ fn dump_layout_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribute) {
105125
}
106126

107127
Err(layout_error) => {
108-
tcx.sess.emit_fatal(Spanned {
109-
node: layout_error.into_diagnostic(),
110-
111-
span: tcx.def_span(item_def_id.to_def_id()),
112-
});
128+
tcx.sess.emit_fatal(Spanned { node: layout_error.into_diagnostic(), span });
113129
}
114130
}
115131
}

tests/ui/abi/compatibility.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#![feature(rustc_attrs, transparent_unions)]
33
#![allow(unused, improper_ctypes_definitions)]
44
use std::marker::PhantomData;
5+
use std::mem::ManuallyDrop;
56
use std::num::NonZeroI32;
67
use std::ptr::NonNull;
78

@@ -37,9 +38,9 @@ enum ReprCEnum<T> {
3738
Variant2(T),
3839
}
3940
#[repr(C)]
40-
union ReprCUnion<T: Copy> {
41+
union ReprCUnion<T> {
4142
nothing: (),
42-
something: T,
43+
something: ManuallyDrop<T>,
4344
}
4445

4546
macro_rules! test_abi_compatible {
@@ -82,9 +83,9 @@ struct Wrapper2<T>((), Zst, T);
8283
#[repr(transparent)]
8384
struct Wrapper3<T>(T, [u8; 0], PhantomData<u64>);
8485
#[repr(transparent)]
85-
union WrapperUnion<T: Copy> {
86+
union WrapperUnion<T> {
8687
nothing: (),
87-
something: T,
88+
something: ManuallyDrop<T>,
8889
}
8990

9091
macro_rules! test_transparent {

tests/ui/abi/debug.rs

+3
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,6 @@ type TestAbiNeFloat = (fn(f32), fn(u32)); //~ ERROR: ABIs are not compatible
4848
// Sign matters on some targets (such as s390x), so let's make sure we never accept this.
4949
#[rustc_abi(assert_eq)]
5050
type TestAbiNeSign = (fn(i32), fn(u32)); //~ ERROR: ABIs are not compatible
51+
52+
#[rustc_abi(assert_eq)]
53+
type TestAbiEqNonsense = (fn((str, str)), fn((str, str))); //~ ERROR: cannot be known at compilation time

0 commit comments

Comments
 (0)