Skip to content

Commit 24deacc

Browse files
committed
fix/update teach_note from 'escaping mutable ref/ptr' const-check
1 parent cf24c73 commit 24deacc

File tree

4 files changed

+33
-66
lines changed

4 files changed

+33
-66
lines changed

compiler/rustc_const_eval/messages.ftl

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,16 @@ const_eval_incompatible_return_types =
134134
const_eval_incompatible_types =
135135
calling a function with argument of type {$callee_ty} passing data of type {$caller_ty}
136136
137-
const_eval_interior_mutable_data_refer =
137+
const_eval_interior_mutable_ref_escaping =
138138
{const_eval_const_context}s cannot refer to interior mutable data
139139
.label = this borrow of an interior mutable value may end up in the final value
140140
.help = to fix this, the value can be extracted to a separate `static` item and then referenced
141141
.teach_note =
142-
A constant containing interior mutable data behind a reference can allow you to modify that data.
143-
This would make multiple uses of a constant to be able to see different values and allow circumventing
144-
the `Send` and `Sync` requirements for shared mutable data, which is unsound.
142+
References that escape into the final value of a constant or static must be immutable.
143+
This is to avoid accidentally creating shared mutable state.
144+
145+
146+
If you really want global mutable state, try using an interior mutable `static` or a `static mut`.
145147
146148
const_eval_intern_kind = {$kind ->
147149
[static] static
@@ -229,6 +231,24 @@ const_eval_modified_global =
229231
230232
const_eval_mutable_ptr_in_final = encountered mutable pointer in final value of {const_eval_intern_kind}
231233
234+
const_eval_mutable_raw_escaping =
235+
raw mutable pointers are not allowed in the final value of {const_eval_const_context}s
236+
.teach_note =
237+
Pointers that escape into the final value of a constant or static must be immutable.
238+
This is to avoid accidentally creating shared mutable state.
239+
240+
241+
If you really want global mutable state, try using an interior mutable `static` or a `static mut`.
242+
243+
const_eval_mutable_ref_escaping =
244+
mutable references are not allowed in the final value of {const_eval_const_context}s
245+
.teach_note =
246+
References that escape into the final value of a constant or static must be immutable.
247+
This is to avoid accidentally creating shared mutable state.
248+
249+
250+
If you really want global mutable state, try using an interior mutable `static` or a `static mut`.
251+
232252
const_eval_nested_static_in_thread_local = #[thread_local] does not support implicit nested statics, please create explicit static items and refer to them instead
233253
const_eval_non_const_fmt_macro_call =
234254
cannot call non-const formatting macro in {const_eval_const_context}s
@@ -361,33 +381,8 @@ const_eval_try_block_from_output_non_const =
361381
`try` block cannot convert `{$ty}` to the result in {const_eval_const_context}s
362382
const_eval_unallowed_fn_pointer_call = function pointer calls are not allowed in {const_eval_const_context}s
363383
364-
const_eval_unallowed_heap_allocations =
365-
allocations are not allowed in {const_eval_const_context}s
366-
.label = allocation not allowed in {const_eval_const_context}s
367-
.teach_note = The value of statics and constants must be known at compile time, and they live for the entire lifetime of a program. Creating a boxed value allocates memory on the heap at runtime, and therefore cannot be done at compile time.
368-
369384
const_eval_unallowed_inline_asm =
370385
inline assembly is not allowed in {const_eval_const_context}s
371-
const_eval_unallowed_mutable_raw =
372-
raw mutable pointers are not allowed in the final value of {const_eval_const_context}s
373-
.teach_note =
374-
References in statics and constants may only refer to immutable values.
375-
376-
377-
Statics are shared everywhere, and if they refer to mutable data one might violate memory
378-
safety since holding multiple mutable references to shared data is not allowed.
379-
380-
381-
If you really want global mutable state, try using static mut or a global UnsafeCell.
382-
383-
const_eval_unallowed_mutable_refs =
384-
mutable references are not allowed in the final value of {const_eval_const_context}s
385-
.teach_note =
386-
Statics are shared everywhere, and if they refer to mutable data one might violate memory
387-
safety since holding multiple mutable references to shared data is not allowed.
388-
389-
390-
If you really want global mutable state, try using static mut or a global UnsafeCell.
391386
392387
const_eval_unallowed_op_in_const_context =
393388
{$msg}

compiler/rustc_const_eval/src/check_consts/check.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -666,11 +666,6 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
666666
}
667667
}
668668

