Skip to content

Commit 581abbf

Browse files
committed
Move some methods to block module
1 parent 89d2600 commit 581abbf

File tree

2 files changed

+111
-95
lines changed

2 files changed

+111
-95
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
use crate::{ImplTraitContext, ImplTraitPosition, LoweringContext};
2+
use rustc_ast::{Block, BlockCheckMode, Local, Stmt, StmtKind};
3+
use rustc_hir as hir;
4+
5+
use smallvec::{smallvec, SmallVec};
6+
7+
impl<'a, 'hir> LoweringContext<'a, 'hir> {
8+
pub(super) fn lower_block(
9+
&mut self,
10+
b: &Block,
11+
targeted_by_break: bool,
12+
) -> &'hir hir::Block<'hir> {
13+
self.arena.alloc(self.lower_block_noalloc(b, targeted_by_break))
14+
}
15+
16+
pub(super) fn lower_block_noalloc(
17+
&mut self,
18+
b: &Block,
19+
targeted_by_break: bool,
20+
) -> hir::Block<'hir> {
21+
let (stmts, expr) = match &*b.stmts {
22+
[stmts @ .., Stmt { kind: StmtKind::Expr(e), .. }] => (stmts, Some(&*e)),
23+
stmts => (stmts, None),
24+
};
25+
let stmts = self.arena.alloc_from_iter(stmts.iter().flat_map(|stmt| self.lower_stmt(stmt)));
26+
let expr = expr.map(|e| self.lower_expr(e));
27+
let rules = self.lower_block_check_mode(&b.rules);
28+
let hir_id = self.lower_node_id(b.id);
29+
30+
hir::Block { hir_id, stmts, expr, rules, span: self.lower_span(b.span), targeted_by_break }
31+
}
32+
33+
fn lower_stmt(&mut self, s: &Stmt) -> SmallVec<[hir::Stmt<'hir>; 1]> {
34+
let (hir_id, kind) = match s.kind {
35+
StmtKind::Local(ref l) => {
36+
let l = self.lower_local(l);
37+
let hir_id = self.lower_node_id(s.id);
38+
self.alias_attrs(hir_id, l.hir_id);
39+
return smallvec![hir::Stmt {
40+
hir_id,
41+
kind: hir::StmtKind::Local(self.arena.alloc(l)),
42+
span: self.lower_span(s.span),
43+
}];
44+
}
45+
StmtKind::Item(ref it) => {
46+
// Can only use the ID once.
47+
let mut id = Some(s.id);
48+
return self
49+
.lower_item_id(it)
50+
.into_iter()
51+
.map(|item_id| {
52+
let hir_id = id
53+
.take()
54+
.map(|id| self.lower_node_id(id))
55+
.unwrap_or_else(|| self.next_id());
56+
57+
hir::Stmt {
58+
hir_id,
59+
kind: hir::StmtKind::Item(item_id),
60+
span: self.lower_span(s.span),
61+
}
62+
})
63+
.collect();
64+
}
65+
StmtKind::Expr(ref e) => {
66+
let e = self.lower_expr(e);
67+
let hir_id = self.lower_node_id(s.id);
68+
self.alias_attrs(hir_id, e.hir_id);
69+
(hir_id, hir::StmtKind::Expr(e))
70+
}
71+
StmtKind::Semi(ref e) => {
72+
let e = self.lower_expr(e);
73+
let hir_id = self.lower_node_id(s.id);
74+
self.alias_attrs(hir_id, e.hir_id);
75+
(hir_id, hir::StmtKind::Semi(e))
76+
}
77+
StmtKind::Empty => return smallvec![],
78+
StmtKind::MacCall(..) => panic!("shouldn't exist here"),
79+
};
80+
smallvec![hir::Stmt { hir_id, kind, span: self.lower_span(s.span) }]
81+
}
82+
83+
fn lower_local(&mut self, l: &Local) -> hir::Local<'hir> {
84+
let ty = l
85+
.ty
86+
.as_ref()
87+
.map(|t| self.lower_ty(t, ImplTraitContext::Disallowed(ImplTraitPosition::Binding)));
88+
let init = l.kind.init().map(|init| self.lower_expr(init));
89+
let hir_id = self.lower_node_id(l.id);
90+
self.lower_attrs(hir_id, &l.attrs);
91+
hir::Local {
92+
hir_id,
93+
ty,
94+
pat: self.lower_pat(&l.pat),
95+
init,
96+
span: self.lower_span(l.span),
97+
source: hir::LocalSource::Normal,
98+
}
99+
}
100+
101+
fn lower_block_check_mode(&mut self, b: &BlockCheckMode) -> hir::BlockCheckMode {
102+
match *b {
103+
BlockCheckMode::Default => hir::BlockCheckMode::DefaultBlock,
104+
BlockCheckMode::Unsafe(u) => {
105+
hir::BlockCheckMode::UnsafeBlock(self.lower_unsafe_source(u))
106+
}
107+
}
108+
}
109+
}

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 2 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ use rustc_span::symbol::{kw, sym, Ident, Symbol};
6464
use rustc_span::{Span, DUMMY_SP};
6565
use rustc_target::spec::abi::Abi;
6666

67-
use smallvec::{smallvec, SmallVec};
67+
use smallvec::SmallVec;
6868
use std::collections::BTreeMap;
6969
use std::mem;
7070
use tracing::{debug, trace};
@@ -77,6 +77,7 @@ macro_rules! arena_vec {
7777
}
7878

7979
mod asm;
80+
mod block;
8081
mod expr;
8182
mod item;
8283
mod pat;
@@ -1793,24 +1794,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17931794
)
17941795
}
17951796

