Skip to content

Commit fed4410

Browse files
committed
Replace Vec by Option.
1 parent 1baac86 commit fed4410

File tree

2 files changed

+14
-35
lines changed

2 files changed

+14
-35
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+6-26
Original file line numberDiff line numberDiff line change
@@ -1186,9 +1186,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11861186
}
11871187
}
11881188
None => self
1189-
.loop_scopes
1190-
.last()
1191-
.cloned()
1189+
.loop_scope
11921190
.map(|id| Ok(self.lower_node_id(id)))
11931191
.unwrap_or(Err(hir::LoopIdError::OutsideLoopScope)),
11941192
};
@@ -1208,18 +1206,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
12081206
}
12091207

12101208
fn with_catch_scope<T>(&mut self, catch_id: NodeId, f: impl FnOnce(&mut Self) -> T) -> T {
1211-
let len = self.catch_scopes.len();
1212-
self.catch_scopes.push(catch_id);
1213-
1209+
let old_scope = self.catch_scope.replace(catch_id);
12141210
let result = f(self);
1215-
assert_eq!(
1216-
len + 1,
1217-
self.catch_scopes.len(),
1218-
"catch scopes should be added and removed in stack order"
1219-
);
1220-
1221-
self.catch_scopes.pop().unwrap();
1222-
1211+
self.catch_scope = old_scope;
12231212
result
12241213
}
12251214

@@ -1228,17 +1217,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
12281217
let was_in_loop_condition = self.is_in_loop_condition;
12291218
self.is_in_loop_condition = false;
12301219

1231-
let len = self.loop_scopes.len();
1232-
self.loop_scopes.push(loop_id);
1233-
1220+
let old_scope = self.loop_scope.replace(loop_id);
12341221
let result = f(self);
1235-
assert_eq!(
1236-
len + 1,
1237-
self.loop_scopes.len(),
1238-
"loop scopes should be added and removed in stack order"
1239-
);
1240-
1241-
self.loop_scopes.pop().unwrap();
1222+
self.loop_scope = old_scope;
12421223

12431224
self.is_in_loop_condition = was_in_loop_condition;
12441225

@@ -1565,8 +1546,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
15651546
unstable_span,
15661547
);
15671548
let thin_attrs = ThinVec::from(attrs);
1568-
let catch_scope = self.catch_scopes.last().copied();
1569-
let ret_expr = if let Some(catch_node) = catch_scope {
1549+
let ret_expr = if let Some(catch_node) = self.catch_scope {
15701550
let target_id = Ok(self.lower_node_id(catch_node));
15711551
self.arena.alloc(self.expr(
15721552
try_span,

compiler/rustc_ast_lowering/src/lib.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ use rustc_span::{Span, DUMMY_SP};
6464

6565
use smallvec::SmallVec;
6666
use std::collections::BTreeMap;
67-
use std::mem;
6867
use tracing::{debug, trace};
6968

7069
macro_rules! arena_vec {
@@ -117,8 +116,8 @@ struct LoweringContext<'a, 'hir: 'a> {
117116
/// outside of an `async fn`.
118117
current_item: Option<Span>,
119118

120-
catch_scopes: Vec<NodeId>,
121-
loop_scopes: Vec<NodeId>,
119+
catch_scope: Option<NodeId>,
120+
loop_scope: Option<NodeId>,
122121
is_in_loop_condition: bool,
123122
is_in_trait_impl: bool,
124123
is_in_dyn_type: bool,
@@ -323,8 +322,8 @@ pub fn lower_crate<'a, 'hir>(
323322
bodies: BTreeMap::new(),
324323
modules: BTreeMap::new(),
325324
attrs: BTreeMap::default(),
326-
catch_scopes: Vec::new(),
327-
loop_scopes: Vec::new(),
325+
catch_scope: None,
326+
loop_scope: None,
328327
is_in_loop_condition: false,
329328
is_in_trait_impl: false,
330329
is_in_dyn_type: false,
@@ -911,11 +910,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
911910
let was_in_loop_condition = self.is_in_loop_condition;
912911
self.is_in_loop_condition = false;
913912

914-
let catch_scopes = mem::take(&mut self.catch_scopes);
915-
let loop_scopes = mem::take(&mut self.loop_scopes);
913+
let catch_scope = self.catch_scope.take();
914+
let loop_scope = self.loop_scope.take();
916915
let ret = f(self);
917-
self.catch_scopes = catch_scopes;
918-
self.loop_scopes = loop_scopes;
916+
self.catch_scope = catch_scope;
917+
self.loop_scope = loop_scope;
919918

920919
self.is_in_loop_condition = was_in_loop_condition;
921920

0 commit comments

Comments
 (0)