Skip to content

Commit

Permalink
Remove LoopIdResult
Browse files Browse the repository at this point in the history
It's redundant as Result already implements Encodable
as well as Decodable.
  • Loading branch information
est31 committed May 15, 2018
1 parent 3ef481a commit 235e7c1
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 64 deletions.
4 changes: 2 additions & 2 deletions src/librustc/cfg/construct.rs
Expand Up @@ -582,7 +582,7 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
scope_cf_kind: ScopeCfKind) -> (region::Scope, CFGIndex) {

match destination.target_id {
hir::LoopIdResult::Ok(loop_id) => {
Ok(loop_id) => {
for b in &self.breakable_block_scopes {
if b.block_expr_id == self.tcx.hir.node_to_hir_id(loop_id).local_id {
let scope_id = self.tcx.hir.node_to_hir_id(loop_id).local_id;
Expand All @@ -603,7 +603,7 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
}
span_bug!(expr.span, "no scope for id {}", loop_id);
}
hir::LoopIdResult::Err(err) => span_bug!(expr.span, "scope error: {}", err),
Err(err) => span_bug!(expr.span, "scope error: {}", err),
}
}
}
Expand Down
10 changes: 4 additions & 6 deletions src/librustc/hir/intravisit.rs
Expand Up @@ -1039,9 +1039,8 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
if let Some(ref label) = destination.label {
visitor.visit_label(label);
match destination.target_id {
LoopIdResult::Ok(node_id) =>
visitor.visit_def_mention(Def::Label(node_id)),
LoopIdResult::Err(_) => {},
Ok(node_id) => visitor.visit_def_mention(Def::Label(node_id)),
Err(_) => {},
};
}
walk_list!(visitor, visit_expr, opt_expr);
Expand All @@ -1050,9 +1049,8 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
if let Some(ref label) = destination.label {
visitor.visit_label(label);
match destination.target_id {
LoopIdResult::Ok(node_id) =>
visitor.visit_def_mention(Def::Label(node_id)),
LoopIdResult::Err(_) => {},
Ok(node_id) => visitor.visit_def_mention(Def::Label(node_id)),
Err(_) => {},
};
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/hir/lowering.rs
Expand Up @@ -929,9 +929,9 @@ impl<'a> LoweringContext<'a> {
match destination {
Some((id, label)) => {
let target_id = if let Def::Label(loop_id) = self.expect_full_def(id) {
hir::LoopIdResult::Ok(self.lower_node_id(loop_id).node_id)
Ok(self.lower_node_id(loop_id).node_id)
} else {
hir::LoopIdResult::Err(hir::LoopIdError::UnresolvedLabel)
Err(hir::LoopIdError::UnresolvedLabel)
};
hir::Destination {
label: self.lower_label(Some(label)),
Expand Down Expand Up @@ -3598,7 +3598,7 @@ impl<'a> LoweringContext<'a> {
hir::ExprBreak(
hir::Destination {
label: None,
target_id: hir::LoopIdResult::Ok(catch_node),
target_id: Ok(catch_node),
},
Some(from_err_expr),
),
Expand Down
35 changes: 1 addition & 34 deletions src/librustc/hir/mod.rs
Expand Up @@ -1502,47 +1502,14 @@ impl fmt::Display for LoopIdError {
}
}

// FIXME(cramertj) this should use `Result` once master compiles w/ a vesion of Rust where
// `Result` implements `Encodable`/`Decodable`
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
pub enum LoopIdResult {
Ok(NodeId),
Err(LoopIdError),
}
impl Into<Result<NodeId, LoopIdError>> for LoopIdResult {
fn into(self) -> Result<NodeId, LoopIdError> {
match self {
LoopIdResult::Ok(ok) => Ok(ok),
LoopIdResult::Err(err) => Err(err),
}
}
}
impl From<Result<NodeId, LoopIdError>> for LoopIdResult {
fn from(res: Result<NodeId, LoopIdError>) -> Self {
match res {
Ok(ok) => LoopIdResult::Ok(ok),
Err(err) => LoopIdResult::Err(err),
}
}
}

impl LoopIdResult {
pub fn ok(self) -> Option<NodeId> {
match self {
LoopIdResult::Ok(node_id) => Some(node_id),
LoopIdResult::Err(_) => None,
}
}
}

#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
pub struct Destination {
// This is `Some(_)` iff there is an explicit user-specified `label
pub label: Option<Label>,

// These errors are caught and then reported during the diagnostics pass in
// librustc_passes/loops.rs
pub target_id: LoopIdResult,
pub target_id: Result<NodeId, LoopIdError>,
}

#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
Expand Down
5 changes: 0 additions & 5 deletions src/librustc/ich/impls_hir.rs
Expand Up @@ -656,11 +656,6 @@ impl_stable_hash_for!(struct hir::Destination {

impl_stable_hash_for_spanned!(ast::Ident);

impl_stable_hash_for!(enum hir::LoopIdResult {
Ok(node_id),
Err(loop_id_error)
});

impl_stable_hash_for!(enum hir::LoopIdError {
OutsideLoopScope,
UnlabeledCfInWhileCondition,
Expand Down
10 changes: 4 additions & 6 deletions src/librustc/middle/liveness.rs
Expand Up @@ -1051,9 +1051,8 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
hir::ExprBreak(label, ref opt_expr) => {
// Find which label this break jumps to
let target = match label.target_id {
hir::LoopIdResult::Ok(node_id) => self.break_ln.get(&node_id),
hir::LoopIdResult::Err(err) =>
span_bug!(expr.span, "loop scope error: {}", err),
Ok(node_id) => self.break_ln.get(&node_id),
Err(err) => span_bug!(expr.span, "loop scope error: {}", err),
}.map(|x| *x);

// Now that we know the label we're going to,
Expand All @@ -1068,9 +1067,8 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
hir::ExprAgain(label) => {
// Find which label this expr continues to
let sc = match label.target_id {
hir::LoopIdResult::Ok(node_id) => node_id,
hir::LoopIdResult::Err(err) =>
span_bug!(expr.span, "loop scope error: {}", err),
Ok(node_id) => node_id,
Err(err) => span_bug!(expr.span, "loop scope error: {}", err),
};

// Now that we know the label we're going to,
Expand Down
10 changes: 4 additions & 6 deletions src/librustc_mir/hair/cx/expr.rs
Expand Up @@ -536,21 +536,19 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
hir::ExprRet(ref v) => ExprKind::Return { value: v.to_ref() },
hir::ExprBreak(dest, ref value) => {
match dest.target_id {
hir::LoopIdResult::Ok(target_id) => ExprKind::Break {
Ok(target_id) => ExprKind::Break {
label: region::Scope::Node(cx.tcx.hir.node_to_hir_id(target_id).local_id),
value: value.to_ref(),
},
hir::LoopIdResult::Err(err) =>
bug!("invalid loop id for break: {}", err)
Err(err) => bug!("invalid loop id for break: {}", err)
}
}
hir::ExprAgain(dest) => {
match dest.target_id {
hir::LoopIdResult::Ok(loop_id) => ExprKind::Continue {
Ok(loop_id) => ExprKind::Continue {
label: region::Scope::Node(cx.tcx.hir.node_to_hir_id(loop_id).local_id),
},
hir::LoopIdResult::Err(err) =>
bug!("invalid loop id for continue: {}", err)
Err(err) => bug!("invalid loop id for continue: {}", err)
}
}
hir::ExprMatch(ref discr, ref arms, _) => {
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_passes/loops.rs
Expand Up @@ -133,8 +133,7 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
self.require_loop("break", e.span);
}
hir::ExprAgain(label) => {
if let hir::LoopIdResult::Err(
hir::LoopIdError::UnlabeledCfInWhileCondition) = label.target_id {
if let Err(hir::LoopIdError::UnlabeledCfInWhileCondition) = label.target_id {
self.emit_unlabled_cf_in_while_condition(e.span, "continue");
}
self.require_loop("continue", e.span)
Expand Down

0 comments on commit 235e7c1

Please sign in to comment.