Skip to content

Commit 36b8d0e

Browse files
committed
downgrade mutable-ptr-in-final-value from hard-error to future-incompat lint to address issue 121610.
1 parent 1c580bc commit 36b8d0e

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

compiler/rustc_const_eval/src/errors.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@ pub(crate) struct DanglingPtrInFinal {
2525
pub kind: InternKind,
2626
}
2727

28-
#[derive(Diagnostic)]
28+
#[derive(LintDiagnostic)]
2929
#[diag(const_eval_mutable_ptr_in_final)]
3030
pub(crate) struct MutablePtrInFinal {
31-
#[primary_span]
31+
// rust-lang/rust#122153: This was marked as `#[primary_span]` under
32+
// `derive(Diagnostic)`. Since we expect we may hard-error in future, we are
33+
// keeping the field (and skipping it under `derive(LintDiagnostic)`).
34+
#[skip_arg]
3235
pub span: Span,
3336
pub kind: InternKind,
3437
}

compiler/rustc_const_eval/src/interpret/intern.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use rustc_errors::ErrorGuaranteed;
1919
use rustc_hir as hir;
2020
use rustc_middle::mir::interpret::{CtfeProvenance, InterpResult};
2121
use rustc_middle::ty::layout::TyAndLayout;
22+
use rustc_session::lint;
2223

2324
use super::{AllocId, Allocation, InterpCx, MPlaceTy, Machine, MemoryKind, PlaceTy};
2425
use crate::const_eval;
@@ -221,10 +222,13 @@ pub fn intern_const_alloc_recursive<
221222
})?);
222223
}
223224
if found_bad_mutable_pointer {
224-
return Err(ecx
225-
.tcx
226-
.dcx()
227-
.emit_err(MutablePtrInFinal { span: ecx.tcx.span, kind: intern_kind }));
225+
let err_diag = MutablePtrInFinal { span: ecx.tcx.span, kind: intern_kind };
226+
ecx.tcx.emit_node_span_lint(
227+
lint::builtin::CONST_EVAL_MUTABLE_PTR_IN_FINAL_VALUE,
228+
ecx.best_lint_scope(),
229+
err_diag.span,
230+
err_diag,
231+
)
228232
}
229233

230234
Ok(())

compiler/rustc_lint_defs/src/builtin.rs

+36
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ declare_lint_pass! {
3030
CENUM_IMPL_DROP_CAST,
3131
COHERENCE_LEAK_CHECK,
3232
CONFLICTING_REPR_HINTS,
33+
CONST_EVAL_MUTABLE_PTR_IN_FINAL_VALUE,
3334
CONST_EVALUATABLE_UNCHECKED,
3435
CONST_ITEM_MUTATION,
3536
DEAD_CODE,
@@ -2765,6 +2766,41 @@ declare_lint! {
27652766
@feature_gate = sym::strict_provenance;
27662767
}
27672768

2769+
declare_lint! {
2770+
/// The `const_eval_mutable_ptr_in_final_value` lint detects if a mutable pointer
2771+
/// has leaked into the final value of a const expression.
2772+
///
2773+
/// ### Example
2774+
///
2775+
/// ```rust
2776+
/// pub enum JsValue {
2777+
/// Undefined,
2778+
/// Object(std::cell::Cell<bool>),
2779+
/// }
2780+
///
2781+
/// impl ::std::ops::Drop for JsValue {
2782+
/// fn drop(&mut self) {}
2783+
/// }
2784+
///
2785+
/// const UNDEFINED: &JsValue = &JsValue::Undefined;
2786+
///
2787+
/// fn main() {
2788+
/// }
2789+
/// ```
2790+
///
2791+
/// This is a [future-incompatible] lint to ease the transition to an error.
2792+
/// See [issue #122153] for more details.
2793+
///
2794+
/// [issue #122153]: https://github.com/rust-lang/rust/issues/122153
2795+
pub CONST_EVAL_MUTABLE_PTR_IN_FINAL_VALUE,
2796+
Warn,
2797+
"detects a mutable pointer that has leaked into final value of a const expression",
2798+
@future_incompatible = FutureIncompatibleInfo {
2799+
reason: FutureIncompatibilityReason::FutureReleaseErrorReportInDeps,
2800+
reference: "issue #122153 <https://github.com/rust-lang/rust/issues/122153>",
2801+
};
2802+
}
2803+
27682804
declare_lint! {
27692805
/// The `const_evaluatable_unchecked` lint detects a generic constant used
27702806
/// in a type.

0 commit comments

Comments
 (0)