Skip to content

Commit d1be529

Browse files
committed
Introduce LocalSource into the AST.
This will be used to keep track of the origin of a local in the AST. In particular, it will be used by `async fn` lowering for the locals in `let <pat>: <ty> = __arg0;` statements.
1 parent 5ce3540 commit d1be529

File tree

9 files changed

+46
-4
lines changed

9 files changed

+46
-4
lines changed

src/librustc/hir/lowering.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2025,10 +2025,17 @@ impl<'a> LoweringContext<'a> {
20252025
init: l.init.as_ref().map(|e| P(self.lower_expr(e))),
20262026
span: l.span,
20272027
attrs: l.attrs.clone(),
2028-
source: hir::LocalSource::Normal,
2028+
source: self.lower_local_source(l.source),
20292029
}, ids)
20302030
}
20312031

2032+
fn lower_local_source(&mut self, ls: LocalSource) -> hir::LocalSource {
2033+
match ls {
2034+
LocalSource::Normal => hir::LocalSource::Normal,
2035+
LocalSource::AsyncFn => hir::LocalSource::AsyncFn,
2036+
}
2037+
}
2038+
20322039
fn lower_mutability(&mut self, m: Mutability) -> hir::Mutability {
20332040
match m {
20342041
Mutability::Mutable => hir::MutMutable,

src/librustc/hir/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,6 +1583,17 @@ pub enum LocalSource {
15831583
Normal,
15841584
/// A desugared `for _ in _ { .. }` loop.
15851585
ForLoopDesugar,
1586+
/// When lowering async functions, we create locals within the `async move` so that
1587+
/// all arguments are dropped after the future is polled.
1588+
///
1589+
/// ```ignore (pseudo-Rust)
1590+
/// async fn foo(<pattern> @ x: Type) {
1591+
/// async move {
1592+
/// let <pattern> = x;
1593+
/// }
1594+
/// }
1595+
/// ```
1596+
AsyncFn,
15861597
}
15871598

15881599
/// Hints at the original code for a `match _ { .. }`.

src/librustc/ich/impls_hir.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,4 +435,3 @@ impl<'hir> HashStable<StableHashingContext<'hir>> for attr::OptimizeAttr {
435435
mem::discriminant(self).hash_stable(hcx, hasher);
436436
}
437437
}
438-

src/librustc_mir/hair/pattern/check_match.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ impl<'a, 'tcx> Visitor<'tcx> for MatchVisitor<'a, 'tcx> {
7676
self.check_irrefutable(&loc.pat, match loc.source {
7777
hir::LocalSource::Normal => "local binding",
7878
hir::LocalSource::ForLoopDesugar => "`for` loop binding",
79+
hir::LocalSource::AsyncFn => "async fn binding",
7980
});
8081

8182
// Check legality of move bindings and `@` patterns.

src/libsyntax/ast.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,17 @@ pub struct Local {
876876
pub id: NodeId,
877877
pub span: Span,
878878
pub attrs: ThinVec<Attribute>,
879+
/// Origin of this local variable.
880+
pub source: LocalSource,
881+
}
882+
883+
#[derive(Clone, Copy, RustcEncodable, RustcDecodable, Debug)]
884+
pub enum LocalSource {
885+
/// Local was parsed from source.
886+
Normal,
887+
/// Within `ast::IsAsync::Async`, a local is generated that will contain the moved arguments
888+
/// of an `async fn`.
889+
AsyncFn,
879890
}
880891

881892
/// An arm of a 'match'.

src/libsyntax/ext/build.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
526526
id: ast::DUMMY_NODE_ID,
527527
span: sp,
528528
attrs: ThinVec::new(),
529+
source: ast::LocalSource::Normal,
529530
});
530531
ast::Stmt {
531532
id: ast::DUMMY_NODE_ID,
@@ -554,6 +555,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
554555
id: ast::DUMMY_NODE_ID,
555556
span: sp,
556557
attrs: ThinVec::new(),
558+
source: ast::LocalSource::Normal,
557559
});
558560
ast::Stmt {
559561
id: ast::DUMMY_NODE_ID,
@@ -571,6 +573,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
571573
id: ast::DUMMY_NODE_ID,
572574
span,
573575
attrs: ThinVec::new(),
576+
source: ast::LocalSource::Normal,
574577
});
575578
ast::Stmt {
576579
id: ast::DUMMY_NODE_ID,

src/libsyntax/mut_visit.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@ pub trait MutVisitor: Sized {
208208
noop_visit_local(l, self);
209209
}
210210

211+
fn visit_local_source(&mut self, l: &mut LocalSource) {
212+
noop_visit_local_source(l, self);
213+
}
214+
211215
fn visit_mac(&mut self, _mac: &mut Mac) {
212216
panic!("visit_mac disabled by default");
213217
// N.B., see note about macros above. If you really want a visitor that
@@ -510,13 +514,17 @@ pub fn noop_visit_parenthesized_parameter_data<T: MutVisitor>(args: &mut Parenth
510514
}
511515

512516
pub fn noop_visit_local<T: MutVisitor>(local: &mut P<Local>, vis: &mut T) {
513-
let Local { id, pat, ty, init, span, attrs } = local.deref_mut();
517+
let Local { id, pat, ty, init, span, attrs, source } = local.deref_mut();
514518
vis.visit_id(id);
515519
vis.visit_pat(pat);
516520
visit_opt(ty, |ty| vis.visit_ty(ty));
517521
visit_opt(init, |init| vis.visit_expr(init));
518522
vis.visit_span(span);
519523
visit_thin_attrs(attrs, vis);
524+
vis.visit_local_source(source);
525+
}
526+
527+
pub fn noop_visit_local_source<T: MutVisitor>(_local_source: &mut LocalSource, _vis: &mut T) {
520528
}
521529

522530
pub fn noop_visit_attribute<T: MutVisitor>(attr: &mut Attribute, vis: &mut T) {

src/libsyntax/parse/parser.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::ast::{GenericParam, GenericParamKind};
1414
use crate::ast::GenericArg;
1515
use crate::ast::{Ident, ImplItem, IsAsync, IsAuto, Item, ItemKind};
1616
use crate::ast::{Label, Lifetime, Lit, LitKind};
17-
use crate::ast::Local;
17+
use crate::ast::{Local, LocalSource};
1818
use crate::ast::MacStmtStyle;
1919
use crate::ast::{Mac, Mac_, MacDelimiter};
2020
use crate::ast::{MutTy, Mutability};
@@ -4879,6 +4879,7 @@ impl<'a> Parser<'a> {
48794879
id: ast::DUMMY_NODE_ID,
48804880
span: lo.to(hi),
48814881
attrs,
4882+
source: LocalSource::Normal,
48824883
}))
48834884
}
48844885

src/libsyntax_ext/deriving/debug.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ fn stmt_let_undescore(cx: &mut ExtCtxt<'_>, sp: Span, expr: P<ast::Expr>) -> ast
132132
id: ast::DUMMY_NODE_ID,
133133
span: sp,
134134
attrs: ThinVec::new(),
135+
source: ast::LocalSource::Normal,
135136
});
136137
ast::Stmt {
137138
id: ast::DUMMY_NODE_ID,

0 commit comments

Comments
 (0)