about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAleksey Kladov <aleksey.kladov@gmail.com>2021-06-14 22:42:43 +0300
committerAleksey Kladov <aleksey.kladov@gmail.com>2021-06-14 22:42:43 +0300
commitc2015e7d182f3cb2cebe686127dd6a3e683df9e6 (patch)
treeeb46a7d8ad5f0549fd79a51268d79859a1cb6c2d
parent4cfc767d7fadeab025227d67f104065c9e8a55d3 (diff)
downloadrust-c2015e7d182f3cb2cebe686127dd6a3e683df9e6.tar.gz
rust-c2015e7d182f3cb2cebe686127dd6a3e683df9e6.zip
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.
-rw-r--r--crates/hir/src/has_source.rs2
-rw-r--r--crates/hir_def/src/generics.rs10
-rw-r--r--crates/hir_def/src/item_tree/lower.rs2
-rw-r--r--crates/ide/src/display/navigation_target.rs6
-rw-r--r--crates/ide_db/src/rename.rs4
5 files changed, 12 insertions, 12 deletions
diff --git a/crates/hir/src/has_source.rs b/crates/hir/src/has_source.rs
index dc10a4d0fab..197149c5ebb 100644
--- a/crates/hir/src/has_source.rs
+++ b/crates/hir/src/has_source.rs
@@ -127,7 +127,7 @@ impl HasSource for Impl {
 }
 
 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>> {
         let child_source = self.id.parent.child_source(db.upcast());
         Some(child_source.map(|it| it[self.id.local_id].clone()))
diff --git a/crates/hir_def/src/generics.rs b/crates/hir_def/src/generics.rs
index 6933f6e3c33..0f04b2bae29 100644
--- a/crates/hir_def/src/generics.rs
+++ b/crates/hir_def/src/generics.rs
@@ -92,7 +92,7 @@ pub enum WherePredicateTypeTarget {
 
 #[derive(Default)]
 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>,
     const_params: ArenaMap<LocalConstParamId, ast::ConstParam>,
 }
@@ -199,7 +199,7 @@ impl GenericParams {
                     default: None,
                     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
                 // i.e., trait Foo: Bar is equivalent to trait Foo where Self: Bar
                 let self_param = TypeRef::Path(name![Self].into());
@@ -277,7 +277,7 @@ impl GenericParams {
                 provenance: TypeParamProvenance::TypeParamList,
             };
             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());
             self.fill_bounds(lower_ctx, &type_param, Either::Left(type_ref));
@@ -413,7 +413,7 @@ impl GenericParams {
 }
 
 impl HasChildSource<LocalTypeParamId> for GenericDefId {
-    type Value = Either<ast::Trait, ast::TypeParam>;
+    type Value = Either<ast::TypeParam, ast::Trait>;
     fn child_source(
         &self,
         db: &dyn DefDatabase,
@@ -449,7 +449,7 @@ impl ChildBySource for GenericDefId {
         let sm = sm.as_ref();
         for (local_id, src) in sm.value.type_params.iter() {
             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)
             }
         }
diff --git a/crates/hir_def/src/item_tree/lower.rs b/crates/hir_def/src/item_tree/lower.rs
index 3f90bda742e..5b1386406b8 100644
--- a/crates/hir_def/src/item_tree/lower.rs
+++ b/crates/hir_def/src/item_tree/lower.rs
@@ -674,7 +674,7 @@ impl<'a> Ctx<'a> {
                     default: None,
                     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
                 // i.e., trait Foo: Bar is equivalent to trait Foo where Self: Bar
                 let self_param = TypeRef::Path(name![Self].into());
diff --git a/crates/ide/src/display/navigation_target.rs b/crates/ide/src/display/navigation_target.rs
index b75ec411cf6..455b3245691 100644
--- a/crates/ide/src/display/navigation_target.rs
+++ b/crates/ide/src/display/navigation_target.rs
@@ -442,10 +442,10 @@ impl TryToNav for hir::TypeParam {
     fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
         let src = self.source(db)?;
         let full_range = match &src.value {
-            Either::Left(it) => it
+            Either::Left(type_param) => type_param.syntax().text_range(),
+            Either::Right(trait_) => trait_
                 .name()
-                .map_or_else(|| it.syntax().text_range(), |name| name.syntax().text_range()),
-            Either::Right(it) => it.syntax().text_range(),
+                .map_or_else(|| trait_.syntax().text_range(), |name| name.syntax().text_range()),
         };
         let focus_range = match &src.value {
             Either::Left(it) => it.name(),
diff --git a/crates/ide_db/src/rename.rs b/crates/ide_db/src/rename.rs
index 82855725fa1..643e6778129 100644
--- a/crates/ide_db/src/rename.rs
+++ b/crates/ide_db/src/rename.rs
@@ -143,8 +143,8 @@ impl Definition {
                 hir::GenericParam::TypeParam(type_param) => {
                     let src = type_param.source(sema.db)?;
                     let name = match &src.value {
-                        Either::Left(_) => return None,
-                        Either::Right(type_param) => type_param.name()?,
+                        Either::Left(type_param) => type_param.name()?,
+                        Either::Right(_trait) => return None,
                     };
                     src.with_value(name.syntax()).original_file_range(sema.db)
                 }