Skip to content

Commit 3ef481a

Browse files
committed
Remove hir::ScopeTarget
When we want to implement label-break-value, we can't really decide whether to emit ScopeTarget::Loop or ScopeTarget::Block in the code that is supposed to create it. So we get rid of it and reconstruct the information when needed.
1 parent eca0da5 commit 3ef481a

File tree

9 files changed

+51
-85
lines changed

9 files changed

+51
-85
lines changed

src/librustc/cfg/construct.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -582,19 +582,16 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
582582
scope_cf_kind: ScopeCfKind) -> (region::Scope, CFGIndex) {
583583

584584
match destination.target_id {
585-
hir::ScopeTarget::Block(block_expr_id) => {
585+
hir::LoopIdResult::Ok(loop_id) => {
586586
for b in &self.breakable_block_scopes {
587-
if b.block_expr_id == self.tcx.hir.node_to_hir_id(block_expr_id).local_id {
588-
let scope_id = self.tcx.hir.node_to_hir_id(block_expr_id).local_id;
587+
if b.block_expr_id == self.tcx.hir.node_to_hir_id(loop_id).local_id {
588+
let scope_id = self.tcx.hir.node_to_hir_id(loop_id).local_id;
589589
return (region::Scope::Node(scope_id), match scope_cf_kind {
590590
ScopeCfKind::Break => b.break_index,
591591
ScopeCfKind::Continue => bug!("can't continue to block"),
592592
});
593593
}
594594
}
595-
span_bug!(expr.span, "no block expr for id {}", block_expr_id);
596-
}
597-
hir::ScopeTarget::Loop(hir::LoopIdResult::Ok(loop_id)) => {
598595
for l in &self.loop_scopes {
599596
if l.loop_id == self.tcx.hir.node_to_hir_id(loop_id).local_id {
600597
let scope_id = self.tcx.hir.node_to_hir_id(loop_id).local_id;
@@ -604,10 +601,9 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
604601
});
605602
}
606603
}
607-
span_bug!(expr.span, "no loop scope for id {}", loop_id);
604+
span_bug!(expr.span, "no scope for id {}", loop_id);
608605
}
609-
hir::ScopeTarget::Loop(hir::LoopIdResult::Err(err)) =>
610-
span_bug!(expr.span, "loop scope error: {}", err),
606+
hir::LoopIdResult::Err(err) => span_bug!(expr.span, "scope error: {}", err),
611607
}
612608
}
613609
}

src/librustc/hir/intravisit.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,10 +1039,9 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
10391039
if let Some(ref label) = destination.label {
10401040
visitor.visit_label(label);
10411041
match destination.target_id {
1042-
ScopeTarget::Block(node_id) |
1043-
ScopeTarget::Loop(LoopIdResult::Ok(node_id)) =>
1042+
LoopIdResult::Ok(node_id) =>
10441043
visitor.visit_def_mention(Def::Label(node_id)),
1045-
ScopeTarget::Loop(LoopIdResult::Err(_)) => {},
1044+
LoopIdResult::Err(_) => {},
10461045
};
10471046
}
10481047
walk_list!(visitor, visit_expr, opt_expr);
@@ -1051,10 +1050,9 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
10511050
if let Some(ref label) = destination.label {
10521051
visitor.visit_label(label);
10531052
match destination.target_id {
1054-
ScopeTarget::Block(_) => bug!("can't `continue` to a non-loop block"),
1055-
ScopeTarget::Loop(LoopIdResult::Ok(node_id)) =>
1053+
LoopIdResult::Ok(node_id) =>
10561054
visitor.visit_def_mention(Def::Label(node_id)),
1057-
ScopeTarget::Loop(LoopIdResult::Err(_)) => {},
1055+
LoopIdResult::Err(_) => {},
10581056
};
10591057
}
10601058
}

