Skip to content

Commit c2d7915

Browse files
committed
review comments
1 parent fd07c3f commit c2d7915

File tree

47 files changed

+104
-192
lines changed

Some content is hidden

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

47 files changed

+104
-192
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/run-make/const-trait-stable-toolchain/const-super-trait-nightly-disabled.stderr

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ LL | #[const_trait] trait Bar: ~const Foo {}
5555
error[E0015]: cannot call non-const method `<T as Foo>::a` in constant functions
5656
--> const-super-trait.rs:10:7
5757
|
58-
LL | const fn foo<T: ~const Bar>(x: &T) {
59-
| ---------------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants
6058
LL | x.a();
6159
| ^^^
6260
|
@@ -68,6 +66,7 @@ LL | trait Foo {
6866
LL | fn a(&self);
6967
| ------------ this method is not const
7068
= help: add `#![feature(const_trait_impl)]` to the crate attributes to enable `#[const_trait]`
69+
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
7170
help: consider making trait `Foo` const
7271
|
7372
LL + #[const_trait]

tests/run-make/const-trait-stable-toolchain/const-super-trait-nightly-enabled.stderr

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ LL | #[const_trait] trait Bar: ~const Foo {}
3535
error[E0015]: cannot call non-const method `<T as Foo>::a` in constant functions
3636
--> const-super-trait.rs:10:7
3737
|
38-
LL | const fn foo<T: ~const Bar>(x: &T) {
39-
| ---------------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants
4038
LL | x.a();
4139
| ^^^
4240
|
@@ -47,6 +45,7 @@ LL | trait Foo {
4745
| ^^^^^^^^^ this trait is not const
4846
LL | fn a(&self);
4947
| ------------ this method is not const
48+
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
5049
help: consider making trait `Foo` const
5150
|
5251
LL + #[const_trait]

tests/run-make/const-trait-stable-toolchain/const-super-trait-stable-disabled.stderr

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ note: `Bar` can't be used with `[const]` because it isn't `const`
5353
error[E0015]: cannot call non-const method `<T as Foo>::a` in constant functions
5454
--> const-super-trait.rs:10:7
5555
|
56-
9 | const fn foo<T: ~const Bar>(x: &T) {
57-
| ---------------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants
5856
10 | x.a();
5957
| ^^^
6058
|
@@ -66,6 +64,7 @@ note: method `a` is not const because trait `Foo` is not const
6664
4 | fn a(&self);
6765
| ------------ this method is not const
6866
= help: const traits are not yet supported on stable Rust
67+
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
6968

7069
error: aborting due to 6 previous errors
7170

tests/run-make/const-trait-stable-toolchain/const-super-trait-stable-enabled.stderr

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ note: `Bar` can't be used with `[const]` because it isn't `const`
4343
error[E0015]: cannot call non-const method `<T as Foo>::a` in constant functions
4444
--> const-super-trait.rs:10:7
4545
|
46-
9 | const fn foo<T: ~const Bar>(x: &T) {
47-
| ---------------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants
4846
10 | x.a();
4947
| ^^^
5048
|
@@ -56,6 +54,7 @@ note: method `a` is not const because trait `Foo` is not const
5654
4 | fn a(&self);
5755
| ------------ this method is not const
5856
= help: const traits are not yet supported on stable Rust
57+
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
5958

6059
error: aborting due to 5 previous errors
6160

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

0 commit comments

Comments
 (0)