Skip to content

Commit 7e6367c

Browse files
authored
Unrolled build for rust-lang#131397
Rollup merge of rust-lang#131397 - RalfJung:const-escaping-ref-teach, r=chenyukang fix/update teach_note from 'escaping mutable ref/ptr' const-check The old note was quite confusing since it talked about statics, but the message is also shown for consts. So let's reword to something that is true for both of them.
2 parents de19f2b + 287eb03 commit 7e6367c

File tree

5 files changed

+37
-35
lines changed

5 files changed

+37
-35
lines changed

compiler/rustc_const_eval/messages.ftl

+26-25
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
@@ -364,30 +384,11 @@ const_eval_unallowed_fn_pointer_call = function pointer calls are not allowed in
364384
const_eval_unallowed_heap_allocations =
365385
allocations are not allowed in {const_eval_const_context}s
366386
.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.
387+
.teach_note =
388+
The runtime heap is not yet available at compile-time, so no runtime heap allocations can be created.
368389
369390
const_eval_unallowed_inline_asm =
370391
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.
391392
392393
const_eval_unallowed_op_in_const_context =
393394
{$msg}

compiler/rustc_const_eval/src/check_consts/check.rs

+1
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
666666
}
667667
}
668668

669+
// This can be called on stable via the `vec!` macro.
669670
if tcx.is_lang_item(callee, LangItem::ExchangeMalloc) {
670671
self.check_op(ops::HeapAllocation);
671672
return;

compiler/rustc_const_eval/src/check_consts/ops.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ impl<'tcx> NonConstOp<'tcx> for EscapingCellBorrow {
402402
DiagImportance::Secondary
403403
}
404404
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
405-
ccx.dcx().create_err(errors::InteriorMutableDataRefer {
405+
ccx.dcx().create_err(errors::InteriorMutableRefEscaping {
406406
span,
407407
opt_help: matches!(ccx.const_kind(), hir::ConstContext::Static(_)),
408408
kind: ccx.const_kind(),
@@ -430,12 +430,12 @@ impl<'tcx> NonConstOp<'tcx> for EscapingMutBorrow {
430430

431431
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
432432
match self.0 {
433-
hir::BorrowKind::Raw => ccx.tcx.dcx().create_err(errors::UnallowedMutableRaw {
433+
hir::BorrowKind::Raw => ccx.tcx.dcx().create_err(errors::MutableRawEscaping {
434434
span,
435435
kind: ccx.const_kind(),
436436
teach: ccx.tcx.sess.teach(E0764),
437437
}),
438-
hir::BorrowKind::Ref => ccx.dcx().create_err(errors::UnallowedMutableRefs {
438+
hir::BorrowKind::Ref => ccx.dcx().create_err(errors::MutableRefEscaping {
439439
span,
440440
kind: ccx.const_kind(),
441441
teach: ccx.tcx.sess.teach(E0764),

compiler/rustc_const_eval/src/errors.rs

+6-6
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,
@@ -181,8 +181,8 @@ pub(crate) struct UnallowedInlineAsm {
181181
}
182182

183183
#[derive(Diagnostic)]
184-
#[diag(const_eval_interior_mutable_data_refer, code = E0492)]
185-
pub(crate) struct InteriorMutableDataRefer {
184+
#[diag(const_eval_interior_mutable_ref_escaping, code = E0492)]
185+
pub(crate) struct InteriorMutableRefEscaping {
186186
#[primary_span]
187187
#[label]
188188
pub span: Span,

tests/ui/error-codes/E0010-teach.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0010]: allocations are not allowed in constants
44
LL | const CON: Vec<i32> = vec![1, 2, 3];
55
| ^^^^^^^^^^^^^ allocation not allowed in constants
66
|
7-
= 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.
7+
= note: The runtime heap is not yet available at compile-time, so no runtime heap allocations can be created.
88
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)
99

1010
error[E0015]: cannot call non-const fn `slice::<impl [i32]>::into_vec::<std::alloc::Global>` in constants

0 commit comments

Comments
 (0)