Skip to content

Commit 7f99507

Browse files
committed
Auto merge of #149375 - oli-obk:const_typeck, r=fee1-dead
Perform many const checks in typeck Some smaller diagnostic changes, the biggest ones avoided by #148641 We should be able to move various checks in mir const checking to using `span_bug!` instead of reporting an error, just like mir typeck does as a sanity check. I would like to start doing so separately though, as this PR is a big enough (in what effects it causes, pun intended). r? @fee1-dead
2 parents b6bed6f + 3127e92 commit 7f99507

37 files changed

+287
-161
lines changed

‎compiler/rustc_hir_typeck/src/callee.rs‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -905,14 +905,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
905905
callee_did: DefId,
906906
callee_args: GenericArgsRef<'tcx>,
907907
) {
908-
// FIXME(const_trait_impl): We should be enforcing these effects unconditionally.
909-
// This can be done as soon as we convert the standard library back to
910-
// using const traits, since if we were to enforce these conditions now,
911-
// we'd fail on basically every builtin trait call (i.e. `1 + 2`).
912-
if !self.tcx.features().const_trait_impl() {
913-
return;
914-
}
915-
916908
// If we have `rustc_do_not_const_check`, do not check `[const]` bounds.
917909
if self.has_rustc_attrs
918910
&& find_attr!(self.tcx.get_all_attrs(self.body_id), AttributeKind::RustcDoNotConstCheck)

‎tests/crashes/137187.rs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
//@ known-bug: #137187
2-
use std::ops::Add;
2+
#![feature(const_trait_impl, const_ops)]
33

4+
use std::ops::Add;
45
const trait A where
5-
*const Self: Add,
6+
*const Self: const Add,
67
{
78
fn b(c: *const Self) -> <*const Self as Add>::Output {
89
c + c
910
}
1011
}
12+
13+
fn main() {}

‎tests/ui/coercion/coerce-loop-issue-122561.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ fn for_single_line() -> bool { for i in 0.. { return false; } }
4242
// that it's readable
4343
fn for_in_arg(a: &[(); for x in 0..2 {}]) -> bool {
4444
//~^ ERROR mismatched types
45+
//~| ERROR `std::ops::Range<{integer}>: const Iterator` is not satisfied
46+
//~| ERROR `std::ops::Range<{integer}>: const Iterator` is not satisfied
4547
true
4648
}
4749

@@ -86,13 +88,17 @@ fn loop_() -> bool {
8688
const C: i32 = {
8789
for i in 0.. {
8890
//~^ ERROR mismatched types
91+
//~| ERROR `std::ops::RangeFrom<{integer}>: const Iterator` is not satisfied
92+
//~| ERROR `std::ops::RangeFrom<{integer}>: const Iterator` is not satisfied
8993
}
9094
};
9195

9296
fn main() {
9397
let _ = [10; {
9498
for i in 0..5 {
9599
//~^ ERROR mismatched types
100+
//~| ERROR `std::ops::Range<{integer}>: const Iterator` is not satisfied
101+
//~| ERROR `std::ops::Range<{integer}>: const Iterator` is not satisfied
96102
}
97103
}];
98104

@@ -105,4 +111,6 @@ fn main() {
105111

106112
let _ = |a: &[(); for x in 0..2 {}]| {};
107113
//~^ ERROR mismatched types
114+
//~| ERROR `std::ops::Range<{integer}>: const Iterator` is not satisfied
115+
//~| ERROR `std::ops::Range<{integer}>: const Iterator` is not satisfied
108116
}

‎tests/ui/coercion/coerce-loop-issue-122561.stderr‎

Lines changed: 91 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,36 @@
11
warning: denote infinite loops with `loop { ... }`
2-
--> $DIR/coerce-loop-issue-122561.rs:49:5
2+
--> $DIR/coerce-loop-issue-122561.rs:51:5
33
|
44
LL | while true {
55
| ^^^^^^^^^^ help: use `loop`
66
|
77
= note: `#[warn(while_true)]` on by default
88

99
warning: denote infinite loops with `loop { ... }`
10-
--> $DIR/coerce-loop-issue-122561.rs:73:5
10+
--> $DIR/coerce-loop-issue-122561.rs:75:5
1111
|
1212
LL | while true {
1313
| ^^^^^^^^^^ help: use `loop`
1414

15+
error[E0277]: the trait bound `std::ops::Range<{integer}>: const Iterator` is not satisfied
16+
--> $DIR/coerce-loop-issue-122561.rs:43:33
17+
|
18+
LL | fn for_in_arg(a: &[(); for x in 0..2 {}]) -> bool {
19+
| ^^^^ required by a bound introduced by this call
20+
|
21+
note: trait `Iterator` is implemented but not `const`
22+
--> $SRC_DIR/core/src/iter/range.rs:LL:COL
23+
= note: required for `std::ops::Range<{integer}>` to implement `const IntoIterator`
24+
25+
error[E0277]: the trait bound `std::ops::Range<{integer}>: const Iterator` is not satisfied
26+
--> $DIR/coerce-loop-issue-122561.rs:43:33
27+
|
28+
LL | fn for_in_arg(a: &[(); for x in 0..2 {}]) -> bool {
29+
| ^^^^
30+
|
31+
note: trait `Iterator` is implemented but not `const`
32+
--> $SRC_DIR/core/src/iter/range.rs:LL:COL
33+
1534
error[E0308]: mismatched types
1635
--> $DIR/coerce-loop-issue-122561.rs:43:24
1736
|
@@ -112,7 +131,7 @@ LL | fn for_single_line() -> bool { for i in 0.. { return false; } /* `bool` val
112131
| ++++++++++++++++++
113132

114133
error[E0308]: mismatched types
115-
--> $DIR/coerce-loop-issue-122561.rs:49:5
134+
--> $DIR/coerce-loop-issue-122561.rs:51:5
116135
|
117136
LL | fn while_inifinite() -> bool {
118137
| ---- expected `bool` because of return type
@@ -131,7 +150,7 @@ LL + /* `bool` value */
131150
|
132151

133152
error[E0308]: mismatched types
134-
--> $DIR/coerce-loop-issue-122561.rs:58:5
153+
--> $DIR/coerce-loop-issue-122561.rs:60:5
135154
|
136155
LL | fn while_finite() -> bool {
137156
| ---- expected `bool` because of return type
@@ -151,7 +170,7 @@ LL + /* `bool` value */
151170
|
152171

153172
error[E0308]: mismatched types
154-
--> $DIR/coerce-loop-issue-122561.rs:66:5
173+
--> $DIR/coerce-loop-issue-122561.rs:68:5
155174
|
156175
LL | fn while_zero_times() -> bool {
157176
| ---- expected `bool` because of return type
@@ -169,7 +188,7 @@ LL + /* `bool` value */
169188
|
170189

171190
error[E0308]: mismatched types
172-
--> $DIR/coerce-loop-issue-122561.rs:73:5
191+
--> $DIR/coerce-loop-issue-122561.rs:75:5
173192
|
174193
LL | fn while_never_type() -> ! {
175194
| - expected `!` because of return type
@@ -187,11 +206,30 @@ LL ~ }
187206
LL + /* `loop {}` or `panic!("...")` */
188207
|
189208

209+
error[E0277]: the trait bound `std::ops::RangeFrom<{integer}>: const Iterator` is not satisfied
210+
--> $DIR/coerce-loop-issue-122561.rs:89:14
211+
|
212+
LL | for i in 0.. {
213+
| ^^^ required by a bound introduced by this call
214+
|
215+
note: trait `Iterator` is implemented but not `const`
216+
--> $SRC_DIR/core/src/iter/range.rs:LL:COL
217+
= note: required for `std::ops::RangeFrom<{integer}>` to implement `const IntoIterator`
218+
219+
error[E0277]: the trait bound `std::ops::RangeFrom<{integer}>: const Iterator` is not satisfied
220+
--> $DIR/coerce-loop-issue-122561.rs:89:14
221+
|
222+
LL | for i in 0.. {
223+
| ^^^
224+
|
225+
note: trait `Iterator` is implemented but not `const`
226+
--> $SRC_DIR/core/src/iter/range.rs:LL:COL
227+
190228
error[E0308]: mismatched types
191-
--> $DIR/coerce-loop-issue-122561.rs:87:5
229+
--> $DIR/coerce-loop-issue-122561.rs:89:5
192230
|
193231
LL | / for i in 0.. {
194-
LL | |
232+
... |
195233
LL | | }
196234
| |_____^ expected `i32`, found `()`
197235
|
@@ -202,11 +240,30 @@ LL ~ }
202240
LL + /* `i32` value */
203241
|
204242

243+
error[E0277]: the trait bound `std::ops::Range<{integer}>: const Iterator` is not satisfied
244+
--> $DIR/coerce-loop-issue-122561.rs:98:18
245+
|
246+
LL | for i in 0..5 {
247+
| ^^^^ required by a bound introduced by this call
248+
|
249+
note: trait `Iterator` is implemented but not `const`
250+
--> $SRC_DIR/core/src/iter/range.rs:LL:COL
251+
= note: required for `std::ops::Range<{integer}>` to implement `const IntoIterator`
252+
253+
error[E0277]: the trait bound `std::ops::Range<{integer}>: const Iterator` is not satisfied
254+
--> $DIR/coerce-loop-issue-122561.rs:98:18
255+
|
256+
LL | for i in 0..5 {
257+
| ^^^^
258+
|
259+
note: trait `Iterator` is implemented but not `const`
260+
--> $SRC_DIR/core/src/iter/range.rs:LL:COL
261+
205262
error[E0308]: mismatched types
206-
--> $DIR/coerce-loop-issue-122561.rs:94:9
263+
--> $DIR/coerce-loop-issue-122561.rs:98:9
207264
|
208265
LL | / for i in 0..5 {
209-
LL | |
266+
... |
210267
LL | | }
211268
| |_________^ expected `usize`, found `()`
212269
|
@@ -218,7 +275,7 @@ LL + /* `usize` value */
218275
|
219276

220277
error[E0308]: mismatched types
221-
--> $DIR/coerce-loop-issue-122561.rs:100:9
278+
--> $DIR/coerce-loop-issue-122561.rs:106:9
222279
|
223280
LL | / while false {
224281
LL | |
@@ -232,8 +289,27 @@ LL ~ }
232289
LL + /* `usize` value */
233290
|
234291

292+
error[E0277]: the trait bound `std::ops::Range<{integer}>: const Iterator` is not satisfied
293+
--> $DIR/coerce-loop-issue-122561.rs:112:32
294+
|
295+
LL | let _ = |a: &[(); for x in 0..2 {}]| {};
296+
| ^^^^ required by a bound introduced by this call
297+
|
298+
note: trait `Iterator` is implemented but not `const`
299+
--> $SRC_DIR/core/src/iter/range.rs:LL:COL
300+
= note: required for `std::ops::Range<{integer}>` to implement `const IntoIterator`
301+
302+
error[E0277]: the trait bound `std::ops::Range<{integer}>: const Iterator` is not satisfied
303+
--> $DIR/coerce-loop-issue-122561.rs:112:32
304+
|
305+
LL | let _ = |a: &[(); for x in 0..2 {}]| {};
306+
| ^^^^
307+
|
308+
note: trait `Iterator` is implemented but not `const`
309+
--> $SRC_DIR/core/src/iter/range.rs:LL:COL
310+
235311
error[E0308]: mismatched types
236-
--> $DIR/coerce-loop-issue-122561.rs:106:23
312+
--> $DIR/coerce-loop-issue-122561.rs:112:23
237313
|
238314
LL | let _ = |a: &[(); for x in 0..2 {}]| {};
239315
| ^^^^^^^^^^^^^^^^ expected `usize`, found `()`
@@ -244,6 +320,7 @@ help: consider returning a value here
244320
LL | let _ = |a: &[(); for x in 0..2 {} /* `usize` value */]| {};
245321
| +++++++++++++++++++
246322

247-
error: aborting due to 14 previous errors; 2 warnings emitted
323+
error: aborting due to 22 previous errors; 2 warnings emitted
248324

249-
For more information about this error, try `rustc --explain E0308`.
325+
Some errors have detailed explanations: E0277, E0308.
326+
For more information about an error, try `rustc --explain E0277`.
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
1-
error: pointers cannot be reliably compared during const eval
1+
error[E0277]: pointers cannot be reliably compared during const eval
22
--> $DIR/const_raw_ptr_ops.rs:7:26
33
|
44
LL | const X: bool = unsafe { &1 as *const i32 == &2 as *const i32 };
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7+
note: trait `PartialEq` is implemented but not `const`
8+
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
79
= note: see issue #53020 <https://github.com/rust-lang/rust/issues/53020> for more information
810

9-
error: pointers cannot be reliably compared during const eval
11+
error[E0277]: pointers cannot be reliably compared during const eval
1012
--> $DIR/const_raw_ptr_ops.rs:9:27
1113
|
1214
LL | const X2: bool = unsafe { 42 as *const i32 == 43 as *const i32 };
1315
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1416
|
17+
note: trait `PartialEq` is implemented but not `const`
18+
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
1519
= note: see issue #53020 <https://github.com/rust-lang/rust/issues/53020> for more information
1620

1721
error: aborting due to 2 previous errors
1822

23+
For more information about this error, try `rustc --explain E0277`.

‎tests/ui/consts/const-fn-error.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ const X : usize = 2;
33
const fn f(x: usize) -> usize {
44
let mut sum = 0;
55
for i in 0..x {
6-
//~^ ERROR cannot use `for`
7-
//~| ERROR cannot use `for`
6+
//~^ ERROR `std::ops::Range<usize>: [const] Iterator` is not satisfied
7+
//~| ERROR `std::ops::Range<usize>: [const] Iterator` is not satisfied
88
sum += i;
99
}
1010
sum
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
error[E0015]: cannot use `for` loop on `std::ops::Range<usize>` in constant functions
1+
error[E0277]: the trait bound `std::ops::Range<usize>: [const] Iterator` is not satisfied
22
--> $DIR/const-fn-error.rs:5:14
33
|
44
LL | for i in 0..x {
5-
| ^^^^
5+
| ^^^^ required by a bound introduced by this call
66
|
7-
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
7+
note: trait `Iterator` is implemented but not `const`
8+
--> $SRC_DIR/core/src/iter/range.rs:LL:COL
9+
= note: required for `std::ops::Range<usize>` to implement `[const] IntoIterator`
810

9-
error[E0015]: cannot use `for` loop on `std::ops::Range<usize>` in constant functions
11+
error[E0277]: the trait bound `std::ops::Range<usize>: [const] Iterator` is not satisfied
1012
--> $DIR/const-fn-error.rs:5:14
1113
|
1214
LL | for i in 0..x {
1315
| ^^^^
1416
|
15-
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
16-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
17+
note: trait `Iterator` is implemented but not `const`
18+
--> $SRC_DIR/core/src/iter/range.rs:LL:COL
1719

1820
error: aborting due to 2 previous errors
1921

20-
For more information about this error, try `rustc --explain E0015`.
22+
For more information about this error, try `rustc --explain E0277`.

‎tests/ui/consts/const-for-feature-gate.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
const _: () = {
44
for _ in 0..5 {}
5-
//~^ ERROR cannot use `for`
6-
//~| ERROR cannot use `for`
5+
//~^ ERROR `std::ops::Range<{integer}>: const Iterator` is not satisfied
6+
//~| ERROR `std::ops::Range<{integer}>: const Iterator` is not satisfied
77
};
88

99
fn main() {}
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
error[E0015]: cannot use `for` loop on `std::ops::Range<i32>` in constants
1+
error[E0277]: the trait bound `std::ops::Range<{integer}>: const Iterator` is not satisfied
22
--> $DIR/const-for-feature-gate.rs:4:14
33
|
44
LL | for _ in 0..5 {}
5-
| ^^^^
5+
| ^^^^ required by a bound introduced by this call
66
|
7-
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
7+
note: trait `Iterator` is implemented but not `const`
8+
--> $SRC_DIR/core/src/iter/range.rs:LL:COL
9+
= note: required for `std::ops::Range<{integer}>` to implement `const IntoIterator`
810

9-
error[E0015]: cannot use `for` loop on `std::ops::Range<i32>` in constants
11+
error[E0277]: the trait bound `std::ops::Range<{integer}>: const Iterator` is not satisfied
1012
--> $DIR/const-for-feature-gate.rs:4:14
1113
|
1214
LL | for _ in 0..5 {}
1315
| ^^^^
1416
|
15-
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
16-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
17+
note: trait `Iterator` is implemented but not `const`
18+
--> $SRC_DIR/core/src/iter/range.rs:LL:COL
1719

1820
error: aborting due to 2 previous errors
1921

20-
For more information about this error, try `rustc --explain E0015`.
22+
For more information about this error, try `rustc --explain E0277`.

‎tests/ui/consts/const-for.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
const _: () = {
44
for _ in 0..5 {}
5-
//~^ ERROR cannot use `for`
6-
//~| ERROR cannot use `for`
5+
//~^ ERROR `std::ops::Range<{integer}>: const Iterator` is not satisfied
6+
//~| ERROR `std::ops::Range<{integer}>: const Iterator` is not satisfied
77
};
88

99
fn main() {}

0 commit comments

Comments
 (0)