internal: more natural order of sources for TypeParam

We usually use first (left) variant of `Either` for "usual" case, and
use right for odd things. For example, pat source is Pat | SelfParam.
This commit is contained in:
Aleksey Kladov 2021-06-14 22:42:43 +03:00
parent 4cfc767d7f
commit c2015e7d18
5 changed files with 12 additions and 12 deletions
crates
hir/src
hir_def/src
ide/src/display
ide_db/src

@ -127,7 +127,7 @@ impl HasSource for Impl {
} }
impl HasSource for TypeParam { impl HasSource for TypeParam {
type Ast = Either<ast::Trait, ast::TypeParam>; type Ast = Either<ast::TypeParam, ast::Trait>;
fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> { fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
let child_source = self.id.parent.child_source(db.upcast()); let child_source = self.id.parent.child_source(db.upcast());
Some(child_source.map(|it| it[self.id.local_id].clone())) Some(child_source.map(|it| it[self.id.local_id].clone()))

@ -92,7 +92,7 @@ pub enum WherePredicateTypeTarget {
#[derive(Default)] #[derive(Default)]
pub(crate) struct SourceMap { pub(crate) struct SourceMap {
pub(crate) type_params: ArenaMap<LocalTypeParamId, Either<ast::Trait, ast::TypeParam>>, pub(crate) type_params: ArenaMap<LocalTypeParamId, Either<ast::TypeParam, ast::Trait>>,
lifetime_params: ArenaMap<LocalLifetimeParamId, ast::LifetimeParam>, lifetime_params: ArenaMap<LocalLifetimeParamId, ast::LifetimeParam>,
const_params: ArenaMap<LocalConstParamId, ast::ConstParam>, const_params: ArenaMap<LocalConstParamId, ast::ConstParam>,
} }
@ -199,7 +199,7 @@ impl GenericParams {
default: None, default: None,
provenance: TypeParamProvenance::TraitSelf, provenance: TypeParamProvenance::TraitSelf,
}); });
sm.type_params.insert(self_param_id, Either::Left(src.value.clone())); sm.type_params.insert(self_param_id, Either::Right(src.value.clone()));
// add super traits as bounds on Self // add super traits as bounds on Self
// i.e., trait Foo: Bar is equivalent to trait Foo where Self: Bar // i.e., trait Foo: Bar is equivalent to trait Foo where Self: Bar
let self_param = TypeRef::Path(name![Self].into()); let self_param = TypeRef::Path(name![Self].into());
@ -277,7 +277,7 @@ impl GenericParams {
provenance: TypeParamProvenance::TypeParamList, provenance: TypeParamProvenance::TypeParamList,
}; };
let param_id = self.types.alloc(param); let param_id = self.types.alloc(param);
sm.type_params.insert(param_id, Either::Right(type_param.clone())); sm.type_params.insert(param_id, Either::Left(type_param.clone()));
let type_ref = TypeRef::Path(name.into()); let type_ref = TypeRef::Path(name.into());
self.fill_bounds(lower_ctx, &type_param, Either::Left(type_ref)); self.fill_bounds(lower_ctx, &type_param, Either::Left(type_ref));
@ -413,7 +413,7 @@ impl GenericParams {
} }
impl HasChildSource<LocalTypeParamId> for GenericDefId { impl HasChildSource<LocalTypeParamId> for GenericDefId {
type Value = Either<ast::Trait, ast::TypeParam>; type Value = Either<ast::TypeParam, ast::Trait>;
fn child_source( fn child_source(
&self, &self,
db: &dyn DefDatabase, db: &dyn DefDatabase,
@ -449,7 +449,7 @@ impl ChildBySource for GenericDefId {
let sm = sm.as_ref(); let sm = sm.as_ref();
for (local_id, src) in sm.value.type_params.iter() { for (local_id, src) in sm.value.type_params.iter() {
let id = TypeParamId { parent: *self, local_id }; let id = TypeParamId { parent: *self, local_id };
if let Either::Right(type_param) = src { if let Either::Left(type_param) = src {
res[keys::TYPE_PARAM].insert(sm.with_value(type_param.clone()), id) res[keys::TYPE_PARAM].insert(sm.with_value(type_param.clone()), id)
} }
} }

@ -674,7 +674,7 @@ impl<'a> Ctx<'a> {
default: None, default: None,
provenance: TypeParamProvenance::TraitSelf, provenance: TypeParamProvenance::TraitSelf,
}); });
sm.type_params.insert(self_param_id, Either::Left(trait_def.clone())); sm.type_params.insert(self_param_id, Either::Right(trait_def.clone()));
// add super traits as bounds on Self // add super traits as bounds on Self
// i.e., trait Foo: Bar is equivalent to trait Foo where Self: Bar // i.e., trait Foo: Bar is equivalent to trait Foo where Self: Bar
let self_param = TypeRef::Path(name![Self].into()); let self_param = TypeRef::Path(name![Self].into());

@ -442,10 +442,10 @@ impl TryToNav for hir::TypeParam {
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
let src = self.source(db)?; let src = self.source(db)?;
let full_range = match &src.value { let full_range = match &src.value {
Either::Left(it) => it Either::Left(type_param) => type_param.syntax().text_range(),
Either::Right(trait_) => trait_
.name() .name()
.map_or_else(|| it.syntax().text_range(), |name| name.syntax().text_range()), .map_or_else(|| trait_.syntax().text_range(), |name| name.syntax().text_range()),
Either::Right(it) => it.syntax().text_range(),
}; };
let focus_range = match &src.value { let focus_range = match &src.value {
Either::Left(it) => it.name(), Either::Left(it) => it.name(),

@ -143,8 +143,8 @@ impl Definition {
hir::GenericParam::TypeParam(type_param) => { hir::GenericParam::TypeParam(type_param) => {
let src = type_param.source(sema.db)?; let src = type_param.source(sema.db)?;
let name = match &src.value { let name = match &src.value {
Either::Left(_) => return None, Either::Left(type_param) => type_param.name()?,
Either::Right(type_param) => type_param.name()?, Either::Right(_trait) => return None,
}; };
src.with_value(name.syntax()).original_file_range(sema.db) src.with_value(name.syntax()).original_file_range(sema.db)
} }