Skip to content

Commit 5d0ee56

Browse files
authored
Rollup merge of #133518 - compiler-errors:structurally-resolve-never, r=lcnr
Structurally resolve before checking `!` in HIR typeck Some more missing structural resolves in HIR typeck :> r? lcnr
2 parents a8b690f + 4c0ea55 commit 5d0ee56

15 files changed

+125
-47
lines changed

Diff for: compiler/rustc_hir_typeck/src/expr.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
6868

6969
// While we don't allow *arbitrary* coercions here, we *do* allow
7070
// coercions from ! to `expected`.
71-
if ty.is_never() && self.expr_guaranteed_to_constitute_read_for_never(expr) {
71+
if self.try_structurally_resolve_type(expr.span, ty).is_never()
72+
&& self.expr_guaranteed_to_constitute_read_for_never(expr)
73+
{
7274
if let Some(_) = self.typeck_results.borrow().adjustments().get(expr.hir_id) {
7375
let reported = self.dcx().span_delayed_bug(
7476
expr.span,
@@ -274,7 +276,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
274276
// unless it's a place expression that isn't being read from, in which case
275277
// diverging would be unsound since we may never actually read the `!`.
276278
// e.g. `let _ = *never_ptr;` with `never_ptr: *const !`.
277-
if ty.is_never() && self.expr_guaranteed_to_constitute_read_for_never(expr) {
279+
if self.try_structurally_resolve_type(expr.span, ty).is_never()
280+
&& self.expr_guaranteed_to_constitute_read_for_never(expr)
281+
{
278282
self.diverges.set(self.diverges.get() | Diverges::always(expr.span));
279283
}
280284

Diff for: tests/ui/associated-consts/associated-const-type-parameter-arrays-2.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ impl Foo for Def {
1515
pub fn test<A: Foo, B: Foo>() {
1616
let _array = [4; <A as Foo>::Y];
1717
//~^ ERROR constant expression depends on a generic parameter
18+
//~| ERROR constant expression depends on a generic parameter
1819
}
1920

2021
fn main() {

Diff for: tests/ui/associated-consts/associated-const-type-parameter-arrays-2.stderr

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,13 @@ LL | let _array = [4; <A as Foo>::Y];
66
|
77
= note: this may fail depending on what value the parameter takes
88

9-
error: aborting due to 1 previous error
9+
error: constant expression depends on a generic parameter
10+
--> $DIR/associated-const-type-parameter-arrays-2.rs:16:18
11+
|
12+
LL | let _array = [4; <A as Foo>::Y];
13+
| ^^^^^^^^^^^^^^^^^^
14+
|
15+
= note: this may fail depending on what value the parameter takes
16+
17+
error: aborting due to 2 previous errors
1018

Diff for: tests/ui/const-generics/const-arg-in-const-arg.min.stderr

+35-27
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ LL | let _: [u8; baz::<'b>(&())];
5353
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
5454

5555
error: generic parameters may not be used in const operations
56-
--> $DIR/const-arg-in-const-arg.rs:26:23
56+
--> $DIR/const-arg-in-const-arg.rs:27:23
5757
|
5858
LL | let _ = [0; bar::<N>()];
5959
| ^ cannot perform const operation using `N`
@@ -62,7 +62,7 @@ LL | let _ = [0; bar::<N>()];
6262
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
6363

6464
error: generic parameters may not be used in const operations
65-
--> $DIR/const-arg-in-const-arg.rs:28:23
65+
--> $DIR/const-arg-in-const-arg.rs:29:23
6666
|
6767
LL | let _ = [0; faz::<'a>(&())];
6868
| ^^ cannot perform const operation using `'a`
@@ -71,7 +71,7 @@ LL | let _ = [0; faz::<'a>(&())];
7171
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
7272

7373
error: generic parameters may not be used in const operations
74-
--> $DIR/const-arg-in-const-arg.rs:30:23
74+
--> $DIR/const-arg-in-const-arg.rs:31:23
7575
|
7676
LL | let _ = [0; baz::<'a>(&())];
7777
| ^^ cannot perform const operation using `'a`
@@ -80,7 +80,7 @@ LL | let _ = [0; baz::<'a>(&())];
8080
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
8181

8282
error: generic parameters may not be used in const operations
83-
--> $DIR/const-arg-in-const-arg.rs:31:23
83+
--> $DIR/const-arg-in-const-arg.rs:32:23
8484
|
8585
LL | let _ = [0; faz::<'b>(&())];
8686
| ^^ cannot perform const operation using `'b`
@@ -89,7 +89,7 @@ LL | let _ = [0; faz::<'b>(&())];
8989
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
9090

9191
error: generic parameters may not be used in const operations
92-
--> $DIR/const-arg-in-const-arg.rs:33:23
92+
--> $DIR/const-arg-in-const-arg.rs:34:23
9393
|
9494
LL | let _ = [0; baz::<'b>(&())];
9595
| ^^ cannot perform const operation using `'b`
@@ -98,7 +98,7 @@ LL | let _ = [0; baz::<'b>(&())];
9898
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
9999

100100
error: generic parameters may not be used in const operations
101-
--> $DIR/const-arg-in-const-arg.rs:34:24
101+
--> $DIR/const-arg-in-const-arg.rs:35:24
102102
|
103103
LL | let _: Foo<{ foo::<T>() }>;
104104
| ^ cannot perform const operation using `T`
@@ -107,7 +107,7 @@ LL | let _: Foo<{ foo::<T>() }>;
107107
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
108108

109109
error: generic parameters may not be used in const operations
110-
--> $DIR/const-arg-in-const-arg.rs:35:24
110+
--> $DIR/const-arg-in-const-arg.rs:36:24
111111
|
112112
LL | let _: Foo<{ bar::<N>() }>;
113113
| ^ cannot perform const operation using `N`
@@ -116,7 +116,7 @@ LL | let _: Foo<{ bar::<N>() }>;
116116
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
117117

118118
error: generic parameters may not be used in const operations
119-
--> $DIR/const-arg-in-const-arg.rs:37:24
119+
--> $DIR/const-arg-in-const-arg.rs:38:24
120120
|
121121
LL | let _: Foo<{ faz::<'a>(&()) }>;
122122
| ^^ cannot perform const operation using `'a`
@@ -125,7 +125,7 @@ LL | let _: Foo<{ faz::<'a>(&()) }>;
125125
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
126126

127127
error: generic parameters may not be used in const operations
128-
--> $DIR/const-arg-in-const-arg.rs:39:24
128+
--> $DIR/const-arg-in-const-arg.rs:40:24
129129
|
130130
LL | let _: Foo<{ baz::<'a>(&()) }>;
131131
| ^^ cannot perform const operation using `'a`
@@ -134,7 +134,7 @@ LL | let _: Foo<{ baz::<'a>(&()) }>;
134134
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
135135

136136
error: generic parameters may not be used in const operations
137-
--> $DIR/const-arg-in-const-arg.rs:40:24
137+
--> $DIR/const-arg-in-const-arg.rs:41:24
138138
|
139139
LL | let _: Foo<{ faz::<'b>(&()) }>;
140140
| ^^ cannot perform const operation using `'b`
@@ -143,7 +143,7 @@ LL | let _: Foo<{ faz::<'b>(&()) }>;
143143
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
144144

145145
error: generic parameters may not be used in const operations
146-
--> $DIR/const-arg-in-const-arg.rs:42:24
146+
--> $DIR/const-arg-in-const-arg.rs:43:24
147147
|
148148
LL | let _: Foo<{ baz::<'b>(&()) }>;
149149
| ^^ cannot perform const operation using `'b`
@@ -152,7 +152,7 @@ LL | let _: Foo<{ baz::<'b>(&()) }>;
152152
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
153153

154154
error: generic parameters may not be used in const operations
155-
--> $DIR/const-arg-in-const-arg.rs:43:27
155+
--> $DIR/const-arg-in-const-arg.rs:44:27
156156
|
157157
LL | let _ = Foo::<{ foo::<T>() }>;
158158
| ^ cannot perform const operation using `T`
@@ -161,7 +161,7 @@ LL | let _ = Foo::<{ foo::<T>() }>;
161161
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
162162

163163
error: generic parameters may not be used in const operations
164-
--> $DIR/const-arg-in-const-arg.rs:44:27
164+
--> $DIR/const-arg-in-const-arg.rs:45:27
165165
|
166166
LL | let _ = Foo::<{ bar::<N>() }>;
167167
| ^ cannot perform const operation using `N`
@@ -170,7 +170,7 @@ LL | let _ = Foo::<{ bar::<N>() }>;
170170
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
171171

172172
error: generic parameters may not be used in const operations
173-
--> $DIR/const-arg-in-const-arg.rs:46:27
173+
--> $DIR/const-arg-in-const-arg.rs:47:27
174174
|
175175
LL | let _ = Foo::<{ faz::<'a>(&()) }>;
176176
| ^^ cannot perform const operation using `'a`
@@ -179,7 +179,7 @@ LL | let _ = Foo::<{ faz::<'a>(&()) }>;
179179
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
180180

181181
error: generic parameters may not be used in const operations
182-
--> $DIR/const-arg-in-const-arg.rs:48:27
182+
--> $DIR/const-arg-in-const-arg.rs:49:27
183183
|
184184
LL | let _ = Foo::<{ baz::<'a>(&()) }>;
185185
| ^^ cannot perform const operation using `'a`
@@ -188,7 +188,7 @@ LL | let _ = Foo::<{ baz::<'a>(&()) }>;
188188
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
189189

190190
error: generic parameters may not be used in const operations
191-
--> $DIR/const-arg-in-const-arg.rs:49:27
191+
--> $DIR/const-arg-in-const-arg.rs:50:27
192192
|
193193
LL | let _ = Foo::<{ faz::<'b>(&()) }>;
194194
| ^^ cannot perform const operation using `'b`
@@ -197,7 +197,7 @@ LL | let _ = Foo::<{ faz::<'b>(&()) }>;
197197
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
198198

199199
error: generic parameters may not be used in const operations
200-
--> $DIR/const-arg-in-const-arg.rs:51:27
200+
--> $DIR/const-arg-in-const-arg.rs:52:27
201201
|
202202
LL | let _ = Foo::<{ baz::<'b>(&()) }>;
203203
| ^^ cannot perform const operation using `'b`
@@ -241,7 +241,7 @@ LL | const fn faz<'a>(_: &'a ()) -> usize { 13 }
241241
| ^^
242242

243243
error[E0747]: unresolved item provided when a constant was expected
244-
--> $DIR/const-arg-in-const-arg.rs:35:24
244+
--> $DIR/const-arg-in-const-arg.rs:36:24
245245
|
246246
LL | let _: Foo<{ bar::<N>() }>;
247247
| ^
@@ -252,7 +252,7 @@ LL | let _: Foo<{ bar::<{ N }>() }>;
252252
| + +
253253

254254
error[E0794]: cannot specify lifetime arguments explicitly if late bound lifetime parameters are present
255-
--> $DIR/const-arg-in-const-arg.rs:37:24
255+
--> $DIR/const-arg-in-const-arg.rs:38:24
256256
|
257257
LL | let _: Foo<{ faz::<'a>(&()) }>;
258258
| ^^
@@ -264,7 +264,7 @@ LL | const fn faz<'a>(_: &'a ()) -> usize { 13 }
264264
| ^^
265265

266266
error[E0794]: cannot specify lifetime arguments explicitly if late bound lifetime parameters are present
267-
--> $DIR/const-arg-in-const-arg.rs:40:24
267+
--> $DIR/const-arg-in-const-arg.rs:41:24
268268
|
269269
LL | let _: Foo<{ faz::<'b>(&()) }>;
270270
| ^^
@@ -283,8 +283,16 @@ LL | let _ = [0; foo::<T>()];
283283
|
284284
= note: this may fail depending on what value the parameter takes
285285

286+
error: constant expression depends on a generic parameter
287+
--> $DIR/const-arg-in-const-arg.rs:25:13
288+
|
289+
LL | let _ = [0; foo::<T>()];
290+
| ^^^^^^^^^^^^^^^
291+
|
292+
= note: this may fail depending on what value the parameter takes
293+
286294
error[E0747]: unresolved item provided when a constant was expected
287-
--> $DIR/const-arg-in-const-arg.rs:26:23
295+
--> $DIR/const-arg-in-const-arg.rs:27:23
288296
|
289297
LL | let _ = [0; bar::<N>()];
290298
| ^
@@ -295,7 +303,7 @@ LL | let _ = [0; bar::<{ N }>()];
295303
| + +
296304

297305
error[E0794]: cannot specify lifetime arguments explicitly if late bound lifetime parameters are present
298-
--> $DIR/const-arg-in-const-arg.rs:28:23
306+
--> $DIR/const-arg-in-const-arg.rs:29:23
299307
|
300308
LL | let _ = [0; faz::<'a>(&())];
301309
| ^^
@@ -307,7 +315,7 @@ LL | const fn faz<'a>(_: &'a ()) -> usize { 13 }
307315
| ^^
308316

309317
error[E0794]: cannot specify lifetime arguments explicitly if late bound lifetime parameters are present
310-
--> $DIR/const-arg-in-const-arg.rs:31:23
318+
--> $DIR/const-arg-in-const-arg.rs:32:23
311319
|
312320
LL | let _ = [0; faz::<'b>(&())];
313321
| ^^
@@ -319,7 +327,7 @@ LL | const fn faz<'a>(_: &'a ()) -> usize { 13 }
319327
| ^^
320328

321329
error[E0747]: unresolved item provided when a constant was expected
322-
--> $DIR/const-arg-in-const-arg.rs:44:27
330+
--> $DIR/const-arg-in-const-arg.rs:45:27
323331
|
324332
LL | let _ = Foo::<{ bar::<N>() }>;
325333
| ^
@@ -330,7 +338,7 @@ LL | let _ = Foo::<{ bar::<{ N }>() }>;
330338
| + +
331339

332340
error[E0794]: cannot specify lifetime arguments explicitly if late bound lifetime parameters are present
333-
--> $DIR/const-arg-in-const-arg.rs:46:27
341+
--> $DIR/const-arg-in-const-arg.rs:47:27
334342
|
335343
LL | let _ = Foo::<{ faz::<'a>(&()) }>;
336344
| ^^
@@ -342,7 +350,7 @@ LL | const fn faz<'a>(_: &'a ()) -> usize { 13 }
342350
| ^^
343351

344352
error[E0794]: cannot specify lifetime arguments explicitly if late bound lifetime parameters are present
345-
--> $DIR/const-arg-in-const-arg.rs:49:27
353+
--> $DIR/const-arg-in-const-arg.rs:50:27
346354
|
347355
LL | let _ = Foo::<{ faz::<'b>(&()) }>;
348356
| ^^
@@ -353,7 +361,7 @@ note: the late bound lifetime parameter is introduced here
353361
LL | const fn faz<'a>(_: &'a ()) -> usize { 13 }
354362
| ^^
355363

356-
error: aborting due to 36 previous errors
364+
error: aborting due to 37 previous errors
357365

358366
Some errors have detailed explanations: E0747, E0794.
359367
For more information about an error, try `rustc --explain E0747`.

Diff for: tests/ui/const-generics/const-arg-in-const-arg.rs

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ fn test<'a, 'b, T, const N: usize>() where &'b (): Sized {
2323
let _: [u8; baz::<'b>(&())]; //[min]~ ERROR generic parameters may not
2424

2525
let _ = [0; foo::<T>()]; //[min]~ ERROR constant expression depends on a generic parameter
26+
//[min]~^ ERROR constant expression depends on a generic parameter
2627
let _ = [0; bar::<N>()]; //[min]~ ERROR generic parameters may not
2728
//[min]~^ ERROR unresolved item provided when a constant was expected
2829
let _ = [0; faz::<'a>(&())]; //[min]~ ERROR generic parameters may not

Diff for: tests/ui/const-generics/generic_const_exprs/dependence_lint.gce.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
error: overly complex generic constant
2-
--> $DIR/dependence_lint.rs:17:9
3-
|
4-
LL | [0; if false { size_of::<T>() } else { 3 }]; // lint on stable, error with gce
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ control flow is not supported in generic constants
6-
|
7-
= help: consider moving this anonymous constant into a `const` function
8-
91
error: overly complex generic constant
102
--> $DIR/dependence_lint.rs:21:17
113
|
@@ -36,5 +28,13 @@ help: try adding a `where` bound
3628
LL | fn foo<T>() where [(); size_of::<*mut T>()]: {
3729
| ++++++++++++++++++++++++++++++++
3830

31+
error: overly complex generic constant
32+
--> $DIR/dependence_lint.rs:17:9
33+
|
34+
LL | [0; if false { size_of::<T>() } else { 3 }]; // lint on stable, error with gce
35+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ control flow is not supported in generic constants
36+
|
37+
= help: consider moving this anonymous constant into a `const` function
38+
3939
error: aborting due to 4 previous errors
4040

Diff for: tests/ui/consts/too_generic_eval_ice.rs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ impl<A, B> Foo<A, B> {
77
[5; Self::HOST_SIZE] == [6; 0]
88
//~^ ERROR constant expression depends on a generic parameter
99
//~| ERROR constant expression depends on a generic parameter
10+
//~| ERROR constant expression depends on a generic parameter
1011
//~| ERROR can't compare `[{integer}; Self::HOST_SIZE]` with `[{integer}; 0]`
1112
}
1213
}

Diff for: tests/ui/consts/too_generic_eval_ice.stderr

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ LL | [5; Self::HOST_SIZE] == [6; 0]
66
|
77
= note: this may fail depending on what value the parameter takes
88

9+
error: constant expression depends on a generic parameter
10+
--> $DIR/too_generic_eval_ice.rs:7:9
11+
|
12+
LL | [5; Self::HOST_SIZE] == [6; 0]
13+
| ^^^^^^^^^^^^^^^^^^^^
14+
|
15+
= note: this may fail depending on what value the parameter takes
16+
917
error: constant expression depends on a generic parameter
1018
--> $DIR/too_generic_eval_ice.rs:7:30
1119
|
@@ -32,6 +40,6 @@ LL | [5; Self::HOST_SIZE] == [6; 0]
3240
`[T; N]` implements `PartialEq<[U]>`
3341
and 3 others
3442

35-
error: aborting due to 3 previous errors
43+
error: aborting due to 4 previous errors
3644

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

Diff for: tests/ui/error-codes/E0746.stderr

+5-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@ error[E0746]: return type cannot have an unboxed trait object
1919
LL | fn bar() -> dyn Trait {
2020
| ^^^^^^^^^ doesn't have a size known at compile-time
2121
|
22-
= help: if there were a single returned type, you could use `impl Trait` instead
23-
help: box the return type, and wrap all of the returned values in `Box::new`
22+
help: consider returning an `impl Trait` instead of a `dyn Trait`
23+
|
24+
LL | fn bar() -> impl Trait {
25+
| ~~~~
26+
help: alternatively, box the return type, and wrap all of the returned values in `Box::new`
2427
|
2528
LL ~ fn bar() -> Box<dyn Trait> {
2629
LL | if true {

0 commit comments

Comments
 (0)