Skip to content

Commit 03e1cde

Browse files
committed
Adapt to new AST
1 parent 1a4ca4e commit 03e1cde

File tree

13 files changed

+140
-98
lines changed

13 files changed

+140
-98
lines changed

crates/hir-def/src/data.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,6 @@ impl TraitData {
294294
#[derive(Debug, Clone, PartialEq, Eq)]
295295
pub struct TraitAliasData {
296296
pub name: Name,
297-
pub bounds: Vec<Interned<TypeBound>>,
298297
pub visibility: RawVisibility,
299298
}
300299

@@ -305,11 +304,7 @@ impl TraitAliasData {
305304
let alias = &item_tree[loc.id.value];
306305
let visibility = item_tree[alias.visibility].clone();
307306

308-
Arc::new(TraitAliasData {
309-
name: alias.name.clone(),
310-
bounds: alias.bounds.to_vec(),
311-
visibility,
312-
})
307+
Arc::new(TraitAliasData { name: alias.name.clone(), visibility })
313308
}
314309
}
315310

crates/hir-def/src/generics.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,10 @@ impl GenericParams {
208208
pub(crate) fn fill_bounds(
209209
&mut self,
210210
lower_ctx: &LowerCtx<'_>,
211-
node: &dyn ast::HasTypeBounds,
211+
type_bounds: Option<ast::TypeBoundList>,
212212
target: Either<TypeRef, LifetimeRef>,
213213
) {
214-
for bound in
215-
node.type_bound_list().iter().flat_map(|type_bound_list| type_bound_list.bounds())
216-
{
214+
for bound in type_bounds.iter().flat_map(|type_bound_list| type_bound_list.bounds()) {
217215
self.add_where_predicate_from_bound(lower_ctx, bound, None, target.clone());
218216
}
219217
}
@@ -234,7 +232,11 @@ impl GenericParams {
234232
};
235233
self.type_or_consts.alloc(param.into());
236234
let type_ref = TypeRef::Path(name.into());
237-
self.fill_bounds(lower_ctx, &type_param, Either::Left(type_ref));
235+
self.fill_bounds(
236+
lower_ctx,
237+
type_param.type_bound_list(),
238+
Either::Left(type_ref),
239+
);
238240
}
239241
ast::TypeOrConstParam::Const(const_param) => {
240242
let name = const_param.name().map_or_else(Name::missing, |it| it.as_name());
@@ -256,7 +258,11 @@ impl GenericParams {
256258
let param = LifetimeParamData { name: name.clone() };
257259
self.lifetimes.alloc(param);
258260
let lifetime_ref = LifetimeRef::new_name(name);
259-
self.fill_bounds(lower_ctx, &lifetime_param, Either::Right(lifetime_ref));
261+
self.fill_bounds(
262+
lower_ctx,
263+
lifetime_param.type_bound_list(),
264+
Either::Right(lifetime_ref),
265+
);
260266
}
261267
}
262268

crates/hir-def/src/item_tree.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ mod_items! {
499499
Const in consts -> ast::Const,
500500
Static in statics -> ast::Static,
501501
Trait in traits -> ast::Trait,
502-
TraitAlias in trait_aliases -> ast::Trait,
502+
TraitAlias in trait_aliases -> ast::TraitAlias,
503503
Impl in impls -> ast::Impl,
504504
TypeAlias in type_aliases -> ast::TypeAlias,
505505
Mod in mods -> ast::Module,
@@ -685,8 +685,7 @@ pub struct TraitAlias {
685685
pub name: Name,
686686
pub visibility: RawVisibilityId,
687687
pub generic_params: Interned<GenericParams>,
688-
pub bounds: Box<[Interned<TypeBound>]>,
689-
pub ast_id: FileAstId<ast::Trait>,
688+
pub ast_id: FileAstId<ast::TraitAlias>,
690689
}
691690

692691
#[derive(Debug, Clone, Eq, PartialEq)]

crates/hir-def/src/item_tree/lower.rs

+64-75
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::{collections::hash_map::Entry, sync::Arc};
44

55
use hir_expand::{ast_id_map::AstIdMap, hygiene::Hygiene, HirFileId};
6-
use syntax::ast::{self, HasModuleItem};
6+
use syntax::ast::{self, HasModuleItem, HasTypeBounds};
77

88
use crate::{
99
generics::{GenericParams, TypeParamData, TypeParamProvenance},
@@ -109,7 +109,8 @@ impl<'a> Ctx<'a> {
109109
ast::Item::Static(ast) => self.lower_static(ast)?.into(),
110110
ast::Item::Const(ast) => self.lower_const(ast).into(),
111111
ast::Item::Module(ast) => self.lower_module(ast)?.into(),
112-
ast::Item::Trait(ast) => self.lower_trait(ast)?,
112+
ast::Item::Trait(ast) => self.lower_trait(ast)?.into(),
113+
ast::Item::TraitAlias(ast) => self.lower_trait_alias(ast)?.into(),
113114
ast::Item::Impl(ast) => self.lower_impl(ast)?.into(),
114115
ast::Item::Use(ast) => self.lower_use(ast)?.into(),
115116
ast::Item::ExternCrate(ast) => self.lower_extern_crate(ast)?.into(),
@@ -147,7 +148,7 @@ impl<'a> Ctx<'a> {
147148
fn lower_struct(&mut self, strukt: &ast::Struct) -> Option<FileItemTreeId<Struct>> {
148149
let visibility = self.lower_visibility(strukt);
149150
let name = strukt.name()?.as_name();
150-
let generic_params = self.lower_generic_params(GenericsOwner::Struct, strukt);
151+
let generic_params = self.lower_generic_params(HasImplicitSelf::No, strukt);
151152
let fields = self.lower_fields(&strukt.kind());
152153
let ast_id = self.source_ast_id_map.ast_id(strukt);
153154
let res = Struct { name, visibility, generic_params, fields, ast_id };
@@ -211,7 +212,7 @@ impl<'a> Ctx<'a> {
211212
fn lower_union(&mut self, union: &ast::Union) -> Option<FileItemTreeId<Union>> {
212213
let visibility = self.lower_visibility(union);
213214
let name = union.name()?.as_name();
214-
let generic_params = self.lower_generic_params(GenericsOwner::Union, union);
215+
let generic_params = self.lower_generic_params(HasImplicitSelf::No, union);
215216
let fields = match union.record_field_list() {
216217
Some(record_field_list) => self.lower_fields(&StructKind::Record(record_field_list)),
217218
None => Fields::Record(IdxRange::new(self.next_field_idx()..self.next_field_idx())),
@@ -224,7 +225,7 @@ impl<'a> Ctx<'a> {
224225
fn lower_enum(&mut self, enum_: &ast::Enum) -> Option<FileItemTreeId<Enum>> {
225226
let visibility = self.lower_visibility(enum_);
226227
let name = enum_.name()?.as_name();
227-
let generic_params = self.lower_generic_params(GenericsOwner::Enum, enum_);
228+
let generic_params = self.lower_generic_params(HasImplicitSelf::No, enum_);
228229
let variants = match &enum_.variant_list() {
229230
Some(variant_list) => self.lower_variants(variant_list),
230231
None => IdxRange::new(self.next_variant_idx()..self.next_variant_idx()),
@@ -372,8 +373,7 @@ impl<'a> Ctx<'a> {
372373
ast_id,
373374
flags,
374375
};
375-
res.explicit_generic_params =
376-
self.lower_generic_params(GenericsOwner::Function(&res), func);
376+
res.explicit_generic_params = self.lower_generic_params(HasImplicitSelf::No, func);
377377

378378
Some(id(self.data().functions.alloc(res)))
379379
}
@@ -386,7 +386,7 @@ impl<'a> Ctx<'a> {
386386
let type_ref = type_alias.ty().map(|it| self.lower_type_ref(&it));
387387
let visibility = self.lower_visibility(type_alias);
388388
let bounds = self.lower_type_bounds(type_alias);
389-
let generic_params = self.lower_generic_params(GenericsOwner::TypeAlias, type_alias);
389+
let generic_params = self.lower_generic_params(HasImplicitSelf::No, type_alias);
390390
let ast_id = self.source_ast_id_map.ast_id(type_alias);
391391
let res = TypeAlias {
392392
name,
@@ -439,43 +439,52 @@ impl<'a> Ctx<'a> {
439439
Some(id(self.data().mods.alloc(res)))
440440
}
441441

442-
fn lower_trait(&mut self, trait_def: &ast::Trait) -> Option<ModItem> {
442+
fn lower_trait(&mut self, trait_def: &ast::Trait) -> Option<FileItemTreeId<Trait>> {
443443
let name = trait_def.name()?.as_name();
444444
let visibility = self.lower_visibility(trait_def);
445-
let generic_params = self.lower_generic_params(GenericsOwner::Trait(trait_def), trait_def);
445+
let generic_params =
446+
self.lower_generic_params(HasImplicitSelf::Yes(trait_def.type_bound_list()), trait_def);
446447
let is_auto = trait_def.auto_token().is_some();
447448
let is_unsafe = trait_def.unsafe_token().is_some();
448449
let ast_id = self.source_ast_id_map.ast_id(trait_def);
449450

450-
let item = if trait_def.eq_token().is_some() {
451-
// trait aliases
452-
let bounds = self.lower_type_bounds(trait_def).into_boxed_slice();
453-
let alias = TraitAlias { name, visibility, generic_params, bounds, ast_id };
454-
id(self.data().trait_aliases.alloc(alias)).into()
455-
} else {
456-
// trait definition
457-
let items = trait_def
458-
.assoc_item_list()
459-
.into_iter()
460-
.flat_map(|list| list.assoc_items())
461-
.filter_map(|item| {
462-
let attrs = RawAttrs::new(self.db.upcast(), &item, self.hygiene());
463-
self.lower_assoc_item(&item).map(|item| {
464-
self.add_attrs(ModItem::from(item).into(), attrs);
465-
item
466-
})
451+
let items = trait_def
452+
.assoc_item_list()
453+
.into_iter()
454+
.flat_map(|list| list.assoc_items())
455+
.filter_map(|item| {
456+
let attrs = RawAttrs::new(self.db.upcast(), &item, self.hygiene());
457+
self.lower_assoc_item(&item).map(|item| {
458+
self.add_attrs(ModItem::from(item).into(), attrs);
459+
item
467460
})
468-
.collect();
461+
})
462+
.collect();
469463

470-
let def = Trait { name, visibility, generic_params, is_auto, is_unsafe, items, ast_id };
471-
id(self.data().traits.alloc(def)).into()
472-
};
464+
let def = Trait { name, visibility, generic_params, is_auto, is_unsafe, items, ast_id };
465+
Some(id(self.data().traits.alloc(def)))
466+
}
473467

474-
Some(item)
468+
fn lower_trait_alias(
469+
&mut self,
470+
trait_alias_def: &ast::TraitAlias,
471+
) -> Option<FileItemTreeId<TraitAlias>> {
472+
let name = trait_alias_def.name()?.as_name();
473+
let visibility = self.lower_visibility(trait_alias_def);
474+
let generic_params = self.lower_generic_params(
475+
HasImplicitSelf::Yes(trait_alias_def.type_bound_list()),
476+
trait_alias_def,
477+
);
478+
let ast_id = self.source_ast_id_map.ast_id(trait_alias_def);
479+
480+
let alias = TraitAlias { name, visibility, generic_params, ast_id };
481+
Some(id(self.data().trait_aliases.alloc(alias)))
475482
}
476483

477484
fn lower_impl(&mut self, impl_def: &ast::Impl) -> Option<FileItemTreeId<Impl>> {
478-
let generic_params = self.lower_generic_params(GenericsOwner::Impl, impl_def);
485+
// Note that trait impls don't get implicit `Self` unlike traits, because here they are a
486+
// type alias rather than a type parameter, so this is handled by the resolver.
487+
let generic_params = self.lower_generic_params(HasImplicitSelf::No, impl_def);
479488
// FIXME: If trait lowering fails, due to a non PathType for example, we treat this impl
480489
// as if it was an non-trait impl. Ideally we want to create a unique missing ref that only
481490
// equals itself.
@@ -579,42 +588,29 @@ impl<'a> Ctx<'a> {
579588

580589
fn lower_generic_params(
581590
&mut self,
582-
owner: GenericsOwner<'_>,
591+
has_implicit_self: HasImplicitSelf,
583592
node: &dyn ast::HasGenericParams,
584593
) -> Interned<GenericParams> {
585594
let mut generics = GenericParams::default();
586-
match owner {
587-
GenericsOwner::Function(_)
588-
| GenericsOwner::Struct
589-
| GenericsOwner::Enum
590-
| GenericsOwner::Union
591-
| GenericsOwner::TypeAlias => {
592-
generics.fill(&self.body_ctx, node);
593-
}
594-
GenericsOwner::Trait(trait_def) => {
595-
// traits get the Self type as an implicit first type parameter
596-
generics.type_or_consts.alloc(
597-
TypeParamData {
598-
name: Some(name![Self]),
599-
default: None,
600-
provenance: TypeParamProvenance::TraitSelf,
601-
}
602-
.into(),
603-
);
604-
// add super traits as bounds on Self
605-
// i.e., trait Foo: Bar is equivalent to trait Foo where Self: Bar
606-
let self_param = TypeRef::Path(name![Self].into());
607-
generics.fill_bounds(&self.body_ctx, trait_def, Either::Left(self_param));
608-
generics.fill(&self.body_ctx, node);
609-
}
610-
GenericsOwner::Impl => {
611-
// Note that we don't add `Self` here: in `impl`s, `Self` is not a
612-
// type-parameter, but rather is a type-alias for impl's target
613-
// type, so this is handled by the resolver.
614-
generics.fill(&self.body_ctx, node);
615-
}
595+
596+
if let HasImplicitSelf::Yes(bounds) = has_implicit_self {
597+
// Traits and trait aliases get the Self type as an implicit first type parameter.
598+
generics.type_or_consts.alloc(
599+
TypeParamData {
600+
name: Some(name![Self]),
601+
default: None,
602+
provenance: TypeParamProvenance::TraitSelf,
603+
}
604+
.into(),
605+
);
606+
// add super traits as bounds on Self
607+
// i.e., `trait Foo: Bar` is equivalent to `trait Foo where Self: Bar`
608+
let self_param = TypeRef::Path(name![Self].into());
609+
generics.fill_bounds(&self.body_ctx, bounds, Either::Left(self_param));
616610
}
617611

612+
generics.fill(&self.body_ctx, node);
613+
618614
generics.shrink_to_fit();
619615
Interned::new(generics)
620616
}
@@ -686,17 +682,10 @@ fn desugar_future_path(orig: TypeRef) -> Path {
686682
Path::from_known_path(path, generic_args)
687683
}
688684

689-
enum GenericsOwner<'a> {
690-
/// We need access to the partially-lowered `Function` for lowering `impl Trait` in argument
691-
/// position.
692-
Function(&'a Function),
693-
Struct,
694-
Enum,
695-
Union,
696-
/// The `TraitDef` is needed to fill the source map for the implicit `Self` parameter.
697-
Trait(&'a ast::Trait),
698-
TypeAlias,
699-
Impl,
685+
enum HasImplicitSelf {
686+
/// Inner list is a type bound list for the implicit `Self`.
687+
Yes(Option<ast::TypeBoundList>),
688+
No,
700689
}
701690

702691
fn lower_abi(abi: ast::Abi) -> Interned<str> {

crates/hir-def/src/item_tree/pretty.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -383,13 +383,11 @@ impl<'a> Printer<'a> {
383383
wln!(self, "}}");
384384
}
385385
ModItem::TraitAlias(it) => {
386-
let TraitAlias { name, visibility, generic_params, bounds, ast_id: _ } =
387-
&self.tree[it];
386+
let TraitAlias { name, visibility, generic_params, ast_id: _ } = &self.tree[it];
388387
self.print_visibility(*visibility);
389388
w!(self, "trait {}", name);
390389
self.print_generic_params(generic_params);
391390
w!(self, " = ");
392-
self.print_type_bounds(bounds);
393391
self.print_where_clause(generic_params);
394392
w!(self, ";");
395393
wln!(self);

crates/hir-def/src/keys.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub const STATIC: Key<ast::Static, StaticId> = Key::new();
2121
pub const TYPE_ALIAS: Key<ast::TypeAlias, TypeAliasId> = Key::new();
2222
pub const IMPL: Key<ast::Impl, ImplId> = Key::new();
2323
pub const TRAIT: Key<ast::Trait, TraitId> = Key::new();
24-
pub const TRAIT_ALIAS: Key<ast::Trait, TraitAliasId> = Key::new();
24+
pub const TRAIT_ALIAS: Key<ast::TraitAlias, TraitAliasId> = Key::new();
2525
pub const STRUCT: Key<ast::Struct, StructId> = Key::new();
2626
pub const UNION: Key<ast::Union, UnionId> = Key::new();
2727
pub const ENUM: Key<ast::Enum, EnumId> = Key::new();

crates/hir/src/display.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,9 @@ impl HirDisplay for TraitAlias {
494494
let def_id = GenericDefId::TraitAliasId(self.id);
495495
write_generic_params(def_id, f)?;
496496
f.write_str(" = ")?;
497-
f.write_joined(&data.bounds, " + ")?;
497+
// FIXME: Currently we lower every bounds in a trait alias as a trait bound on `Self` i.e.
498+
// `trait Foo = Bar` is stored and displayed as `trait Foo = where Self: Bar`, which might
499+
// be less readable.
498500
write_where_clause(def_id, f)?;
499501
Ok(())
500502
}

crates/hir/src/has_source.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ impl HasSource for Trait {
123123
}
124124
}
125125
impl HasSource for TraitAlias {
126-
type Ast = ast::Trait;
126+
type Ast = ast::TraitAlias;
127127
fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
128128
Some(self.id.lookup(db.upcast()).source(db.upcast()))
129129
}

crates/hir/src/semantics.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1557,6 +1557,7 @@ to_def_impls![
15571557
(crate::Enum, ast::Enum, enum_to_def),
15581558
(crate::Union, ast::Union, union_to_def),
15591559
(crate::Trait, ast::Trait, trait_to_def),
1560+
(crate::TraitAlias, ast::TraitAlias, trait_alias_to_def),
15601561
(crate::Impl, ast::Impl, impl_to_def),
15611562
(crate::TypeAlias, ast::TypeAlias, type_alias_to_def),
15621563
(crate::Const, ast::Const, const_to_def),

crates/hir/src/semantics/source_to_def.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ use hir_def::{
9393
keys::{self, Key},
9494
AdtId, ConstId, ConstParamId, DefWithBodyId, EnumId, EnumVariantId, FieldId, FunctionId,
9595
GenericDefId, GenericParamId, ImplId, LifetimeParamId, MacroId, ModuleId, StaticId, StructId,
96-
TraitId, TypeAliasId, TypeParamId, UnionId, VariantId,
96+
TraitAliasId, TraitId, TypeAliasId, TypeParamId, UnionId, VariantId,
9797
};
9898
use hir_expand::{attrs::AttrId, name::AsName, HirFileId, MacroCallId};
9999
use rustc_hash::FxHashMap;
@@ -159,6 +159,12 @@ impl SourceToDefCtx<'_, '_> {
159159
pub(super) fn trait_to_def(&mut self, src: InFile<ast::Trait>) -> Option<TraitId> {
160160
self.to_def(src, keys::TRAIT)
161161
}
162+
pub(super) fn trait_alias_to_def(
163+
&mut self,
164+
src: InFile<ast::TraitAlias>,
165+
) -> Option<TraitAliasId> {
166+
self.to_def(src, keys::TRAIT_ALIAS)
167+
}
162168
pub(super) fn impl_to_def(&mut self, src: InFile<ast::Impl>) -> Option<ImplId> {
163169
self.to_def(src, keys::IMPL)
164170
}
@@ -400,6 +406,9 @@ impl SourceToDefCtx<'_, '_> {
400406
ast::Item::Struct(it) => self.struct_to_def(InFile::new(file_id, it))?.into(),
401407
ast::Item::Enum(it) => self.enum_to_def(InFile::new(file_id, it))?.into(),
402408
ast::Item::Trait(it) => self.trait_to_def(InFile::new(file_id, it))?.into(),
409+
ast::Item::TraitAlias(it) => {
410+
self.trait_alias_to_def(InFile::new(file_id, it))?.into()
411+
}
403412
ast::Item::TypeAlias(it) => {
404413
self.type_alias_to_def(InFile::new(file_id, it))?.into()
405414
}

crates/ide-db/src/defs.rs

+1
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ impl NameClass {
305305
ast::Item::Module(it) => Definition::Module(sema.to_def(&it)?),
306306
ast::Item::Static(it) => Definition::Static(sema.to_def(&it)?),
307307
ast::Item::Trait(it) => Definition::Trait(sema.to_def(&it)?),
308+
ast::Item::TraitAlias(it) => Definition::TraitAlias(sema.to_def(&it)?),
308309
ast::Item::TypeAlias(it) => Definition::TypeAlias(sema.to_def(&it)?),
309310
ast::Item::Enum(it) => Definition::Adt(hir::Adt::Enum(sema.to_def(&it)?)),
310311
ast::Item::Struct(it) => Definition::Adt(hir::Adt::Struct(sema.to_def(&it)?)),

0 commit comments

Comments
 (0)