Skip to content

Commit e4c7e2c

Browse files
committed
Add bool item is_in_impl_trait to LoweringContext
This is for tracking if an ImplItem is part of a trait impl. Add a with_trait_impl_ref method to ItemLowerer to appropriately save the state to allow appropriate nesting of trait and non-trait impls.
1 parent 8fd48e7 commit e4c7e2c

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

src/librustc/hir/lowering.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ pub struct LoweringContext<'a> {
103103
catch_scopes: Vec<NodeId>,
104104
loop_scopes: Vec<NodeId>,
105105
is_in_loop_condition: bool,
106+
is_in_trait_impl: bool,
106107

107108
type_def_lifetime_params: DefIdMap<usize>,
108109

@@ -174,6 +175,7 @@ pub fn lower_crate(sess: &Session,
174175
item_local_id_counters: NodeMap(),
175176
node_id_to_hir_id: IndexVec::new(),
176177
is_generator: false,
178+
is_in_trait_impl: false,
177179
}.lower_crate(krate)
178180
}
179181

@@ -241,6 +243,21 @@ impl<'a> LoweringContext<'a> {
241243
lctx: &'lcx mut LoweringContext<'interner>,
242244
}
243245

246+
impl<'lcx, 'interner> ItemLowerer<'lcx, 'interner> {
247+
fn with_trait_impl_ref<F>(&mut self, trait_impl_ref: &Option<TraitRef>, f: F)
248+
where F: FnOnce(&mut Self)
249+
{
250+
let old = self.lctx.is_in_trait_impl;
251+
self.lctx.is_in_trait_impl = if let &None = trait_impl_ref {
252+
false
253+
} else {
254+
true
255+
};
256+
f(self);
257+
self.lctx.is_in_trait_impl = old;
258+
}
259+
}
260+
244261
impl<'lcx, 'interner> Visitor<'lcx> for ItemLowerer<'lcx, 'interner> {
245262
fn visit_item(&mut self, item: &'lcx Item) {
246263
let mut item_lowered = true;
@@ -253,7 +270,13 @@ impl<'a> LoweringContext<'a> {
253270
});
254271

255272
if item_lowered {
256-
visit::walk_item(self, item);
273+
if let ItemKind::Impl(_,_,_,_,ref opt_trait_ref,_,_) = item.node {
274+
self.with_trait_impl_ref(opt_trait_ref, |this| {
275+
visit::walk_item(this, item)
276+
});
277+
} else {
278+
visit::walk_item(self, item);
279+
}
257280
}
258281
}
259282

0 commit comments

Comments
 (0)