From d67db0042c1f4e163292ea59bdfac546c67f7001 Mon Sep 17 00:00:00 2001 From: Taylor Cramer Date: Tue, 18 Jun 2019 14:34:51 -0700 Subject: [PATCH] Preserve generator and yield source for error messages Previously, error messages after HIR lowering all referred to generators and yield, regardless of whether the original source was a generator or an async/await body. This change tracks the kind of each generator and yield source in order to provide appropriately tailored error messages. --- src/librustc/cfg/construct.rs | 2 +- src/librustc/hir/intravisit.rs | 2 +- src/librustc/hir/lowering.rs | 148 ++++++++++-------- src/librustc/hir/mod.rs | 72 +++++++-- src/librustc/hir/print.rs | 2 +- src/librustc/ich/impls_hir.rs | 10 +- .../infer/error_reporting/need_type_info.rs | 9 +- src/librustc/middle/expr_use_visitor.rs | 2 +- src/librustc/middle/liveness.rs | 2 +- src/librustc/middle/region.rs | 29 +++- src/librustc_mir/build/mod.rs | 4 +- src/librustc_mir/hair/cx/expr.rs | 2 +- src/librustc_passes/rvalue_promotion.rs | 2 +- src/librustc_typeck/check/expr.rs | 2 +- .../check/generator_interior.rs | 43 +++-- src/librustc_typeck/check/mod.rs | 12 +- .../ui/async-await/unresolved_type_param.rs | 4 +- .../async-await/unresolved_type_param.stderr | 4 +- 18 files changed, 222 insertions(+), 129 deletions(-) diff --git a/src/librustc/cfg/construct.rs b/src/librustc/cfg/construct.rs index 139c144fbcf4e..ffbdc0373f80c 100644 --- a/src/librustc/cfg/construct.rs +++ b/src/librustc/cfg/construct.rs @@ -330,7 +330,7 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> { hir::ExprKind::DropTemps(ref e) | hir::ExprKind::Unary(_, ref e) | hir::ExprKind::Field(ref e, _) | - hir::ExprKind::Yield(ref e) | + hir::ExprKind::Yield(ref e, _) | hir::ExprKind::Repeat(ref e, _) => { self.straightline(expr, pred, Some(&**e).into_iter()) } diff --git a/src/librustc/hir/intravisit.rs b/src/librustc/hir/intravisit.rs index f4f9d6261de48..0ab6f7bc46a84 100644 --- a/src/librustc/hir/intravisit.rs +++ b/src/librustc/hir/intravisit.rs @@ -1089,7 +1089,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) { visitor.visit_expr(expr) } } - ExprKind::Yield(ref subexpression) => { + ExprKind::Yield(ref subexpression, _) => { visitor.visit_expr(subexpression); } ExprKind::Lit(_) | ExprKind::Err => {} diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs index 5a548ce8d9ff4..d25f3ed5c89a6 100644 --- a/src/librustc/hir/lowering.rs +++ b/src/librustc/hir/lowering.rs @@ -95,8 +95,7 @@ pub struct LoweringContext<'a> { modules: BTreeMap, - is_generator: bool, - is_async_body: bool, + generator_kind: Option, /// Used to get the current `fn`'s def span to point to when using `await` /// outside of an `async fn`. @@ -261,8 +260,7 @@ pub fn lower_crate( current_hir_id_owner: vec![(CRATE_DEF_INDEX, 0)], item_local_id_counters: Default::default(), node_id_to_hir_id: IndexVec::new(), - is_generator: false, - is_async_body: false, + generator_kind: None, current_item: None, lifetimes_to_define: Vec::new(), is_collecting_in_band_lifetimes: false, @@ -790,18 +788,49 @@ impl<'a> LoweringContext<'a> { }) } - fn record_body(&mut self, arguments: HirVec, value: hir::Expr) -> hir::BodyId { - if self.is_generator && self.is_async_body { - span_err!( - self.sess, - value.span, - E0727, - "`async` generators are not yet supported", - ); - self.sess.abort_if_errors(); + fn generator_movability_for_fn( + &mut self, + decl: &ast::FnDecl, + fn_decl_span: Span, + generator_kind: Option, + movability: Movability, + ) -> Option { + match generator_kind { + Some(hir::GeneratorKind::Gen) => { + if !decl.inputs.is_empty() { + span_err!( + self.sess, + fn_decl_span, + E0628, + "generators cannot have explicit arguments" + ); + self.sess.abort_if_errors(); + } + Some(match movability { + Movability::Movable => hir::GeneratorMovability::Movable, + Movability::Static => hir::GeneratorMovability::Static, + }) + }, + Some(hir::GeneratorKind::Async) => { + bug!("non-`async` closure body turned `async` during lowering"); + }, + None => { + if movability == Movability::Static { + span_err!( + self.sess, + fn_decl_span, + E0697, + "closures cannot be static" + ); + } + None + }, } + } + + fn record_body(&mut self, arguments: HirVec, value: hir::Expr) -> hir::BodyId { let body = hir::Body { - is_generator: self.is_generator || self.is_async_body, + generator_kind: self.generator_kind, arguments, value, }; @@ -1142,7 +1171,7 @@ impl<'a> LoweringContext<'a> { }; let decl = self.lower_fn_decl(&ast_decl, None, /* impl trait allowed */ false, None); let body_id = self.lower_fn_body(&ast_decl, |this| { - this.is_async_body = true; + this.generator_kind = Some(hir::GeneratorKind::Async); body(this) }); let generator = hir::Expr { @@ -1167,12 +1196,10 @@ impl<'a> LoweringContext<'a> { &mut self, f: impl FnOnce(&mut LoweringContext<'_>) -> (HirVec, hir::Expr), ) -> hir::BodyId { - let prev_is_generator = mem::replace(&mut self.is_generator, false); - let prev_is_async_body = mem::replace(&mut self.is_async_body, false); + let prev_gen_kind = self.generator_kind.take(); let (arguments, result) = f(self); let body_id = self.record_body(arguments, result); - self.is_generator = prev_is_generator; - self.is_async_body = prev_is_async_body; + self.generator_kind = prev_gen_kind; body_id } @@ -4475,37 +4502,18 @@ impl<'a> LoweringContext<'a> { self.with_new_scopes(|this| { this.current_item = Some(fn_decl_span); - let mut is_generator = false; + let mut generator_kind = None; let body_id = this.lower_fn_body(decl, |this| { let e = this.lower_expr(body); - is_generator = this.is_generator; + generator_kind = this.generator_kind; e }); - let generator_option = if is_generator { - if !decl.inputs.is_empty() { - span_err!( - this.sess, - fn_decl_span, - E0628, - "generators cannot have explicit arguments" - ); - this.sess.abort_if_errors(); - } - Some(match movability { - Movability::Movable => hir::GeneratorMovability::Movable, - Movability::Static => hir::GeneratorMovability::Static, - }) - } else { - if movability == Movability::Static { - span_err!( - this.sess, - fn_decl_span, - E0697, - "closures cannot be static" - ); - } - None - }; + let generator_option = this.generator_movability_for_fn( + &decl, + fn_decl_span, + generator_kind, + movability, + ); hir::ExprKind::Closure( this.lower_capture_clause(capture_clause), fn_decl, @@ -4677,12 +4685,26 @@ impl<'a> LoweringContext<'a> { } ExprKind::Yield(ref opt_expr) => { - self.is_generator = true; + match self.generator_kind { + Some(hir::GeneratorKind::Gen) => {}, + Some(hir::GeneratorKind::Async) => { + span_err!( + self.sess, + e.span, + E0727, + "`async` generators are not yet supported", + ); + self.sess.abort_if_errors(); + }, + None => { + self.generator_kind = Some(hir::GeneratorKind::Gen); + } + } let expr = opt_expr .as_ref() .map(|x| self.lower_expr(x)) .unwrap_or_else(|| self.expr_unit(e.span)); - hir::ExprKind::Yield(P(expr)) + hir::ExprKind::Yield(P(expr), hir::YieldSource::Yield) } ExprKind::Err => hir::ExprKind::Err, @@ -5754,19 +5776,23 @@ impl<'a> LoweringContext<'a> { // yield (); // } // } - if !self.is_async_body { - let mut err = struct_span_err!( - self.sess, - await_span, - E0728, - "`await` is only allowed inside `async` functions and blocks" - ); - err.span_label(await_span, "only allowed inside `async` functions and blocks"); - if let Some(item_sp) = self.current_item { - err.span_label(item_sp, "this is not `async`"); + match self.generator_kind { + Some(hir::GeneratorKind::Async) => {}, + Some(hir::GeneratorKind::Gen) | + None => { + let mut err = struct_span_err!( + self.sess, + await_span, + E0728, + "`await` is only allowed inside `async` functions and blocks" + ); + err.span_label(await_span, "only allowed inside `async` functions and blocks"); + if let Some(item_sp) = self.current_item { + err.span_label(item_sp, "this is not `async`"); + } + err.emit(); + return hir::ExprKind::Err; } - err.emit(); - return hir::ExprKind::Err; } let span = self.mark_span_with_reason( CompilerDesugaringKind::Await, @@ -5864,7 +5890,7 @@ impl<'a> LoweringContext<'a> { let unit = self.expr_unit(span); let yield_expr = P(self.expr( span, - hir::ExprKind::Yield(P(unit)), + hir::ExprKind::Yield(P(unit), hir::YieldSource::Await), ThinVec::new(), )); self.stmt(span, hir::StmtKind::Expr(yield_expr)) diff --git a/src/librustc/hir/mod.rs b/src/librustc/hir/mod.rs index 6ace4c4174b56..d570d34ec4e01 100644 --- a/src/librustc/hir/mod.rs +++ b/src/librustc/hir/mod.rs @@ -1306,7 +1306,7 @@ pub struct BodyId { /// /// - an `arguments` array containing the `(x, y)` pattern /// - a `value` containing the `x + y` expression (maybe wrapped in a block) -/// - `is_generator` would be false +/// - `generator_kind` would be `None` /// /// All bodies have an **owner**, which can be accessed via the HIR /// map using `body_owner_def_id()`. @@ -1314,7 +1314,7 @@ pub struct BodyId { pub struct Body { pub arguments: HirVec, pub value: Expr, - pub is_generator: bool, + pub generator_kind: Option, } impl Body { @@ -1325,6 +1325,26 @@ impl Body { } } +/// The type of source expression that caused this generator to be created. +// Not `IsAsync` because we want to eventually add support for `AsyncGen` +#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, HashStable, + RustcEncodable, RustcDecodable, Hash, Debug, Copy)] +pub enum GeneratorKind { + /// An `async` block or function. + Async, + /// A generator literal created via a `yield` inside a closure. + Gen, +} + +impl fmt::Display for GeneratorKind { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(match self { + GeneratorKind::Async => "`async` object", + GeneratorKind::Gen => "generator", + }) + } +} + #[derive(Copy, Clone, Debug)] pub enum BodyOwnerKind { /// Functions and methods. @@ -1531,8 +1551,8 @@ pub enum ExprKind { /// /// The final span is the span of the argument block `|...|`. /// - /// This may also be a generator literal, indicated by the final boolean, - /// in that case there is an `GeneratorClause`. + /// This may also be a generator literal or an `async block` as indicated by the + /// `Option`. Closure(CaptureClause, P, BodyId, Span, Option), /// A block (e.g., `'label: { ... }`). Block(P, Option