about summary refs log tree commit diff
path: root/compiler/rustc_hir_analysis
diff options
context:
space:
mode:
authorDing Xiang Fei <dingxiangfei2009@protonmail.ch>2025-01-29 06:15:56 +0800
committerDing Xiang Fei <dingxiangfei2009@protonmail.ch>2025-02-09 20:40:42 +0800
commitc0673246371b1a5ecac940f1ea6418857f932d7c (patch)
tree67469e07d6909259ef45948d51ac2c312ba2bb17 /compiler/rustc_hir_analysis
parentde405dcb8fbcd0add1e60c75800dac9b8fbe4884 (diff)
downloadrust-c0673246371b1a5ecac940f1ea6418857f932d7c.tar.gz
rust-c0673246371b1a5ecac940f1ea6418857f932d7c.zip
rename the trait to validity and place a feature gate afront
Diffstat (limited to 'compiler/rustc_hir_analysis')
-rw-r--r--compiler/rustc_hir_analysis/messages.ftl2
-rw-r--r--compiler/rustc_hir_analysis/src/coherence/builtin.rs18
-rw-r--r--compiler/rustc_hir_analysis/src/errors.rs7
3 files changed, 19 insertions, 8 deletions
diff --git a/compiler/rustc_hir_analysis/messages.ftl b/compiler/rustc_hir_analysis/messages.ftl
index 2417ff3bb1a..9c38193c84e 100644
--- a/compiler/rustc_hir_analysis/messages.ftl
+++ b/compiler/rustc_hir_analysis/messages.ftl
@@ -85,6 +85,8 @@ hir_analysis_cmse_output_stack_spill =
     .note1 = functions with the `"{$abi_name}"` ABI must pass their result via the available return registers
     .note2 = the result must either be a (transparently wrapped) i64, u64 or f64, or be at most 4 bytes in size
 
+hir_analysis_coerce_pointee_no_user_validity_assertion = asserting applicability of `derive(CoercePointee)` on a target data is forbidden
+
 hir_analysis_coerce_pointee_not_concrete_ty = `derive(CoercePointee)` is only applicable to `struct`
 
 hir_analysis_coerce_pointee_not_struct = `derive(CoercePointee)` is only applicable to `struct`, instead of `{$kind}`
diff --git a/compiler/rustc_hir_analysis/src/coherence/builtin.rs b/compiler/rustc_hir_analysis/src/coherence/builtin.rs
index a872bb72bef..c21576a5e7c 100644
--- a/compiler/rustc_hir_analysis/src/coherence/builtin.rs
+++ b/compiler/rustc_hir_analysis/src/coherence/builtin.rs
@@ -49,8 +49,8 @@ pub(super) fn check_trait<'tcx>(
         .check(lang_items.dispatch_from_dyn_trait(), visit_implementation_of_dispatch_from_dyn)?;
     checker.check(lang_items.pointer_like(), visit_implementation_of_pointer_like)?;
     checker.check(
-        lang_items.coerce_pointee_wellformed_trait(),
-        visit_implementation_of_coerce_pointee_wellformed,
+        lang_items.coerce_pointee_validated_trait(),
+        visit_implementation_of_coerce_pointee_validity,
     )?;
     Ok(())
 }
@@ -787,19 +787,21 @@ fn visit_implementation_of_pointer_like(checker: &Checker<'_>) -> Result<(), Err
         .emit())
 }
 
-fn visit_implementation_of_coerce_pointee_wellformed(
+fn visit_implementation_of_coerce_pointee_validity(
     checker: &Checker<'_>,
 ) -> Result<(), ErrorGuaranteed> {
     let tcx = checker.tcx;
     let self_ty = tcx.impl_trait_ref(checker.impl_def_id).unwrap().instantiate_identity().self_ty();
+    let span = tcx.def_span(checker.impl_def_id);
+    if !tcx.is_builtin_derived(checker.impl_def_id.into()) {
+        return Err(tcx.dcx().emit_err(errors::CoercePointeeNoUserValidityAssertion { span }));
+    }
     let ty::Adt(def, _args) = self_ty.kind() else {
-        return Err(tcx.dcx().emit_err(errors::CoercePointeeNotConcreteType {
-            span: tcx.def_span(checker.impl_def_id),
-        }));
+        return Err(tcx.dcx().emit_err(errors::CoercePointeeNotConcreteType { span }));
     };
     let did = def.did();
-    let span =
-        if let Some(local) = did.as_local() { tcx.source_span(local) } else { tcx.def_span(did) };
+    // Now get a more precise span of the `struct`.
+    let span = tcx.def_span(did);
     if !def.is_struct() {
         return Err(tcx
             .dcx()
diff --git a/compiler/rustc_hir_analysis/src/errors.rs b/compiler/rustc_hir_analysis/src/errors.rs
index a921022a112..c49e06d7448 100644
--- a/compiler/rustc_hir_analysis/src/errors.rs
+++ b/compiler/rustc_hir_analysis/src/errors.rs
@@ -1196,6 +1196,13 @@ pub(crate) struct CoercePointeeNotConcreteType {
 }
 
 #[derive(Diagnostic)]
+#[diag(hir_analysis_coerce_pointee_no_user_validity_assertion, code = E0802)]
+pub(crate) struct CoercePointeeNoUserValidityAssertion {
+    #[primary_span]
+    pub span: Span,
+}
+
+#[derive(Diagnostic)]
 #[diag(hir_analysis_coerce_pointee_not_transparent, code = E0802)]
 pub(crate) struct CoercePointeeNotTransparent {
     #[primary_span]