Skip to content

Commit 75a0049

Browse files
committed
Auto merge of #50750 - est31:master, r=eddyb
Remove ScopeTarget and LoopIdResult * Remove ScopeTarget in preparation of label-break-value (PR #50045) * Replace LoopIdResult by Result which is possible now thanks to commit 8ac65af " Implement Encodable and Decodable for Result." r? @eddyb
2 parents 3c31e17 + 4d328f7 commit 75a0049

File tree

9 files changed

+48
-127
lines changed

9 files changed

+48
-127
lines changed

src/librustc/cfg/construct.rs

+5-9
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+
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+
Err(err) => span_bug!(expr.span, "scope error: {}", err),
611607
}
612608
}
613609
}

src/librustc/hir/intravisit.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -1039,10 +1039,8 @@ 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)) =>
1044-
visitor.visit_def_mention(Def::Label(node_id)),
1045-
ScopeTarget::Loop(LoopIdResult::Err(_)) => {},
1042+
Ok(node_id) => visitor.visit_def_mention(Def::Label(node_id)),
1043+
Err(_) => {},
10461044
};
10471045
}
10481046
walk_list!(visitor, visit_expr, opt_expr);
@@ -1051,10 +1049,8 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
10511049
if let Some(ref label) = destination.label {
10521050
visitor.visit_label(label);
10531051
match destination.target_id {
1054-
ScopeTarget::Block(_) => bug!("can't `continue` to a non-loop block"),
1055-
ScopeTarget::Loop(LoopIdResult::Ok(node_id)) =>
1056-
visitor.visit_def_mention(Def::Label(node_id)),
1057-
ScopeTarget::Loop(LoopIdResult::Err(_)) => {},
1052+
Ok(node_id) => visitor.visit_def_mention(Def::Label(node_id)),
1053+
Err(_) => {},
10581054
};
10591055
}
10601056
}

src/librustc/hir/lowering.rs

