Lower let-else to HIR
This commit is contained in:
parent
120d46e255
commit
ae32e88909
2 changed files with 83 additions and 16 deletions
|
@ -1,6 +1,8 @@
|
||||||
use crate::{ImplTraitContext, ImplTraitPosition, LoweringContext};
|
use crate::{ImplTraitContext, ImplTraitPosition, LoweringContext};
|
||||||
use rustc_ast::{Block, BlockCheckMode, Local, Stmt, StmtKind};
|
use rustc_ast::{AttrVec, Block, BlockCheckMode, Expr, Local, LocalKind, Stmt, StmtKind};
|
||||||
use rustc_hir as hir;
|
use rustc_hir as hir;
|
||||||
|
use rustc_span::symbol::Ident;
|
||||||
|
use rustc_span::{sym, DesugaringKind};
|
||||||
|
|
||||||
use smallvec::SmallVec;
|
use smallvec::SmallVec;
|
||||||
|
|
||||||
|
@ -32,13 +34,24 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||||
let mut expr = None;
|
let mut expr = None;
|
||||||
while let [s, tail @ ..] = ast_stmts {
|
while let [s, tail @ ..] = ast_stmts {
|
||||||
match s.kind {
|
match s.kind {
|
||||||
StmtKind::Local(ref l) => {
|
StmtKind::Local(ref local) => {
|
||||||
let l = self.lower_local(l);
|
|
||||||
let hir_id = self.lower_node_id(s.id);
|
let hir_id = self.lower_node_id(s.id);
|
||||||
self.alias_attrs(hir_id, l.hir_id);
|
match &local.kind {
|
||||||
let kind = hir::StmtKind::Local(self.arena.alloc(l));
|
LocalKind::InitElse(init, els) => {
|
||||||
let span = self.lower_span(s.span);
|
let (s, e) = self.lower_let_else(hir_id, local, init, els, tail);
|
||||||
stmts.push(hir::Stmt { hir_id, kind, span });
|
stmts.push(s);
|
||||||
|
expr = Some(e);
|
||||||
|
// remaining statements are in let-else expression
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
let local = self.lower_local(local);
|
||||||
|
self.alias_attrs(hir_id, local.hir_id);
|
||||||
|
let kind = hir::StmtKind::Local(local);
|
||||||
|
let span = self.lower_span(s.span);
|
||||||
|
stmts.push(hir::Stmt { hir_id, kind, span });
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
StmtKind::Item(ref it) => {
|
StmtKind::Item(ref it) => {
|
||||||
stmts.extend(self.lower_item_id(it).into_iter().enumerate().map(
|
stmts.extend(self.lower_item_id(it).into_iter().enumerate().map(
|
||||||
|
@ -81,22 +94,18 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||||
(self.arena.alloc_from_iter(stmts), expr)
|
(self.arena.alloc_from_iter(stmts), expr)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn lower_local(&mut self, l: &Local) -> hir::Local<'hir> {
|
fn lower_local(&mut self, l: &Local) -> &'hir hir::Local<'hir> {
|
||||||
let ty = l
|
let ty = l
|
||||||
.ty
|
.ty
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|t| self.lower_ty(t, ImplTraitContext::Disallowed(ImplTraitPosition::Binding)));
|
.map(|t| self.lower_ty(t, ImplTraitContext::Disallowed(ImplTraitPosition::Binding)));
|
||||||
let init = l.kind.init().map(|init| self.lower_expr(init));
|
let init = l.kind.init().map(|init| self.lower_expr(init));
|
||||||
let hir_id = self.lower_node_id(l.id);
|
let hir_id = self.lower_node_id(l.id);
|
||||||
|
let pat = self.lower_pat(&l.pat);
|
||||||
|
let span = self.lower_span(l.span);
|
||||||
|
let source = hir::LocalSource::Normal;
|
||||||
self.lower_attrs(hir_id, &l.attrs);
|
self.lower_attrs(hir_id, &l.attrs);
|
||||||
hir::Local {
|
self.arena.alloc(hir::Local { hir_id, ty, pat, init, span, source })
|
||||||
hir_id,
|
|
||||||
ty,
|
|
||||||
pat: self.lower_pat(&l.pat),
|
|
||||||
init,
|
|
||||||
span: self.lower_span(l.span),
|
|
||||||
source: hir::LocalSource::Normal,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn lower_block_check_mode(&mut self, b: &BlockCheckMode) -> hir::BlockCheckMode {
|
fn lower_block_check_mode(&mut self, b: &BlockCheckMode) -> hir::BlockCheckMode {
|
||||||
|
@ -107,4 +116,60 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn lower_let_else(
|
||||||
|
&mut self,
|
||||||
|
stmt_hir_id: hir::HirId,
|
||||||
|
local: &Local,
|
||||||
|
init: &Expr,
|
||||||
|
els: &Block,
|
||||||
|
tail: &[Stmt],
|
||||||
|
) -> (hir::Stmt<'hir>, &'hir hir::Expr<'hir>) {
|
||||||
|
let ty = local
|
||||||
|
.ty
|
||||||
|
.as_ref()
|
||||||
|
.map(|t| self.lower_ty(t, ImplTraitContext::Disallowed(ImplTraitPosition::Binding)));
|
||||||
|
let span = self.lower_span(local.span);
|
||||||
|
let span = self.mark_span_with_reason(DesugaringKind::LetElse, span, None);
|
||||||
|
let init = Some(self.lower_expr(init));
|
||||||
|
let val = Ident::with_dummy_span(sym::val);
|
||||||
|
let (pat, val_id) =
|
||||||
|
self.pat_ident_binding_mode(span, val, hir::BindingAnnotation::Unannotated);
|
||||||
|
let local_hir_id = self.lower_node_id(local.id);
|
||||||
|
self.lower_attrs(local_hir_id, &local.attrs);
|
||||||
|
// first statement which basically exists for the type annotation
|
||||||
|
let stmt = {
|
||||||
|
let local = self.arena.alloc(hir::Local {
|
||||||
|
hir_id: local_hir_id,
|
||||||
|
ty,
|
||||||
|
pat,
|
||||||
|
init,
|
||||||
|
span,
|
||||||
|
source: hir::LocalSource::Normal,
|
||||||
|
});
|
||||||
|
let kind = hir::StmtKind::Local(local);
|
||||||
|
hir::Stmt { hir_id: stmt_hir_id, kind, span }
|
||||||
|
};
|
||||||
|
let let_expr = {
|
||||||
|
let scrutinee = self.expr_ident(span, val, val_id);
|
||||||
|
let let_kind = hir::ExprKind::Let(self.lower_pat(&local.pat), scrutinee, span);
|
||||||
|
self.arena.alloc(self.expr(span, let_kind, AttrVec::new()))
|
||||||
|
};
|
||||||
|
let then_expr = {
|
||||||
|
let (stmts, expr) = self.lower_stmts(tail);
|
||||||
|
let block = self.block_all(span, stmts, expr);
|
||||||
|
self.arena.alloc(self.expr_block(block, AttrVec::new()))
|
||||||
|
};
|
||||||
|
let else_expr = {
|
||||||
|
let block = self.lower_block(els, false);
|
||||||
|
self.arena.alloc(self.expr_block(block, AttrVec::new()))
|
||||||
|
};
|
||||||
|
self.alias_attrs(else_expr.hir_id, local_hir_id);
|
||||||
|
let if_expr = self.arena.alloc(hir::Expr {
|
||||||
|
hir_id: self.next_id(),
|
||||||
|
span,
|
||||||
|
kind: hir::ExprKind::If(let_expr, then_expr, Some(else_expr)),
|
||||||
|
});
|
||||||
|
(stmt, if_expr)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1097,6 +1097,7 @@ pub enum DesugaringKind {
|
||||||
Async,
|
Async,
|
||||||
Await,
|
Await,
|
||||||
ForLoop(ForLoopLoc),
|
ForLoop(ForLoopLoc),
|
||||||
|
LetElse,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A location in the desugaring of a `for` loop
|
/// A location in the desugaring of a `for` loop
|
||||||
|
@ -1117,6 +1118,7 @@ impl DesugaringKind {
|
||||||
DesugaringKind::TryBlock => "`try` block",
|
DesugaringKind::TryBlock => "`try` block",
|
||||||
DesugaringKind::OpaqueTy => "`impl Trait`",
|
DesugaringKind::OpaqueTy => "`impl Trait`",
|
||||||
DesugaringKind::ForLoop(_) => "`for` loop",
|
DesugaringKind::ForLoop(_) => "`for` loop",
|
||||||
|
DesugaringKind::LetElse => "`let...else`",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue