Skip to content

Commit b543444

Browse files
Rollup merge of #100738 - nidnogg:diagnostics_migr_const_eval, r=davidtwco
Diagnostics migr const eval This PR should eventually contain all diagnostic migrations for the `rustc_const_eval` crate. r? `@davidtwco` `@rustbot` label +A-translation
2 parents 181b041 + 5101688 commit b543444

File tree

5 files changed

+228
-115
lines changed

5 files changed

+228
-115
lines changed

compiler/rustc_const_eval/src/const_eval/mod.rs

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// Not in interpret to make sure we do not use private implementation details
22

3+
use crate::errors::MaxNumNodesInConstErr;
4+
use crate::interpret::{
5+
intern_const_alloc_recursive, ConstValue, InternKind, InterpCx, InterpResult, MemPlaceMeta,
6+
Scalar,
7+
};
38
use rustc_hir::Mutability;
49
use rustc_middle::mir;
510
use rustc_middle::mir::interpret::{EvalToValTreeResult, GlobalId};
611
use rustc_middle::ty::{self, TyCtxt};
712
use rustc_span::{source_map::DUMMY_SP, symbol::Symbol};
813

9-
use crate::interpret::{
10-
intern_const_alloc_recursive, ConstValue, InternKind, InterpCx, InterpResult, MemPlaceMeta,
11-
Scalar,
12-
};
13-
1414
mod error;
1515
mod eval_queries;
1616
mod fn_queries;
@@ -72,12 +72,17 @@ pub(crate) fn eval_to_valtree<'tcx>(
7272
Ok(valtree) => Ok(Some(valtree)),
7373
Err(err) => {
7474
let did = cid.instance.def_id();
75-
let s = cid.display(tcx);
75+
let global_const_id = cid.display(tcx);
7676
match err {
7777
ValTreeCreationError::NodesOverflow => {
78-
let msg = format!("maximum number of nodes exceeded in constant {}", &s);
78+
let msg = format!(
79+
"maximum number of nodes exceeded in constant {}",
80+
&global_const_id
81+
);
7982
let mut diag = match tcx.hir().span_if_local(did) {
80-
Some(span) => tcx.sess.struct_span_err(span, &msg),
83+
Some(span) => {
84+
tcx.sess.create_err(MaxNumNodesInConstErr { span, global_const_id })
85+
}
8186
None => tcx.sess.struct_err(&msg),
8287
};
8388
diag.emit();

compiler/rustc_const_eval/src/errors.rs

+107
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,110 @@ pub(crate) struct TransientMutBorrowErrRaw {
8787
pub span: Span,
8888
pub kind: ConstContext,
8989
}
90+
91+
#[derive(SessionDiagnostic)]
92+
#[diag(const_eval::max_num_nodes_in_const)]
93+
pub(crate) struct MaxNumNodesInConstErr {
94+
#[primary_span]
95+
pub span: Span,
96+
pub global_const_id: String,
97+
}
98+
99+
#[derive(SessionDiagnostic)]
100+
#[diag(const_eval::unallowed_fn_pointer_call)]
101+
pub(crate) struct UnallowedFnPointerCall {
102+
#[primary_span]
103+
pub span: Span,
104+
pub kind: ConstContext,
105+
}
106+
107+
#[derive(SessionDiagnostic)]
108+
#[diag(const_eval::unstable_const_fn)]
109+
pub(crate) struct UnstableConstFn {
110+
#[primary_span]
111+
pub span: Span,
112+
pub def_path: String,
113+
}
114+
115+
#[derive(SessionDiagnostic)]
116+
#[diag(const_eval::unallowed_mutable_refs, code = "E0764")]
117+
pub(crate) struct UnallowedMutableRefs {
118+
#[primary_span]
119+
pub span: Span,
120+
pub kind: ConstContext,
121+
#[note(const_eval::teach_note)]
122+
pub teach: Option<()>,
123+
}
124+
125+
#[derive(SessionDiagnostic)]
126+
#[diag(const_eval::unallowed_mutable_refs_raw, code = "E0764")]
127+
pub(crate) struct UnallowedMutableRefsRaw {
128+
#[primary_span]
129+
pub span: Span,
130+
pub kind: ConstContext,
131+
#[note(const_eval::teach_note)]
132+
pub teach: Option<()>,
133+
}
134+
#[derive(SessionDiagnostic)]
135+
#[diag(const_eval::non_const_fmt_macro_call, code = "E0015")]
136+
pub(crate) struct NonConstFmtMacroCall {
137+
#[primary_span]
138+
pub span: Span,
139+
pub kind: ConstContext,
140+
}
141+
142+
#[derive(SessionDiagnostic)]
143+
#[diag(const_eval::non_const_fn_call, code = "E0015")]
144+
pub(crate) struct NonConstFnCall {
145+
#[primary_span]
146+
pub span: Span,
147+
pub def_path_str: String,
148+
pub kind: ConstContext,
149+
}
150+
151+
#[derive(SessionDiagnostic)]
152+
#[diag(const_eval::unallowed_op_in_const_context)]
153+
pub(crate) struct UnallowedOpInConstContext {
154+
#[primary_span]
155+
pub span: Span,
156+
pub msg: String,
157+
}
158+
159+
#[derive(SessionDiagnostic)]
160+
#[diag(const_eval::unallowed_heap_allocations, code = "E0010")]
161+
pub(crate) struct UnallowedHeapAllocations {
162+
#[primary_span]
163+
#[label]
164+
pub span: Span,
165+
pub kind: ConstContext,
166+
#[note(const_eval::teach_note)]
167+
pub teach: Option<()>,
168+
}
169+
170+
#[derive(SessionDiagnostic)]
171+
#[diag(const_eval::unallowed_inline_asm, code = "E0015")]
172+
pub(crate) struct UnallowedInlineAsm {
173+
#[primary_span]
174+
pub span: Span,
175+
pub kind: ConstContext,
176+
}
177+
178+
#[derive(SessionDiagnostic)]
179+
#[diag(const_eval::interior_mutable_data_refer, code = "E0492")]
180+
pub(crate) struct InteriorMutableDataRefer {
181+
#[primary_span]
182+
#[label]
183+
pub span: Span,
184+
#[help]
185+
pub opt_help: Option<()>,
186+
pub kind: ConstContext,
187+
#[note(const_eval::teach_note)]
188+
pub teach: Option<()>,
189+
}
190+
191+
#[derive(SessionDiagnostic)]
192+
#[diag(const_eval::interior_mutability_borrow)]
193+
pub(crate) struct InteriorMutabilityBorrow {
194+
#[primary_span]
195+
pub span: Span,
196+
}

compiler/rustc_const_eval/src/transform/check_consts/ops.rs

+51-105
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ use rustc_trait_selection::traits::SelectionContext;
2424

2525
use super::ConstCx;
2626
use crate::errors::{
27-
MutDerefErr, NonConstOpErr, PanicNonStrErr, RawPtrToIntErr, StaticAccessErr,
28-
TransientMutBorrowErr, TransientMutBorrowErrRaw,
27+
InteriorMutabilityBorrow, InteriorMutableDataRefer, MutDerefErr, NonConstFmtMacroCall,
28+
NonConstFnCall, NonConstOpErr, PanicNonStrErr, RawPtrToIntErr, StaticAccessErr,
29+
TransientMutBorrowErr, TransientMutBorrowErrRaw, UnallowedFnPointerCall,
30+
UnallowedHeapAllocations, UnallowedInlineAsm, UnallowedMutableRefs, UnallowedMutableRefsRaw,
31+
UnallowedOpInConstContext, UnstableConstFn,
2932
};
3033
use crate::util::{call_kind, CallDesugaringKind, CallKind};
3134

@@ -97,10 +100,7 @@ impl<'tcx> NonConstOp<'tcx> for FnCallIndirect {
97100
ccx: &ConstCx<'_, 'tcx>,
98101
span: Span,
99102
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
100-
ccx.tcx.sess.struct_span_err(
101-
span,
102-
&format!("function pointer calls are not allowed in {}s", ccx.const_kind()),
103-
)
103+
ccx.tcx.sess.create_err(UnallowedFnPointerCall { span, kind: ccx.const_kind() })
104104
}
105105
}
106106

@@ -308,22 +308,13 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
308308
err
309309
}
310310
_ if tcx.opt_parent(callee) == tcx.get_diagnostic_item(sym::ArgumentV1Methods) => {
311-
struct_span_err!(
312-
ccx.tcx.sess,
313-
span,
314-
E0015,
315-
"cannot call non-const formatting macro in {}s",
316-
ccx.const_kind(),
317-
)
311+
ccx.tcx.sess.create_err(NonConstFmtMacroCall { span, kind: ccx.const_kind() })
318312
}
319-
_ => struct_span_err!(
320-
ccx.tcx.sess,
313+
_ => ccx.tcx.sess.create_err(NonConstFnCall {
321314
span,
322-
E0015,
323-
"cannot call non-const fn `{}` in {}s",
324-
ccx.tcx.def_path_str_with_substs(callee, substs),
325-
ccx.const_kind(),
326-
),
315+
def_path_str: ccx.tcx.def_path_str_with_substs(callee, substs),
316+
kind: ccx.const_kind(),
317+
}),
327318
};
328319

