Skip to content

Handle type errors in closure/generator upvar_tys #78432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 30, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3883,6 +3883,7 @@ version = "0.0.0"
dependencies = [
"bitflags",
"chalk-ir",
"either",
"measureme 9.0.0",
"polonius-engine",
"rustc-rayon-core",
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_middle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2018"
doctest = false

[dependencies]
either = "1.5.0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note, this is an extra dependency, don't know how willing we are to accept that.

rustc_arena = { path = "../rustc_arena" }
bitflags = "1.2.1"
tracing = "0.1"
Expand Down
34 changes: 27 additions & 7 deletions compiler/rustc_middle/src/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use self::InferTy::*;
use self::TyKind::*;

use either::Either;

use crate::infer::canonical::Canonical;
use crate::ty::subst::{GenericArg, InternalSubsts, Subst, SubstsRef};
use crate::ty::{
Expand Down Expand Up @@ -388,9 +390,17 @@ impl<'tcx> ClosureSubsts<'tcx> {
self.split().parent_substs
}

/// Returns an iterator over the list of types of captured paths by the closure.
/// In case there was a type error in figuring out the types of the captured path, an
/// empty iterator is returned.
#[inline]
pub fn upvar_tys(self) -> impl Iterator<Item = Ty<'tcx>> + 'tcx {
self.tupled_upvars_ty().tuple_fields()
match self.tupled_upvars_ty().kind() {
TyKind::Error(_) => Either::Left(std::iter::empty()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I suggest returning None here...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

TyKind::Tuple(..) => Either::Right(self.tupled_upvars_ty().tuple_fields()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...and Some(self.tupled_upvars_ty().tuple_fields()) here...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

TyKind::Infer(_) => bug!("upvar_tys called before capture types are inferred"),
ty => bug!("Unexpected representation of upvar types tuple {:?}", ty),
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and then calling .into_iter().flatten() here, then you can avoid the Either dependency.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

}

/// Returns the tuple type representing the upvars for this closure.
Expand Down Expand Up @@ -515,9 +525,17 @@ impl<'tcx> GeneratorSubsts<'tcx> {
self.split().witness.expect_ty()
}

/// Returns an iterator over the list of types of captured paths by the generator.
/// In case there was a type error in figuring out the types of the captured path, an
/// empty iterator is returned.
#[inline]
pub fn upvar_tys(self) -> impl Iterator<Item = Ty<'tcx>> + 'tcx {
self.tupled_upvars_ty().tuple_fields()
match self.tupled_upvars_ty().kind() {
TyKind::Error(_) => Either::Left(std::iter::empty()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

TyKind::Tuple(..) => Either::Right(self.tupled_upvars_ty().tuple_fields()),
TyKind::Infer(_) => bug!("upvar_tys called before capture types are inferred"),
ty => bug!("Unexpected representation of upvar types tuple {:?}", ty),
}
}

/// Returns the tuple type representing the upvars for this generator.
Expand Down Expand Up @@ -660,13 +678,15 @@ pub enum UpvarSubsts<'tcx> {
}

impl<'tcx> UpvarSubsts<'tcx> {
/// Returns an iterator over the list of types of captured paths by the closure/generator.
/// In case there was a type error in figuring out the types of the captured path, an
/// empty iterator is returned.
#[inline]
pub fn upvar_tys(self) -> impl Iterator<Item = Ty<'tcx>> + 'tcx {
let tupled_upvars_ty = match self {
UpvarSubsts::Closure(substs) => substs.as_closure().split().tupled_upvars_ty,
UpvarSubsts::Generator(substs) => substs.as_generator().split().tupled_upvars_ty,
};
tupled_upvars_ty.expect_ty().tuple_fields()
match self {
UpvarSubsts::Closure(substs) => Either::Left(substs.as_closure().upvar_tys()),
UpvarSubsts::Generator(substs) => Either::Right(substs.as_generator().upvar_tys()),
}
}

#[inline]
Expand Down
12 changes: 12 additions & 0 deletions src/test/ui/issues/issue-77993-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#[derive(Clone)]
struct InGroup<F> {
it: It,
//~^ ERROR cannot find type `It` in this scope
f: F,
}
fn dates_in_year() -> impl Clone {
InGroup { f: |d| d }
//~^ ERROR missing field `it` in initializer of `InGroup<_>`
}

fn main() {}
16 changes: 16 additions & 0 deletions src/test/ui/issues/issue-77993-1.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
error[E0412]: cannot find type `It` in this scope
--> $DIR/issue-77993-1.rs:3:9
|
LL | it: It,
| ^^ not found in this scope

error[E0063]: missing field `it` in initializer of `InGroup<_>`
--> $DIR/issue-77993-1.rs:8:5
|
LL | InGroup { f: |d| d }
| ^^^^^^^ missing `it`

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0063, E0412.
For more information about an error, try `rustc --explain E0063`.
9 changes: 9 additions & 0 deletions src/test/ui/issues/issue-77993-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// edition:2018

async fn test() -> Result<(), Box<dyn std::error::Error>> {
macro!();
//~^ ERROR expected identifier, found `!`
Ok(())
}

fn main() {}
8 changes: 8 additions & 0 deletions src/test/ui/issues/issue-77993-2.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: expected identifier, found `!`
--> $DIR/issue-77993-2.rs:4:10
|
LL | macro!();
| ^ expected identifier

error: aborting due to previous error