src/librustc/hir/lowering.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -928,29 +928,27 @@ impl<'a> LoweringContext<'a> {
928928
fn lower_loop_destination(&mut self, destination: Option<(NodeId, Label)>) -> hir::Destination {
929929
match destination {
930930
Some((id, label)) => {
931-
let target = if let Def::Label(loop_id) = self.expect_full_def(id) {
931+
let target_id = if let Def::Label(loop_id) = self.expect_full_def(id) {
932932
hir::LoopIdResult::Ok(self.lower_node_id(loop_id).node_id)
933933
} else {
934934
hir::LoopIdResult::Err(hir::LoopIdError::UnresolvedLabel)
935935
};
936936
hir::Destination {
937937
label: self.lower_label(Some(label)),
938-
target_id: hir::ScopeTarget::Loop(target),
938+
target_id,
939939
}
940940
}
941941
None => {
942-
let loop_id = self.loop_scopes
942+
let target_id = self.loop_scopes
943943
.last()
944-
.map(|innermost_loop_id| *innermost_loop_id);
944+
.map(|innermost_loop_id| *innermost_loop_id)
945+
.map(|id| Ok(self.lower_node_id(id).node_id))
946+
.unwrap_or(Err(hir::LoopIdError::OutsideLoopScope))
947+
.into();
945948

946949
hir::Destination {
947950
label: None,
948-
target_id: hir::ScopeTarget::Loop(
949-
loop_id
950-
.map(|id| Ok(self.lower_node_id(id).node_id))
951-
.unwrap_or(Err(hir::LoopIdError::OutsideLoopScope))
952-
.into(),
953-
),
951+
target_id,
954952
}
955953
}
956954
}
@@ -3193,9 +3191,7 @@ impl<'a> LoweringContext<'a> {
31933191
let destination = if self.is_in_loop_condition && opt_label.is_none() {
31943192
hir::Destination {
31953193
label: None,
3196-
target_id: hir::ScopeTarget::Loop(
3197-
Err(hir::LoopIdError::UnlabeledCfInWhileCondition).into(),
3198-
),
3194+
target_id: Err(hir::LoopIdError::UnlabeledCfInWhileCondition).into(),
31993195
}
32003196
} else {
32013197
self.lower_loop_destination(opt_label.map(|label| (e.id, label)))
@@ -3209,9 +3205,7 @@ impl<'a> LoweringContext<'a> {
32093205
hir::ExprAgain(if self.is_in_loop_condition && opt_label.is_none() {
32103206
hir::Destination {
32113207
label: None,
3212-
target_id: hir::ScopeTarget::Loop(
3213-
Err(hir::LoopIdError::UnlabeledCfInWhileCondition).into(),
3214-
),
3208+
target_id: Err(hir::LoopIdError::UnlabeledCfInWhileCondition).into(),
32153209
}
32163210
} else {
32173211
self.lower_loop_destination(opt_label.map(|label| (e.id, label)))
@@ -3604,7 +3598,7 @@ impl<'a> LoweringContext<'a> {
36043598
hir::ExprBreak(
36053599
hir::Destination {
36063600
label: None,
3607-
target_id: hir::ScopeTarget::Block(catch_node),
3601+
target_id: hir::LoopIdResult::Ok(catch_node),
36083602
},
36093603
Some(from_err_expr),
36103604
),

src/librustc/hir/mod.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,18 +1526,11 @@ impl From<Result<NodeId, LoopIdError>> for LoopIdResult {
15261526
}
15271527
}
15281528

1529-
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1530-
pub enum ScopeTarget {
1531-
Block(NodeId),
1532-
Loop(LoopIdResult),
1533-
}
1534-
1535-
impl ScopeTarget {
1536-
pub fn opt_id(self) -> Option<NodeId> {
1529+
impl LoopIdResult {
1530+
pub fn ok(self) -> Option<NodeId> {
15371531
match self {
1538-
ScopeTarget::Block(node_id) |
1539-
ScopeTarget::Loop(LoopIdResult::Ok(node_id)) => Some(node_id),
1540-
ScopeTarget::Loop(LoopIdResult::Err(_)) => None,
1532+
LoopIdResult::Ok(node_id) => Some(node_id),
1533+
LoopIdResult::Err(_) => None,
15411534
}
15421535
}
15431536
}
@@ -1549,7 +1542,7 @@ pub struct Destination {
15491542

15501543
// These errors are caught and then reported during the diagnostics pass in
15511544
// librustc_passes/loops.rs
1552-
pub target_id: ScopeTarget,
1545+
pub target_id: LoopIdResult,
15531546
}
15541547

15551548
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]

src/librustc/ich/impls_hir.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -667,11 +667,6 @@ impl_stable_hash_for!(enum hir::LoopIdError {
667667
UnresolvedLabel
668668
});
669669

670-
impl_stable_hash_for!(enum hir::ScopeTarget {
671-
Block(node_id),
672-
Loop(loop_id_result)
673-
});
674-
675670
impl<'a> HashStable<StableHashingContext<'a>> for ast::Ident {
676671
fn hash_stable<W: StableHasherResult>(&self,
677672
hcx: &mut StableHashingContext<'a>,

src/librustc/middle/liveness.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -571,9 +571,6 @@ struct Liveness<'a, 'tcx: 'a> {
571571
// it probably doesn't now)
572572
break_ln: NodeMap<LiveNode>,
573573
cont_ln: NodeMap<LiveNode>,
574-
575-
// mappings from node ID to LiveNode for "breakable" blocks-- currently only `catch {...}`
576-
breakable_block_ln: NodeMap<LiveNode>,
577574
}
578575

579576
impl<'a, 'tcx> Liveness<'a, 'tcx> {
@@ -601,7 +598,6 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
601598
users: vec![invalid_users(); num_live_nodes * num_vars],
602599
break_ln: NodeMap(),
603600
cont_ln: NodeMap(),
604-
breakable_block_ln: NodeMap(),
605601
}
606602
}
607603

@@ -870,7 +866,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
870866
fn propagate_through_block(&mut self, blk: &hir::Block, succ: LiveNode)
871867
-> LiveNode {
872868
if blk.targeted_by_break {
873-
self.breakable_block_ln.insert(blk.id, succ);
869+
self.break_ln.insert(blk.id, succ);
874870
}
875871
let succ = self.propagate_through_opt_expr(blk.expr.as_ref().map(|e| &**e), succ);
876872
blk.stmts.iter().rev().fold(succ, |succ, stmt| {
@@ -1055,11 +1051,8 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
10551051
hir::ExprBreak(label, ref opt_expr) => {
10561052
// Find which label this break jumps to
10571053
let target = match label.target_id {
1058-
hir::ScopeTarget::Block(node_id) =>
1059-
self.breakable_block_ln.get(&node_id),
1060-
hir::ScopeTarget::Loop(hir::LoopIdResult::Ok(node_id)) =>
1061-
self.break_ln.get(&node_id),
1062-
hir::ScopeTarget::Loop(hir::LoopIdResult::Err(err)) =>
1054+
hir::LoopIdResult::Ok(node_id) => self.break_ln.get(&node_id),
1055+
hir::LoopIdResult::Err(err) =>
10631056
span_bug!(expr.span, "loop scope error: {}", err),
10641057
}.map(|x| *x);
10651058

@@ -1075,9 +1068,8 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
10751068
hir::ExprAgain(label) => {
10761069
// Find which label this expr continues to
10771070
let sc = match label.target_id {
1078-
hir::ScopeTarget::Block(_) => bug!("can't `continue` to a non-loop block"),
1079-
hir::ScopeTarget::Loop(hir::LoopIdResult::Ok(node_id)) => node_id,
1080-
hir::ScopeTarget::Loop(hir::LoopIdResult::Err(err)) =>
1071+
hir::LoopIdResult::Ok(node_id) => node_id,
1072+
hir::LoopIdResult::Err(err) =>
10811073
span_bug!(expr.span, "loop scope error: {}", err),
10821074
};
10831075

src/librustc_mir/hair/cx/expr.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -536,22 +536,20 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
536536
hir::ExprRet(ref v) => ExprKind::Return { value: v.to_ref() },
537537
hir::ExprBreak(dest, ref value) => {
538538
match dest.target_id {
539-
hir::ScopeTarget::Block(target_id) |
540-
hir::ScopeTarget::Loop(hir::LoopIdResult::Ok(target_id)) => ExprKind::Break {
539+
hir::LoopIdResult::Ok(target_id) => ExprKind::Break {
541540
label: region::Scope::Node(cx.tcx.hir.node_to_hir_id(target_id).local_id),
542541
value: value.to_ref(),
543542
},
544-
hir::ScopeTarget::Loop(hir::LoopIdResult::Err(err)) =>
543+
hir::LoopIdResult::Err(err) =>
545544
bug!("invalid loop id for break: {}", err)
546545
}
547546
}
548547
hir::ExprAgain(dest) => {
549548
match dest.target_id {
550-
hir::ScopeTarget::Block(_) => bug!("cannot continue to blocks"),
551-
hir::ScopeTarget::Loop(hir::LoopIdResult::Ok(loop_id)) => ExprKind::Continue {
549+
hir::LoopIdResult::Ok(loop_id) => ExprKind::Continue {
552550
label: region::Scope::Node(cx.tcx.hir.node_to_hir_id(loop_id).local_id),
553551
},
554-
hir::ScopeTarget::Loop(hir::LoopIdResult::Err(err)) =>
552+
hir::LoopIdResult::Err(err) =>
555553
bug!("invalid loop id for continue: {}", err)
556554
}
557555
}

src/librustc_passes/loops.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -85,20 +85,21 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
8585
self.with_context(Closure, |v| v.visit_nested_body(b));
8686
}
8787
hir::ExprBreak(label, ref opt_expr) => {
88-
let loop_id = match label.target_id {
89-
hir::ScopeTarget::Block(_) => return,
90-
hir::ScopeTarget::Loop(loop_res) => {
91-
match loop_res.into() {
92-
Ok(loop_id) => loop_id,
93-
Err(hir::LoopIdError::OutsideLoopScope) => ast::DUMMY_NODE_ID,
94-
Err(hir::LoopIdError::UnlabeledCfInWhileCondition) => {
95-
self.emit_unlabled_cf_in_while_condition(e.span, "break");
96-
ast::DUMMY_NODE_ID
97-
},
98-
Err(hir::LoopIdError::UnresolvedLabel) => ast::DUMMY_NODE_ID,
99-
}
100-
}
88+
let loop_id = match label.target_id.into() {
89+
Ok(loop_id) => loop_id,
90+
Err(hir::LoopIdError::OutsideLoopScope) => ast::DUMMY_NODE_ID,
91+
Err(hir::LoopIdError::UnlabeledCfInWhileCondition) => {
92+
self.emit_unlabled_cf_in_while_condition(e.span, "break");
93+
ast::DUMMY_NODE_ID
94+
},
95+
Err(hir::LoopIdError::UnresolvedLabel) => ast::DUMMY_NODE_ID,
10196
};
97+
if loop_id != ast::DUMMY_NODE_ID {
98+
match self.hir_map.find(loop_id).unwrap() {
99+
hir::map::NodeBlock(_) => return,
100+
_=> (),
101+
}
102+
}
102103

103104
if opt_expr.is_some() {
104105
let loop_kind = if loop_id == ast::DUMMY_NODE_ID {
@@ -132,9 +133,8 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
132133
self.require_loop("break", e.span);
133134
}
134135
hir::ExprAgain(label) => {
135-
if let hir::ScopeTarget::Loop(
136-
hir::LoopIdResult::Err(
137-
hir::LoopIdError::UnlabeledCfInWhileCondition)) = label.target_id {
136+
if let hir::LoopIdResult::Err(
137+
hir::LoopIdError::UnlabeledCfInWhileCondition) = label.target_id {
138138
self.emit_unlabled_cf_in_while_condition(e.span, "continue");
139139
}
140140
self.require_loop("continue", e.span)

src/librustc_typeck/check/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3723,7 +3723,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
37233723
tcx.mk_nil()
37243724
}
37253725
hir::ExprBreak(destination, ref expr_opt) => {
3726-
if let Some(target_id) = destination.target_id.opt_id() {
3726+
if let Some(target_id) = destination.target_id.ok() {
37273727
let (e_ty, cause);
37283728
if let Some(ref e) = *expr_opt {
37293729
// If this is a break with a value, we need to type-check

0 commit comments

Comments
 (0)