Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 437561f

Browse files
authoredJan 10, 2025··
Unrolled build for rust-lang#134732
Rollup merge of rust-lang#134732 - compiler-errors:unify-conditional-const-error-reporting, r=RalfJung Unify conditional-const error reporting with non-const error reporting This PR unifies the error reporting between `ConditionallyConstCall` and `FnCallNonConst` so that the former will refer to syntactical sugar like operators by their sugared name, rather than calling all operators "methods". We achieve this by making the "non-const" part of the error message generic over the "non" part so we can plug in "conditionally" instead. This should ensure that as we constify traits in the standard library, we don't regress error messages for things like `==`. r? fmease or reassign
2 parents b44e14f + 924000d commit 437561f

12 files changed

+325
-248
lines changed
 

‎compiler/rustc_const_eval/messages.ftl

+31-28
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,11 +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-
29-
const_eval_conditionally_const_call =
30-
cannot call conditionally-const {$def_descr} `{$def_path_str}` in {const_eval_const_context}s
3124
3225
const_eval_consider_dereferencing =
3326
consider dereferencing here
@@ -62,10 +55,6 @@ const_eval_dealloc_incorrect_layout =
6255
const_eval_dealloc_kind_mismatch =
6356
deallocating {$alloc}, which is {$alloc_kind} memory, using {$kind} deallocation operation
6457
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
6958
const_eval_deref_function_pointer =
7059
accessing {$allocation} which contains a function
7160
const_eval_deref_vtable_pointer =
@@ -109,9 +98,6 @@ const_eval_extern_type_field = `extern type` field does not have a known offset
10998
11099
const_eval_fn_ptr_call =
111100
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-
115101
const_eval_frame_note = {$times ->
116102
[0] {const_eval_frame_note_inner}
117103
*[other] [... {$times} additional calls {const_eval_frame_note_inner} ...]
@@ -216,9 +202,6 @@ const_eval_long_running =
216202
.label = the const evaluator is currently interpreting this expression
217203
.help = the constant being evaluated
218204
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-
222205
const_eval_max_num_nodes_in_const = maximum number of nodes exceeded in constant {$global_const_id}
223206
224207
const_eval_memory_access_test = memory access failed
@@ -249,18 +232,47 @@ const_eval_mutable_ref_escaping =
249232
If you really want global mutable state, try using an interior mutable `static` or a `static mut`.
250233
251234
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
235+
236+
const_eval_non_const_await =
237+
cannot convert `{$ty}` into a future in {const_eval_const_context}s
238+
239+
const_eval_non_const_closure =
240+
cannot call {$non_or_conditionally}-const closure in {const_eval_const_context}s
241+
242+
const_eval_non_const_deref_coercion =
243+
cannot perform {$non_or_conditionally}-const deref coercion on `{$ty}` in {const_eval_const_context}s
244+
.note = attempting to deref into `{$target_ty}`
245+
.target_note = deref defined here
246+
252247
const_eval_non_const_fmt_macro_call =
253-
cannot call non-const formatting macro in {const_eval_const_context}s
248+
cannot call {$non_or_conditionally}-const formatting macro in {const_eval_const_context}s
254249
255250
const_eval_non_const_fn_call =
256-
cannot call non-const {$def_descr} `{$def_path_str}` in {const_eval_const_context}s
251+
cannot call {$non_or_conditionally}-const {$def_descr} `{$def_path_str}` in {const_eval_const_context}s
252+
253+
const_eval_non_const_for_loop_into_iter =
254+
cannot use `for` loop on `{$ty}` in {const_eval_const_context}s
257255
258256
const_eval_non_const_impl =
259257
impl defined here, but it is not `const`
260258
261259
const_eval_non_const_intrinsic =
262260
cannot call non-const intrinsic `{$name}` in {const_eval_const_context}s
263261
262+
const_eval_non_const_match_eq = cannot match on `{$ty}` in {const_eval_const_context}s
263+
.note = `{$ty}` cannot be compared in compile-time, and therefore cannot be used in `match`es
264+
265+
const_eval_non_const_operator =
266+
cannot call {$non_or_conditionally}-const operator in {const_eval_const_context}s
267+
268+
const_eval_non_const_question_branch =
269+
`?` is not allowed on `{$ty}` in {const_eval_const_context}s
270+
const_eval_non_const_question_from_residual =
271+
`?` is not allowed on `{$ty}` in {const_eval_const_context}s
272+
273+
const_eval_non_const_try_block_from_output =
274+
`try` block cannot convert `{$ty}` to the result in {const_eval_const_context}s
275+
264276
const_eval_not_enough_caller_args =
265277
calling a function with fewer arguments than it requires
266278
@@ -281,8 +293,6 @@ const_eval_offset_from_unsigned_overflow =
281293
*[false] offset
282294
} than second: {$a_offset} < {$b_offset}
283295
284-
const_eval_operator_non_const =
285-
cannot call non-const operator in {const_eval_const_context}s
286296
const_eval_overflow_arith =
287297
arithmetic overflow in `{$intrinsic}`
288298
const_eval_overflow_shift =
@@ -325,11 +335,6 @@ const_eval_ptr_as_bytes_1 =
325335
const_eval_ptr_as_bytes_2 =
326336
the absolute address of a pointer is not known at compile-time, so such operations are not supported
327337
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-
333338
const_eval_range = in the range {$lo}..={$hi}
334339
const_eval_range_lower = greater or equal to {$lo}
335340
const_eval_range_singular = equal to {$lo}
@@ -379,8 +384,6 @@ const_eval_too_generic =
379384
const_eval_too_many_caller_args =
380385
calling a function with more arguments than it expected
381386
382-
const_eval_try_block_from_output_non_const =
383-
`try` block cannot convert `{$ty}` to the result in {const_eval_const_context}s
384387
const_eval_unallowed_fn_pointer_call = function pointer calls are not allowed in {const_eval_const_context}s
385388
386389
const_eval_unallowed_heap_allocations =

