Skip to content

Commit c067324

Browse files
rename the trait to validity and place a feature gate afront
1 parent de405dc commit c067324

File tree

10 files changed

+43
-26
lines changed

10 files changed

+43
-26
lines changed

compiler/rustc_builtin_macros/src/deriving/coerce_pointee.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ pub(crate) fn expand_deriving_coerce_pointee(
110110
// Declare helper function that adds implementation blocks.
111111
// FIXME(dingxiangfei2009): Investigate the set of attributes on target struct to be propagated to impls
112112
let attrs = thin_vec![cx.attr_word(sym::automatically_derived, span),];
113-
// # Wellformed-ness assertion
113+
// # Validity assertion which will be checked later in `rustc_hir_analysis::coherence::builtins`.
114114
{
115115
let trait_path =
116-
cx.path_all(span, true, path!(span, core::marker::CoercePointeeWellformed), vec![]);
116+
cx.path_all(span, true, path!(span, core::marker::CoercePointeeValidated), vec![]);
117117
let trait_ref = cx.trait_ref(trait_path);
118118
push(Annotatable::Item(
119119
cx.item(

compiler/rustc_hir/src/lang_items.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ language_item_table! {
370370

371371
PointerLike, sym::pointer_like, pointer_like, Target::Trait, GenericRequirement::Exact(0);
372372

373-
CoercePointeeWellformed, sym::coerce_pointee_wellformed, coerce_pointee_wellformed_trait, Target::Trait, GenericRequirement::Exact(0);
373+
CoercePointeeValidated, sym::coerce_pointee_validated, coerce_pointee_validated_trait, Target::Trait, GenericRequirement::Exact(0);
374374

375375
ConstParamTy, sym::const_param_ty, const_param_ty_trait, Target::Trait, GenericRequirement::Exact(0);
376376
UnsizedConstParamTy, sym::unsized_const_param_ty, unsized_const_param_ty_trait, Target::Trait, GenericRequirement::Exact(0);

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_user_validity_assertion = asserting applicability of `derive(CoercePointee)` on a target data is forbidden
89+
8890
hir_analysis_coerce_pointee_not_concrete_ty = `derive(CoercePointee)` is only applicable to `struct`
8991
9092
hir_analysis_coerce_pointee_not_struct = `derive(CoercePointee)` is only applicable to `struct`, instead of `{$kind}`

compiler/rustc_hir_analysis/src/coherence/builtin.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ pub(super) fn check_trait<'tcx>(
4949
.check(lang_items.dispatch_from_dyn_trait(), visit_implementation_of_dispatch_from_dyn)?;
5050
checker.check(lang_items.pointer_like(), visit_implementation_of_pointer_like)?;
5151
checker.check(
52-
lang_items.coerce_pointee_wellformed_trait(),
53-
visit_implementation_of_coerce_pointee_wellformed,
52+
lang_items.coerce_pointee_validated_trait(),
53+
visit_implementation_of_coerce_pointee_validity,
5454
)?;
5555
Ok(())
5656
}
@@ -787,19 +787,21 @@ fn visit_implementation_of_pointer_like(checker: &Checker<'_>) -> Result<(), Err
787787
.emit())
788788
}
789789

790-
fn visit_implementation_of_coerce_pointee_wellformed(
790+
fn visit_implementation_of_coerce_pointee_validity(
791791
checker: &Checker<'_>,
792792
) -> Result<(), ErrorGuaranteed> {
793793
let tcx = checker.tcx;
794794
let self_ty = tcx.impl_trait_ref(checker.impl_def_id).unwrap().instantiate_identity().self_ty();
795+
let span = tcx.def_span(checker.impl_def_id);
796+
if !tcx.is_builtin_derived(checker.impl_def_id.into()) {
797+
return Err(tcx.dcx().emit_err(errors::CoercePointeeNoUserValidityAssertion { span }));
798+
}
795799
let ty::Adt(def, _args) = self_ty.kind() else {
796-
return Err(tcx.dcx().emit_err(errors::CoercePointeeNotConcreteType {
797-
span: tcx.def_span(checker.impl_def_id),
798-
}));
800+
return Err(tcx.dcx().emit_err(errors::CoercePointeeNotConcreteType { span }));
799801
};
800802
let did = def.did();
801-
let span =
802-
if let Some(local) = did.as_local() { tcx.source_span(local) } else { tcx.def_span(did) };
803+
// Now get a more precise span of the `struct`.
804+
let span = tcx.def_span(did);
803805
if !def.is_struct() {
804806
return Err(tcx
805807
.dcx()

compiler/rustc_hir_analysis/src/errors.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,13 @@ pub(crate) struct CoercePointeeNotConcreteType {
11951195
pub span: Span,
11961196
}
11971197

1198+
#[derive(Diagnostic)]
1199+
#[diag(hir_analysis_coerce_pointee_no_user_validity_assertion, code = E0802)]
1200+
pub(crate) struct CoercePointeeNoUserValidityAssertion {
1201+
#[primary_span]
1202+
pub span: Span,
1203+
}
1204+
11981205
#[derive(Diagnostic)]
11991206
#[diag(hir_analysis_coerce_pointee_not_transparent, code = E0802)]
12001207
pub(crate) struct CoercePointeeNotTransparent {

compiler/rustc_span/src/symbol.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ symbols! {
193193
Cleanup,
194194
Clone,
195195
CoercePointee,
196-
CoercePointeeWellformed,
196+
CoercePointeeValidated,
197197
CoerceUnsized,
198198
Command,
199199
ConstParamTy,
@@ -620,7 +620,7 @@ symbols! {
620620
cmp_partialord_lt,
621621
cmpxchg16b_target_feature,
622622
cmse_nonsecure_entry,
623-
coerce_pointee_wellformed,
623+
coerce_pointee_validated,
624624
coerce_unsized,
625625
cold,
626626
cold_path,

library/core/src/marker.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -1284,14 +1284,23 @@ pub trait FnPtr: Copy + Clone {
12841284
/// }
12851285
/// ```
12861286
#[rustc_builtin_macro(CoercePointee, attributes(pointee))]
1287-
#[allow_internal_unstable(dispatch_from_dyn, coerce_unsized, unsize)]
1287+
#[allow_internal_unstable(dispatch_from_dyn, coerce_unsized, unsize, coerce_pointee_validated)]
12881288
#[unstable(feature = "derive_coerce_pointee", issue = "123430")]
12891289
pub macro CoercePointee($item:item) {
12901290
/* compiler built-in */
12911291
}
12921292

1293+
/// A validation trait that is implemented on data with `derive(CoercePointee)`
1294+
/// so that the compiler can enforce a set of rules that the target data must
1295+
/// conform to in order for the derived behaviours are safe and useful for
1296+
/// the purpose of the said macro.
1297+
///
1298+
/// This trait will not ever be exposed for use as public part of the library
1299+
/// and shall not ever be stabilised.
12931300
#[cfg(not(bootstrap))]
1294-
#[lang = "coerce_pointee_wellformed"]
1295-
#[unstable(feature = "derive_coerce_pointee", issue = "123430")]
1301+
#[lang = "coerce_pointee_validated"]
1302+
#[unstable(feature = "coerce_pointee_validated", issue = "123430")]
12961303
#[doc(hidden)]
1297-
pub trait CoercePointeeWellformed {}
1304+
pub trait CoercePointeeValidated {
1305+
/* compiler built-in */
1306+
}

tests/ui/deriving/built-in-proc-macro-scope.stdout

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub struct Ptr<'a, #[pointee] T: ?Sized> {
2020
data: &'a mut T,
2121
}
2222
#[automatically_derived]
23-
impl<'a, T: ?Sized> ::core::marker::CoercePointeeWellformed for Ptr<'a, T> { }
23+
impl<'a, T: ?Sized> ::core::marker::CoercePointeeValidated for Ptr<'a, T> { }
2424
#[automatically_derived]
2525
impl<'a, T: ?Sized + ::core::marker::Unsize<__S>, __S: ?Sized>
2626
::core::ops::DispatchFromDyn<Ptr<'a, __S>> for Ptr<'a, T> {

tests/ui/deriving/deriving-coerce-pointee-expanded.stdout

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ struct MyPointer<'a, #[pointee] T: ?Sized> {
1616
ptr: &'a T,
1717
}
1818
#[automatically_derived]
19-
impl<'a, T: ?Sized> ::core::marker::CoercePointeeWellformed for
19+
impl<'a, T: ?Sized> ::core::marker::CoercePointeeValidated for
2020
MyPointer<'a, T> {
2121
}
2222
#[automatically_derived]
@@ -36,7 +36,7 @@ pub struct MyPointer2<'a, Y, Z: MyTrait<T>, #[pointee] T: ?Sized + MyTrait<T>,
3636
}
3737
#[automatically_derived]
3838
impl<'a, Y, Z: MyTrait<T>, T: ?Sized + MyTrait<T>, X: MyTrait<T>>
39-
::core::marker::CoercePointeeWellformed for MyPointer2<'a, Y, Z, T, X>
39+
::core::marker::CoercePointeeValidated for MyPointer2<'a, Y, Z, T, X>
4040
where Y: MyTrait<T> {
4141
}
4242
#[automatically_derived]
@@ -57,7 +57,7 @@ struct MyPointerWithoutPointee<'a, T: ?Sized> {
5757
ptr: &'a T,
5858
}
5959
#[automatically_derived]
60-
impl<'a, T: ?Sized> ::core::marker::CoercePointeeWellformed for
60+
impl<'a, T: ?Sized> ::core::marker::CoercePointeeValidated for
6161
MyPointerWithoutPointee<'a, T> {
6262
}
6363
#[automatically_derived]

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

+2-5
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,8 @@ LL | struct NoFieldUnit<'a, #[pointee] T: ?Sized>();
117117
error[E0802]: `derive(CoercePointee)` is only applicable to `struct` with `repr(transparent)` layout
118118
--> $DIR/deriving-coerce-pointee-neg.rs:140:1
119119
|
120-
LL | / struct TryToWipeRepr<'a, #[pointee] T: ?Sized> {
121-
LL | |
122-
LL | | ptr: &'a T,
123-
LL | | }
124-
| |_^
120+
LL | struct TryToWipeRepr<'a, #[pointee] T: ?Sized> {
121+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
125122

126123
error: aborting due to 17 previous errors
127124

0 commit comments

Comments
 (0)