329320
err.note(&format!(
@@ -354,10 +345,10 @@ impl<'tcx> NonConstOp<'tcx> for FnCallUnstable {
354345
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
355346
let FnCallUnstable(def_id, feature) = *self;
356347

357-
let mut err = ccx.tcx.sess.struct_span_err(
358-
span,
359-
&format!("`{}` is not yet stable as a const fn", ccx.tcx.def_path_str(def_id)),
360-
);
348+
let mut err = ccx
349+
.tcx
350+
.sess
351+
.create_err(UnstableConstFn { span, def_path: ccx.tcx.def_path_str(def_id) });
361352

362353
if ccx.is_const_stable_const_fn() {
363354
err.help("const-stable functions can only call other const-stable functions");
@@ -392,9 +383,12 @@ impl<'tcx> NonConstOp<'tcx> for Generator {
392383
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
393384
let msg = format!("{}s are not allowed in {}s", self.0, ccx.const_kind());
394385
if let hir::GeneratorKind::Async(hir::AsyncGeneratorKind::Block) = self.0 {
395-
feature_err(&ccx.tcx.sess.parse_sess, sym::const_async_blocks, span, &msg)
386+
ccx.tcx.sess.create_feature_err(
387+
UnallowedOpInConstContext { span, msg },
388+
sym::const_async_blocks,
389+
)
396390
} else {
397-
ccx.tcx.sess.struct_span_err(span, &msg)
391+
ccx.tcx.sess.create_err(UnallowedOpInConstContext { span, msg })
398392
}
399393
}
400394
}
@@ -407,23 +401,11 @@ impl<'tcx> NonConstOp<'tcx> for HeapAllocation {
407401
ccx: &ConstCx<'_, 'tcx>,
408402
span: Span,
409403
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
410-
let mut err = struct_span_err!(
411-
ccx.tcx.sess,
404+
ccx.tcx.sess.create_err(UnallowedHeapAllocations {
412405
span,
413-
E0010,
414-
"allocations are not allowed in {}s",
415-
ccx.const_kind()
416-
);
417-
err.span_label(span, format!("allocation not allowed in {}s", ccx.const_kind()));
418-
if ccx.tcx.sess.teach(&err.get_code().unwrap()) {
419-
err.note(
420-
"The value of statics and constants must be known at compile time, \
421-
and they live for the entire lifetime of a program. Creating a boxed \
422-
value allocates memory on the heap at runtime, and therefore cannot \
423-
be done at compile time.",
424-
);
425-
}
426-
err
406+
kind: ccx.const_kind(),
407+
teach: ccx.tcx.sess.teach(&error_code!(E0010)).then_some(()),
408+
})
427409
}
428410
}
429411

@@ -435,13 +417,7 @@ impl<'tcx> NonConstOp<'tcx> for InlineAsm {
435417
ccx: &ConstCx<'_, 'tcx>,
436418
span: Span,
437419
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
438-
struct_span_err!(
439-
ccx.tcx.sess,
440-
span,
441-
E0015,
442-
"inline assembly is not allowed in {}s",
443-
ccx.const_kind()
444-
)
420+
ccx.tcx.sess.create_err(UnallowedInlineAsm { span, kind: ccx.const_kind() })
445421
}
446422
}
447423

@@ -487,12 +463,7 @@ impl<'tcx> NonConstOp<'tcx> for TransientCellBorrow {
487463
ccx: &ConstCx<'_, 'tcx>,
488464
span: Span,
489465
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
490-
feature_err(
491-
&ccx.tcx.sess.parse_sess,
492-
sym::const_refs_to_cell,
493-
span,
494-
"cannot borrow here, since the borrowed element may contain interior mutability",
495-
)
466+
ccx.tcx.sess.create_feature_err(InteriorMutabilityBorrow { span }, sym::const_refs_to_cell)
496467
}
497468
}
498469

