Skip to content

Commit 41c6bb1

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 8b57be1 commit 41c6bb1

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
@@ -2224,10 +2224,17 @@ impl<'a> LoweringContext<'a> {
22242224
init: l.init.as_ref().map(|e| P(self.lower_expr(e))),
22252225
span: l.span,
22262226
attrs: l.attrs.clone(),
2227-
source: hir::LocalSource::Normal,
2227+
source: self.lower_local_source(l.source),
22282228
}, ids)
22292229
}
22302230

2231+
fn lower_local_source(&mut self, ls: LocalSource) -> hir::LocalSource {
2232+
match ls {
2233+
LocalSource::Normal => hir::LocalSource::Normal,
2234+
LocalSource::AsyncFn => hir::LocalSource::AsyncFn,
2235+
}
2236+
}
2237+
22312238
fn lower_mutability(&mut self, m: Mutability) -> hir::Mutability {
22322239
match m {
22332240
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
@@ -888,6 +888,17 @@ pub struct Local {
888888
pub id: NodeId,
889889
pub span: Span,
890890
pub attrs: ThinVec<Attribute>,
891+
/// Origin of this local variable.
892+
pub source: LocalSource,
893+
}
894+
895+
#[derive(Clone, Copy, RustcEncodable, RustcDecodable, Debug)]
896+
pub enum LocalSource {
897+
/// Local was parsed from source.
898+
Normal,
899+
/// Within `ast::IsAsync::Async`, a local is generated that will contain the moved arguments
900+
/// of an `async fn`.
901+
AsyncFn,
891902
}
892903

893904
/// 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
@@ -511,13 +515,17 @@ pub fn noop_visit_parenthesized_parameter_data<T: MutVisitor>(args: &mut Parenth
511515
}
512516

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

523531
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};
@@ -5029,6 +5029,7 @@ impl<'a> Parser<'a> {
50295029
id: ast::DUMMY_NODE_ID,
50305030
span: lo.to(hi),
50315031
attrs,
5032+
source: LocalSource::Normal,
50325033
}))
50335034
}
50345035

src/libsyntax_ext/deriving/debug.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ fn stmt_let_undescore(cx: &mut ExtCtxt<'_>, sp: Span, expr: P<ast::Expr>) -> ast
128128
id: ast::DUMMY_NODE_ID,
129129
span: sp,
130130
attrs: ThinVec::new(),
131+
source: ast::LocalSource::Normal,
131132
});
132133
ast::Stmt {
133134
id: ast::DUMMY_NODE_ID,

0 commit comments

Comments
 (0)