Skip to content

Commit be3bb9f

Browse files
Rollup merge of #162595 - amirHdev:uninferred-upvars, r=lcnr
avoid accessing uninferred closure upvars in diagnostics fixes #162440 `note_closure_capture` can run before closure capture analysis has finished and the upvar tuple may still be an inference variable at that point and calling `upvar_tys()` causes an ICE we can just fall back to the existing closure-level note and wait until the upvar types are available before trying to emit the more specific capture note
2 parents 03290b3 + c263e3d commit be3bb9f

5 files changed

Lines changed: 128 additions & 1 deletion

File tree

‎compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3795,9 +3795,18 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
37953795
if typeck_results.hir_owner.to_def_id() != typeck_root {
37963796
return false;
37973797
}
3798+
3799+
// Error reporting can run before closure capture analysis has inferred the
3800+
// tuple of upvar types. avoid accessing upvar types until they are available.
3801+
let upvar_tys = match upvar_args.tupled_upvars_ty().kind() {
3802+
ty::Tuple(args) => args,
3803+
ty::Error(_) => ty::List::empty(),
3804+
ty::Infer(_) => return false,
3805+
ty => unreachable!("unexpected upvar types tuple: {ty:?}"),
3806+
};
3807+
37983808
let captures: Vec<_> =
37993809
typeck_results.closure_min_captures_flattened(closure_def_id).collect();
3800-
let upvar_tys = upvar_args.upvar_tys();
38013810
if captures.len() != upvar_tys.len() {
38023811
return false;
38033812
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Regression test for #162440. Diagnostics can run before closure capture
2+
// analysis has inferred the tuple of upvar types. The capture-specific note
3+
// must fall back instead of trying to access uninferred upvar types.
4+
5+
//@ compile-flags: -Znext-solver=globally
6+
7+
fn main() {
8+
Some([0]).map(|s| s[..]);
9+
//~^ ERROR the size for values of type `[{integer}]` cannot be known at compilation time
10+
//~| ERROR the size for values of type `[{integer}]` cannot be known at compilation time
11+
//~| ERROR the size for values of type `[{integer}]` cannot be known at compilation time
12+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
error[E0277]: the size for values of type `[{integer}]` cannot be known at compilation time
2+
--> $DIR/closure-capture-uninferred-upvars-issue-162440.rs:8:15
3+
|
4+
LL | Some([0]).map(|s| s[..]);
5+
| ^^^ doesn't have a size known at compile-time
6+
|
7+
= help: the trait `Sized` is not implemented for `[{integer}]`
8+
note: required by an implicit `Sized` bound in `Option::<T>::map`
9+
--> $SRC_DIR/core/src/option.rs:LL:COL
10+
11+
error[E0277]: the size for values of type `[{integer}]` cannot be known at compilation time
12+
--> $DIR/closure-capture-uninferred-upvars-issue-162440.rs:8:23
13+
|
14+
LL | Some([0]).map(|s| s[..]);
15+
| ^^^^^ doesn't have a size known at compile-time
16+
|
17+
= help: the trait `Sized` is not implemented for `[{integer}]`
18+
= note: the return type of a function must have a statically known size
19+
20+
error[E0277]: the size for values of type `[{integer}]` cannot be known at compilation time
21+
--> $DIR/closure-capture-uninferred-upvars-issue-162440.rs:8:19
22+
|
23+
LL | Some([0]).map(|s| s[..]);
24+
| --- ---^^^^^^
25+
| | |
26+
| | doesn't have a size known at compile-time
27+
| | within this `{closure@$DIR/closure-capture-uninferred-upvars-issue-162440.rs:8:19: 8:22}`
28+
| required by a bound introduced by this call
29+
|
30+
= help: within `{closure@$DIR/closure-capture-uninferred-upvars-issue-162440.rs:8:19: 8:22}`, the trait `Sized` is not implemented for `[{integer}]`
31+
note: required because it's used within this closure
32+
--> $DIR/closure-capture-uninferred-upvars-issue-162440.rs:8:19
33+
|
34+
LL | Some([0]).map(|s| s[..]);
35+
| ^^^
36+
note: required by a bound in `Option::<T>::map`
37+
--> $SRC_DIR/core/src/option.rs:LL:COL
38+
39+
error: aborting due to 3 previous errors
40+
41+
For more information about this error, try `rustc --explain E0277`.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Diagnostics may run before closure capture analysis has inferred the upvar
2+
// tuple, even for a closure which actually captures a value.
3+
4+
//@ compile-flags: -Znext-solver=globally
5+
6+
fn main() {
7+
let x = String::new();
8+
9+
Some([0]).map(|s| {
10+
//~^ ERROR the size for values of type `[{integer}]` cannot be known at compilation time
11+
//~| ERROR the size for values of type `[{integer}]` cannot be known at compilation time
12+
//~| ERROR the size for values of type `[{integer}]` cannot be known at compilation time
13+
let _ = &x;
14+
s[..]
15+
});
16+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
error[E0277]: the size for values of type `[{integer}]` cannot be known at compilation time
2+
--> $DIR/closure-capture-uninferred-upvars-with-capture.rs:9:15
3+
|
4+
LL | Some([0]).map(|s| {
5+
| ^^^ doesn't have a size known at compile-time
6+
|
7+
= help: the trait `Sized` is not implemented for `[{integer}]`
8+
note: required by an implicit `Sized` bound in `Option::<T>::map`
9+
--> $SRC_DIR/core/src/option.rs:LL:COL
10+
11+
error[E0277]: the size for values of type `[{integer}]` cannot be known at compilation time
12+
--> $DIR/closure-capture-uninferred-upvars-with-capture.rs:9:23
13+
|
14+
LL | Some([0]).map(|s| {
15+
| _______________________^
16+
... |
17+
LL | | s[..]
18+
LL | | });
19+
| |_____^ doesn't have a size known at compile-time
20+
|
21+
= help: the trait `Sized` is not implemented for `[{integer}]`
22+
= note: the return type of a function must have a statically known size
23+
24+
error[E0277]: the size for values of type `[{integer}]` cannot be known at compilation time
25+
--> $DIR/closure-capture-uninferred-upvars-with-capture.rs:9:19
26+
|
27+
LL | Some([0]).map(|s| {
28+
| --- ^--
29+
| | |
30+
| _______________|___within this `{closure@$DIR/closure-capture-uninferred-upvars-with-capture.rs:9:19: 9:22}`
31+
| | |
32+
| | required by a bound introduced by this call
33+
... |
34+
LL | | s[..]
35+
LL | | });
36+
| |_____^ doesn't have a size known at compile-time
37+
|
38+
= help: within `{closure@$DIR/closure-capture-uninferred-upvars-with-capture.rs:9:19: 9:22}`, the trait `Sized` is not implemented for `[{integer}]`
39+
note: required because it's used within this closure
40+
--> $DIR/closure-capture-uninferred-upvars-with-capture.rs:9:19
41+
|
42+
LL | Some([0]).map(|s| {
43+
| ^^^
44+
note: required by a bound in `Option::<T>::map`
45+
--> $SRC_DIR/core/src/option.rs:LL:COL
46+
47+
error: aborting due to 3 previous errors
48+
49+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)