Skip to content

Commit bbe951e

Browse files
Make the non-const part swappable in the diagnostic
1 parent 1559a39 commit bbe951e

File tree

3 files changed

+63
-41
lines changed

3 files changed

+63
-41
lines changed

compiler/rustc_const_eval/messages.ftl

+32-27
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ const_eval_already_reported =
1212
const_eval_assume_false =
1313
`assume` called with `false`
1414
15-
const_eval_await_non_const =
16-
cannot convert `{$ty}` into a future in {const_eval_const_context}s
1715
const_eval_bounds_check_failed =
1816
indexing out of bounds: the len is {$len} but the index is {$index}
1917
const_eval_call_nonzero_intrinsic =
@@ -23,9 +21,6 @@ const_eval_closure_call =
2321
closures need an RFC before allowed to be called in {const_eval_const_context}s
2422
const_eval_closure_fndef_not_const =
2523
function defined here, but it is not `const`
26-
const_eval_closure_non_const =
27-
cannot call non-const closure in {const_eval_const_context}s
28-
2924
const_eval_conditionally_const_call =
3025
cannot call conditionally-const {$def_descr} `{$def_path_str}` in {const_eval_const_context}s
3126
@@ -62,10 +57,6 @@ const_eval_dealloc_incorrect_layout =
6257
const_eval_dealloc_kind_mismatch =
6358
deallocating {$alloc}, which is {$alloc_kind} memory, using {$kind} deallocation operation
6459
65-
const_eval_deref_coercion_non_const =
66-
cannot perform deref coercion on `{$ty}` in {const_eval_const_context}s
67-
.note = attempting to deref into `{$target_ty}`
68-
.target_note = deref defined here
6960
const_eval_deref_function_pointer =
7061
accessing {$allocation} which contains a function
7162
const_eval_deref_vtable_pointer =
@@ -109,9 +100,6 @@ const_eval_extern_type_field = `extern type` field does not have a known offset
109100
110101
const_eval_fn_ptr_call =
111102
function pointers need an RFC before allowed to be called in {const_eval_const_context}s
112-
const_eval_for_loop_into_iter_non_const =
113-
cannot use `for` loop on `{$ty}` in {const_eval_const_context}s
114-
115103
const_eval_frame_note = {$times ->
116104
[0] {const_eval_frame_note_inner}
117105
*[other] [... {$times} additional calls {const_eval_frame_note_inner} ...]
@@ -216,9 +204,6 @@ const_eval_long_running =
216204
.label = the const evaluator is currently interpreting this expression
217205
.help = the constant being evaluated
218206
219-
const_eval_match_eq_non_const = cannot match on `{$ty}` in {const_eval_const_context}s
220-
.note = `{$ty}` cannot be compared in compile-time, and therefore cannot be used in `match`es
221-
222207
const_eval_max_num_nodes_in_const = maximum number of nodes exceeded in constant {$global_const_id}
223208
224209
const_eval_memory_access_test = memory access failed
@@ -249,17 +234,46 @@ const_eval_mutable_ref_escaping =
249234
If you really want global mutable state, try using an interior mutable `static` or a `static mut`.
250235
251236
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
237+
238+
const_eval_non_const_await =
239+
cannot convert `{$ty}` into a future in {const_eval_const_context}s
240+
241+
const_eval_non_const_closure =
242+
cannot call {$non}-const closure in {const_eval_const_context}s
243+
244+
const_eval_non_const_deref_coercion =
245+
cannot perform {$non}-const deref coercion on `{$ty}` in {const_eval_const_context}s
246+
.note = attempting to deref into `{$target_ty}`
247+
.target_note = deref defined here
248+
252249
const_eval_non_const_fmt_macro_call =
253-
cannot call non-const formatting macro in {const_eval_const_context}s
250+
cannot call {$non}-const formatting macro in {const_eval_const_context}s
254251
255252
const_eval_non_const_fn_call =
256-
cannot call non-const {$def_descr} `{$def_path_str}` in {const_eval_const_context}s
253+
cannot call {$non}-const {$def_descr} `{$def_path_str}` in {const_eval_const_context}s
254+
255+
const_eval_non_const_for_loop_into_iter =
256+
cannot use `for` loop on `{$ty}` in {const_eval_const_context}s
257257
258258
const_eval_non_const_impl =
259259
impl defined here, but it is not `const`
260260
261261
const_eval_non_const_intrinsic =
262-
cannot call non-const intrinsic `{$name}` in {const_eval_const_context}s
262+
cannot call {$non}-const intrinsic `{$name}` in {const_eval_const_context}s
263+
264+
const_eval_non_const_match_eq = cannot match on `{$ty}` in {const_eval_const_context}s
265+
.note = `{$ty}` cannot be compared in compile-time, and therefore cannot be used in `match`es
266+
267+
const_eval_non_const_operator =
268+
cannot call {$non}-const operator in {const_eval_const_context}s
269+
270+
const_eval_non_const_question_branch =
271+
`?` is not allowed on `{$ty}` in {const_eval_const_context}s
272+
const_eval_non_const_question_from_residual =
273+
`?` is not allowed on `{$ty}` in {const_eval_const_context}s
274+
275+
const_eval_non_const_try_block_from_output =
276+
`try` block cannot convert `{$ty}` to the result in {const_eval_const_context}s
263277
264278
const_eval_not_enough_caller_args =
265279
calling a function with fewer arguments than it requires
@@ -281,8 +295,6 @@ const_eval_offset_from_unsigned_overflow =
281295
*[false] offset
282296
} than second: {$a_offset} < {$b_offset}
283297
284-
const_eval_operator_non_const =
285-
cannot call non-const operator in {const_eval_const_context}s
286298
const_eval_overflow_arith =
287299
arithmetic overflow in `{$intrinsic}`
288300
const_eval_overflow_shift =
@@ -325,11 +337,6 @@ const_eval_ptr_as_bytes_1 =
325337
const_eval_ptr_as_bytes_2 =
326338
the absolute address of a pointer is not known at compile-time, so such operations are not supported
327339
328-
const_eval_question_branch_non_const =
329-
`?` is not allowed on `{$ty}` in {const_eval_const_context}s
330-
const_eval_question_from_residual_non_const =
331-
`?` is not allowed on `{$ty}` in {const_eval_const_context}s
332-
333340
const_eval_range = in the range {$lo}..={$hi}
334341
const_eval_range_lower = greater or equal to {$lo}
335342
const_eval_range_singular = equal to {$lo}
@@ -379,8 +386,6 @@ const_eval_too_generic =
379386
const_eval_too_many_caller_args =
380387
calling a function with more arguments than it expected
381388
382-
const_eval_try_block_from_output_non_const =
383-
`try` block cannot convert `{$ty}` to the result in {const_eval_const_context}s
384389
const_eval_unallowed_fn_pointer_call = function pointer calls are not allowed in {const_eval_const_context}s
385390
386391
const_eval_unallowed_heap_allocations =

