Skip to content

Commit 479d1f7

Browse files
bors[bot]Veykril
andauthored
Merge #6818
6818: Add Lifetimes to the HIR r=matklad a=Veykril This doesn't handle resolve yet as I don't know yet how that will be used. I'll get to that once I start moving the lifetime reference PR to the hir. This also adds a new `hir` name type for lifetimes and labels, `hir::LifetimeName`. Co-authored-by: Lukas Wirth <[email protected]>
2 parents 64a1c98 + 11f8664 commit 479d1f7

File tree

16 files changed

+249
-96
lines changed

16 files changed

+249
-96
lines changed

crates/assists/src/ast_transform.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl<'a> SubstituteTypeParams<'a> {
8989
let substs = get_syntactic_substs(impl_def).unwrap_or_default();
9090
let generic_def: hir::GenericDef = trait_.into();
9191
let substs_by_param: FxHashMap<_, _> = generic_def
92-
.params(source_scope.db)
92+
.type_params(source_scope.db)
9393
.into_iter()
9494
// this is a trait impl, so we need to skip the first type parameter -- this is a bit hacky
9595
.skip(1)

crates/hir/src/code_model.rs

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ use hir_def::{
1919
src::HasSource as _,
2020
type_ref::{Mutability, TypeRef},
2121
AdtId, AssocContainerId, AssocItemId, AssocItemLoc, AttrDefId, ConstId, DefWithBodyId, EnumId,
22-
FunctionId, GenericDefId, HasModule, ImplId, LocalEnumVariantId, LocalFieldId, LocalModuleId,
23-
Lookup, ModuleId, StaticId, StructId, TraitId, TypeAliasId, TypeParamId, UnionId,
22+
FunctionId, GenericDefId, HasModule, ImplId, LifetimeParamId, LocalEnumVariantId, LocalFieldId,
23+
LocalModuleId, Lookup, ModuleId, StaticId, StructId, TraitId, TypeAliasId, TypeParamId,
24+
UnionId,
2425
};
2526
use hir_def::{find_path::PrefixKind, item_scope::ItemInNs, visibility::Visibility};
2627
use hir_expand::{
@@ -831,7 +832,7 @@ impl SelfParam {
831832
.params
832833
.first()
833834
.map(|param| match *param {
834-
TypeRef::Reference(_, mutability) => mutability.into(),
835+
TypeRef::Reference(.., mutability) => mutability.into(),
835836
_ => Access::Owned,
836837
})
837838
.unwrap_or(Access::Owned)
@@ -1098,8 +1099,25 @@ impl_from!(
10981099
);
10991100

11001101
impl GenericDef {
1101-
pub fn params(self, db: &dyn HirDatabase) -> Vec<TypeParam> {
1102-
let generics: Arc<hir_def::generics::GenericParams> = db.generic_params(self.into());
1102+
pub fn params(self, db: &dyn HirDatabase) -> Vec<GenericParam> {
1103+
let generics = db.generic_params(self.into());
1104+
let ty_params = generics
1105+
.types
1106+
.iter()
1107+
.map(|(local_id, _)| TypeParam { id: TypeParamId { parent: self.into(), local_id } })
1108+
.map(GenericParam::TypeParam);
1109+
let lt_params = generics
1110+
.lifetimes
1111+
.iter()
1112+
.map(|(local_id, _)| LifetimeParam {
1113+
id: LifetimeParamId { parent: self.into(), local_id },
1114+
})
1115+
.map(GenericParam::LifetimeParam);
1116+
ty_params.chain(lt_params).collect()
1117+
}
1118+
1119+
pub fn type_params(self, db: &dyn HirDatabase) -> Vec<TypeParam> {
1120+
let generics = db.generic_params(self.into());
11031121
generics
11041122
.types
11051123
.iter()
@@ -1175,6 +1193,13 @@ impl Local {
11751193
}
11761194
}
11771195

1196+
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
1197+
pub enum GenericParam {
1198+
TypeParam(TypeParam),
1199+
LifetimeParam(LifetimeParam),
1200+
}
1201+
impl_from!(TypeParam, LifetimeParam for GenericParam);
1202+
11781203
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
11791204
pub struct TypeParam {
11801205
pub(crate) id: TypeParamId,
@@ -1215,6 +1240,18 @@ impl TypeParam {
12151240
}
12161241
}
12171242

1243+
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
1244+
pub struct LifetimeParam {
1245+
pub(crate) id: LifetimeParamId,
1246+
}
1247+
1248+
impl LifetimeParam {
1249+
pub fn name(self, db: &dyn HirDatabase) -> Name {
1250+
let params = db.generic_params(self.id.parent);
1251+
params.lifetimes[self.id.local_id].name.clone()
1252+
}
1253+
}
1254+
12181255
// FIXME: rename from `ImplDef` to `Impl`
12191256
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
12201257
pub struct ImplDef {

crates/hir/src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ pub use crate::{
3535
code_model::{
3636
Access, Adt, AsAssocItem, AssocItem, AssocItemContainer, Callable, CallableKind, Const,
3737
Crate, CrateDependency, DefWithBody, Enum, EnumVariant, Field, FieldSource, Function,
38-
GenericDef, HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, ScopeDef, Static,
39-
Struct, Trait, Type, TypeAlias, TypeParam, Union, VariantDef,
38+
GenericDef, HasVisibility, ImplDef, LifetimeParam, Local, MacroDef, Module, ModuleDef,
39+
ScopeDef, Static, Struct, Trait, Type, TypeAlias, TypeParam, Union, VariantDef,
4040
},
4141
has_source::HasSource,
4242
semantics::{PathResolution, Semantics, SemanticsScope},
@@ -56,8 +56,9 @@ pub use hir_def::{
5656
visibility::Visibility,
5757
};
5858
pub use hir_expand::{
59-
name::known, name::AsName, name::Name, ExpandResult, HirFileId, InFile, MacroCallId,
60-
MacroCallLoc, /* FIXME */ MacroDefId, MacroFile, Origin,
59+
name::{known, AsName, Name},
60+
ExpandResult, HirFileId, InFile, MacroCallId, MacroCallLoc, /* FIXME */ MacroDefId,
61+
MacroFile, Origin,
6162
};
6263
pub use hir_ty::display::HirDisplay;
6364

crates/hir_def/src/generics.rs

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::{
2121
keys,
2222
src::HasChildSource,
2323
src::HasSource,
24-
type_ref::{TypeBound, TypeRef},
24+
type_ref::{LifetimeRef, TypeBound, TypeRef},
2525
AdtId, GenericDefId, LocalTypeParamId, Lookup, TypeParamId,
2626
};
2727

@@ -33,6 +33,12 @@ pub struct TypeParamData {
3333
pub provenance: TypeParamProvenance,
3434
}
3535

36+
/// Data about a generic parameter (to a function, struct, impl, ...).
37+
#[derive(Clone, PartialEq, Eq, Debug)]
38+
pub struct LifetimeParamData {
39+
pub name: Name,
40+
}
41+
3642
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
3743
pub enum TypeParamProvenance {
3844
TypeParamList,
@@ -44,7 +50,7 @@ pub enum TypeParamProvenance {
4450
#[derive(Clone, PartialEq, Eq, Debug, Default)]
4551
pub struct GenericParams {
4652
pub types: Arena<TypeParamData>,
47-
// lifetimes: Arena<LocalLifetimeParamId, LifetimeParamData>,
53+
pub lifetimes: Arena<LifetimeParamData>,
4854
pub where_predicates: Vec<WherePredicate>,
4955
}
5056

@@ -53,16 +59,17 @@ pub struct GenericParams {
5359
/// It might still result in multiple actual predicates though, because of
5460
/// associated type bindings like `Iterator<Item = u32>`.
5561
#[derive(Clone, PartialEq, Eq, Debug)]
56-
pub struct WherePredicate {
57-
pub target: WherePredicateTarget,
58-
pub bound: TypeBound,
62+
pub enum WherePredicate {
63+
TypeBound { target: WherePredicateTypeTarget, bound: TypeBound },
64+
Lifetime { target: LifetimeRef, bound: LifetimeRef },
5965
}
6066

6167
#[derive(Clone, PartialEq, Eq, Debug)]
62-
pub enum WherePredicateTarget {
68+
pub enum WherePredicateTypeTarget {
6369
TypeRef(TypeRef),
6470
/// For desugared where predicates that can directly refer to a type param.
6571
TypeParam(LocalTypeParamId),
72+
// FIXME: ForLifetime(Vec<LifetimeParamId>, TypeRef)
6673
}
6774

6875
type SourceMap = ArenaMap<LocalTypeParamId, Either<ast::Trait, ast::TypeParam>>;
@@ -123,7 +130,7 @@ impl GenericParams {
123130
}
124131

125132
fn new(db: &dyn DefDatabase, def: GenericDefId) -> (GenericParams, InFile<SourceMap>) {
126-
let mut generics = GenericParams { types: Arena::default(), where_predicates: Vec::new() };
133+
let mut generics = GenericParams::default();
127134
let mut sm = ArenaMap::default();
128135

129136
// FIXME: add `: Sized` bound for everything except for `Self` in traits
@@ -171,7 +178,7 @@ impl GenericParams {
171178
// add super traits as bounds on Self
172179
// i.e., trait Foo: Bar is equivalent to trait Foo where Self: Bar
173180
let self_param = TypeRef::Path(name![Self].into());
174-
generics.fill_bounds(&lower_ctx, &src.value, self_param);
181+
generics.fill_bounds(&lower_ctx, &src.value, Either::Left(self_param));
175182

176183
generics.fill(&lower_ctx, &mut sm, &src.value);
177184
src.file_id
@@ -218,12 +225,12 @@ impl GenericParams {
218225
&mut self,
219226
lower_ctx: &LowerCtx,
220227
node: &dyn ast::TypeBoundsOwner,
221-
type_ref: TypeRef,
228+
target: Either<TypeRef, LifetimeRef>,
222229
) {
223230
for bound in
224231
node.type_bound_list().iter().flat_map(|type_bound_list| type_bound_list.bounds())
225232
{
226-
self.add_where_predicate_from_bound(lower_ctx, bound, type_ref.clone());
233+
self.add_where_predicate_from_bound(lower_ctx, bound, target.clone());
227234
}
228235
}
229236

@@ -246,19 +253,30 @@ impl GenericParams {
246253
sm.insert(param_id, Either::Right(type_param.clone()));
247254

248255
let type_ref = TypeRef::Path(name.into());
249-
self.fill_bounds(&lower_ctx, &type_param, type_ref);
256+
self.fill_bounds(&lower_ctx, &type_param, Either::Left(type_ref));
257+
}
258+
for lifetime_param in params.lifetime_params() {
259+
let name = lifetime_param
260+
.lifetime_token()
261+
.map_or_else(Name::missing, |tok| Name::new_lifetime(&tok));
262+
let param = LifetimeParamData { name: name.clone() };
263+
let _param_id = self.lifetimes.alloc(param);
264+
let lifetime_ref = LifetimeRef::new_name(name);
265+
self.fill_bounds(&lower_ctx, &lifetime_param, Either::Right(lifetime_ref));
250266
}
251267
}
252268

253269
fn fill_where_predicates(&mut self, lower_ctx: &LowerCtx, where_clause: ast::WhereClause) {
254270
for pred in where_clause.predicates() {
255-
let type_ref = match pred.ty() {
256-
Some(type_ref) => type_ref,
257-
None => continue,
271+
let target = if let Some(type_ref) = pred.ty() {
272+
Either::Left(TypeRef::from_ast(lower_ctx, type_ref))
273+
} else if let Some(lifetime_tok) = pred.lifetime_token() {
274+
Either::Right(LifetimeRef::from_token(lifetime_tok))
275+
} else {
276+
continue;
258277
};
259-
let type_ref = TypeRef::from_ast(lower_ctx, type_ref);
260278
for bound in pred.type_bound_list().iter().flat_map(|l| l.bounds()) {
261-
self.add_where_predicate_from_bound(lower_ctx, bound, type_ref.clone());
279+
self.add_where_predicate_from_bound(lower_ctx, bound, target.clone());
262280
}
263281
}
264282
}
@@ -267,15 +285,24 @@ impl GenericParams {
267285
&mut self,
268286
lower_ctx: &LowerCtx,
269287
bound: ast::TypeBound,
270-
type_ref: TypeRef,
288+
target: Either<TypeRef, LifetimeRef>,
271289
) {
272290
if bound.question_mark_token().is_some() {
273291
// FIXME: remove this bound
274292
return;
275293
}
276294
let bound = TypeBound::from_ast(lower_ctx, bound);
277-
self.where_predicates
278-
.push(WherePredicate { target: WherePredicateTarget::TypeRef(type_ref), bound });
295+
let predicate = match (target, bound) {
296+
(Either::Left(type_ref), bound) => WherePredicate::TypeBound {
297+
target: WherePredicateTypeTarget::TypeRef(type_ref),
298+
bound,
299+
},
300+
(Either::Right(lifetime), TypeBound::Lifetime(bound)) => {
301+
WherePredicate::Lifetime { target: lifetime, bound }
302+
}
303+
_ => return,
304+
};
305+
self.where_predicates.push(predicate);
279306
}
280307

281308
pub(crate) fn fill_implicit_impl_trait_args(&mut self, type_ref: &TypeRef) {
@@ -288,8 +315,8 @@ impl GenericParams {
288315
};
289316
let param_id = self.types.alloc(param);
290317
for bound in bounds {
291-
self.where_predicates.push(WherePredicate {
292-
target: WherePredicateTarget::TypeParam(param_id),
318+
self.where_predicates.push(WherePredicate::TypeBound {
319+
target: WherePredicateTypeTarget::TypeParam(param_id),
293320
bound: bound.clone(),
294321
});
295322
}

crates/hir_def/src/item_tree.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ impl GenericParamsStorage {
255255
}
256256

257257
static EMPTY_GENERICS: GenericParams =
258-
GenericParams { types: Arena::new(), where_predicates: Vec::new() };
258+
GenericParams { types: Arena::new(), lifetimes: Arena::new(), where_predicates: Vec::new() };
259259

260260
#[derive(Default, Debug, Eq, PartialEq)]
261261
struct ItemTreeData {

crates/hir_def/src/item_tree/lower.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use syntax::{
1313
use crate::{
1414
attr::Attrs,
1515
generics::{GenericParams, TypeParamData, TypeParamProvenance},
16+
type_ref::LifetimeRef,
1617
};
1718

1819
use super::*;
@@ -292,12 +293,16 @@ impl Ctx {
292293
let self_type = TypeRef::Path(name![Self].into());
293294
match self_param.kind() {
294295
ast::SelfParamKind::Owned => self_type,
295-
ast::SelfParamKind::Ref => {
296-
TypeRef::Reference(Box::new(self_type), Mutability::Shared)
297-
}
298-
ast::SelfParamKind::MutRef => {
299-
TypeRef::Reference(Box::new(self_type), Mutability::Mut)
300-
}
296+
ast::SelfParamKind::Ref => TypeRef::Reference(
297+
Box::new(self_type),
298+
self_param.lifetime_token().map(LifetimeRef::from_token),
299+
Mutability::Shared,
300+
),
301+
ast::SelfParamKind::MutRef => TypeRef::Reference(
302+
Box::new(self_type),
303+
self_param.lifetime_token().map(LifetimeRef::from_token),
304+
Mutability::Mut,
305+
),
301306
}
302307
}
303308
};
@@ -629,8 +634,7 @@ impl Ctx {
629634
// add super traits as bounds on Self
630635
// i.e., trait Foo: Bar is equivalent to trait Foo where Self: Bar
631636
let self_param = TypeRef::Path(name![Self].into());
632-
generics.fill_bounds(&self.body_ctx, trait_def, self_param);
633-
637+
generics.fill_bounds(&self.body_ctx, trait_def, Either::Left(self_param));
634638
generics.fill(&self.body_ctx, &mut sm, node);
635639
}
636640
GenericsOwner::Impl => {

crates/hir_def/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,13 @@ pub struct TypeParamId {
224224

225225
pub type LocalTypeParamId = Idx<generics::TypeParamData>;
226226

227+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
228+
pub struct LifetimeParamId {
229+
pub parent: GenericDefId,
230+
pub local_id: LocalLifetimeParamId,
231+
}
232+
pub type LocalLifetimeParamId = Idx<generics::LifetimeParamData>;
233+
227234
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
228235
pub enum ContainerId {
229236
ModuleId(ModuleId),

crates/hir_def/src/path.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::{
77
sync::Arc,
88
};
99

10-
use crate::body::LowerCtx;
10+
use crate::{body::LowerCtx, type_ref::LifetimeRef};
1111
use base_db::CrateId;
1212
use hir_expand::{
1313
hygiene::Hygiene,
@@ -145,7 +145,7 @@ pub struct AssociatedTypeBinding {
145145
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
146146
pub enum GenericArg {
147147
Type(TypeRef),
148-
// or lifetime...
148+
Lifetime(LifetimeRef),
149149
}
150150

151151
impl Path {

crates/hir_def/src/path/lower.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use super::AssociatedTypeBinding;
1515
use crate::{
1616
body::LowerCtx,
1717
path::{GenericArg, GenericArgs, ModPath, Path, PathKind},
18-
type_ref::{TypeBound, TypeRef},
18+
type_ref::{LifetimeRef, TypeBound, TypeRef},
1919
};
2020

2121
pub(super) use lower_use::lower_use_tree;
@@ -170,8 +170,14 @@ pub(super) fn lower_generic_args(
170170
bindings.push(AssociatedTypeBinding { name, type_ref, bounds });
171171
}
172172
}
173-
// Lifetimes and constants are ignored for now.
174-
ast::GenericArg::LifetimeArg(_) | ast::GenericArg::ConstArg(_) => (),
173+
ast::GenericArg::LifetimeArg(lifetime_arg) => {
174+
if let Some(lifetime) = lifetime_arg.lifetime_token() {
175+
let lifetime_ref = LifetimeRef::from_token(lifetime);
176+
args.push(GenericArg::Lifetime(lifetime_ref))
177+
}
178+
}
179+
// constants are ignored for now.
180+
ast::GenericArg::ConstArg(_) => (),
175181
}
176182
}
177183

0 commit comments

Comments
 (0)