Skip to content

Commit 1cd30e7

Browse files
move else block into the Local struct
1 parent 6c529de commit 1cd30e7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+138
-131
lines changed

compiler/rustc_ast_lowering/src/block.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
5252
};
5353
let local = self.lower_local(local);
5454
self.alias_attrs(hir_id, local.hir_id);
55-
let kind = hir::StmtKind::Local(local, els);
55+
let kind = hir::StmtKind::Local(local);
5656
let span = self.lower_span(s.span);
5757
stmts.push(hir::Stmt { hir_id, kind, span });
5858
}
@@ -105,10 +105,24 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
105105
let init = l.kind.init().map(|init| self.lower_expr(init));
106106
let hir_id = self.lower_node_id(l.id);
107107
let pat = self.lower_pat(&l.pat);
108+
let els = if let LocalKind::InitElse(_, els) = &l.kind {
109+
if !self.sess.features_untracked().let_else {
110+
feature_err(
111+
&self.sess.parse_sess,
112+
sym::let_else,
113+
l.span,
114+
"`let...else` statements are unstable",
115+
)
116+
.emit();
117+
}
118+
Some(self.lower_block(els, false))
119+
} else {
120+
None
121+
};
108122
let span = self.lower_span(l.span);
109123
let source = hir::LocalSource::Normal;
110124
self.lower_attrs(hir_id, &l.attrs);
111-
self.arena.alloc(hir::Local { hir_id, ty, pat, init, span, source })
125+
self.arena.alloc(hir::Local { hir_id, ty, pat, init, els, span, source })
112126
}
113127

114128
fn lower_block_check_mode(&mut self, b: &BlockCheckMode) -> hir::BlockCheckMode {

compiler/rustc_ast_lowering/src/index.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,10 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
284284
});
285285
}
286286

287-
fn visit_local(&mut self, l: &'hir Local<'hir>, e: Option<&'hir Block<'hir>>) {
287+
fn visit_local(&mut self, l: &'hir Local<'hir>) {
288288
self.insert(l.span, l.hir_id, Node::Local(l));
289289
self.with_parent(l.hir_id, |this| {
290-
intravisit::walk_local(this, l, e);
290+
intravisit::walk_local(this, l);
291291
})
292292
}
293293

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2146,8 +2146,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21462146
debug_assert!(!a.is_empty());
21472147
self.attrs.insert(hir_id.local_id, a);
21482148
}
2149-
let local = hir::Local { hir_id, init, pat, source, span: self.lower_span(span), ty: None };
2150-
self.stmt(span, hir::StmtKind::Local(self.arena.alloc(local), None))
2149+
let local = hir::Local {
2150+
hir_id,
2151+
init,
2152+
pat,
2153+
els: None,
2154+
source,
2155+
span: self.lower_span(span),
2156+
ty: None,
2157+
};
2158+
self.stmt(span, hir::StmtKind::Local(self.arena.alloc(local)))
21512159
}
21522160