compiler/rustc_const_eval/src/check_consts/ops.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
130130
let ConstCx { tcx, typing_env, .. } = *ccx;
131131
let caller = ccx.def_id();
132132

133-
let diag_trait = |err, self_ty: Ty<'_>, trait_id| {
133+
let note_trait_if_possible = |err, self_ty: Ty<'_>, trait_id| {
134134
let trait_ref = TraitRef::from_method(tcx, trait_id, args);
135135

136136
match self_ty.kind() {
@@ -183,6 +183,7 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
183183
span,
184184
ty: self_ty,
185185
kind: ccx.const_kind(),
186+
non: "non",
186187
})
187188
};
188189
}
@@ -226,9 +227,10 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
226227
span,
227228
kind: ccx.const_kind(),
228229
note,
230+
non: "non",
229231
});
230232

231-
diag_trait(&mut err, self_ty, fn_trait_id);
233+
note_trait_if_possible(&mut err, self_ty, fn_trait_id);
232234
err
233235
}
234236
CallKind::Operator { trait_id, self_ty, .. } => {
@@ -237,6 +239,7 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
237239
span,
238240
kind: ccx.const_kind(),
239241
ty: self_ty,
242+
non: "non",
240243
})
241244
} else {
242245
let mut sugg = None;
@@ -282,10 +285,11 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
282285
span,
283286
kind: ccx.const_kind(),
284287
sugg,
288+
non: "non",
285289
})
286290
};
287291

288-
diag_trait(&mut err, self_ty, trait_id);
292+
note_trait_if_possible(&mut err, self_ty, trait_id);
289293
err
290294
}
291295
CallKind::DerefCoercion { deref_target, deref_target_ty, self_ty } => {
@@ -302,19 +306,21 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
302306
kind: ccx.const_kind(),
303307
target_ty: deref_target_ty,
304308
deref_target: target,
309+
non: "non",
305310
});
306311

307-
diag_trait(&mut err, self_ty, tcx.require_lang_item(LangItem::Deref, Some(span)));
312+
note_trait_if_possible(&mut err, self_ty, tcx.require_lang_item(LangItem::Deref, Some(span)));
308313
err
309314
}
310315
_ if tcx.opt_parent(callee) == tcx.get_diagnostic_item(sym::ArgumentMethods) => {
311-
ccx.dcx().create_err(errors::NonConstFmtMacroCall { span, kind: ccx.const_kind() })
316+
ccx.dcx().create_err(errors::NonConstFmtMacroCall { span, kind: ccx.const_kind(), non: "non" })
312317
}
313318
_ => ccx.dcx().create_err(errors::NonConstFnCall {
314319
span,
315320
def_descr: ccx.tcx.def_descr(callee),
316321
def_path_str: ccx.tcx.def_path_str_with_args(callee, args),
317322
kind: ccx.const_kind(),
323+
non: "non",
318324
}),
319325
};
320326

compiler/rustc_const_eval/src/errors.rs

+20-9
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ pub(crate) struct NonConstFmtMacroCall {
174174
#[primary_span]
175175
pub span: Span,
176176
pub kind: ConstContext,
177+
pub non: &'static str,
177178
}
178179

