Skip to content

Commit 146cb8e

Browse files
committed
or-patterns: remove hack from lowering.
1 parent 60895fd commit 146cb8e

File tree

3 files changed

+19
-63
lines changed

3 files changed

+19
-63
lines changed

src/librustc/hir/lowering.rs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -434,35 +434,6 @@ impl<'a> LoweringContext<'a> {
434434
visit::walk_pat(self, p)
435435
}
436436

437-
// HACK(or_patterns; Centril | dlrobertson): Avoid creating
438-
// HIR nodes for `PatKind::Or` for the top level of a `ast::Arm`.
439-
// This is a temporary hack that should go away once we push down
440-
// `arm.pats: HirVec<P<Pat>>` -> `arm.pat: P<Pat>` to HIR. // Centril
441-
fn visit_arm(&mut self, arm: &'tcx Arm) {
442-
match &arm.pat.node {
443-
PatKind::Or(pats) => pats.iter().for_each(|p| self.visit_pat(p)),
444-
_ => self.visit_pat(&arm.pat),
445-
}
446-
walk_list!(self, visit_expr, &arm.guard);
447-
self.visit_expr(&arm.body);
448-
walk_list!(self, visit_attribute, &arm.attrs);
449-
}
450-
451-
// HACK(or_patterns; Centril | dlrobertson): Same as above. // Centril
452-
fn visit_expr(&mut self, e: &'tcx Expr) {
453-
if let ExprKind::Let(pat, scrutinee) = &e.node {
454-
walk_list!(self, visit_attribute, e.attrs.iter());
455-
match &pat.node {
456-
PatKind::Or(pats) => pats.iter().for_each(|p| self.visit_pat(p)),
457-
_ => self.visit_pat(&pat),
458-
}
459-
self.visit_expr(scrutinee);
460-
self.visit_expr_post(e);
461-
return;
462-
}
463-
visit::walk_expr(self, e)
464-
}
465-
466437
fn visit_item(&mut self, item: &'tcx Item) {
467438
let hir_id = self.lctx.allocate_hir_id_counter(item.id);
468439

src/librustc/hir/lowering/expr.rs

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -250,14 +250,14 @@ impl LoweringContext<'_> {
250250
// 4. The return type of the block is `bool` which seems like what the user wanted.
251251
let scrutinee = self.lower_expr(scrutinee);
252252
let then_arm = {
253-
let pat = self.lower_pat_top_hack(pat);
253+
let pat = self.lower_pat(pat);
254254
let expr = self.expr_bool(span, true);
255255
self.arm(pat, P(expr))
256256
};
257257
let else_arm = {
258258
let pat = self.pat_wild(span);
259259
let expr = self.expr_bool(span, false);
260-
self.arm(hir_vec![pat], P(expr))
260+
self.arm(pat, P(expr))
261261
};
262262
hir::ExprKind::Match(
263263
P(scrutinee),
@@ -281,7 +281,7 @@ impl LoweringContext<'_> {
281281
None => (self.expr_block_empty(span), false),
282282
Some(els) => (self.lower_expr(els), true),
283283
};
284-
let else_arm = self.arm(hir_vec![else_pat], P(else_expr));
284+
let else_arm = self.arm(else_pat, P(else_expr));
285285

286286
// Handle then + scrutinee:
287287
let then_blk = self.lower_block(then, false);
@@ -290,7 +290,7 @@ impl LoweringContext<'_> {
290290
// `<pat> => <then>`:
291291
ExprKind::Let(ref pat, ref scrutinee) => {
292292
let scrutinee = self.lower_expr(scrutinee);
293-
let pat = self.lower_pat_top_hack(pat);
293+
let pat = self.lower_pat(pat);
294294
(pat, scrutinee, hir::MatchSource::IfLetDesugar { contains_else_clause })
295295
}
296296
// `true => <then>`:
@@ -307,7 +307,7 @@ impl LoweringContext<'_> {
307307
// let temporaries live outside of `cond`.
308308
let cond = self.expr_drop_temps(span_block, P(cond), ThinVec::new());
309309
let pat = self.pat_bool(span, true);
310-
(hir_vec![pat], cond, hir::MatchSource::IfDesugar { contains_else_clause })
310+
(pat, cond, hir::MatchSource::IfDesugar { contains_else_clause })
311311
}
312312
};
313313
let then_arm = self.arm(then_pat, P(then_expr));
@@ -331,7 +331,7 @@ impl LoweringContext<'_> {
331331
let else_arm = {
332332
let else_pat = self.pat_wild(span);
333333
let else_expr = self.expr_break(span, ThinVec::new());
334-
self.arm(hir_vec![else_pat], else_expr)
334+
self.arm(else_pat, else_expr)
335335
};
336336

