Skip to content

Commit 0d33ab7

Browse files
committed
Auto merge of #78432 - sexxi-goose:fix-77993-take3, r=nikomatsakis
Handle type errors in closure/generator upvar_tys Fixes #77993
2 parents c792f03 + 5229571 commit 0d33ab7

File tree

5 files changed

+82
-6
lines changed

5 files changed

+82
-6
lines changed

compiler/rustc_middle/src/ty/sty.rs

+37-6
Original file line numberDiff line numberDiff line change
@@ -388,9 +388,19 @@ impl<'tcx> ClosureSubsts<'tcx> {
388388
self.split().parent_substs
389389
}
390390

391+
/// Returns an iterator over the list of types of captured paths by the closure.
392+
/// In case there was a type error in figuring out the types of the captured path, an
393+
/// empty iterator is returned.
391394
#[inline]
392395
pub fn upvar_tys(self) -> impl Iterator<Item = Ty<'tcx>> + 'tcx {
393-
self.tupled_upvars_ty().tuple_fields()
396+
match self.tupled_upvars_ty().kind() {
397+
TyKind::Error(_) => None,
398+
TyKind::Tuple(..) => Some(self.tupled_upvars_ty().tuple_fields()),
399+
TyKind::Infer(_) => bug!("upvar_tys called before capture types are inferred"),
400+
ty => bug!("Unexpected representation of upvar types tuple {:?}", ty),
401+
}
402+
.into_iter()
403+
.flatten()
394404
}
395405

396406
/// Returns the tuple type representing the upvars for this closure.
@@ -515,9 +525,19 @@ impl<'tcx> GeneratorSubsts<'tcx> {
515525
self.split().witness.expect_ty()
516526
}
517527

528+
/// Returns an iterator over the list of types of captured paths by the generator.
529+
/// In case there was a type error in figuring out the types of the captured path, an
530+
/// empty iterator is returned.
518531
#[inline]
519532
pub fn upvar_tys(self) -> impl Iterator<Item = Ty<'tcx>> + 'tcx {
520-
self.tupled_upvars_ty().tuple_fields()
533+
match self.tupled_upvars_ty().kind() {
534+
TyKind::Error(_) => None,
535+
TyKind::Tuple(..) => Some(self.tupled_upvars_ty().tuple_fields()),
536+
TyKind::Infer(_) => bug!("upvar_tys called before capture types are inferred"),
537+
ty => bug!("Unexpected representation of upvar types tuple {:?}", ty),
538+
}
539+
.into_iter()
540+
.flatten()
521541
}
522542

523543
/// Returns the tuple type representing the upvars for this generator.
@@ -660,13 +680,24 @@ pub enum UpvarSubsts<'tcx> {
660680
}
661681

662682
impl<'tcx> UpvarSubsts<'tcx> {
683+
/// Returns an iterator over the list of types of captured paths by the closure/generator.
684+
/// In case there was a type error in figuring out the types of the captured path, an
685+
/// empty iterator is returned.
663686
#[inline]
664687
pub fn upvar_tys(self) -> impl Iterator<Item = Ty<'tcx>> + 'tcx {
665-
let tupled_upvars_ty = match self {
666-
UpvarSubsts::Closure(substs) => substs.as_closure().split().tupled_upvars_ty,
667-
UpvarSubsts::Generator(substs) => substs.as_generator().split().tupled_upvars_ty,
688+
let tupled_tys = match self {
689+
UpvarSubsts::Closure(substs) => substs.as_closure().tupled_upvars_ty(),
690+
UpvarSubsts::Generator(substs) => substs.as_generator().tupled_upvars_ty(),
668691
};
669-
tupled_upvars_ty.expect_ty().tuple_fields()
692+
693+
match tupled_tys.kind() {
694+
TyKind::Error(_) => None,
695+
TyKind::Tuple(..) => Some(self.tupled_upvars_ty().tuple_fields()),
696+
TyKind::Infer(_) => bug!("upvar_tys called before capture types are inferred"),
697+
ty => bug!("Unexpected representation of upvar types tuple {:?}", ty),
698+
}
699+
.into_iter()
700+
.flatten()
670701
}
671702

672703
#[inline]

src/test/ui/issues/issue-77993-1.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#[derive(Clone)]
2+
struct InGroup<F> {
3+
it: It,
4+
//~^ ERROR cannot find type `It` in this scope
5+
f: F,
6+
}
7+
fn dates_in_year() -> impl Clone {
8+
InGroup { f: |d| d }
9+
//~^ ERROR missing field `it` in initializer of `InGroup<_>`
10+
}
11+
12+
fn main() {}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0412]: cannot find type `It` in this scope
2+
--> $DIR/issue-77993-1.rs:3:9
3+
|
4+
LL | it: It,
5+
| ^^ not found in this scope
6+
7+
error[E0063]: missing field `it` in initializer of `InGroup<_>`
8+
--> $DIR/issue-77993-1.rs:8:5
9+
|
10+
LL | InGroup { f: |d| d }
11+
| ^^^^^^^ missing `it`
12+
13+
error: aborting due to 2 previous errors
14+
15+
Some errors have detailed explanations: E0063, E0412.
16+
For more information about an error, try `rustc --explain E0063`.

src/test/ui/issues/issue-77993-2.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// edition:2018
2+
3+
async fn test() -> Result<(), Box<dyn std::error::Error>> {
4+
macro!();
5+
//~^ ERROR expected identifier, found `!`
6+
Ok(())
7+
}
8+
9+
fn main() {}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: expected identifier, found `!`
2+
--> $DIR/issue-77993-2.rs:4:10
3+
|
4+
LL | macro!();
5+
| ^ expected identifier
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)