Skip to content

Commit 5953c57

Browse files
committed
Introduce LifetimeCtxt.
1 parent 52cc779 commit 5953c57

File tree

6 files changed

+23
-11
lines changed

6 files changed

+23
-11
lines changed

compiler/rustc_ast/src/visit.rs

+17-5
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,16 @@ impl<'a> FnKind<'a> {
8989
}
9090
}
9191

92+
#[derive(Copy, Clone, Debug)]
93+
pub enum LifetimeCtxt {
94+
/// Appears in a reference type.
95+
Rptr,
96+
/// Appears as a bound on a type or another lifetime.
97+
Bound,
98+
/// Appears as a generic argument.
99+
GenericArg,
100+
}
101+
92102
/// Each method of the `Visitor` trait is a hook to be potentially
93103
/// overridden. Each method's default implementation recursively visits
94104
/// the substructure of the input via the corresponding `walk` method;
@@ -184,7 +194,7 @@ pub trait Visitor<'ast>: Sized {
184194
fn visit_label(&mut self, label: &'ast Label) {
185195
walk_label(self, label)
186196
}
187-
fn visit_lifetime(&mut self, lifetime: &'ast Lifetime) {
197+
fn visit_lifetime(&mut self, lifetime: &'ast Lifetime, _: LifetimeCtxt) {
188198
walk_lifetime(self, lifetime)
189199
}
190200
fn visit_mac_call(&mut self, mac: &'ast MacCall) {
@@ -414,7 +424,7 @@ pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) {
414424
TyKind::Slice(ref ty) | TyKind::Paren(ref ty) => visitor.visit_ty(ty),
415425
TyKind::Ptr(ref mutable_type) => visitor.visit_ty(&mutable_type.ty),
416426
TyKind::Rptr(ref opt_lifetime, ref mutable_type) => {
417-
walk_list!(visitor, visit_lifetime, opt_lifetime);
427+
walk_list!(visitor, visit_lifetime, opt_lifetime, LifetimeCtxt::Rptr);
418428
visitor.visit_ty(&mutable_type.ty)
419429
}
420430
TyKind::Tup(ref tuple_element_types) => {
@@ -507,7 +517,7 @@ where
507517
V: Visitor<'a>,
508518
{
509519
match generic_arg {
510-
GenericArg::Lifetime(lt) => visitor.visit_lifetime(lt),
520+
GenericArg::Lifetime(lt) => visitor.visit_lifetime(lt, LifetimeCtxt::GenericArg),
511521
GenericArg::Type(ty) => visitor.visit_ty(ty),
512522
GenericArg::Const(ct) => visitor.visit_anon_const(ct),
513523
}
@@ -599,7 +609,9 @@ pub fn walk_foreign_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a ForeignI
599609
pub fn walk_param_bound<'a, V: Visitor<'a>>(visitor: &mut V, bound: &'a GenericBound) {
600610
match *bound {
601611
GenericBound::Trait(ref typ, ref modifier) => visitor.visit_poly_trait_ref(typ, modifier),
602-
GenericBound::Outlives(ref lifetime) => visitor.visit_lifetime(lifetime),
612+
GenericBound::Outlives(ref lifetime) => {
613+
visitor.visit_lifetime(lifetime, LifetimeCtxt::Bound)
614+
}
603615
}
604616
}
605617

@@ -639,7 +651,7 @@ pub fn walk_where_predicate<'a, V: Visitor<'a>>(visitor: &mut V, predicate: &'a
639651
WherePredicate::RegionPredicate(WhereRegionPredicate {
640652
ref lifetime, ref bounds, ..
641653
}) => {
642-
visitor.visit_lifetime(lifetime);
654+
visitor.visit_lifetime(lifetime, LifetimeCtxt::Bound);
643655
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
644656
}
645657
WherePredicate::EqPredicate(WhereEqPredicate { ref lhs_ty, ref rhs_ty, .. }) => {

compiler/rustc_ast_passes/src/ast_validation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
10701070
visit::walk_label(self, label);
10711071
}
10721072

1073-
fn visit_lifetime(&mut self, lifetime: &'a Lifetime) {
1073+
fn visit_lifetime(&mut self, lifetime: &'a Lifetime, _: visit::LifetimeCtxt) {
10741074
self.check_lifetime(lifetime.ident);
10751075
visit::walk_lifetime(self, lifetime);
10761076
}

compiler/rustc_ast_passes/src/node_count.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl<'ast> Visitor<'ast> for NodeCounter {
106106
self.count += 1;
107107
walk_variant(self, v)
108108
}
109-
fn visit_lifetime(&mut self, lifetime: &Lifetime) {
109+
fn visit_lifetime(&mut self, lifetime: &Lifetime, _: visit::LifetimeCtxt) {
110110
self.count += 1;
111111
walk_lifetime(self, lifetime)
112112
}

compiler/rustc_lint/src/early.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T>
272272
});
273273
}
274274

275-
fn visit_lifetime(&mut self, lt: &'a ast::Lifetime) {
275+
fn visit_lifetime(&mut self, lt: &'a ast::Lifetime, _: ast_visit::LifetimeCtxt) {
276276
run_early_pass!(self, check_lifetime, lt);
277277
self.check_id(lt.id);
278278
}

compiler/rustc_passes/src/hir_stats.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ impl<'v> ast_visit::Visitor<'v> for StatCollector<'v> {
318318
ast_visit::walk_variant(self, v)
319319
}
320320

321-
fn visit_lifetime(&mut self, lifetime: &'v ast::Lifetime) {
321+
fn visit_lifetime(&mut self, lifetime: &'v ast::Lifetime, _: ast_visit::LifetimeCtxt) {
322322
self.record("Lifetime", Id::None, lifetime);
323323
ast_visit::walk_lifetime(self, lifetime)
324324
}

compiler/rustc_resolve/src/late.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
793793
});
794794
self.diagnostic_metadata.current_function = previous_value;
795795
}
796-
fn visit_lifetime(&mut self, lifetime: &'ast Lifetime) {
796+
fn visit_lifetime(&mut self, lifetime: &'ast Lifetime, _: visit::LifetimeCtxt) {
797797
self.resolve_lifetime(lifetime)
798798
}
799799

@@ -859,7 +859,7 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
859859

860860
self.visit_ty(ty);
861861
}
862-
GenericArg::Lifetime(lt) => self.visit_lifetime(lt),
862+
GenericArg::Lifetime(lt) => self.visit_lifetime(lt, visit::LifetimeCtxt::GenericArg),
863863
GenericArg::Const(ct) => self.visit_anon_const(ct),
864864
}
865865
self.diagnostic_metadata.currently_processing_generics = prev;

0 commit comments

Comments
 (0)