337337
// Handle then + scrutinee:
@@ -348,7 +348,7 @@ impl LoweringContext<'_> {
348348
// }
349349
// }
350350
let scrutinee = self.with_loop_condition_scope(|t| t.lower_expr(scrutinee));
351-
let pat = self.lower_pat_top_hack(pat);
351+
let pat = self.lower_pat(pat);
352352
(pat, scrutinee, hir::MatchSource::WhileLetDesugar, hir::LoopSource::WhileLet)
353353
}
354354
_ => {
@@ -376,7 +376,7 @@ impl LoweringContext<'_> {
376376
let cond = self.expr_drop_temps(span_block, P(cond), ThinVec::new());
377377
// `true => <then>`:
378378
let pat = self.pat_bool(span, true);
379-
(hir_vec![pat], cond, hir::MatchSource::WhileDesugar, hir::LoopSource::While)
379+
(pat, cond, hir::MatchSource::WhileDesugar, hir::LoopSource::While)
380380
}
381381
};
382382
let then_arm = self.arm(then_pat, P(then_expr));
@@ -429,7 +429,7 @@ impl LoweringContext<'_> {
429429
hir::Arm {
430430
hir_id: self.next_id(),
431431
attrs: self.lower_attrs(&arm.attrs),
432-
pats: self.lower_pat_top_hack(&arm.pat),
432+
pat: self.lower_pat(&arm.pat),
433433
guard: match arm.guard {
434434
Some(ref x) => Some(hir::Guard::If(P(self.lower_expr(x)))),
435435
_ => None,
@@ -439,16 +439,6 @@ impl LoweringContext<'_> {
439439
}
440440
}
441441

442-
/// HACK(or_patterns; Centril | dlrobertson): For now we don't push down top level or-patterns
443-
/// `p | q` into `hir::PatKind::Or(...)` as post-lowering bits of the compiler are not ready
444-
/// to deal with it. This should by fixed by pushing it down to HIR and then HAIR.
445-
fn lower_pat_top_hack(&mut self, pat: &Pat) -> HirVec<P<hir::Pat>> {
446-
match pat.node {
447-
PatKind::Or(ref ps) => ps.iter().map(|x| self.lower_pat(x)).collect(),
448-
_ => hir_vec![self.lower_pat(pat)],
449-
}
450-
}
451-
452442
pub(super) fn make_async_expr(
453443
&mut self,
454444
capture_clause: CaptureBy,
@@ -597,7 +587,7 @@ impl LoweringContext<'_> {
597587
);
598588
P(this.expr(await_span, expr_break, ThinVec::new()))
599589
});
600-
self.arm(hir_vec![ready_pat], break_x)
590+
self.arm(ready_pat, break_x)
601591
};
602592

603593
// `::std::task::Poll::Pending => {}`
@@ -608,7 +598,7 @@ impl LoweringContext<'_> {
608598
hir_vec![],
609599
);
610600
let empty_block = P(self.expr_block_empty(span));
611-
self.arm(hir_vec![pending_pat], empty_block)
601+
self.arm(pending_pat, empty_block)
612602
};
613603

