Skip to content

Commit 41b35cb

Browse files
committed
review comments
1 parent fd07c3f commit 41b35cb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+100
-184
lines changed

compiler/rustc_const_eval/src/check_consts/ops.rs

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@ use hir::{ConstContext, LangItem};
44
use rustc_errors::codes::*;
55
use rustc_errors::{Applicability, Diag, MultiSpan};
66
use rustc_hir as hir;
7-
use rustc_hir::def::DefKind;
87
use rustc_hir::def_id::DefId;
98
use rustc_infer::infer::TyCtxtInferExt;
109
use rustc_infer::traits::{ImplSource, Obligation, ObligationCause};
1110
use rustc_middle::mir::CallSource;
1211
use rustc_middle::span_bug;
1312
use rustc_middle::ty::print::{PrintTraitRefExt as _, with_no_trimmed_paths};
1413
use rustc_middle::ty::{
15-
self, Closure, FnDef, FnPtr, GenericArgKind, GenericArgsRef, Param, TraitRef, Ty,
16-
suggest_constraining_type_param,
14+
self, AssocItemContainer, Closure, FnDef, FnPtr, GenericArgKind, GenericArgsRef, Param,
15+
TraitRef, Ty, suggest_constraining_type_param,
1716
};
1817
use rustc_session::parse::add_feature_diagnostics;
1918
use rustc_span::{BytePos, Pos, Span, Symbol, sym};
@@ -212,7 +211,6 @@ fn build_error_for_const_call<'tcx>(
212211

213212
debug!(?call_kind);
214213

215-
let mut note = true;
216214
let mut err = match call_kind {
217215
CallKind::Normal { desugaring: Some((kind, self_ty)), .. } => {
218216
macro_rules! error {
@@ -366,16 +364,9 @@ fn build_error_for_const_call<'tcx>(
366364
kind: ccx.const_kind(),
367365
non_or_conditionally,
368366
});
369-
let context_span = ccx.tcx.def_span(ccx.def_id());
370-
err.span_label(context_span, format!(
371-
"calls in {}s are limited to constant functions, tuple structs and tuple variants",
372-
ccx.const_kind(),
373-
));
374-
note = false;
375-
let def_kind = ccx.tcx.def_kind(callee);
376-
if let DefKind::AssocTy | DefKind::AssocConst | DefKind::AssocFn = def_kind {
377-
let parent = ccx.tcx.parent(callee);
378-
if let DefKind::Trait = ccx.tcx.def_kind(parent)
367+
if let Some(item) = ccx.tcx.opt_associated_item(callee) {
368+
if let AssocItemContainer::Trait = item.container
369+
&& let parent = item.container_id(ccx.tcx)
379370
&& !ccx.tcx.is_const_trait(parent)
380371
{
381372
let assoc_span = ccx.tcx.def_span(callee);
@@ -410,7 +401,7 @@ fn build_error_for_const_call<'tcx>(
410401
trait_span.shrink_to_lo(),
411402
format!("consider making trait `{trait_name}` const"),
412403
format!("#[const_trait]\n{indentation}"),
413-
Applicability::MachineApplicable,
404+
Applicability::MaybeIncorrect,
414405
);
415406
} else if !ccx.tcx.sess.is_nightly_build() {
416407
err.help("const traits are not yet supported on stable Rust");
@@ -427,12 +418,10 @@ fn build_error_for_const_call<'tcx>(
427418
}
428419
};
429420

430-
if note {
431-
err.note(format!(
432-
"calls in {}s are limited to constant functions, tuple structs and tuple variants",
433-
ccx.const_kind(),
434-
));
435-
}
421+
err.note(format!(
422+
"calls in {}s are limited to constant functions, tuple structs and tuple variants",
423+
ccx.const_kind(),
424+
));
436425

437426
err
438427
}

tests/ui/asm/non-const.stderr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ error[E0015]: cannot call non-const function `non_const_fn` in constants
22
--> $DIR/non-const.rs:10:31
33
|
44
LL | global_asm!("/* {} */", const non_const_fn(0));
5-
| ^^^^^^^^^^^^^^^ calls in constants are limited to constant functions, tuple structs and tuple variants
5+
| ^^^^^^^^^^^^^^^
66
|
77
note: function `non_const_fn` is not const
88
--> $DIR/non-const.rs:8:1
99
|
1010
LL | fn non_const_fn(x: i32) -> i32 { x }
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
1213

1314
error: aborting due to 1 previous error
1415