+13-19
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) {
932-
hir::LoopIdResult::Ok(self.lower_node_id(loop_id).node_id)
931+
let target_id = if let Def::Label(loop_id) = self.expect_full_def(id) {
932+
Ok(self.lower_node_id(loop_id).node_id)
933933
} else {
934-
hir::LoopIdResult::Err(hir::LoopIdError::UnresolvedLabel)
934+
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
}
@@ -3192,9 +3190,7 @@ impl<'a> LoweringContext<'a> {
31923190
let destination = if self.is_in_loop_condition && opt_label.is_none() {
31933191
hir::Destination {
31943192
label: None,
3195-
target_id: hir::ScopeTarget::Loop(
3196-
Err(hir::LoopIdError::UnlabeledCfInWhileCondition).into(),
3197-
),
3193+
target_id: Err(hir::LoopIdError::UnlabeledCfInWhileCondition).into(),
31983194
}
31993195
} else {
32003196
self.lower_loop_destination(opt_label.map(|label| (e.id, label)))
@@ -3208,9 +3204,7 @@ impl<'a> LoweringContext<'a> {
32083204
hir::ExprAgain(if self.is_in_loop_condition && opt_label.is_none() {
32093205
hir::Destination {
32103206
label: None,
3211-
target_id: hir::ScopeTarget::Loop(
3212-
Err(hir::LoopIdError::UnlabeledCfInWhileCondition).into(),
3213-
),
3207+
target_id: Err(hir::LoopIdError::UnlabeledCfInWhileCondition).into(),
32143208
}
32153209
} else {
32163210
self.lower_loop_destination(opt_label.map(|label| (e.id, label)))
@@ -3603,7 +3597,7 @@ impl<'a> LoweringContext<'a> {
36033597
hir::ExprBreak(
36043598
hir::Destination {
36053599
label: None,
3606-
target_id: hir::ScopeTarget::Block(catch_node),
3600+
target_id: Ok(catch_node),
36073601
},
36083602
Some(from_err_expr),
36093603
),

src/librustc/hir/mod.rs

+1-41
Original file line numberDiff line numberDiff line change
@@ -1502,54 +1502,14 @@ impl fmt::Display for LoopIdError {
15021502
}
15031503
}
15041504

1505-
// FIXME(cramertj) this should use `Result` once master compiles w/ a vesion of Rust where
1506-
// `Result` implements `Encodable`/`Decodable`
1507-
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1508-
pub enum LoopIdResult {
1509-
Ok(NodeId),
1510-
Err(LoopIdError),
1511-
}
1512-
impl Into<Result<NodeId, LoopIdError>> for LoopIdResult {
1513-
fn into(self) -> Result<NodeId, LoopIdError> {
1514-
match self {
1515-
LoopIdResult::Ok(ok) => Ok(ok),
1516-
LoopIdResult::Err(err) => Err(err),
1517-
}
1518-
}
1519-
}
1520-
impl From<Result<NodeId, LoopIdError>> for LoopIdResult {
1521-
fn from(res: Result<NodeId, LoopIdError>) -> Self {
1522-
match res {
1523-
Ok(ok) => LoopIdResult::Ok(ok),
1524-
Err(err) => LoopIdResult::Err(err),
1525-
}
1526-
}
1527-
}
1528-
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> {
1537-
match self {
1538-
ScopeTarget::Block(node_id) |
1539-
ScopeTarget::Loop(LoopIdResult::Ok(node_id)) => Some(node_id),
1540-
ScopeTarget::Loop(LoopIdResult::Err(_)) => None,
1541-
}
1542-
}
1543-
}
1544-
15451505
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
15461506
pub struct Destination {
15471507
// This is `Some(_)` iff there is an explicit user-specified `label
15481508
pub label: Option<Label>,
15491509

15501510
// These errors are caught and then reported during the diagnostics pass in
15511511
// librustc_passes/loops.rs
1552-
pub target_id: ScopeTarget,
1512+
pub target_id: Result<NodeId, LoopIdError>,
15531513
}
15541514

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

src/librustc/ich/impls_hir.rs

-10
Original file line numberDiff line numberDiff line change
@@ -656,22 +656,12 @@ impl_stable_hash_for!(struct hir::Destination {
656656

657657
impl_stable_hash_for_spanned!(ast::Ident);
658658

659-
impl_stable_hash_for!(enum hir::LoopIdResult {
660-
Ok(node_id),
661-
Err(loop_id_error)
662-
});
663-
664659
impl_stable_hash_for!(enum hir::LoopIdError {
665660
OutsideLoopScope,
666661
UnlabeledCfInWhileCondition,
667662
UnresolvedLabel
668663
});
669664

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

src/librustc/middle/liveness.rs

+5-15
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,12 +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)) =>
1063-
span_bug!(expr.span, "loop scope error: {}", err),
1054+
Ok(node_id) => self.break_ln.get(&node_id),
1055+
Err(err) => span_bug!(expr.span, "loop scope error: {}", err),
10641056
}.map(|x| *x);
10651057

10661058
// Now that we know the label we're going to,
@@ -1075,10 +1067,8 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
10751067
hir::ExprAgain(label) => {
10761068
// Find which label this expr continues to
10771069
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)) =>
1081-
span_bug!(expr.span, "loop scope error: {}", err),
1070+
Ok(node_id) => node_id,
1071+
Err(err) => span_bug!(expr.span, "loop scope error: {}", err),
10821072
};
10831073

10841074
// Now that we know the label we're going to,

src/librustc_mir/hair/cx/expr.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -536,23 +536,19 @@ 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+
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)) =>
545-
bug!("invalid loop id for break: {}", err)
543+
Err(err) => bug!("invalid loop id for break: {}", err)
546544
}
547545
}
548546
hir::ExprAgain(dest) => {
549547
match dest.target_id {
550-
hir::ScopeTarget::Block(_) => bug!("cannot continue to blocks"),
551-
hir::ScopeTarget::Loop(hir::LoopIdResult::Ok(loop_id)) => ExprKind::Continue {
548+
Ok(loop_id) => ExprKind::Continue {
552549
label: region::Scope::Node(cx.tcx.hir.node_to_hir_id(loop_id).local_id),
553550
},
554-
hir::ScopeTarget::Loop(hir::LoopIdResult::Err(err)) =>
555-
bug!("invalid loop id for continue: {}", err)
551+
Err(err) => bug!("invalid loop id for continue: {}", err)
556552
}
557553
}
558554
hir::ExprMatch(ref discr, ref arms, _) => {

src/librustc_passes/loops.rs

+15-16
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,7 @@ 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 Err(hir::LoopIdError::UnlabeledCfInWhileCondition) = label.target_id {
138137
self.emit_unlabled_cf_in_while_condition(e.span, "continue");
139138
}
140139
self.require_loop("continue", e.span)

src/librustc_typeck/check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3726,7 +3726,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
37263726
tcx.mk_nil()
37273727
}
37283728
hir::ExprBreak(destination, ref expr_opt) => {
3729-
if let Some(target_id) = destination.target_id.opt_id() {
3729+
if let Ok(target_id) = destination.target_id {
37303730
let (e_ty, cause);
37313731
if let Some(ref e) = *expr_opt {
37323732
// If this is a break with a value, we need to type-check

0 commit comments

Comments
 (0)