1 //! Many kinds of items or constructs can have generic parameters: functions,
2 //! structs, impls, traits, etc. This module provides a common HIR for these
3 //! generic parameters. See also the `Generics` type and the `generics_of` query
10 ExpandResult, HirFileId, InFile,
12 use la_arena::{Arena, ArenaMap};
13 use once_cell::unsync::Lazy;
14 use std::ops::DerefMut;
15 use syntax::ast::{self, HasGenericParams, HasName, HasTypeBounds};
18 body::{Expander, LowerCtx},
19 child_by_source::ChildBySource,
24 src::{HasChildSource, HasSource},
25 type_ref::{LifetimeRef, TypeBound, TypeRef},
26 AdtId, ConstParamId, GenericDefId, HasModule, LifetimeParamId, LocalConstParamId,
27 LocalLifetimeParamId, LocalTypeParamId, Lookup, TypeParamId,
30 /// Data about a generic type parameter (to a function, struct, impl, ...).
31 #[derive(Clone, PartialEq, Eq, Debug, Hash)]
32 pub struct TypeParamData {
33 pub name: Option<Name>,
34 pub default: Option<Interned<TypeRef>>,
35 pub provenance: TypeParamProvenance,
38 /// Data about a generic lifetime parameter (to a function, struct, impl, ...).
39 #[derive(Clone, PartialEq, Eq, Debug, Hash)]
40 pub struct LifetimeParamData {
44 /// Data about a generic const parameter (to a function, struct, impl, ...).
45 #[derive(Clone, PartialEq, Eq, Debug, Hash)]
46 pub struct ConstParamData {
48 pub ty: Interned<TypeRef>,
51 #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
52 pub enum TypeParamProvenance {
58 /// Data about the generic parameters of a function, struct, impl, etc.
59 #[derive(Clone, PartialEq, Eq, Debug, Default, Hash)]
60 pub struct GenericParams {
61 pub types: Arena<TypeParamData>,
62 pub lifetimes: Arena<LifetimeParamData>,
63 pub consts: Arena<ConstParamData>,
64 pub where_predicates: Vec<WherePredicate>,
67 /// A single predicate from a where clause, i.e. `where Type: Trait`. Combined
68 /// where clauses like `where T: Foo + Bar` are turned into multiple of these.
69 /// It might still result in multiple actual predicates though, because of
70 /// associated type bindings like `Iterator<Item = u32>`.
71 #[derive(Clone, PartialEq, Eq, Debug, Hash)]
72 pub enum WherePredicate {
74 target: WherePredicateTypeTarget,
75 bound: Interned<TypeBound>,
82 lifetimes: Box<[Name]>,
83 target: WherePredicateTypeTarget,
84 bound: Interned<TypeBound>,
88 #[derive(Clone, PartialEq, Eq, Debug, Hash)]
89 pub enum WherePredicateTypeTarget {
90 TypeRef(Interned<TypeRef>),
91 /// For desugared where predicates that can directly refer to a type param.
92 TypeParam(LocalTypeParamId),
96 pub(crate) fn generic_params_query(
99 ) -> Interned<GenericParams> {
100 let _p = profile::span("generic_params_query");
103 GenericDefId::FunctionId(id) => {
104 let loc = id.lookup(db);
105 let tree = loc.id.item_tree(db);
106 let item = &tree[loc.id.value];
108 let mut generic_params = GenericParams::clone(&item.explicit_generic_params);
110 let module = loc.container.module(db);
111 let func_data = db.function_data(id);
113 // Don't create an `Expander` nor call `loc.source(db)` if not needed since this
114 // causes a reparse after the `ItemTree` has been created.
115 let mut expander = Lazy::new(|| Expander::new(db, loc.source(db).file_id, module));
116 for param in &func_data.params {
117 generic_params.fill_implicit_impl_trait_args(db, &mut expander, param);
120 Interned::new(generic_params)
122 GenericDefId::AdtId(AdtId::StructId(id)) => {
123 let id = id.lookup(db).id;
124 let tree = id.item_tree(db);
125 let item = &tree[id.value];
126 item.generic_params.clone()
128 GenericDefId::AdtId(AdtId::EnumId(id)) => {
129 let id = id.lookup(db).id;
130 let tree = id.item_tree(db);
131 let item = &tree[id.value];
132 item.generic_params.clone()
134 GenericDefId::AdtId(AdtId::UnionId(id)) => {
135 let id = id.lookup(db).id;
136 let tree = id.item_tree(db);
137 let item = &tree[id.value];
138 item.generic_params.clone()
140 GenericDefId::TraitId(id) => {
141 let id = id.lookup(db).id;
142 let tree = id.item_tree(db);
143 let item = &tree[id.value];
144 item.generic_params.clone()
146 GenericDefId::TypeAliasId(id) => {
147 let id = id.lookup(db).id;
148 let tree = id.item_tree(db);
149 let item = &tree[id.value];
150 item.generic_params.clone()
152 GenericDefId::ImplId(id) => {
153 let id = id.lookup(db).id;
154 let tree = id.item_tree(db);
155 let item = &tree[id.value];
156 item.generic_params.clone()
158 GenericDefId::EnumVariantId(_) | GenericDefId::ConstId(_) => {
159 Interned::new(GenericParams::default())
164 pub(crate) fn fill(&mut self, lower_ctx: &LowerCtx, node: &dyn HasGenericParams) {
165 if let Some(params) = node.generic_param_list() {
166 self.fill_params(lower_ctx, params)
168 if let Some(where_clause) = node.where_clause() {
169 self.fill_where_predicates(lower_ctx, where_clause);
173 pub(crate) fn fill_bounds(
175 lower_ctx: &LowerCtx,
176 node: &dyn ast::HasTypeBounds,
177 target: Either<TypeRef, LifetimeRef>,
180 node.type_bound_list().iter().flat_map(|type_bound_list| type_bound_list.bounds())
182 self.add_where_predicate_from_bound(lower_ctx, bound, None, target.clone());
186 fn fill_params(&mut self, lower_ctx: &LowerCtx, params: ast::GenericParamList) {
187 for type_param in params.type_params() {
188 let name = type_param.name().map_or_else(Name::missing, |it| it.as_name());
189 // FIXME: Use `Path::from_src`
191 type_param.default_type().map(|it| Interned::new(TypeRef::from_ast(lower_ctx, it)));
192 let param = TypeParamData {
193 name: Some(name.clone()),
195 provenance: TypeParamProvenance::TypeParamList,
197 self.types.alloc(param);
198 let type_ref = TypeRef::Path(name.into());
199 self.fill_bounds(lower_ctx, &type_param, Either::Left(type_ref));
201 for lifetime_param in params.lifetime_params() {
203 lifetime_param.lifetime().map_or_else(Name::missing, |lt| Name::new_lifetime(<));
204 let param = LifetimeParamData { name: name.clone() };
205 self.lifetimes.alloc(param);
206 let lifetime_ref = LifetimeRef::new_name(name);
207 self.fill_bounds(lower_ctx, &lifetime_param, Either::Right(lifetime_ref));
209 for const_param in params.const_params() {
210 let name = const_param.name().map_or_else(Name::missing, |it| it.as_name());
211 let ty = const_param.ty().map_or(TypeRef::Error, |it| TypeRef::from_ast(lower_ctx, it));
212 let param = ConstParamData { name, ty: Interned::new(ty) };
213 self.consts.alloc(param);
217 fn fill_where_predicates(&mut self, lower_ctx: &LowerCtx, where_clause: ast::WhereClause) {
218 for pred in where_clause.predicates() {
219 let target = if let Some(type_ref) = pred.ty() {
220 Either::Left(TypeRef::from_ast(lower_ctx, type_ref))
221 } else if let Some(lifetime) = pred.lifetime() {
222 Either::Right(LifetimeRef::new(&lifetime))
227 let lifetimes: Option<Box<_>> = pred.generic_param_list().map(|param_list| {
228 // Higher-Ranked Trait Bounds
231 .map(|lifetime_param| {
234 .map_or_else(Name::missing, |lt| Name::new_lifetime(<))
238 for bound in pred.type_bound_list().iter().flat_map(|l| l.bounds()) {
239 self.add_where_predicate_from_bound(
249 fn add_where_predicate_from_bound(
251 lower_ctx: &LowerCtx,
252 bound: ast::TypeBound,
253 hrtb_lifetimes: Option<&Box<[Name]>>,
254 target: Either<TypeRef, LifetimeRef>,
256 let bound = TypeBound::from_ast(lower_ctx, bound);
257 let predicate = match (target, bound) {
258 (Either::Left(type_ref), bound) => match hrtb_lifetimes {
259 Some(hrtb_lifetimes) => WherePredicate::ForLifetime {
260 lifetimes: hrtb_lifetimes.clone(),
261 target: WherePredicateTypeTarget::TypeRef(Interned::new(type_ref)),
262 bound: Interned::new(bound),
264 None => WherePredicate::TypeBound {
265 target: WherePredicateTypeTarget::TypeRef(Interned::new(type_ref)),
266 bound: Interned::new(bound),
269 (Either::Right(lifetime), TypeBound::Lifetime(bound)) => {
270 WherePredicate::Lifetime { target: lifetime, bound }
274 self.where_predicates.push(predicate);
277 pub(crate) fn fill_implicit_impl_trait_args(
279 db: &dyn DefDatabase,
280 expander: &mut impl DerefMut<Target = Expander>,
283 type_ref.walk(&mut |type_ref| {
284 if let TypeRef::ImplTrait(bounds) = type_ref {
285 let param = TypeParamData {
288 provenance: TypeParamProvenance::ArgumentImplTrait,
290 let param_id = self.types.alloc(param);
291 for bound in bounds {
292 self.where_predicates.push(WherePredicate::TypeBound {
293 target: WherePredicateTypeTarget::TypeParam(param_id),
294 bound: bound.clone(),
298 if let TypeRef::Macro(mc) = type_ref {
299 let macro_call = mc.to_node(db.upcast());
300 match expander.enter_expand::<ast::Type>(db, macro_call) {
301 Ok(ExpandResult { value: Some((mark, expanded)), .. }) => {
302 let ctx = LowerCtx::new(db, expander.current_file_id());
303 let type_ref = TypeRef::from_ast(&ctx, expanded);
304 self.fill_implicit_impl_trait_args(db, expander, &type_ref);
305 expander.exit(db, mark);
313 pub(crate) fn shrink_to_fit(&mut self) {
314 let Self { consts, lifetimes, types, where_predicates } = self;
315 consts.shrink_to_fit();
316 lifetimes.shrink_to_fit();
317 types.shrink_to_fit();
318 where_predicates.shrink_to_fit();
321 pub fn find_type_by_name(&self, name: &Name) -> Option<LocalTypeParamId> {
324 .find_map(|(id, p)| if p.name.as_ref() == Some(name) { Some(id) } else { None })
327 pub fn find_const_by_name(&self, name: &Name) -> Option<LocalConstParamId> {
328 self.consts.iter().find_map(|(id, p)| if p.name == *name { Some(id) } else { None })
331 pub fn find_trait_self_param(&self) -> Option<LocalTypeParamId> {
332 self.types.iter().find_map(|(id, p)| {
333 if p.provenance == TypeParamProvenance::TraitSelf {
342 fn file_id_and_params_of(
344 db: &dyn DefDatabase,
345 ) -> (HirFileId, Option<ast::GenericParamList>) {
347 GenericDefId::FunctionId(it) => {
348 let src = it.lookup(db).source(db);
349 (src.file_id, src.value.generic_param_list())
351 GenericDefId::AdtId(AdtId::StructId(it)) => {
352 let src = it.lookup(db).source(db);
353 (src.file_id, src.value.generic_param_list())
355 GenericDefId::AdtId(AdtId::UnionId(it)) => {
356 let src = it.lookup(db).source(db);
357 (src.file_id, src.value.generic_param_list())
359 GenericDefId::AdtId(AdtId::EnumId(it)) => {
360 let src = it.lookup(db).source(db);
361 (src.file_id, src.value.generic_param_list())
363 GenericDefId::TraitId(it) => {
364 let src = it.lookup(db).source(db);
365 (src.file_id, src.value.generic_param_list())
367 GenericDefId::TypeAliasId(it) => {
368 let src = it.lookup(db).source(db);
369 (src.file_id, src.value.generic_param_list())
371 GenericDefId::ImplId(it) => {
372 let src = it.lookup(db).source(db);
373 (src.file_id, src.value.generic_param_list())
375 // We won't be using this ID anyway
376 GenericDefId::EnumVariantId(_) | GenericDefId::ConstId(_) => (FileId(!0).into(), None),
380 impl HasChildSource<LocalTypeParamId> for GenericDefId {
381 type Value = Either<ast::TypeParam, ast::Trait>;
384 db: &dyn DefDatabase,
385 ) -> InFile<ArenaMap<LocalTypeParamId, Self::Value>> {
386 let generic_params = db.generic_params(*self);
387 let mut idx_iter = generic_params.types.iter().map(|(idx, _)| idx);
389 let (file_id, generic_params_list) = file_id_and_params_of(*self, db);
391 let mut params = ArenaMap::default();
393 // For traits the first type index is `Self`, we need to add it before the other params.
394 if let GenericDefId::TraitId(id) = *self {
395 let trait_ref = id.lookup(db).source(db).value;
396 let idx = idx_iter.next().unwrap();
397 params.insert(idx, Either::Right(trait_ref))
400 if let Some(generic_params_list) = generic_params_list {
401 for (idx, ast_param) in idx_iter.zip(generic_params_list.type_params()) {
402 params.insert(idx, Either::Left(ast_param));
406 InFile::new(file_id, params)
410 impl HasChildSource<LocalLifetimeParamId> for GenericDefId {
411 type Value = ast::LifetimeParam;
414 db: &dyn DefDatabase,
415 ) -> InFile<ArenaMap<LocalLifetimeParamId, Self::Value>> {
416 let generic_params = db.generic_params(*self);
417 let idx_iter = generic_params.lifetimes.iter().map(|(idx, _)| idx);
419 let (file_id, generic_params_list) = file_id_and_params_of(*self, db);
421 let mut params = ArenaMap::default();
423 if let Some(generic_params_list) = generic_params_list {
424 for (idx, ast_param) in idx_iter.zip(generic_params_list.lifetime_params()) {
425 params.insert(idx, ast_param);
429 InFile::new(file_id, params)
433 impl HasChildSource<LocalConstParamId> for GenericDefId {
434 type Value = ast::ConstParam;
437 db: &dyn DefDatabase,
438 ) -> InFile<ArenaMap<LocalConstParamId, Self::Value>> {
439 let generic_params = db.generic_params(*self);
440 let idx_iter = generic_params.consts.iter().map(|(idx, _)| idx);
442 let (file_id, generic_params_list) = file_id_and_params_of(*self, db);
444 let mut params = ArenaMap::default();
446 if let Some(generic_params_list) = generic_params_list {
447 for (idx, ast_param) in idx_iter.zip(generic_params_list.const_params()) {
448 params.insert(idx, ast_param);
452 InFile::new(file_id, params)
456 impl ChildBySource for GenericDefId {
457 fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, _: HirFileId) {
458 let generic_params = db.generic_params(*self);
459 let mut types_idx_iter = generic_params.types.iter().map(|(idx, _)| idx);
460 let lts_idx_iter = generic_params.lifetimes.iter().map(|(idx, _)| idx);
461 let consts_idx_iter = generic_params.consts.iter().map(|(idx, _)| idx);
463 let (file_id, generic_params_list) = file_id_and_params_of(*self, db);
465 // For traits the first type index is `Self`, skip it.
466 if let GenericDefId::TraitId(_) = *self {
467 types_idx_iter.next().unwrap(); // advance_by(1);
470 if let Some(generic_params_list) = generic_params_list {
471 for (local_id, ast_param) in types_idx_iter.zip(generic_params_list.type_params()) {
472 let id = TypeParamId { parent: *self, local_id };
473 res[keys::TYPE_PARAM].insert(InFile::new(file_id, ast_param), id);
475 for (local_id, ast_param) in lts_idx_iter.zip(generic_params_list.lifetime_params()) {
476 let id = LifetimeParamId { parent: *self, local_id };
477 res[keys::LIFETIME_PARAM].insert(InFile::new(file_id, ast_param), id);
479 for (local_id, ast_param) in consts_idx_iter.zip(generic_params_list.const_params()) {
480 let id = ConstParamId { parent: *self, local_id };
481 res[keys::CONST_PARAM].insert(InFile::new(file_id, ast_param), id);