Skip to content

Commit

Permalink
Make trait aliases source for type parameter
Browse files Browse the repository at this point in the history
Just like traits, trait aliases have implicit type parameter `Self`.
  • Loading branch information
lowr committed Feb 25, 2023
1 parent 03e1cde commit 7fcfd3a
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 46 deletions.
46 changes: 10 additions & 36 deletions crates/hir-def/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,26 +316,14 @@ impl AttrsWithOwner {
let src = it.parent().child_source(db);
RawAttrs::from_attrs_owner(
db.upcast(),
src.with_value(src.value[it.local_id()].as_ref().either(
|it| match it {
ast::TypeOrConstParam::Type(it) => it as _,
ast::TypeOrConstParam::Const(it) => it as _,
},
|it| it as _,
)),
src.with_value(&src.value[it.local_id()]),
)
}
GenericParamId::TypeParamId(it) => {
let src = it.parent().child_source(db);
RawAttrs::from_attrs_owner(
db.upcast(),
src.with_value(src.value[it.local_id()].as_ref().either(
|it| match it {
ast::TypeOrConstParam::Type(it) => it as _,
ast::TypeOrConstParam::Const(it) => it as _,
},
|it| it as _,
)),
src.with_value(&src.value[it.local_id()]),
)
}
GenericParamId::LifetimeParamId(it) => {
Expand Down Expand Up @@ -414,28 +402,14 @@ impl AttrsWithOwner {
},
AttrDefId::ImplId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
AttrDefId::GenericParamId(id) => match id {
GenericParamId::ConstParamId(id) => {
id.parent().child_source(db).map(|source| match &source[id.local_id()] {
Either::Left(ast::TypeOrConstParam::Type(id)) => {
ast::AnyHasAttrs::new(id.clone())
}
Either::Left(ast::TypeOrConstParam::Const(id)) => {
ast::AnyHasAttrs::new(id.clone())
}
Either::Right(id) => ast::AnyHasAttrs::new(id.clone()),
})
}
GenericParamId::TypeParamId(id) => {
id.parent().child_source(db).map(|source| match &source[id.local_id()] {
Either::Left(ast::TypeOrConstParam::Type(id)) => {
ast::AnyHasAttrs::new(id.clone())
}
Either::Left(ast::TypeOrConstParam::Const(id)) => {
ast::AnyHasAttrs::new(id.clone())
}
Either::Right(id) => ast::AnyHasAttrs::new(id.clone()),
})
}
GenericParamId::ConstParamId(id) => id
.parent()
.child_source(db)
.map(|source| ast::AnyHasAttrs::new(source[id.local_id()].clone())),
GenericParamId::TypeParamId(id) => id
.parent()
.child_source(db)
.map(|source| ast::AnyHasAttrs::new(source[id.local_id()].clone())),
GenericParamId::LifetimeParamId(id) => id
.parent
.child_source(db)
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-def/src/child_by_source.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! When *constructing* `hir`, we start at some parent syntax node and recursively
//! lower the children.
//!
//! This modules allows one to go in the opposite direction: start with a syntax
//! This module allows one to go in the opposite direction: start with a syntax
//! node for a *child*, and get its hir.

use either::Either;
Expand Down
21 changes: 15 additions & 6 deletions crates/hir-def/src/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ fn file_id_and_params_of(
}

impl HasChildSource<LocalTypeOrConstParamId> for GenericDefId {
type Value = Either<ast::TypeOrConstParam, ast::Trait>;
type Value = Either<ast::TypeOrConstParam, ast::TraitOrAlias>;
fn child_source(
&self,
db: &dyn DefDatabase,
Expand All @@ -458,11 +458,20 @@ impl HasChildSource<LocalTypeOrConstParamId> for GenericDefId {

let mut params = ArenaMap::default();

// For traits the first type index is `Self`, we need to add it before the other params.
if let GenericDefId::TraitId(id) = *self {
let trait_ref = id.lookup(db).source(db).value;
let idx = idx_iter.next().unwrap();
params.insert(idx, Either::Right(trait_ref));
// For traits and trait aliases the first type index is `Self`, we need to add it before
// the other params.
match *self {
GenericDefId::TraitId(id) => {
let trait_ref = id.lookup(db).source(db).value;
let idx = idx_iter.next().unwrap();
params.insert(idx, Either::Right(ast::TraitOrAlias::Trait(trait_ref)));
}
GenericDefId::TraitAliasId(id) => {
let alias = id.lookup(db).source(db).value;
let idx = idx_iter.next().unwrap();
params.insert(idx, Either::Right(ast::TraitOrAlias::TraitAlias(alias)));
}
_ => {}
}

if let Some(generic_params_list) = generic_params_list {
Expand Down
2 changes: 1 addition & 1 deletion crates/hir/src/has_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ impl HasSource for Impl {
}

impl HasSource for TypeOrConstParam {
type Ast = Either<ast::TypeOrConstParam, ast::Trait>;
type Ast = Either<ast::TypeOrConstParam, ast::TraitOrAlias>;
fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
let child_source = self.id.parent.child_source(db.upcast());
Some(child_source.map(|it| it[self.id.local_id].clone()))
Expand Down
1 change: 1 addition & 0 deletions crates/hir/src/semantics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1331,6 +1331,7 @@ impl<'db> SemanticsImpl<'db> {
})
}
ChildContainer::TraitId(it) => it.resolver(self.db.upcast()),
ChildContainer::TraitAliasId(it) => it.resolver(self.db.upcast()),
ChildContainer::ImplId(it) => it.resolver(self.db.upcast()),
ChildContainer::ModuleId(it) => it.resolver(self.db.upcast()),
ChildContainer::EnumId(it) => it.resolver(self.db.upcast()),
Expand Down
6 changes: 6 additions & 0 deletions crates/hir/src/semantics/source_to_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,9 @@ impl SourceToDefCtx<'_, '_> {
match item {
ast::Item::Module(it) => self.module_to_def(container.with_value(it))?.into(),
ast::Item::Trait(it) => self.trait_to_def(container.with_value(it))?.into(),
ast::Item::TraitAlias(it) => {
self.trait_alias_to_def(container.with_value(it))?.into()
}
ast::Item::Impl(it) => self.impl_to_def(container.with_value(it))?.into(),
ast::Item::Enum(it) => self.enum_to_def(container.with_value(it))?.into(),
ast::Item::TypeAlias(it) => {
Expand Down Expand Up @@ -444,6 +447,7 @@ pub(crate) enum ChildContainer {
DefWithBodyId(DefWithBodyId),
ModuleId(ModuleId),
TraitId(TraitId),
TraitAliasId(TraitAliasId),
ImplId(ImplId),
EnumId(EnumId),
VariantId(VariantId),
Expand All @@ -456,6 +460,7 @@ impl_from! {
DefWithBodyId,
ModuleId,
TraitId,
TraitAliasId,
ImplId,
EnumId,
VariantId,
Expand All @@ -471,6 +476,7 @@ impl ChildContainer {
ChildContainer::DefWithBodyId(it) => it.child_by_source(db, file_id),
ChildContainer::ModuleId(it) => it.child_by_source(db, file_id),
ChildContainer::TraitId(it) => it.child_by_source(db, file_id),
ChildContainer::TraitAliasId(_) => DynMap::default(),
ChildContainer::ImplId(it) => it.child_by_source(db, file_id),
ChildContainer::EnumId(it) => it.child_by_source(db, file_id),
ChildContainer::VariantId(it) => it.child_by_source(db, file_id),
Expand Down
3 changes: 2 additions & 1 deletion crates/ide-db/src/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ impl NameRefClass {
}

impl_from!(
Field, Module, Function, Adt, Variant, Const, Static, Trait, TypeAlias, BuiltinType, Local,
Field, Module, Function, Adt, Variant, Const, Static, Trait, TraitAlias, TypeAlias, BuiltinType, Local,
GenericParam, Label, Macro
for Definition
);
Expand Down Expand Up @@ -623,6 +623,7 @@ impl From<Definition> for Option<ItemInNs> {
Definition::Const(it) => ModuleDef::Const(it),
Definition::Static(it) => ModuleDef::Static(it),
Definition::Trait(it) => ModuleDef::Trait(it),
Definition::TraitAlias(it) => ModuleDef::TraitAlias(it),
Definition::TypeAlias(it) => ModuleDef::TypeAlias(it),
Definition::BuiltinType(it) => ModuleDef::BuiltinType(it),
_ => return None,
Expand Down
10 changes: 9 additions & 1 deletion crates/syntax/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ pub use self::{
generated::{nodes::*, tokens::*},
node_ext::{
AttrKind, FieldKind, Macro, NameLike, NameOrNameRef, PathSegmentKind, SelfParamKind,
SlicePatComponents, StructKind, TypeBoundKind, TypeOrConstParam, VisibilityKind,
SlicePatComponents, StructKind, TraitOrAlias, TypeBoundKind, TypeOrConstParam,
VisibilityKind,
},
operators::{ArithOp, BinaryOp, CmpOp, LogicOp, Ordering, RangeOp, UnaryOp},
token_ext::{CommentKind, CommentPlacement, CommentShape, IsString, QuoteOffsets, Radix},
Expand Down Expand Up @@ -128,6 +129,13 @@ where
}
}

impl<L, R> HasAttrs for Either<L, R>
where
L: HasAttrs,
R: HasAttrs,
{
}

mod support {
use super::{AstChildren, AstNode, SyntaxKind, SyntaxNode, SyntaxToken};

Expand Down
75 changes: 75 additions & 0 deletions crates/syntax/src/ast/node_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,81 @@ impl TypeOrConstParam {
}
}

impl AstNode for TypeOrConstParam {
fn can_cast(kind: SyntaxKind) -> bool
where
Self: Sized,
{
matches!(kind, SyntaxKind::TYPE_PARAM | SyntaxKind::CONST_PARAM)
}

fn cast(syntax: SyntaxNode) -> Option<Self>
where
Self: Sized,
{
let res = match syntax.kind() {
SyntaxKind::TYPE_PARAM => TypeOrConstParam::Type(ast::TypeParam { syntax }),
SyntaxKind::CONST_PARAM => TypeOrConstParam::Const(ast::ConstParam { syntax }),
_ => return None,
};
Some(res)
}

fn syntax(&self) -> &SyntaxNode {
match self {
TypeOrConstParam::Type(it) => it.syntax(),
TypeOrConstParam::Const(it) => it.syntax(),
}
}
}

impl HasAttrs for TypeOrConstParam {}

#[derive(Debug, Clone)]
pub enum TraitOrAlias {
Trait(ast::Trait),
TraitAlias(ast::TraitAlias),
}

impl TraitOrAlias {
pub fn name(&self) -> Option<ast::Name> {
match self {
TraitOrAlias::Trait(x) => x.name(),
TraitOrAlias::TraitAlias(x) => x.name(),
}
}
}

impl AstNode for TraitOrAlias {
fn can_cast(kind: SyntaxKind) -> bool
where
Self: Sized,
{
matches!(kind, SyntaxKind::TRAIT | SyntaxKind::TRAIT_ALIAS)
}

fn cast(syntax: SyntaxNode) -> Option<Self>
where
Self: Sized,
{
let res = match syntax.kind() {
SyntaxKind::TRAIT => TraitOrAlias::Trait(ast::Trait { syntax }),
SyntaxKind::TRAIT_ALIAS => TraitOrAlias::TraitAlias(ast::TraitAlias { syntax }),
_ => return None,
};
Some(res)
}

fn syntax(&self) -> &SyntaxNode {
match self {
TraitOrAlias::Trait(it) => it.syntax(),
TraitOrAlias::TraitAlias(it) => it.syntax(),
}
}
}

impl HasAttrs for TraitOrAlias {}

pub enum VisibilityKind {
In(ast::Path),
PubCrate,
Expand Down

0 comments on commit 7fcfd3a

Please sign in to comment.