669-
if tcx.is_lang_item(callee, LangItem::ExchangeMalloc) {
670-
self.check_op(ops::HeapAllocation);
671-
return;
672-
}
673-
674669
if !tcx.is_const_fn_raw(callee) && !is_trait {
675670
self.check_op(ops::FnCallNonConst {
676671
caller,

compiler/rustc_const_eval/src/check_consts/ops.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -354,18 +354,6 @@ impl<'tcx> NonConstOp<'tcx> for Coroutine {
354354
}
355355
}
356356

357-
#[derive(Debug)]
358-
pub(crate) struct HeapAllocation;
359-
impl<'tcx> NonConstOp<'tcx> for HeapAllocation {
360-
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
361-
ccx.dcx().create_err(errors::UnallowedHeapAllocations {
362-
span,
363-
kind: ccx.const_kind(),
364-
teach: ccx.tcx.sess.teach(E0010),
365-
})
366-
}
367-
}
368-
369357
#[derive(Debug)]
370358
pub(crate) struct InlineAsm;
371359
impl<'tcx> NonConstOp<'tcx> for InlineAsm {
@@ -402,7 +390,7 @@ impl<'tcx> NonConstOp<'tcx> for EscapingCellBorrow {
402390
DiagImportance::Secondary
403391
}
404392
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
405-
ccx.dcx().create_err(errors::InteriorMutableDataRefer {
393+
ccx.dcx().create_err(errors::InteriorMutableRefEscaping {
406394
span,
407395
opt_help: matches!(ccx.const_kind(), hir::ConstContext::Static(_)),
408396
kind: ccx.const_kind(),
@@ -430,12 +418,12 @@ impl<'tcx> NonConstOp<'tcx> for EscapingMutBorrow {
430418

431419
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
432420
match self.0 {
433-
hir::BorrowKind::Raw => ccx.tcx.dcx().create_err(errors::UnallowedMutableRaw {
421+
hir::BorrowKind::Raw => ccx.tcx.dcx().create_err(errors::MutableRawEscaping {
434422
span,
435423
kind: ccx.const_kind(),
436424
teach: ccx.tcx.sess.teach(E0764),
437425
}),
438-
hir::BorrowKind::Ref => ccx.dcx().create_err(errors::UnallowedMutableRefs {
426+
hir::BorrowKind::Ref => ccx.dcx().create_err(errors::MutableRefEscaping {
439427
span,
440428
kind: ccx.const_kind(),
441429
teach: ccx.tcx.sess.teach(E0764),

compiler/rustc_const_eval/src/errors.rs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ pub(crate) struct UnstableConstFn {
118118
}
119119

120120
#[derive(Diagnostic)]
121-
#[diag(const_eval_unallowed_mutable_refs, code = E0764)]
122-
pub(crate) struct UnallowedMutableRefs {
121+
#[diag(const_eval_mutable_ref_escaping, code = E0764)]
122+
pub(crate) struct MutableRefEscaping {
123123
#[primary_span]
124124
pub span: Span,
125125
pub kind: ConstContext,
@@ -128,8 +128,8 @@ pub(crate) struct UnallowedMutableRefs {
128128
}
129129

130130
#[derive(Diagnostic)]
131-
#[diag(const_eval_unallowed_mutable_raw, code = E0764)]
132-
pub(crate) struct UnallowedMutableRaw {
131+
#[diag(const_eval_mutable_raw_escaping, code = E0764)]
132+
pub(crate) struct MutableRawEscaping {
133133
#[primary_span]
134134
pub span: Span,
135135
pub kind: ConstContext,
@@ -161,17 +161,6 @@ pub(crate) struct UnallowedOpInConstContext {
161161
pub msg: String,
162162
}
163163

164-
#[derive(Diagnostic)]
165-
#[diag(const_eval_unallowed_heap_allocations, code = E0010)]
166-
pub(crate) struct UnallowedHeapAllocations {
167-
#[primary_span]
168-
#[label]
169-
pub span: Span,
170-
pub kind: ConstContext,
171-
#[note(const_eval_teach_note)]
172-
pub teach: bool,
173-
}
174-
175164
#[derive(Diagnostic)]
176165
#[diag(const_eval_unallowed_inline_asm, code = E0015)]
177166
pub(crate) struct UnallowedInlineAsm {
@@ -181,8 +170,8 @@ pub(crate) struct UnallowedInlineAsm {
181170
}
182171

183172
#[derive(Diagnostic)]
184-
#[diag(const_eval_interior_mutable_data_refer, code = E0492)]
185-
pub(crate) struct InteriorMutableDataRefer {
173+
#[diag(const_eval_interior_mutable_ref_escaping, code = E0492)]
174+
pub(crate) struct InteriorMutableRefEscaping {
186175
#[primary_span]
187176
#[label]
188177
pub span: Span,

0 commit comments

Comments
 (0)