Skip to content

Commit 25d3cbb

Browse files
pnkfelixcuviper
authored andcommitted
downgrade mutable-ptr-in-final-value from hard-error to future-incompat lint to address issue 121610.
(cherry picked from commit a8549b4)
1 parent 92512b1 commit 25d3cbb

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
@@ -27,10 +27,13 @@ pub(crate) struct DanglingPtrInFinal {
2727
pub kind: InternKind,
2828
}
2929

30-
#[derive(Diagnostic)]
30+
#[derive(LintDiagnostic)]
3131
#[diag(const_eval_mutable_ptr_in_final)]
3232
pub(crate) struct MutablePtrInFinal {
33-
#[primary_span]
33+
// rust-lang/rust#122153: This was marked as `#[primary_span]` under
34+
// `derive(Diagnostic)`. Since we expect we may hard-error in future, we are
35+
// keeping the field (and skipping it under `derive(LintDiagnostic)`).
36+
#[skip_arg]
3437
pub span: Span,
3538
pub kind: InternKind,
3639
}

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;
@@ -195,10 +196,13 @@ pub fn intern_const_alloc_recursive<
195196
})?;
196197
}
197198
if found_bad_mutable_pointer {
198-
return Err(ecx
199-
.tcx
200-
.dcx()
201-
.emit_err(MutablePtrInFinal { span: ecx.tcx.span, kind: intern_kind }));
199+
let err_diag = MutablePtrInFinal { span: ecx.tcx.span, kind: intern_kind };
200+
ecx.tcx.emit_node_span_lint(
201+
lint::builtin::CONST_EVAL_MUTABLE_PTR_IN_FINAL_VALUE,
202+
ecx.best_lint_scope(),
203+
err_diag.span,
204+
err_diag,
205+
)
202206
}
203207

204208
Ok(())

compiler/rustc_lint_defs/src/builtin.rs

+36
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ declare_lint_pass! {
2727
CENUM_IMPL_DROP_CAST,
2828
COHERENCE_LEAK_CHECK,
2929
CONFLICTING_REPR_HINTS,
30+
CONST_EVAL_MUTABLE_PTR_IN_FINAL_VALUE,
3031
CONST_EVALUATABLE_UNCHECKED,
3132
CONST_ITEM_MUTATION,
3233
CONST_PATTERNS_WITHOUT_PARTIAL_EQ,
@@ -2905,6 +2906,41 @@ declare_lint! {
29052906
@feature_gate = sym::strict_provenance;
29062907
}
29072908

2909+
declare_lint! {
2910+
/// The `const_eval_mutable_ptr_in_final_value` lint detects if a mutable pointer
2911+
/// has leaked into the final value of a const expression.
2912+
///
2913+
/// ### Example
2914+
///
2915+
/// ```rust
2916+
/// pub enum JsValue {
2917+
/// Undefined,
2918+
/// Object(std::cell::Cell<bool>),
2919+
/// }
2920+
///
2921+
/// impl ::std::ops::Drop for JsValue {
2922+
/// fn drop(&mut self) {}
2923+
/// }
2924+
///
2925+
/// const UNDEFINED: &JsValue = &JsValue::Undefined;
2926+
///
2927+
/// fn main() {
2928+
/// }
2929+
/// ```
2930+
///
2931+
/// This is a [future-incompatible] lint to ease the transition to an error.
2932+
/// See [issue #122153] for more details.
2933+
///
2934+
/// [issue #122153]: https://github.com/rust-lang/rust/issues/122153
2935+
pub CONST_EVAL_MUTABLE_PTR_IN_FINAL_VALUE,
2936+
Warn,
2937+
"detects a mutable pointer that has leaked into final value of a const expression",
2938+
@future_incompatible = FutureIncompatibleInfo {
2939+
reason: FutureIncompatibilityReason::FutureReleaseErrorReportInDeps,
2940+
reference: "issue #122153 <https://github.com/rust-lang/rust/issues/122153>",
2941+
};
2942+
}
2943+
29082944
declare_lint! {
29092945
/// The `const_evaluatable_unchecked` lint detects a generic constant used
29102946
/// in a type.

0 commit comments

Comments
 (0)