‎compiler/rustc_const_eval/src/check_consts/check.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,12 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
708708

709709
if trait_is_const {
710710
// Trait calls are always conditionally-const.
711-
self.check_op(ops::ConditionallyConstCall { callee, args: fn_args });
711+
self.check_op(ops::ConditionallyConstCall {
712+
callee,
713+
args: fn_args,
714+
span: *fn_span,
715+
call_source,
716+
});
712717
// FIXME(const_trait_impl): do a more fine-grained check whether this
713718
// particular trait can be const-stably called.
714719
} else {
@@ -726,7 +731,12 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
726731

727732
// Even if we know the callee, ensure we can use conditionally-const calls.
728733
if has_const_conditions {
729-
self.check_op(ops::ConditionallyConstCall { callee, args: fn_args });
734+
self.check_op(ops::ConditionallyConstCall {
735+
callee,
736+
args: fn_args,
737+
span: *fn_span,
738+
call_source,
739+
});
730740
}
731741

732742
// At this point, we are calling a function, `callee`, whose `DefId` is known...

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

+236-187
Large diffs are not rendered by default.

‎compiler/rustc_const_eval/src/errors.rs

+20-19
Original file line numberDiff line numberDiff line change
@@ -174,16 +174,7 @@ pub(crate) struct NonConstFmtMacroCall {
174174
#[primary_span]
175175
pub span: Span,
176176
pub kind: ConstContext,
177-
}
178-
179-
#[derive(Diagnostic)]
180-
#[diag(const_eval_conditionally_const_call)]
181-
pub(crate) struct ConditionallyConstCall {
182-
#[primary_span]
183-
pub span: Span,
184-
pub def_path_str: String,
185-
pub def_descr: &'static str,
186-
pub kind: ConstContext,
177+
pub non_or_conditionally: &'static str,
187178
}
188179

189180
#[derive(Diagnostic)]
@@ -194,6 +185,7 @@ pub(crate) struct NonConstFnCall {
194185
pub def_path_str: String,
195186
pub def_descr: &'static str,
196187
pub kind: ConstContext,
188+
pub non_or_conditionally: &'static str,
197189
}
198190

199191
#[derive(Diagnostic)]
@@ -293,68 +285,75 @@ pub struct RawBytesNote {
293285
// FIXME(fee1-dead) do not use stringly typed `ConstContext`
294286

295287
#[derive(Diagnostic)]
296-
#[diag(const_eval_match_eq_non_const, code = E0015)]
288+
#[diag(const_eval_non_const_match_eq, code = E0015)]
297289
#[note]
298290
pub struct NonConstMatchEq<'tcx> {
299291
#[primary_span]
300292
pub span: Span,
301293
pub ty: Ty<'tcx>,
302294
pub kind: ConstContext,
295+
pub non_or_conditionally: &'static str,
303296
}
304297

305298
#[derive(Diagnostic)]
306-
#[diag(const_eval_for_loop_into_iter_non_const, code = E0015)]
299+
#[diag(const_eval_non_const_for_loop_into_iter, code = E0015)]
307300
pub struct NonConstForLoopIntoIter<'tcx> {
308301
#[primary_span]
309302
pub span: Span,
310303
pub ty: Ty<'tcx>,
311304
pub kind: ConstContext,
305+
pub non_or_conditionally: &'static str,
312306
}
313307