21532161
fn block_expr(&mut self, expr: &'hir hir::Expr<'hir>) -> &'hir hir::Block<'hir> {

compiler/rustc_hir/src/hir.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1297,7 +1297,7 @@ pub struct Stmt<'hir> {
12971297
pub enum StmtKind<'hir> {
12981298
/// A local (`let`) binding.
12991299
/// FIXME: bundle the last two components into another `struct`
1300-
Local(&'hir Local<'hir>, Option<&'hir Block<'hir>>),
1300+
Local(&'hir Local<'hir>),
13011301

13021302
/// An item binding.
13031303
Item(ItemId),
@@ -1317,6 +1317,8 @@ pub struct Local<'hir> {
13171317
pub ty: Option<&'hir Ty<'hir>>,
13181318
/// Initializer expression to set the value, if any.
13191319
pub init: Option<&'hir Expr<'hir>>,
1320+
/// Else block for a `let...else` binding.
1321+
pub els: Option<&'hir Block<'hir>>,
13201322
pub hir_id: HirId,
13211323
pub span: Span,
13221324
/// Can be `ForLoopDesugar` if the `let` statement is part of a `for` loop

compiler/rustc_hir/src/intravisit.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,8 @@ pub trait Visitor<'v>: Sized {
310310
fn visit_foreign_item(&mut self, i: &'v ForeignItem<'v>) {
311311
walk_foreign_item(self, i)
312312
}
313-
fn visit_local(&mut self, l: &'v Local<'v>, els: Option<&'v Block<'v>>) {
314-
walk_local(self, l, els)
313+
fn visit_local(&mut self, l: &'v Local<'v>) {
314+
walk_local(self, l)
315315
}
316316
fn visit_block(&mut self, b: &'v Block<'v>) {
317317
walk_block(self, b)
@@ -466,17 +466,13 @@ pub fn walk_body<'v, V: Visitor<'v>>(visitor: &mut V, body: &'v Body<'v>) {
466466
visitor.visit_expr(&body.value);
467467
}
468468

469-
pub fn walk_local<'v, V: Visitor<'v>>(
470-
visitor: &mut V,
471-
local: &'v Local<'v>,
472-
els: Option<&'v Block<'v>>,
473-
) {
469+
pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local<'v>) {
474470
// Intentionally visiting the expr first - the initialization expr
475471
// dominates the local's definition.
476472
walk_list!(visitor, visit_expr, &local.init);
477473
visitor.visit_id(local.hir_id);
478474
visitor.visit_pat(&local.pat);
479-
if let Some(els) = els {
475+
if let Some(els) = local.els {
480476
visitor.visit_block(els);
481477
}
482478
walk_list!(visitor, visit_ty, &local.ty);
@@ -1063,7 +1059,7 @@ pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block<'v>) {
10631059
pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt<'v>) {
10641060
visitor.visit_id(statement.hir_id);
10651061
match &statement.kind {
1066-
StmtKind::Local(ref local, els) => visitor.visit_local(local, *els),
1062+
StmtKind::Local(ref local) => visitor.visit_local(local),
10671063
StmtKind::Item(item) => visitor.visit_nested_item(*item),
10681064
StmtKind::Expr(ref expression) | StmtKind::Semi(ref expression) => {
10691065
visitor.visit_expr(expression)

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -915,8 +915,8 @@ impl<'a> State<'a> {
915915
pub fn print_stmt(&mut self, st: &hir::Stmt<'_>) {
916916
self.maybe_print_comment(st.span.lo());
917917
match st.kind {
918-
hir::StmtKind::Local(loc, els) => {
919-
self.print_local(loc.init, els, |this| this.print_local_decl(loc));
918+
hir::StmtKind::Local(loc) => {
919+
self.print_local(loc.init, loc.els, |this| this.print_local_decl(loc));
920920
}
921921
hir::StmtKind::Item(item) => self.ann.nested(self, Nested::Item(item)),
922922
hir::StmtKind::Expr(expr) => {
@@ -2305,7 +2305,7 @@ fn expr_requires_semi_to_be_stmt(e: &hir::Expr<'_>) -> bool {
23052305
/// seen the semicolon, and thus don't need another.
23062306
fn stmt_ends_with_semi(stmt: &hir::StmtKind<'_>) -> bool {
23072307
match *stmt {
2308-
hir::StmtKind::Local(_, _) => true,
2308+
hir::StmtKind::Local(_) => true,
23092309
hir::StmtKind::Item(_) => false,
23102310
hir::StmtKind::Expr(e) => expr_requires_semi_to_be_stmt(e),
23112311
hir::StmtKind::Semi(..) => false,

compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::infer::type_variable::TypeVariableOriginKind;
22
use crate::infer::InferCtxt;
3-
use hir::{Block, LocalSource};
3+
use hir::LocalSource;
44
use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder, ErrorGuaranteed};
55
use rustc_hir as hir;
66
use rustc_hir::def::Res;
@@ -953,8 +953,8 @@ impl<'a, 'tcx> Visitor<'tcx> for FindInferSourceVisitor<'a, 'tcx> {
953953
self.infcx.tcx.hir()
954954
}
955955

956-
fn visit_local(&mut self, local: &'tcx Local<'tcx>, els: Option<&'tcx Block<'tcx>>) {
957-
intravisit::walk_local(self, local, els);
956+
fn visit_local(&mut self, local: &'tcx Local<'tcx>) {
957+
intravisit::walk_local(self, local);
958958

959959
if let Some(ty) = self.opt_node_type(local.hir_id) {
960960
if self.generic_arg_contains_target(ty.into()) {

compiler/rustc_lint/src/late.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,10 @@ impl<'tcx, T: LateLintPass<'tcx>> hir_visit::Visitor<'tcx> for LateContextAndPas
251251
}
252252
}
253253

254-
fn visit_local(&mut self, l: &'tcx hir::Local<'tcx>, e: Option<&'tcx hir::Block<'tcx>>) {
254+
fn visit_local(&mut self, l: &'tcx hir::Local<'tcx>) {
255255
self.with_lint_attrs(l.hir_id, |cx| {
256-
lint_callback!(cx, check_local, l, e);
257-
hir_visit::walk_local(cx, l, e);
256+
lint_callback!(cx, check_local, l);
257+
hir_visit::walk_local(cx, l);
258258
})
259259
}
260260

compiler/rustc_lint/src/levels.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -783,9 +783,9 @@ impl<'tcx> intravisit::Visitor<'tcx> for LintLevelMapBuilder<'tcx> {
783783
})
784784
}
785785

786-
fn visit_local(&mut self, l: &'tcx hir::Local<'tcx>, e: Option<&'tcx hir::Block<'tcx>>) {
786+
fn visit_local(&mut self, l: &'tcx hir::Local<'tcx>) {
787787
self.with_lint_attrs(l.hir_id, |builder| {
788-
intravisit::walk_local(builder, l, e);
788+
intravisit::walk_local(builder, l);
789789
})
790790
}
791791

compiler/rustc_lint/src/passes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ macro_rules! late_lint_methods {
2424
fn check_foreign_item_post(a: &$hir hir::ForeignItem<$hir>);
2525
fn check_item(a: &$hir hir::Item<$hir>);
2626
fn check_item_post(a: &$hir hir::Item<$hir>);
27-
fn check_local(a: &$hir hir::Local<$hir>, b: Option<&$hir hir::Block<$hir>>);
27+
fn check_local(a: &$hir hir::Local<$hir>);
2828
fn check_block(a: &$hir hir::Block<$hir>);
2929
fn check_block_post(a: &$hir hir::Block<$hir>);
3030
fn check_stmt(a: &$hir hir::Stmt<$hir>);

0 commit comments

Comments
 (0)