179180
#[derive(Diagnostic)]
@@ -194,6 +195,7 @@ pub(crate) struct NonConstFnCall {
194195
pub def_path_str: String,
195196
pub def_descr: &'static str,
196197
pub kind: ConstContext,
198+
pub non: &'static str,
197199
}
198200

199201
#[derive(Diagnostic)]
@@ -293,68 +295,75 @@ pub struct RawBytesNote {
293295
// FIXME(fee1-dead) do not use stringly typed `ConstContext`
294296

295297
#[derive(Diagnostic)]
296-
#[diag(const_eval_match_eq_non_const, code = E0015)]
298+
#[diag(const_eval_non_const_match_eq, code = E0015)]
297299
#[note]
298300
pub struct NonConstMatchEq<'tcx> {
299301
#[primary_span]
300302
pub span: Span,
301303
pub ty: Ty<'tcx>,
302304
pub kind: ConstContext,
305+
pub non: &'static str,
303306
}
304307

305308
#[derive(Diagnostic)]
306-
#[diag(const_eval_for_loop_into_iter_non_const, code = E0015)]
309+
#[diag(const_eval_non_const_for_loop_into_iter, code = E0015)]
307310
pub struct NonConstForLoopIntoIter<'tcx> {
308311
#[primary_span]
309312
pub span: Span,
310313
pub ty: Ty<'tcx>,
311314
pub kind: ConstContext,
315+
pub non: &'static str,
312316
}
313317

314318
#[derive(Diagnostic)]
315-
#[diag(const_eval_question_branch_non_const, code = E0015)]
319+
#[diag(const_eval_non_const_question_branch, code = E0015)]
316320
pub struct NonConstQuestionBranch<'tcx> {
317321
#[primary_span]
318322
pub span: Span,
319323
pub ty: Ty<'tcx>,
320324
pub kind: ConstContext,
325+
pub non: &'static str,
321326
}
322327

323328
#[derive(Diagnostic)]
324-
#[diag(const_eval_question_from_residual_non_const, code = E0015)]
329+
#[diag(const_eval_non_const_question_from_residual, code = E0015)]
325330
pub struct NonConstQuestionFromResidual<'tcx> {
326331
#[primary_span]
327332
pub span: Span,
328333
pub ty: Ty<'tcx>,
329334
pub kind: ConstContext,
335+
pub non: &'static str,
330336
}
331337

332338
#[derive(Diagnostic)]
333-
#[diag(const_eval_try_block_from_output_non_const, code = E0015)]
339+
#[diag(const_eval_non_const_try_block_from_output, code = E0015)]
334340
pub struct NonConstTryBlockFromOutput<'tcx> {
335341
#[primary_span]
336342
pub span: Span,
337343
pub ty: Ty<'tcx>,
338344
pub kind: ConstContext,
345+
pub non: &'static str,
339346
}
340347

341348
#[derive(Diagnostic)]
342-
#[diag(const_eval_await_non_const, code = E0015)]
349+
#[diag(const_eval_non_const_await, code = E0015)]
343350
pub struct NonConstAwait<'tcx> {
344351
#[primary_span]
345352
pub span: Span,
346353
pub ty: Ty<'tcx>,
347354
pub kind: ConstContext,
355+
pub non: &'static str,
348356
}
349357

350358
#[derive(Diagnostic)]
351-
#[diag(const_eval_closure_non_const, code = E0015)]
359+
#[diag(const_eval_non_const_closure, code = E0015)]
352360
pub struct NonConstClosure {
353361
#[primary_span]
354362
pub span: Span,
355363
pub kind: ConstContext,
356364
#[subdiagnostic]
357365
pub note: Option<NonConstClosureNote>,
366+
pub non: &'static str,
358367
}
359368

360369
#[derive(Subdiagnostic)]
@@ -381,17 +390,18 @@ pub struct ConsiderDereferencing {
381390
}
382391

383392
#[derive(Diagnostic)]
384-
#[diag(const_eval_operator_non_const, code = E0015)]
393+
#[diag(const_eval_non_const_operator, code = E0015)]
385394
pub struct NonConstOperator {
386395
#[primary_span]
387396
pub span: Span,
388397
pub kind: ConstContext,
389398
#[subdiagnostic]
390399
pub sugg: Option<ConsiderDereferencing>,
400+
pub non: &'static str,
391401
}
392402

393403
#[derive(Diagnostic)]
394-
#[diag(const_eval_deref_coercion_non_const, code = E0015)]
404+
#[diag(const_eval_non_const_deref_coercion, code = E0015)]
395405
#[note]
396406
pub struct NonConstDerefCoercion<'tcx> {
397407
#[primary_span]
@@ -401,6 +411,7 @@ pub struct NonConstDerefCoercion<'tcx> {
401411
pub target_ty: Ty<'tcx>,
402412
#[note(const_eval_target_note)]
403413
pub deref_target: Option<Span>,
414+
pub non: &'static str,
404415
}
405416

406417
#[derive(Diagnostic)]

0 commit comments

Comments
 (0)