Skip to content

Commit 3a49cc4

Browse files
rename the trait to validity and place a feature gate afront
1 parent 9d89276 commit 3a49cc4

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
@@ -369,7 +369,7 @@ language_item_table! {
369369

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

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

374374
ConstParamTy, sym::const_param_ty, const_param_ty_trait, Target::Trait, GenericRequirement::Exact(0);
375375
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
@@ -50,8 +50,8 @@ pub(super) fn check_trait<'tcx>(
5050
.check(lang_items.dispatch_from_dyn_trait(), visit_implementation_of_dispatch_from_dyn)?;
5151
checker.check(lang_items.pointer_like(), visit_implementation_of_pointer_like)?;
5252
checker.check(
53-
lang_items.coerce_pointee_wellformed_trait(),
54-
visit_implementation_of_coerce_pointee_wellformed,
53+
lang_items.coerce_pointee_validated_trait(),
54+
visit_implementation_of_coerce_pointee_validity,
5555
)?;
5656
Ok(())
5757
}
@@ -795,19 +795,21 @@ fn visit_implementation_of_pointer_like(checker: &Checker<'_>) -> Result<(), Err
795795
.emit())
796796
}
797797

798-
fn visit_implementation_of_coerce_pointee_wellformed(
798+
fn visit_implementation_of_coerce_pointee_validity(
799799
checker: &Checker<'_>,
800800
) -> Result<(), ErrorGuaranteed> {
801801
let tcx = checker.tcx;
802802
let self_ty = tcx.impl_trait_ref(checker.impl_def_id).unwrap().instantiate_identity().self_ty();
803+
let span = tcx.def_span(checker.impl_def_id);
804+
if !tcx.is_builtin_derived(checker.impl_def_id.into()) {
805+
return Err(tcx.dcx().emit_err(errors::CoercePointeeNoUserValidityAssertion { span }));
806+
}
803807
let ty::Adt(def, _args) = self_ty.kind() else {
804-
return Err(tcx.dcx().emit_err(errors::CoercePointeeNotConcreteType {
805-
span: tcx.def_span(checker.impl_def_id),
806-
}));
808+
return Err(tcx.dcx().emit_err(errors::CoercePointeeNotConcreteType { span }));
807809
};
808810
let did = def.did();
809-
let span =
810-
if let Some(local) = did.as_local() { tcx.source_span(local) } else { tcx.def_span(did) };
811+
// Now get a more precise span of the `struct`.
812+
let span = tcx.def_span(did);
811813
if !def.is_struct() {
812814
return Err(tcx
813815
.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
@@ -191,7 +191,7 @@ symbols! {
191191
Cleanup,
192192
Clone,
193193
CoercePointee,
194-
CoercePointeeWellformed,
194+
CoercePointeeValidated,
195195
CoerceUnsized,
196196
Command,
197197
ConstParamTy,
@@ -618,7 +618,7 @@ symbols! {
618618
cmp_partialord_lt,
619619
cmpxchg16b_target_feature,
620620
cmse_nonsecure_entry,
621-
coerce_pointee_wellformed,
621+
coerce_pointee_validated,
622622
coerce_unsized,
623623
cold,
624624
cold_path,

library/core/src/marker.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -1086,14 +1086,23 @@ pub trait FnPtr: Copy + Clone {
10861086

10871087
/// Derive macro generating impls of traits related to smart pointers.
10881088
#[rustc_builtin_macro(CoercePointee, attributes(pointee))]
1089-
#[allow_internal_unstable(dispatch_from_dyn, coerce_unsized, unsize)]
1089+
#[allow_internal_unstable(dispatch_from_dyn, coerce_unsized, unsize, coerce_pointee_validated)]
10901090
#[unstable(feature = "derive_coerce_pointee", issue = "123430")]
10911091
pub macro CoercePointee($item:item) {
10921092
/* compiler built-in */
10931093
}
10941094

1095+
/// A validation trait that is implemented on data with `derive(CoercePointee)`
1096+
/// so that the compiler can enforce a set of rules that the target data must
1097+
/// conform to in order for the derived behaviours are safe and useful for
1098+
/// the purpose of the said macro.
1099+
///
1100+
/// This trait will not ever be exposed for use as public part of the library
1101+
/// and shall not ever be stabilised.
10951102
#[cfg(not(bootstrap))]
1096-
#[lang = "coerce_pointee_wellformed"]
1097-
#[unstable(feature = "derive_coerce_pointee", issue = "123430")]
1103+
#[lang = "coerce_pointee_validated"]
1104+
#[unstable(feature = "coerce_pointee_validated", issue = "123430")]
10981105
#[doc(hidden)]
1099-
pub trait CoercePointeeWellformed {}
1106+
pub trait CoercePointeeValidated {
1107+
/* compiler built-in */
1108+
}

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)