Skip to content

Commit a8549b4

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

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
@@ -23,6 +23,7 @@ use rustc_middle::query::TyCtxtAt;
2323
use rustc_middle::ty::layout::TyAndLayout;
2424
use rustc_span::def_id::LocalDefId;
2525
use rustc_span::sym;
26+
use rustc_session::lint;
2627

2728
use super::{AllocId, Allocation, InterpCx, MPlaceTy, Machine, MemoryKind, PlaceTy};
2829
use crate::const_eval;
@@ -262,10 +263,13 @@ pub fn intern_const_alloc_recursive<
262263
})?);
263264
}
264265
if found_bad_mutable_pointer {
265-
return Err(ecx
266-
.tcx
267-
.dcx()
268-
.emit_err(MutablePtrInFinal { span: ecx.tcx.span, kind: intern_kind }));
266+
let err_diag = MutablePtrInFinal { span: ecx.tcx.span, kind: intern_kind };
267+
ecx.tcx.emit_node_span_lint(
268+
lint::builtin::CONST_EVAL_MUTABLE_PTR_IN_FINAL_VALUE,
269+
ecx.best_lint_scope(),
270+
err_diag.span,
271+
err_diag,
272+
)
269273
}
270274

271275
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,
@@ -2796,6 +2797,41 @@ declare_lint! {
27962797
@feature_gate = sym::strict_provenance;
27972798
}
27982799

2800+
declare_lint! {
2801+
/// The `const_eval_mutable_ptr_in_final_value` lint detects if a mutable pointer
2802+
/// has leaked into the final value of a const expression.
2803+
///
2804+
/// ### Example
2805+
///
2806+
/// ```rust
2807+
/// pub enum JsValue {
2808+
/// Undefined,
2809+
/// Object(std::cell::Cell<bool>),
2810+
/// }
2811+
///
2812+
/// impl ::std::ops::Drop for JsValue {
2813+
/// fn drop(&mut self) {}
2814+
/// }
2815+
///
2816+
/// const UNDEFINED: &JsValue = &JsValue::Undefined;
2817+
///
2818+
/// fn main() {
2819+
/// }
2820+
/// ```
2821+
///
2822+
/// This is a [future-incompatible] lint to ease the transition to an error.
2823+
/// See [issue #122153] for more details.
2824+
///
2825+
/// [issue #122153]: https://github.com/rust-lang/rust/issues/122153
2826+
pub CONST_EVAL_MUTABLE_PTR_IN_FINAL_VALUE,
2827+
Warn,
2828+
"detects a mutable pointer that has leaked into final value of a const expression",
2829+
@future_incompatible = FutureIncompatibleInfo {
2830+
reason: FutureIncompatibilityReason::FutureReleaseErrorReportInDeps,
2831+
reference: "issue #122153 <https://github.com/rust-lang/rust/issues/122153>",
2832+
};
2833+
}
2834+
27992835
declare_lint! {
28002836
/// The `const_evaluatable_unchecked` lint detects a generic constant used
28012837
/// in a type.

0 commit comments

Comments
 (0)