mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-11-02 12:59:12 +00:00
refactor: Turn LifetimeRef into an enum
This makes things more structured
This commit is contained in:
parent
dc363f7f77
commit
07212140db
8 changed files with 104 additions and 87 deletions
|
|
@ -544,13 +544,19 @@ impl ExprCollector<'_> {
|
|||
}
|
||||
|
||||
pub fn lower_lifetime_ref(&mut self, lifetime: ast::Lifetime) -> LifetimeRef {
|
||||
LifetimeRef::new(&lifetime)
|
||||
// FIXME: Keyword check?
|
||||
match &*lifetime.text() {
|
||||
"" | "'" => LifetimeRef::Error,
|
||||
"'static" => LifetimeRef::Static,
|
||||
"'_" => LifetimeRef::Placeholder,
|
||||
text => LifetimeRef::Named(Name::new_lifetime(text)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn lower_lifetime_ref_opt(&mut self, lifetime: Option<ast::Lifetime>) -> LifetimeRef {
|
||||
match lifetime {
|
||||
Some(lifetime) => LifetimeRef::new(&lifetime),
|
||||
None => LifetimeRef::missing(),
|
||||
Some(lifetime) => self.lower_lifetime_ref(lifetime),
|
||||
None => LifetimeRef::Placeholder,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -590,7 +596,7 @@ impl ExprCollector<'_> {
|
|||
}
|
||||
ast::Type::RefType(inner) => {
|
||||
let inner_ty = self.lower_type_ref_opt(inner.ty(), impl_trait_lower_fn);
|
||||
let lifetime = inner.lifetime().map(|lt| LifetimeRef::new(<));
|
||||
let lifetime = inner.lifetime().map(|lt| self.lower_lifetime_ref(lt));
|
||||
let mutability = Mutability::from_mutable(inner.mut_token().is_some());
|
||||
TypeRef::Reference(Box::new(RefType { ty: inner_ty, lifetime, mutability }))
|
||||
}
|
||||
|
|
@ -824,7 +830,7 @@ impl ExprCollector<'_> {
|
|||
}
|
||||
ast::GenericArg::LifetimeArg(lifetime_arg) => {
|
||||
if let Some(lifetime) = lifetime_arg.lifetime() {
|
||||
let lifetime_ref = LifetimeRef::new(&lifetime);
|
||||
let lifetime_ref = self.lower_lifetime_ref(lifetime);
|
||||
args.push(GenericArg::Lifetime(lifetime_ref))
|
||||
}
|
||||
}
|
||||
|
|
@ -911,7 +917,7 @@ impl ExprCollector<'_> {
|
|||
let lt_refs = match for_type.generic_param_list() {
|
||||
Some(gpl) => gpl
|
||||
.lifetime_params()
|
||||
.flat_map(|lp| lp.lifetime().map(|lt| Name::new_lifetime(<)))
|
||||
.flat_map(|lp| lp.lifetime().map(|lt| Name::new_lifetime(<.text())))
|
||||
.collect(),
|
||||
None => Box::default(),
|
||||
};
|
||||
|
|
@ -932,14 +938,14 @@ impl ExprCollector<'_> {
|
|||
gal.use_bound_generic_args()
|
||||
.map(|p| match p {
|
||||
ast::UseBoundGenericArg::Lifetime(l) => {
|
||||
UseArgRef::Lifetime(LifetimeRef::new(&l))
|
||||
UseArgRef::Lifetime(self.lower_lifetime_ref(l))
|
||||
}
|
||||
ast::UseBoundGenericArg::NameRef(n) => UseArgRef::Name(n.as_name()),
|
||||
})
|
||||
.collect(),
|
||||
),
|
||||
ast::TypeBoundKind::Lifetime(lifetime) => {
|
||||
TypeBound::Lifetime(LifetimeRef::new(&lifetime))
|
||||
TypeBound::Lifetime(self.lower_lifetime_ref(lifetime))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -2491,7 +2497,10 @@ impl ExprCollector<'_> {
|
|||
|
||||
fn collect_label(&mut self, ast_label: ast::Label) -> LabelId {
|
||||
let label = Label {
|
||||
name: ast_label.lifetime().as_ref().map_or_else(Name::missing, Name::new_lifetime),
|
||||
name: ast_label
|
||||
.lifetime()
|
||||
.as_ref()
|
||||
.map_or_else(Name::missing, |lt| Name::new_lifetime(<.text())),
|
||||
};
|
||||
self.alloc_label(label, AstPtr::new(&ast_label))
|
||||
}
|
||||
|
|
@ -2511,7 +2520,7 @@ impl ExprCollector<'_> {
|
|||
(hygiene_id.lookup().parent(self.db), expansion.def)
|
||||
})
|
||||
};
|
||||
let name = Name::new_lifetime(&lifetime);
|
||||
let name = Name::new_lifetime(&lifetime.text());
|
||||
|
||||
for (rib_idx, rib) in self.label_ribs.iter().enumerate().rev() {
|
||||
match &rib.kind {
|
||||
|
|
|
|||
|
|
@ -123,12 +123,14 @@ impl<'db, 'c> GenericParamsCollector<'db, 'c> {
|
|||
ast::GenericParam::LifetimeParam(lifetime_param) => {
|
||||
let lifetime_ref =
|
||||
self.expr_collector.lower_lifetime_ref_opt(lifetime_param.lifetime());
|
||||
let param = LifetimeParamData { name: lifetime_ref.name.clone() };
|
||||
let _idx = self.lifetimes.alloc(param);
|
||||
self.lower_bounds(
|
||||
lifetime_param.type_bound_list(),
|
||||
Either::Right(lifetime_ref),
|
||||
);
|
||||
if let LifetimeRef::Named(name) = &lifetime_ref {
|
||||
let param = LifetimeParamData { name: name.clone() };
|
||||
let _idx = self.lifetimes.alloc(param);
|
||||
self.lower_bounds(
|
||||
lifetime_param.type_bound_list(),
|
||||
Either::Right(lifetime_ref),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -151,7 +153,7 @@ impl<'db, 'c> GenericParamsCollector<'db, 'c> {
|
|||
.map(|lifetime_param| {
|
||||
lifetime_param
|
||||
.lifetime()
|
||||
.map_or_else(Name::missing, |lt| Name::new_lifetime(<))
|
||||
.map_or_else(Name::missing, |lt| Name::new_lifetime(<.text()))
|
||||
})
|
||||
.collect()
|
||||
});
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ use crate::{
|
|||
},
|
||||
lang_item::LangItemTarget,
|
||||
signatures::{FnFlags, FunctionSignature, StructSignature},
|
||||
type_ref::{ConstRef, Mutability, TraitBoundModifier, TypeBound, UseArgRef},
|
||||
type_ref::{ConstRef, LifetimeRef, Mutability, TraitBoundModifier, TypeBound, UseArgRef},
|
||||
};
|
||||
|
||||
use super::*;
|
||||
|
|
@ -268,12 +268,9 @@ fn print_where_clauses(db: &dyn DefDatabase, generic_params: &GenericParams, p:
|
|||
}
|
||||
},
|
||||
WherePredicate::Lifetime { target, bound } => {
|
||||
w!(
|
||||
p,
|
||||
"{}: {}",
|
||||
target.name.display(db.upcast(), p.edition),
|
||||
bound.name.display(db.upcast(), p.edition)
|
||||
);
|
||||
p.print_lifetime_ref(target);
|
||||
w!(p, ": ");
|
||||
p.print_lifetime_ref(bound);
|
||||
}
|
||||
WherePredicate::ForLifetime { lifetimes, target, bound } => {
|
||||
w!(p, "for<");
|
||||
|
|
@ -1140,9 +1137,7 @@ impl Printer<'_> {
|
|||
match arg {
|
||||
GenericArg::Type(ty) => self.print_type_ref(*ty),
|
||||
GenericArg::Const(ConstRef { expr }) => self.print_expr(*expr),
|
||||
GenericArg::Lifetime(lt) => {
|
||||
w!(self, "{}", lt.name.display(self.db.upcast(), self.edition))
|
||||
}
|
||||
GenericArg::Lifetime(lt) => self.print_lifetime_ref(lt),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1155,6 +1150,17 @@ impl Printer<'_> {
|
|||
}
|
||||
}
|
||||
|
||||
pub(crate) fn print_lifetime_ref(&mut self, lt_ref: &LifetimeRef) {
|
||||
match lt_ref {
|
||||
LifetimeRef::Static => w!(self, "'static"),
|
||||
LifetimeRef::Named(lt) => {
|
||||
w!(self, "{}", lt.display(self.db.upcast(), self.edition))
|
||||
}
|
||||
LifetimeRef::Placeholder => w!(self, "'_"),
|
||||
LifetimeRef::Error => w!(self, "'{{error}}"),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn print_type_ref(&mut self, type_ref: TypeRefId) {
|
||||
// FIXME: deduplicate with `HirDisplay` impl
|
||||
match &self.store[type_ref] {
|
||||
|
|
@ -1187,7 +1193,8 @@ impl Printer<'_> {
|
|||
};
|
||||
w!(self, "&");
|
||||
if let Some(lt) = &ref_.lifetime {
|
||||
w!(self, "{} ", lt.name.display(self.db.upcast(), self.edition));
|
||||
self.print_lifetime_ref(lt);
|
||||
w!(self, " ");
|
||||
}
|
||||
w!(self, "{mtbl}");
|
||||
self.print_type_ref(ref_.ty);
|
||||
|
|
@ -1269,9 +1276,7 @@ impl Printer<'_> {
|
|||
);
|
||||
self.print_path(&self.store[*path]);
|
||||
}
|
||||
TypeBound::Lifetime(lt) => {
|
||||
w!(self, "{}", lt.name.display(self.db.upcast(), self.edition))
|
||||
}
|
||||
TypeBound::Lifetime(lt) => self.print_lifetime_ref(lt),
|
||||
TypeBound::Use(args) => {
|
||||
w!(self, "use<");
|
||||
let mut first = true;
|
||||
|
|
@ -1283,9 +1288,7 @@ impl Printer<'_> {
|
|||
UseArgRef::Name(it) => {
|
||||
w!(self, "{}", it.display(self.db.upcast(), self.edition))
|
||||
}
|
||||
UseArgRef::Lifetime(it) => {
|
||||
w!(self, "{}", it.name.display(self.db.upcast(), self.edition))
|
||||
}
|
||||
UseArgRef::Lifetime(it) => self.print_lifetime_ref(it),
|
||||
}
|
||||
}
|
||||
w!(self, ">")
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ use std::fmt::Write;
|
|||
use hir_expand::name::Name;
|
||||
use intern::Symbol;
|
||||
use la_arena::Idx;
|
||||
use syntax::ast;
|
||||
use thin_vec::ThinVec;
|
||||
|
||||
use crate::{
|
||||
|
|
@ -145,18 +144,11 @@ const _: () = assert!(size_of::<TypeRef>() == 16);
|
|||
pub type TypeRefId = Idx<TypeRef>;
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
|
||||
pub struct LifetimeRef {
|
||||
pub name: Name,
|
||||
}
|
||||
|
||||
impl LifetimeRef {
|
||||
pub(crate) fn new(lifetime: &ast::Lifetime) -> Self {
|
||||
LifetimeRef { name: Name::new_lifetime(lifetime) }
|
||||
}
|
||||
|
||||
pub fn missing() -> LifetimeRef {
|
||||
LifetimeRef { name: Name::missing() }
|
||||
}
|
||||
pub enum LifetimeRef {
|
||||
Named(Name),
|
||||
Static,
|
||||
Placeholder,
|
||||
Error,
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
|
||||
|
|
|
|||
|
|
@ -501,16 +501,16 @@ impl Resolver {
|
|||
}
|
||||
|
||||
pub fn resolve_lifetime(&self, lifetime: &LifetimeRef) -> Option<LifetimeNs> {
|
||||
if lifetime.name == sym::tick_static.clone() {
|
||||
return Some(LifetimeNs::Static);
|
||||
match lifetime {
|
||||
LifetimeRef::Static => Some(LifetimeNs::Static),
|
||||
LifetimeRef::Named(name) => self.scopes().find_map(|scope| match scope {
|
||||
Scope::GenericParams { def, params } => {
|
||||
params.find_lifetime_by_name(name, *def).map(LifetimeNs::LifetimeParam)
|
||||
}
|
||||
_ => None,
|
||||
}),
|
||||
LifetimeRef::Placeholder | LifetimeRef::Error => None,
|
||||
}
|
||||
|
||||
self.scopes().find_map(|scope| match scope {
|
||||
Scope::GenericParams { def, params } => {
|
||||
params.find_lifetime_by_name(&lifetime.name, *def).map(LifetimeNs::LifetimeParam)
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Returns a set of names available in the current scope.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue