Skip to content

Commit 29e9b5d

Browse files
committed
lowering: refactor label/dest -> expr.rs
1 parent 961ace3 commit 29e9b5d

File tree

2 files changed

+43
-47
lines changed

2 files changed

+43
-47
lines changed

src/librustc/hir/lowering.rs

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,36 +1263,6 @@ impl<'a> LoweringContext<'a> {
12631263
}
12641264
}
12651265

1266-
fn lower_label(&mut self, label: Option<Label>) -> Option<hir::Label> {
1267-
label.map(|label| hir::Label {
1268-
ident: label.ident,
1269-
})
1270-
}
1271-
1272-
fn lower_loop_destination(&mut self, destination: Option<(NodeId, Label)>) -> hir::Destination {
1273-
let target_id = match destination {
1274-
Some((id, _)) => {
1275-
if let Some(loop_id) = self.resolver.get_label_res(id) {
1276-
Ok(self.lower_node_id(loop_id))
1277-
} else {
1278-
Err(hir::LoopIdError::UnresolvedLabel)
1279-
}
1280-
}
1281-
None => {
1282-
self.loop_scopes
1283-
.last()
1284-
.cloned()
1285-
.map(|id| Ok(self.lower_node_id(id)))
1286-
.unwrap_or(Err(hir::LoopIdError::OutsideLoopScope))
1287-
.into()
1288-
}
1289-
};
1290-
hir::Destination {
1291-
label: self.lower_label(destination.map(|(_, label)| label)),
1292-
target_id,
1293-
}
1294-
}
1295-
12961266
fn lower_attrs_extendable(&mut self, attrs: &[Attribute]) -> Vec<Attribute> {
12971267
attrs
12981268
.iter()

src/librustc/hir/lowering/expr.rs

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -138,28 +138,13 @@ impl LoweringContext<'_> {
138138
hir::ExprKind::Path(qpath)
139139
}
140140
ExprKind::Break(opt_label, ref opt_expr) => {
141-
let destination = if self.is_in_loop_condition && opt_label.is_none() {
142-
hir::Destination {
143-
label: None,
144-
target_id: Err(hir::LoopIdError::UnlabeledCfInWhileCondition).into(),
145-
}
146-
} else {
147-
self.lower_loop_destination(opt_label.map(|label| (e.id, label)))
148-
};
149141
hir::ExprKind::Break(
150-
destination,
142+
self.lower_jump_destination(e.id, opt_label),
151143
opt_expr.as_ref().map(|x| P(self.lower_expr(x))),
152144
)
153145
}
154146
ExprKind::Continue(opt_label) => {
155-
hir::ExprKind::Continue(if self.is_in_loop_condition && opt_label.is_none() {
156-
hir::Destination {
157-
label: None,
158-
target_id: Err(hir::LoopIdError::UnlabeledCfInWhileCondition).into(),
159-
}
160-
} else {
161-
self.lower_loop_destination(opt_label.map(|label| (e.id, label)))
162-
})
147+
hir::ExprKind::Continue(self.lower_jump_destination(e.id, opt_label))
163148
}
164149
ExprKind::Ret(ref e) => hir::ExprKind::Ret(e.as_ref().map(|x| P(self.lower_expr(x)))),
165150
ExprKind::InlineAsm(ref asm) => self.lower_expr_asm(asm),
@@ -818,6 +803,47 @@ impl LoweringContext<'_> {
818803
}
819804
}
820805

806+
fn lower_label(&mut self, label: Option<Label>) -> Option<hir::Label> {
807+
label.map(|label| hir::Label {
808+
ident: label.ident,
809+
})
810+
}
811+
812+
fn lower_loop_destination(&mut self, destination: Option<(NodeId, Label)>) -> hir::Destination {
813+
let target_id = match destination {
814+
Some((id, _)) => {
815+
if let Some(loop_id) = self.resolver.get_label_res(id) {
816+
Ok(self.lower_node_id(loop_id))
817+
} else {
818+
Err(hir::LoopIdError::UnresolvedLabel)
819+
}
820+
}
821+
None => {
822+
self.loop_scopes
823+
.last()
824+
.cloned()
825+
.map(|id| Ok(self.lower_node_id(id)))
826+
.unwrap_or(Err(hir::LoopIdError::OutsideLoopScope))
827+
.into()
828+
}
829+
};
830+
hir::Destination {
831+
label: self.lower_label(destination.map(|(_, label)| label)),
832+
target_id,
833+
}
834+
}
835+
836+
fn lower_jump_destination(&mut self, id: NodeId, opt_label: Option<Label>) -> hir::Destination {
837+
if self.is_in_loop_condition && opt_label.is_none() {
838+
hir::Destination {
839+
label: None,
840+
target_id: Err(hir::LoopIdError::UnlabeledCfInWhileCondition).into(),
841+
}
842+
} else {
843+
self.lower_loop_destination(opt_label.map(|label| (id, label)))
844+
}
845+
}
846+
821847
fn lower_expr_asm(&mut self, asm: &InlineAsm) -> hir::ExprKind {
822848
let hir_asm = hir::InlineAsm {
823849
inputs: asm.inputs.iter().map(|&(ref c, _)| c.clone()).collect(),

0 commit comments

Comments
 (0)