Skip to content

Commit 8a7e431

Browse files
committed
replace either with ast::TypeOrConstParam
1 parent e2e6ed6 commit 8a7e431

File tree

10 files changed

+56
-33
lines changed

10 files changed

+56
-33
lines changed

crates/hir/src/has_source.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ impl HasSource for Impl {
137137
}
138138

139139
impl HasSource for TypeOrConstParam {
140-
type Ast = Either<Either<ast::TypeParam, ast::ConstParam>, ast::Trait>;
140+
type Ast = Either<ast::TypeOrConstParam, ast::Trait>;
141141
fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
142142
let child_source = self.id.parent.child_source(db.upcast());
143143
Some(child_source.map(|it| it[self.id.local_id].clone()))

crates/hir_def/src/attr.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,9 @@ impl AttrsWithOwner {
349349
RawAttrs::from_attrs_owner(
350350
db,
351351
src.with_value(src.value[it.local_id].as_ref().either(
352-
|it| {
353-
it.as_ref().either(|it| it as &dyn ast::HasAttrs, |it| it as _) as _
352+
|it| match it {
353+
ast::TypeOrConstParam::Type(it) => it as _,
354+
ast::TypeOrConstParam::Const(it) => it as _,
354355
},
355356
|it| it as _,
356357
)),
@@ -430,8 +431,12 @@ impl AttrsWithOwner {
430431
AttrDefId::GenericParamId(id) => match id {
431432
GenericParamId::TypeParamId(id) | GenericParamId::ConstParamId(id) => {
432433
id.parent.child_source(db).map(|source| match &source[id.local_id] {
433-
Either::Left(Either::Left(id)) => ast::AnyHasAttrs::new(id.clone()),
434-
Either::Left(Either::Right(id)) => ast::AnyHasAttrs::new(id.clone()),
434+
Either::Left(ast::TypeOrConstParam::Type(id)) => {
435+
ast::AnyHasAttrs::new(id.clone())
436+
}
437+
Either::Left(ast::TypeOrConstParam::Const(id)) => {
438+
ast::AnyHasAttrs::new(id.clone())
439+
}
435440
Either::Right(id) => ast::AnyHasAttrs::new(id.clone()),
436441
})
437442
}

crates/hir_def/src/generics.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ impl GenericParams {
227227
fn fill_params(&mut self, lower_ctx: &LowerCtx, params: ast::GenericParamList) {
228228
for type_or_const_param in params.type_or_const_params() {
229229
match type_or_const_param {
230-
Either::Left(type_param) => {
230+
ast::TypeOrConstParam::Type(type_param) => {
231231
let name = type_param.name().map_or_else(Name::missing, |it| it.as_name());
232232
// FIXME: Use `Path::from_src`
233233
let default = type_param
@@ -242,7 +242,7 @@ impl GenericParams {
242242
let type_ref = TypeRef::Path(name.into());
243243
self.fill_bounds(lower_ctx, &type_param, Either::Left(type_ref));
244244
}
245-
Either::Right(const_param) => {
245+
ast::TypeOrConstParam::Const(const_param) => {
246246
let name = const_param.name().map_or_else(Name::missing, |it| it.as_name());
247247
let ty = const_param
248248
.ty()
@@ -425,7 +425,7 @@ fn file_id_and_params_of(
425425
}
426426

427427
impl HasChildSource<LocalTypeOrConstParamId> for GenericDefId {
428-
type Value = Either<Either<ast::TypeParam, ast::ConstParam>, ast::Trait>;
428+
type Value = Either<ast::TypeOrConstParam, ast::Trait>;
429429
fn child_source(
430430
&self,
431431
db: &dyn DefDatabase,
@@ -496,8 +496,12 @@ impl ChildBySource for GenericDefId {
496496
{
497497
let id = TypeOrConstParamId { parent: *self, local_id };
498498
match ast_param {
499-
Either::Left(a) => res[keys::TYPE_PARAM].insert(InFile::new(file_id, a), id),
500-
Either::Right(a) => res[keys::CONST_PARAM].insert(InFile::new(file_id, a), id),
499+
ast::TypeOrConstParam::Type(a) => {
500+
res[keys::TYPE_PARAM].insert(InFile::new(file_id, a), id)
501+
}
502+
ast::TypeOrConstParam::Const(a) => {
503+
res[keys::CONST_PARAM].insert(InFile::new(file_id, a), id)
504+
}
501505
}
502506
}
503507
for (local_id, ast_param) in lts_idx_iter.zip(generic_params_list.lifetime_params()) {

crates/ide/src/navigation_target.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,8 @@ impl TryToNav for hir::TypeParam {
458458
let name = self.name(db).to_smol_str();
459459

460460
let value = match value {
461-
Either::Left(Either::Left(x)) => Either::Left(x),
462-
Either::Left(Either::Right(_)) => {
461+
Either::Left(ast::TypeOrConstParam::Type(x)) => Either::Left(x),
462+
Either::Left(ast::TypeOrConstParam::Const(_)) => {
463463
never!();
464464
return None;
465465
}
@@ -524,8 +524,11 @@ impl TryToNav for hir::ConstParam {
524524
let name = self.name(db).to_smol_str();
525525

526526
let value = match value {
527-
Either::Left(Either::Right(x)) => x,
528-
_ => unreachable!(),
527+
Either::Left(ast::TypeOrConstParam::Const(x)) => x,
528+
_ => {
529+
never!();
530+
return None;
531+
}
529532
};
530533

531534
let focus_range = value

crates/ide_assists/src/handlers/generate_default_from_new.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ fn generate_trait_impl_text_from_impl(impl_: &ast::Impl, trait_text: &str, code:
8686
if let Some(generic_params) = &generic_params {
8787
let lifetimes = generic_params.lifetime_params().map(|lt| format!("{}", lt.syntax()));
8888
let toc_params = generic_params.type_or_const_params().map(|toc_param| match toc_param {
89-
either::Either::Left(type_param) => {
89+
ast::TypeOrConstParam::Type(type_param) => {
9090
let mut buf = String::new();
9191
if let Some(it) = type_param.name() {
9292
format_to!(buf, "{}", it.syntax());
@@ -99,7 +99,7 @@ fn generate_trait_impl_text_from_impl(impl_: &ast::Impl, trait_text: &str, code:
9999
}
100100
buf
101101
}
102-
either::Either::Right(const_param) => const_param.syntax().to_string(),
102+
ast::TypeOrConstParam::Const(const_param) => const_param.syntax().to_string(),
103103
});
104104
let generics = lifetimes.chain(toc_params).format(", ");
105105
format_to!(buf, "<{}>", generics);

crates/ide_assists/src/handlers/move_bounds.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use either::Either;
21
use syntax::{
32
ast::{self, edit_in_place::GenericParamsOwnerEdit, make, AstNode, HasName, HasTypeBounds},
43
match_ast,
@@ -26,8 +25,8 @@ pub(crate) fn move_bounds_to_where_clause(acc: &mut Assists, ctx: &AssistContext
2625

2726
let mut type_params = type_param_list.type_or_const_params();
2827
if type_params.all(|p| match p {
29-
either::Either::Left(t) => t.type_bound_list().is_none(),
30-
either::Either::Right(_) => true,
28+
ast::TypeOrConstParam::Type(t) => t.type_bound_list().is_none(),
29+
ast::TypeOrConstParam::Const(_) => true,
3130
}) {
3231
return None;
3332
}
@@ -56,8 +55,8 @@ pub(crate) fn move_bounds_to_where_clause(acc: &mut Assists, ctx: &AssistContext
5655

5756
for toc_param in type_param_list.type_or_const_params() {
5857
let type_param = match toc_param {
59-
Either::Left(x) => x,
60-
_ => continue,
58+
ast::TypeOrConstParam::Type(x) => x,
59+
ast::TypeOrConstParam::Const(_) => continue,
6160
};
6261
if let Some(tbl) = type_param.type_bound_list() {
6362
if let Some(predicate) = build_predicate(type_param) {

crates/ide_assists/src/utils.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,8 @@ fn generate_impl_text_inner(adt: &ast::Adt, trait_text: Option<&str>, code: &str
437437
let lifetimes = generic_params.lifetime_params().map(|lt| format!("{}", lt.syntax()));
438438
let toc_params = generic_params.type_or_const_params().map(|toc_param| {
439439
let type_param = match toc_param {
440-
either::Either::Left(x) => x,
441-
either::Either::Right(x) => return x.syntax().to_string(),
440+
ast::TypeOrConstParam::Type(x) => x,
441+
ast::TypeOrConstParam::Const(x) => return x.syntax().to_string(),
442442
};
443443
let mut buf = String::new();
444444
if let Some(it) = type_param.name() {
@@ -468,7 +468,7 @@ fn generate_impl_text_inner(adt: &ast::Adt, trait_text: Option<&str>, code: &str
468468
.map(|it| SmolStr::from(it.text()));
469469
let toc_params = generic_params
470470
.type_or_const_params()
471-
.filter_map(|it| it.either(|it| it.name(), |it| it.name()))
471+
.filter_map(|it| it.name())
472472
.map(|it| SmolStr::from(it.text()));
473473
format_to!(buf, "<{}>", lifetime_params.chain(toc_params).format(", "))
474474
}

crates/ide_db/src/rename.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ impl Definition {
136136
};
137137
let src = x.source(sema.db)?;
138138
let name = match &src.value {
139-
Either::Left(Either::Left(x)) => x.name()?,
140-
Either::Left(Either::Right(x)) => x.name()?,
139+
Either::Left(x) => x.name()?,
141140
Either::Right(_) => return None,
142141
};
143142
src.with_value(name.syntax()).original_file_range_opt(sema.db)

crates/syntax/src/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub use self::{
2222
generated::{nodes::*, tokens::*},
2323
node_ext::{
2424
AttrKind, FieldKind, Macro, NameLike, NameOrNameRef, PathSegmentKind, SelfParamKind,
25-
SlicePatComponents, StructKind, TypeBoundKind, VisibilityKind,
25+
SlicePatComponents, StructKind, TypeBoundKind, TypeOrConstParam, VisibilityKind,
2626
},
2727
operators::{ArithOp, BinaryOp, CmpOp, LogicOp, Ordering, RangeOp, UnaryOp},
2828
token_ext::{

crates/syntax/src/ast/node_ext.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
use std::{borrow::Cow, fmt, iter::successors};
77

8-
use itertools::{Either, Itertools};
8+
use itertools::Itertools;
99
use parser::SyntaxKind;
1010
use rowan::{GreenNodeData, GreenTokenData};
1111

@@ -641,6 +641,21 @@ impl ast::TypeBound {
641641
}
642642
}
643643

644+
#[derive(Debug, Clone)]
645+
pub enum TypeOrConstParam {
646+
Type(ast::TypeParam),
647+
Const(ast::ConstParam),
648+
}
649+
650+
impl TypeOrConstParam {
651+
pub fn name(&self) -> Option<ast::Name> {
652+
match self {
653+
TypeOrConstParam::Type(x) => x.name(),
654+
TypeOrConstParam::Const(x) => x.name(),
655+
}
656+
}
657+
}
658+
644659
pub enum VisibilityKind {
645660
In(ast::Path),
646661
PubCrate,
@@ -740,13 +755,11 @@ impl ast::GenericParamList {
740755
ast::GenericParam::TypeParam(_) | ast::GenericParam::ConstParam(_) => None,
741756
})
742757
}
743-
pub fn type_or_const_params(
744-
&self,
745-
) -> impl Iterator<Item = Either<ast::TypeParam, ast::ConstParam>> {
758+
pub fn type_or_const_params(&self) -> impl Iterator<Item = ast::TypeOrConstParam> {
746759
self.generic_params().filter_map(|param| match param {
747-
ast::GenericParam::TypeParam(it) => Some(Either::Left(it)),
760+
ast::GenericParam::TypeParam(it) => Some(ast::TypeOrConstParam::Type(it)),
748761
ast::GenericParam::LifetimeParam(_) => None,
749-
ast::GenericParam::ConstParam(it) => Some(Either::Right(it)),
762+
ast::GenericParam::ConstParam(it) => Some(ast::TypeOrConstParam::Const(it)),
750763
})
751764
}
752765
}

0 commit comments

Comments
 (0)