Skip to content

Commit 4c42131

Browse files
move repr(transparent) checks to coherence
1 parent 3a49cc4 commit 4c42131

File tree

6 files changed

+19
-19
lines changed

6 files changed

+19
-19
lines changed

compiler/rustc_builtin_macros/src/deriving/coerce_pointee.rs

-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use rustc_ast::{
66
self as ast, GenericArg, GenericBound, GenericParamKind, Generics, ItemKind, MetaItem,
77
TraitBoundModifiers, VariantData, WherePredicate,
88
};
9-
use rustc_attr_parsing as attr;
109
use rustc_data_structures::flat_map_in_place::FlatMapInPlace;
1110
use rustc_errors::E0802;
1211
use rustc_expand::base::{Annotatable, ExtCtxt};
@@ -33,15 +32,6 @@ pub(crate) fn expand_deriving_coerce_pointee(
3332
let (name_ident, generics) = if let Annotatable::Item(aitem) = item
3433
&& let ItemKind::Struct(struct_data, g) = &aitem.kind
3534
{
36-
let is_transparent = aitem.attrs.iter().any(|attr| {
37-
attr::find_repr_attrs(cx.sess, attr)
38-
.into_iter()
39-
.any(|r| matches!(r, attr::ReprTransparent))
40-
});
41-
if !is_transparent {
42-
cx.dcx().emit_err(RequireTransparent { span });
43-
return;
44-
}
4535
if !matches!(
4636
struct_data,
4737
VariantData::Struct { fields, recovered: _ } | VariantData::Tuple(fields, _)

compiler/rustc_hir_analysis/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ hir_analysis_cmse_output_stack_spill =
8585
.note1 = functions with the `"{$abi_name}"` ABI must pass their result via the available return registers
8686
.note2 = the result must either be a (transparently wrapped) i64, u64 or f64, or be at most 4 bytes in size
8787
88+
hir_analysis_coerce_pointee_no_field = `CoercePointee` can only be derived on `struct`s with at least one field
89+
8890
hir_analysis_coerce_pointee_no_user_validity_assertion = asserting applicability of `derive(CoercePointee)` on a target data is forbidden
8991
9092
hir_analysis_coerce_pointee_not_concrete_ty = `derive(CoercePointee)` is only applicable to `struct`

compiler/rustc_hir_analysis/src/coherence/builtin.rs

+3
Original file line numberDiff line numberDiff line change
@@ -818,5 +818,8 @@ fn visit_implementation_of_coerce_pointee_validity(
818818
if !def.repr().transparent() {
819819
return Err(tcx.dcx().emit_err(errors::CoercePointeeNotTransparent { span }));
820820
}
821+
if def.all_fields().next().is_none() {
822+
return Err(tcx.dcx().emit_err(errors::CoercePointeeNoField { span }));
823+
}
821824
Ok(())
822825
}

compiler/rustc_hir_analysis/src/errors.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1209,6 +1209,13 @@ pub(crate) struct CoercePointeeNotTransparent {
12091209
pub span: Span,
12101210
}
12111211

1212+
#[derive(Diagnostic)]
1213+
#[diag(hir_analysis_coerce_pointee_no_field, code = E0802)]
1214+
pub(crate) struct CoercePointeeNoField {
1215+
#[primary_span]
1216+
pub span: Span,
1217+
}
1218+
12121219
#[derive(Diagnostic)]
12131220
#[diag(hir_analysis_inherent_ty_outside_relevant, code = E0390)]
12141221
#[help]

tests/ui/deriving/deriving-coerce-pointee-neg.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ struct TooManyPointees<'a, #[pointee] A: ?Sized, #[pointee] B: ?Sized>((&'a A, &
4444
//~^ ERROR: only one type parameter can be marked as `#[pointee]` when deriving `CoercePointee` traits
4545

4646
#[derive(CoercePointee)]
47-
//~^ ERROR: `CoercePointee` can only be derived on `struct`s with `#[repr(transparent)]`
4847
struct NotTransparent<'a, #[pointee] T: ?Sized> {
48+
//~^ ERROR: `derive(CoercePointee)` is only applicable to `struct` with `repr(transparent)` layout
4949
ptr: &'a T,
5050
}
5151

tests/ui/deriving/deriving-coerce-pointee-neg.stderr

+6-8
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,6 @@ error[E0802]: only one type parameter can be marked as `#[pointee]` when derivin
4444
LL | struct TooManyPointees<'a, #[pointee] A: ?Sized, #[pointee] B: ?Sized>((&'a A, &'a B));
4545
| ^ - here another type parameter is marked as `#[pointee]`
4646

47-
error[E0802]: `CoercePointee` can only be derived on `struct`s with `#[repr(transparent)]`
48-
--> $DIR/deriving-coerce-pointee-neg.rs:46:10
49-
|
50-
LL | #[derive(CoercePointee)]
51-
| ^^^^^^^^^^^^^
52-
|
53-
= note: this error originates in the derive macro `CoercePointee` (in Nightly builds, run with -Z macro-backtrace for more info)
54-
5547
error[E0802]: `derive(CoercePointee)` requires `T` to be marked `?Sized`
5648
--> $DIR/deriving-coerce-pointee-neg.rs:54:36
5749
|
@@ -114,6 +106,12 @@ LL | struct NoFieldUnit<'a, #[pointee] T: ?Sized>();
114106
|
115107
= help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`
116108

109+
error[E0802]: `derive(CoercePointee)` is only applicable to `struct` with `repr(transparent)` layout
110+
--> $DIR/deriving-coerce-pointee-neg.rs:47:1
111+
|
112+
LL | struct NotTransparent<'a, #[pointee] T: ?Sized> {
113+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
114+
117115
error[E0802]: `derive(CoercePointee)` is only applicable to `struct` with `repr(transparent)` layout
118116
--> $DIR/deriving-coerce-pointee-neg.rs:140:1
119117
|

0 commit comments

Comments
 (0)