314308
#[derive(Diagnostic)]
315-
#[diag(const_eval_question_branch_non_const, code = E0015)]
309+
#[diag(const_eval_non_const_question_branch, code = E0015)]
316310
pub struct NonConstQuestionBranch<'tcx> {
317311
#[primary_span]
318312
pub span: Span,
319313
pub ty: Ty<'tcx>,
320314
pub kind: ConstContext,
315+
pub non_or_conditionally: &'static str,
321316
}
322317

323318
#[derive(Diagnostic)]
324-
#[diag(const_eval_question_from_residual_non_const, code = E0015)]
319+
#[diag(const_eval_non_const_question_from_residual, code = E0015)]
325320
pub struct NonConstQuestionFromResidual<'tcx> {
326321
#[primary_span]
327322
pub span: Span,
328323
pub ty: Ty<'tcx>,
329324
pub kind: ConstContext,
325+
pub non_or_conditionally: &'static str,
330326
}
331327

332328
#[derive(Diagnostic)]
333-
#[diag(const_eval_try_block_from_output_non_const, code = E0015)]
329+
#[diag(const_eval_non_const_try_block_from_output, code = E0015)]
334330
pub struct NonConstTryBlockFromOutput<'tcx> {
335331
#[primary_span]
336332
pub span: Span,
337333
pub ty: Ty<'tcx>,
338334
pub kind: ConstContext,
335+
pub non_or_conditionally: &'static str,
339336
}
340337

341338
#[derive(Diagnostic)]
342-
#[diag(const_eval_await_non_const, code = E0015)]
339+
#[diag(const_eval_non_const_await, code = E0015)]
343340
pub struct NonConstAwait<'tcx> {
344341
#[primary_span]
345342
pub span: Span,
346343
pub ty: Ty<'tcx>,
347344
pub kind: ConstContext,
345+
pub non_or_conditionally: &'static str,
348346
}
349347

350348
#[derive(Diagnostic)]
351-
#[diag(const_eval_closure_non_const, code = E0015)]
349+
#[diag(const_eval_non_const_closure, code = E0015)]
352350
pub struct NonConstClosure {
353351
#[primary_span]
354352
pub span: Span,
355353
pub kind: ConstContext,
356354
#[subdiagnostic]
357355
pub note: Option<NonConstClosureNote>,
356+
pub non_or_conditionally: &'static str,
358357
}
359358

360359
#[derive(Subdiagnostic)]
@@ -381,17 +380,18 @@ pub struct ConsiderDereferencing {
381380
}
382381

383382
#[derive(Diagnostic)]
384-
#[diag(const_eval_operator_non_const, code = E0015)]
383+
#[diag(const_eval_non_const_operator, code = E0015)]
385384
pub struct NonConstOperator {
386385
#[primary_span]
387386
pub span: Span,
388387
pub kind: ConstContext,
389388
#[subdiagnostic]
390389
pub sugg: Option<ConsiderDereferencing>,
390+
pub non_or_conditionally: &'static str,
391391
}
392392

393393
#[derive(Diagnostic)]
394-
#[diag(const_eval_deref_coercion_non_const, code = E0015)]
394+
#[diag(const_eval_non_const_deref_coercion, code = E0015)]
395395
#[note]
396396
pub struct NonConstDerefCoercion<'tcx> {
397397
#[primary_span]
@@ -401,6 +401,7 @@ pub struct NonConstDerefCoercion<'tcx> {
401401
pub target_ty: Ty<'tcx>,
402402
#[note(const_eval_target_note)]
403403
pub deref_target: Option<Span>,
404+
pub non_or_conditionally: &'static str,
404405
}
405406

406407
#[derive(Diagnostic)]

‎tests/ui/issues/issue-25901.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ struct A;
22
struct B;
33

44
static S: &'static B = &A;
5-
//~^ ERROR cannot call conditionally-const method
5+
//~^ ERROR cannot perform conditionally-const deref coercion
66

77
use std::ops::Deref;
88