@@ -507,32 +478,22 @@ impl<'tcx> NonConstOp<'tcx> for CellBorrow {
507478
ccx: &ConstCx<'_, 'tcx>,
508479
span: Span,
509480
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
510-
let mut err = struct_span_err!(
511-
ccx.tcx.sess,
512-
span,
513-
E0492,
514-
"{}s cannot refer to interior mutable data",
515-
ccx.const_kind(),
516-
);
517-
err.span_label(
518-
span,
519-
"this borrow of an interior mutable value may end up in the final value",
520-
);
481+
// FIXME: Maybe a more elegant solution to this if else case
521482
if let hir::ConstContext::Static(_) = ccx.const_kind() {
522-
err.help(
523-
"to fix this, the value can be extracted to a separate \
524-
`static` item and then referenced",
525-
);
526-
}
527-
if ccx.tcx.sess.teach(&err.get_code().unwrap()) {
528-
err.note(
529-
"A constant containing interior mutable data behind a reference can allow you
530-
to modify that data. This would make multiple uses of a constant to be able to
531-
see different values and allow circumventing the `Send` and `Sync` requirements
532-
for shared mutable data, which is unsound.",
533-
);
483+
ccx.tcx.sess.create_err(InteriorMutableDataRefer {
484+
span,
485+
opt_help: Some(()),
486+
kind: ccx.const_kind(),
487+
teach: ccx.tcx.sess.teach(&error_code!(E0492)).then_some(()),
488+
})
489+
} else {
490+
ccx.tcx.sess.create_err(InteriorMutableDataRefer {
491+
span,
492+
opt_help: None,
493+
kind: ccx.const_kind(),
494+
teach: ccx.tcx.sess.teach(&error_code!(E0492)).then_some(()),
495+
})
534496
}
535-
err
536497
}
537498
}
538499