614604
let inner_match_stmt = {
@@ -650,7 +640,7 @@ impl LoweringContext<'_> {
650640
});
651641

652642
// mut pinned => loop { ... }
653-
let pinned_arm = self.arm(hir_vec![pinned_pat], loop_expr);
643+
let pinned_arm = self.arm(pinned_pat, loop_expr);
654644

655645
// match <expr> {
656646
// mut pinned => loop { .. }
@@ -1084,15 +1074,15 @@ impl LoweringContext<'_> {
10841074
ThinVec::new(),
10851075
));
10861076
let some_pat = self.pat_some(pat.span, val_pat);
1087-
self.arm(hir_vec![some_pat], assign)
1077+
self.arm(some_pat, assign)
10881078
};
10891079

10901080
// `::std::option::Option::None => break`
10911081
let break_arm = {
10921082
let break_expr =
10931083
self.with_loop_scope(e.id, |this| this.expr_break(e.span, ThinVec::new()));
10941084
let pat = self.pat_none(e.span);
1095-
self.arm(hir_vec![pat], break_expr)
1085+
self.arm(pat, break_expr)
10961086
};
10971087

10981088
// `mut iter`
@@ -1163,7 +1153,7 @@ impl LoweringContext<'_> {
11631153
});
11641154

11651155
// `mut iter => { ... }`
1166-
let iter_arm = self.arm(hir_vec![iter_pat], loop_expr);
1156+
let iter_arm = self.arm(iter_pat, loop_expr);
11671157

11681158
// `match ::std::iter::IntoIterator::into_iter(<head>) { ... }`
11691159
let into_iter_expr = {
@@ -1249,7 +1239,7 @@ impl LoweringContext<'_> {
12491239
ThinVec::from(attrs.clone()),
12501240
));
12511241
let ok_pat = self.pat_ok(span, val_pat);
1252-
self.arm(hir_vec![ok_pat], val_expr)
1242+
self.arm(ok_pat, val_expr)
12531243
};
12541244

12551245
// `Err(err) => #[allow(unreachable_code)]
@@ -1284,7 +1274,7 @@ impl LoweringContext<'_> {
12841274
};
12851275

12861276
let err_pat = self.pat_err(try_span, err_local);
1287-
self.arm(hir_vec![err_pat], ret_expr)
1277+
self.arm(err_pat, ret_expr)
12881278
};
12891279

12901280
hir::ExprKind::Match(
@@ -1479,14 +1469,11 @@ impl LoweringContext<'_> {
14791469
}
14801470
}
14811471

1482-
/// HACK(or_patterns; Centril | dlrobertson): For now we don't push down top level or-patterns
1483-
/// `p | q` into `hir::PatKind::Or(...)` as post-lowering bits of the compiler are not ready
1484-
/// to deal with it. This should by fixed by pushing it down to HIR and then HAIR.
1485-
fn arm(&mut self, pats: HirVec<P<hir::Pat>>, expr: P<hir::Expr>) -> hir::Arm {
1472+
fn arm(&mut self, pat: P<hir::Pat>, expr: P<hir::Expr>) -> hir::Arm {
14861473
hir::Arm {
14871474
hir_id: self.next_id(),
14881475
attrs: hir_vec![],
1489-
pats,
1476+
pat,
14901477
guard: None,
14911478
span: expr.span,
14921479
body: expr,

src/libsyntax/visit.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -834,8 +834,6 @@ pub fn walk_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a Param) {
834834

835835
pub fn walk_arm<'a, V: Visitor<'a>>(visitor: &mut V, arm: &'a Arm) {
836836
visitor.visit_pat(&arm.pat);
837-
// NOTE(or_patterns; Centril | dlrobertson):
838-
// If you change this, also change the hack in `lowering.rs`.
839837
walk_list!(visitor, visit_expr, &arm.guard);
840838
visitor.visit_expr(&arm.body);
841839
walk_list!(visitor, visit_attribute, &arm.attrs);

0 commit comments

Comments
 (0)