1796-
fn lower_local(&mut self, l: &Local) -> hir::Local<'hir> {
1797-
let ty = l
1798-
.ty
1799-
.as_ref()
1800-
.map(|t| self.lower_ty(t, ImplTraitContext::Disallowed(ImplTraitPosition::Binding)));
1801-
let init = l.kind.init().map(|init| self.lower_expr(init));
1802-
let hir_id = self.lower_node_id(l.id);
1803-
self.lower_attrs(hir_id, &l.attrs);
1804-
hir::Local {
1805-
hir_id,
1806-
ty,
1807-
pat: self.lower_pat(&l.pat),
1808-
init,
1809-
span: self.lower_span(l.span),
1810-
source: hir::LocalSource::Normal,
1811-
}
1812-
}
1813-
18141797
fn lower_fn_params_to_names(&mut self, decl: &FnDecl) -> &'hir [Ident] {
18151798
// Skip the `...` (`CVarArgs`) trailing arguments from the AST,
18161799
// as they are not explicit in HIR/Ty function signatures.
@@ -2396,23 +2379,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23962379
bounds.iter().map(move |bound| self.lower_param_bound(bound, itctx.reborrow()))
23972380
}
23982381

2399-
fn lower_block(&mut self, b: &Block, targeted_by_break: bool) -> &'hir hir::Block<'hir> {
2400-
self.arena.alloc(self.lower_block_noalloc(b, targeted_by_break))
2401-
}
2402-
2403-
fn lower_block_noalloc(&mut self, b: &Block, targeted_by_break: bool) -> hir::Block<'hir> {
2404-
let (stmts, expr) = match &*b.stmts {
2405-
[stmts @ .., Stmt { kind: StmtKind::Expr(e), .. }] => (stmts, Some(&*e)),
2406-
stmts => (stmts, None),
2407-
};
2408-
let stmts = self.arena.alloc_from_iter(stmts.iter().flat_map(|stmt| self.lower_stmt(stmt)));
2409-
let expr = expr.map(|e| self.lower_expr(e));
2410-
let rules = self.lower_block_check_mode(&b.rules);
2411-
let hir_id = self.lower_node_id(b.id);
2412-
2413-
hir::Block { hir_id, stmts, expr, rules, span: self.lower_span(b.span), targeted_by_break }
2414-
}
2415-
24162382
/// Lowers a block directly to an expression, presuming that it
24172383
/// has no attributes and is not targeted by a `break`.
24182384
fn lower_block_expr(&mut self, b: &Block) -> hir::Expr<'hir> {
@@ -2427,65 +2393,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24272393
})
24282394
}
24292395

2430-
fn lower_stmt(&mut self, s: &Stmt) -> SmallVec<[hir::Stmt<'hir>; 1]> {
2431-
let (hir_id, kind) = match s.kind {
2432-
StmtKind::Local(ref l) => {
2433-
let l = self.lower_local(l);
2434-
let hir_id = self.lower_node_id(s.id);
2435-
self.alias_attrs(hir_id, l.hir_id);
2436-
return smallvec![hir::Stmt {
2437-
hir_id,
2438-
kind: hir::StmtKind::Local(self.arena.alloc(l)),
2439-
span: self.lower_span(s.span),
2440-
}];
2441-
}
2442-
StmtKind::Item(ref it) => {
2443-
// Can only use the ID once.
2444-
let mut id = Some(s.id);
2445-
return self
2446-
.lower_item_id(it)
2447-
.into_iter()
2448-
.map(|item_id| {
2449-
let hir_id = id
2450-
.take()
2451-
.map(|id| self.lower_node_id(id))
2452-
.unwrap_or_else(|| self.next_id());
2453-
2454-
hir::Stmt {
2455-
hir_id,
2456-
kind: hir::StmtKind::Item(item_id),
2457-
span: self.lower_span(s.span),
2458-
}
2459-
})
2460-
.collect();
2461-
}
2462-
StmtKind::Expr(ref e) => {
2463-
let e = self.lower_expr(e);
2464-
let hir_id = self.lower_node_id(s.id);
2465-
self.alias_attrs(hir_id, e.hir_id);
2466-
(hir_id, hir::StmtKind::Expr(e))
2467-
}
2468-
StmtKind::Semi(ref e) => {
2469-
let e = self.lower_expr(e);
2470-
let hir_id = self.lower_node_id(s.id);
2471-
self.alias_attrs(hir_id, e.hir_id);
2472-
(hir_id, hir::StmtKind::Semi(e))
2473-
}
2474-
StmtKind::Empty => return smallvec![],
2475-
StmtKind::MacCall(..) => panic!("shouldn't exist here"),
2476-
};
2477-
smallvec![hir::Stmt { hir_id, kind, span: self.lower_span(s.span) }]
2478-
}
2479-
2480-
fn lower_block_check_mode(&mut self, b: &BlockCheckMode) -> hir::BlockCheckMode {
2481-
match *b {
2482-
BlockCheckMode::Default => hir::BlockCheckMode::DefaultBlock,
2483-
BlockCheckMode::Unsafe(u) => {
2484-
hir::BlockCheckMode::UnsafeBlock(self.lower_unsafe_source(u))
2485-
}
2486-
}
2487-
}
2488-
24892396
fn lower_unsafe_source(&mut self, u: UnsafeSource) -> hir::UnsafeSource {
24902397
match u {
24912398
CompilerGenerated => hir::UnsafeSource::CompilerGenerated,

0 commit comments

Comments
 (0)