@@ -558,33 +519,18 @@ impl<'tcx> NonConstOp<'tcx> for MutBorrow {
558519
ccx: &ConstCx<'_, 'tcx>,
559520
span: Span,
560521
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
561-
let raw = match self.0 {
562-
hir::BorrowKind::Raw => "raw ",
563-
hir::BorrowKind::Ref => "",
564-
};
565-
566-
let mut err = struct_span_err!(
567-
ccx.tcx.sess,
568-
span,
569-
E0764,
570-
"{}mutable references are not allowed in the final value of {}s",
571-
raw,
572-
ccx.const_kind(),
573-
);
574-
575-
if ccx.tcx.sess.teach(&err.get_code().unwrap()) {
576-
err.note(
577-
"References in statics and constants may only refer \
578-
to immutable values.\n\n\
579-
Statics are shared everywhere, and if they refer to \
580-
mutable data one might violate memory safety since \
581-
holding multiple mutable references to shared data \
582-
is not allowed.\n\n\
583-
If you really want global mutable state, try using \
584-
static mut or a global UnsafeCell.",
585-
);
522+
match self.0 {
523+
hir::BorrowKind::Raw => ccx.tcx.sess.create_err(UnallowedMutableRefsRaw {
524+
span,
525+
kind: ccx.const_kind(),
526+
teach: ccx.tcx.sess.teach(&error_code!(E0764)).then_some(()),
527+
}),
528+
hir::BorrowKind::Ref => ccx.tcx.sess.create_err(UnallowedMutableRefs {
529+
span,
530+
kind: ccx.const_kind(),
531+
teach: ccx.tcx.sess.teach(&error_code!(E0764)).then_some(()),
532+
}),
586533
}
587-
err
588534
}
589535
}
590536

0 commit comments

Comments
 (0)