tests/ui/borrowck/issue-64453.stderr

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ error[E0015]: cannot call non-const function `format` in statics
22
--> $DIR/issue-64453.rs:4:31
33
|
44
LL | static settings_dir: String = format!("");
5-
| --------------------------- ^^^^^^^^^^^
6-
| |
7-
| calls in statics are limited to constant functions, tuple structs and tuple variants
5+
| ^^^^^^^^^^^
86
|
97
note: function `format` is not const
108
--> $SRC_DIR/alloc/src/fmt.rs:LL:COL
9+
= note: calls in statics are limited to constant functions, tuple structs and tuple variants
1110
= note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)`
1211
= note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info)
1312

tests/ui/const-generics/nested-type.full.stderr

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
error[E0015]: cannot call non-const associated function `Foo::{constant#0}::Foo::<17>::value` in constants
22
--> $DIR/nested-type.rs:15:5
33
|
4-
LL | struct Foo<const N: [u8; {
5-
| __________________________-
6-
LL | | struct Foo<const N: usize>;
7-
LL | |
8-
LL | | impl<const N: usize> Foo<N> {
9-
... |
10-
LL | | Foo::<17>::value()
11-
| | ^^^^^^^^^^^^^^^^^^
12-
LL | |
13-
LL | | }]>;
14-
| |_- calls in constants are limited to constant functions, tuple structs and tuple variants
4+
LL | Foo::<17>::value()
5+
| ^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
158

169
error: aborting due to 1 previous error
1710

tests/ui/const-generics/nested-type.min.stderr

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
error[E0015]: cannot call non-const associated function `Foo::{constant#0}::Foo::<17>::value` in constants
22
--> $DIR/nested-type.rs:15:5
33
|
4-
LL | struct Foo<const N: [u8; {
5-
| __________________________-
6-
LL | | struct Foo<const N: usize>;
7-
LL | |
8-
LL | | impl<const N: usize> Foo<N> {
9-
... |
10-
LL | | Foo::<17>::value()
11-
| | ^^^^^^^^^^^^^^^^^^
12-
LL | |
13-
LL | | }]>;
14-
| |_- calls in constants are limited to constant functions, tuple structs and tuple variants
4+
LL | Foo::<17>::value()
5+
| ^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
158

169
error: `[u8; {
1710
struct Foo<const N: usize>;

tests/ui/consts/const-call.stderr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ error[E0015]: cannot call non-const function `f` in constants
22
--> $DIR/const-call.rs:6:17
33
|
44
LL | let _ = [0; f(2)];
5-
| ^^^^ calls in constants are limited to constant functions, tuple structs and tuple variants
5+
| ^^^^
66
|
77
note: function `f` is not const
88
--> $DIR/const-call.rs:1:1
99
|
1010
LL | fn f(x: usize) -> usize {
1111
| ^^^^^^^^^^^^^^^^^^^^^^^
12+
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
1213

1314
error: aborting due to 1 previous error
1415

tests/ui/consts/const-eval/format.stderr

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@ LL | println!("{:?}", 0);
1818
error[E0015]: cannot call non-const function `_print` in constant functions
1919
--> $DIR/format.rs:7:5
2020
|
21-
LL | const fn print() {
22-
| ---------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants
2321
LL | println!("{:?}", 0);
2422
| ^^^^^^^^^^^^^^^^^^^
2523
|
2624
note: function `_print` is not const
2725
--> $SRC_DIR/std/src/io/stdio.rs:LL:COL
26+
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
2827
= note: this error originates in the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
2928

3029
error[E0015]: cannot call non-const formatting macro in constant functions

tests/ui/consts/const-extern-fn/const-extern-fn-call-extern-fn.stderr

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
error[E0015]: cannot call non-const function `regular_in_block` in constant functions
22
--> $DIR/const-extern-fn-call-extern-fn.rs:7:9
33
|
4-
LL | const extern "C" fn bar() {
5-
| ------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants
6-
LL | unsafe {
74
LL | regular_in_block();
85
| ^^^^^^^^^^^^^^^^^^
96
|
@@ -12,13 +9,11 @@ note: function `regular_in_block` is not const
129
|
1310
LL | fn regular_in_block();
1411
| ^^^^^^^^^^^^^^^^^^^^^^
12+
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
1513

1614
error[E0015]: cannot call non-const function `regular` in constant functions
1715
--> $DIR/const-extern-fn-call-extern-fn.rs:16:9
1816
|
19-
LL | const extern "C" fn foo() {
20-
| ------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants
21-
LL | unsafe {
2217
LL | regular();
2318
| ^^^^^^^^^
2419
|
@@ -27,6 +22,7 @@ note: function `regular` is not const
2722
|
2823
LL | extern "C" fn regular() {}
2924
| ^^^^^^^^^^^^^^^^^^^^^^^
25+
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
3026

3127
error: aborting due to 2 previous errors
3228

tests/ui/consts/const-fn-not-safe-for-const.stderr

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
error[E0015]: cannot call non-const function `random` in constant functions
22
--> $DIR/const-fn-not-safe-for-const.rs:14:5
33
|
4-
LL | const fn sub1() -> u32 {
5-
| ---------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants
64
LL | random()
75
| ^^^^^^^^
86
|
@@ -11,6 +9,7 @@ note: function `random` is not const
119
|
1210
LL | fn random() -> u32 {
1311
| ^^^^^^^^^^^^^^^^^^
12+
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
1413

1514
error: aborting due to 1 previous error
1615

tests/ui/consts/control-flow/issue-46843.stderr

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ error[E0015]: cannot call non-const function `non_const` in constants
22
--> $DIR/issue-46843.rs:10:26
33
|
44
LL | pub const Q: i32 = match non_const() {
5-
| ---------------- ^^^^^^^^^^^
6-
| |
7-
| calls in constants are limited to constant functions, tuple structs and tuple variants
5+
| ^^^^^^^^^^^
86
|
97
note: function `non_const` is not const
108
--> $DIR/issue-46843.rs:6:1
119
|
1210
LL | fn non_const() -> Thing {
1311
| ^^^^^^^^^^^^^^^^^^^^^^^
12+
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
1413

1514
error: aborting due to 1 previous error
1615

0 commit comments

Comments
 (0)