‎tests/ui/issues/issue-25901.stderr

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1-
error[E0658]: cannot call conditionally-const method `<A as Deref>::deref` in statics
1+
error[E0658]: cannot perform conditionally-const deref coercion on `A` in statics
22
--> $DIR/issue-25901.rs:4:24
33
|
44
LL | static S: &'static B = &A;
55
| ^^
66
|
7+
= note: attempting to deref into `B`
8+
note: deref defined here
9+
--> $DIR/issue-25901.rs:10:5
10+
|
11+
LL | type Target = B;
12+
| ^^^^^^^^^^^
13+
= note: calls in statics are limited to constant functions, tuple structs and tuple variants
714
= note: see issue #67792 <https://github.com/rust-lang/rust/issues/67792> for more information
815
= help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
916
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

‎tests/ui/self/arbitrary-self-from-method-substs-ice.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ error[E0658]: cannot call conditionally-const method `<R as Deref>::deref` in co
44
LL | self.0
55
| ^^^^^^
66
|
7+
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
78
= note: see issue #67792 <https://github.com/rust-lang/rust/issues/67792> for more information
89
= help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
910
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

‎tests/ui/traits/const-traits/cross-crate.stock.stderr

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
error[E0658]: cannot call conditionally-const method `<cross_crate::Const as cross_crate::MyTrait>::func` in constant functions
2-
--> $DIR/cross-crate.rs:22:5
2+
--> $DIR/cross-crate.rs:22:11
33
|
44
LL | Const.func();
5-
| ^^^^^^^^^^^^
5+
| ^^^^^^
66
|
7+
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
78
= note: see issue #67792 <https://github.com/rust-lang/rust/issues/67792> for more information
89
= help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
910
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

‎tests/ui/traits/const-traits/cross-crate.stocknc.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
error[E0658]: cannot call conditionally-const method `<cross_crate::NonConst as cross_crate::MyTrait>::func` in constant functions
2-
--> $DIR/cross-crate.rs:19:5
2+
--> $DIR/cross-crate.rs:19:14
33
|
44
LL | NonConst.func();
5-
| ^^^^^^^^^^^^^^^
5+
| ^^^^^^
66
|
7+
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
78
= note: see issue #67792 <https://github.com/rust-lang/rust/issues/67792> for more information
89
= help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
910
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
1011

1112
error[E0658]: cannot call conditionally-const method `<cross_crate::Const as cross_crate::MyTrait>::func` in constant functions
12-
--> $DIR/cross-crate.rs:22:5
13+
--> $DIR/cross-crate.rs:22:11
1314
|
1415
LL | Const.func();
15-
| ^^^^^^^^^^^^
16+
| ^^^^^^
1617
|
18+
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
1719
= note: see issue #67792 <https://github.com/rust-lang/rust/issues/67792> for more information
1820
= help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
1921
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

‎tests/ui/traits/const-traits/staged-api-user-crate.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ error[E0658]: cannot call conditionally-const associated function `<staged_api::
44
LL | Unstable::func();
55
| ^^^^^^^^^^^^^^^^
66
|
7+
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
78
= note: see issue #67792 <https://github.com/rust-lang/rust/issues/67792> for more information
89
= help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
910
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

‎tests/ui/traits/const-traits/super-traits-fail-3.nyn.stderr

+3-2
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@ LL | #[cfg_attr(any(yyy, yny, nyy, nyn), const_trait)]
3939
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
4040

4141
error[E0658]: cannot call conditionally-const method `<T as Foo>::a` in constant functions
42-
--> $DIR/super-traits-fail-3.rs:36:5
42+
--> $DIR/super-traits-fail-3.rs:36:7
4343
|
4444
LL | x.a();
45-
| ^^^^^
45+
| ^^^
4646
|
47+
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
4748
= note: see issue #67792 <https://github.com/rust-lang/rust/issues/67792> for more information
4849
= help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
4950
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

‎tests/ui/traits/const-traits/super-traits-fail-3.nyy.stderr

+3-2
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@ LL | #[cfg_attr(any(yyy, yny, nyy, nyn), const_trait)]
3939
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
4040

4141
error[E0658]: cannot call conditionally-const method `<T as Foo>::a` in constant functions
42-
--> $DIR/super-traits-fail-3.rs:36:5
42+
--> $DIR/super-traits-fail-3.rs:36:7
4343
|
4444
LL | x.a();
45-
| ^^^^^
45+
| ^^^
4646
|
47+
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
4748
= note: see issue #67792 <https://github.com/rust-lang/rust/issues/67792> for more information
4849
= help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
4950
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

0 commit comments

Comments
 (0)
Please sign in to comment.