Skip to content

Commit 32b40de

Browse files
committed
Auto merge of rust-lang#12628 - Veykril:simplify, r=Veykril
internal: Simplify
2 parents 19fad54 + 2642f64 commit 32b40de

File tree

6 files changed

+75
-94
lines changed

6 files changed

+75
-94
lines changed

crates/hir-def/src/generics.rs

Lines changed: 23 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,15 @@ impl GenericParams {
146146
) -> Interned<GenericParams> {
147147
let _p = profile::span("generic_params_query");
148148

149+
macro_rules! id_to_generics {
150+
($id:ident) => {{
151+
let id = $id.lookup(db).id;
152+
let tree = id.item_tree(db);
153+
let item = &tree[id.value];
154+
item.generic_params.clone()
155+
}};
156+
}
157+
149158
match def {
150159
GenericDefId::FunctionId(id) => {
151160
let loc = id.lookup(db);
@@ -166,42 +175,12 @@ impl GenericParams {
166175

167176
Interned::new(generic_params)
168177
}
169-
GenericDefId::AdtId(AdtId::StructId(id)) => {
170-
let id = id.lookup(db).id;
171-
let tree = id.item_tree(db);
172-
let item = &tree[id.value];
173-
item.generic_params.clone()
174-
}
175-
GenericDefId::AdtId(AdtId::EnumId(id)) => {
176-
let id = id.lookup(db).id;
177-
let tree = id.item_tree(db);
178-
let item = &tree[id.value];
179-
item.generic_params.clone()
180-
}
181-
GenericDefId::AdtId(AdtId::UnionId(id)) => {
182-
let id = id.lookup(db).id;
183-
let tree = id.item_tree(db);
184-
let item = &tree[id.value];
185-
item.generic_params.clone()
186-
}
187-
GenericDefId::TraitId(id) => {
188-
let id = id.lookup(db).id;
189-
let tree = id.item_tree(db);
190-
let item = &tree[id.value];
191-
item.generic_params.clone()
192-
}
193-
GenericDefId::TypeAliasId(id) => {
194-
let id = id.lookup(db).id;
195-
let tree = id.item_tree(db);
196-
let item = &tree[id.value];
197-
item.generic_params.clone()
198-
}
199-
GenericDefId::ImplId(id) => {
200-
let id = id.lookup(db).id;
201-
let tree = id.item_tree(db);
202-
let item = &tree[id.value];
203-
item.generic_params.clone()
204-
}
178+
GenericDefId::AdtId(AdtId::StructId(id)) => id_to_generics!(id),
179+
GenericDefId::AdtId(AdtId::EnumId(id)) => id_to_generics!(id),
180+
GenericDefId::AdtId(AdtId::UnionId(id)) => id_to_generics!(id),
181+
GenericDefId::TraitId(id) => id_to_generics!(id),
182+
GenericDefId::TypeAliasId(id) => id_to_generics!(id),
183+
GenericDefId::ImplId(id) => id_to_generics!(id),
205184
GenericDefId::EnumVariantId(_) | GenericDefId::ConstId(_) => {
206185
Interned::new(GenericParams::default())
207186
}
@@ -393,15 +372,14 @@ impl GenericParams {
393372

394373
pub fn find_trait_self_param(&self) -> Option<LocalTypeOrConstParamId> {
395374
self.type_or_consts.iter().find_map(|(id, p)| {
396-
if let TypeOrConstParamData::TypeParamData(p) = p {
397-
if p.provenance == TypeParamProvenance::TraitSelf {
398-
Some(id)
399-
} else {
400-
None
401-
}
402-
} else {
403-
None
404-
}
375+
matches!(
376+
p,
377+
TypeOrConstParamData::TypeParamData(TypeParamData {
378+
provenance: TypeParamProvenance::TraitSelf,
379+
..
380+
})
381+
)
382+
.then(|| id)
405383
})
406384
}
407385
}

crates/hir-def/src/path.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
intern::Interned,
1212
type_ref::{ConstScalarOrPath, LifetimeRef},
1313
};
14-
use hir_expand::name::{name, Name};
14+
use hir_expand::name::Name;
1515
use syntax::ast;
1616

1717
use crate::type_ref::{TypeBound, TypeRef};
@@ -134,9 +134,7 @@ impl Path {
134134
}
135135

136136
pub fn is_self_type(&self) -> bool {
137-
self.type_anchor.is_none()
138-
&& *self.generic_args == [None]
139-
&& self.mod_path.as_ident() == Some(&name!(Self))
137+
self.type_anchor.is_none() && *self.generic_args == [None] && self.mod_path.is_Self()
140138
}
141139
}
142140

crates/hir-def/src/type_ref.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ pub enum TypeRef {
109109
Slice(Box<TypeRef>),
110110
/// A fn pointer. Last element of the vector is the return type.
111111
Fn(Vec<(Option<Name>, TypeRef)>, bool /*varargs*/),
112-
// For
113112
ImplTrait(Vec<Interned<TypeBound>>),
114113
DynTrait(Vec<Interned<TypeBound>>),
115114
Macro(AstId<ast::MacroCall>),

crates/hir-expand/src/mod_path.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ impl ModPath {
8080
self.kind == PathKind::Super(0) && self.segments.is_empty()
8181
}
8282

83+
#[allow(non_snake_case)]
84+
pub fn is_Self(&self) -> bool {
85+
self.kind == PathKind::Plain
86+
&& matches!(&*self.segments, [name] if *name == known::SELF_TYPE)
87+
}
88+
8389
/// If this path is a single identifier, like `foo`, return its name.
8490
pub fn as_ident(&self) -> Option<&Name> {
8591
if self.kind != PathKind::Plain {

crates/hir-ty/src/lower.rs

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,33 @@
55
//! - Building the type for an item: This happens through the `type_for_def` query.
66
//!
77
//! This usually involves resolving names, collecting generic arguments etc.
8-
use std::cell::{Cell, RefCell};
9-
use std::{iter, sync::Arc};
8+
use std::{
9+
cell::{Cell, RefCell},
10+
iter,
11+
sync::Arc,
12+
};
1013

1114
use base_db::CrateId;
12-
use chalk_ir::fold::Fold;
13-
use chalk_ir::interner::HasInterner;
14-
use chalk_ir::{cast::Cast, fold::Shift, Mutability, Safety};
15-
use hir_def::generics::TypeOrConstParamData;
16-
use hir_def::intern::Interned;
17-
use hir_def::lang_item::lang_attr;
18-
use hir_def::path::{ModPath, PathKind};
19-
use hir_def::type_ref::ConstScalarOrPath;
15+
use chalk_ir::{cast::Cast, fold::Fold, fold::Shift, interner::HasInterner, Mutability, Safety};
16+
2017
use hir_def::{
2118
adt::StructKind,
2219
body::{Expander, LowerCtx},
2320
builtin_type::BuiltinType,
24-
generics::{TypeParamProvenance, WherePredicate, WherePredicateTypeTarget},
25-
path::{GenericArg, Path, PathSegment, PathSegments},
21+
generics::{
22+
TypeOrConstParamData, TypeParamProvenance, WherePredicate, WherePredicateTypeTarget,
23+
},
24+
intern::Interned,
25+
lang_item::lang_attr,
26+
path::{GenericArg, ModPath, Path, PathKind, PathSegment, PathSegments},
2627
resolver::{HasResolver, Resolver, TypeNs},
27-
type_ref::{TraitBoundModifier, TraitRef as HirTraitRef, TypeBound, TypeRef},
28-
AdtId, AssocItemId, ConstId, EnumId, EnumVariantId, FunctionId, GenericDefId, HasModule,
29-
ImplId, ItemContainerId, LocalFieldId, Lookup, StaticId, StructId, TraitId, TypeAliasId,
30-
UnionId, VariantId,
28+
type_ref::{
29+
ConstScalarOrPath, TraitBoundModifier, TraitRef as HirTraitRef, TypeBound, TypeRef,
30+
},
31+
AdtId, AssocItemId, ConstId, ConstParamId, EnumId, EnumVariantId, FunctionId, GenericDefId,
32+
HasModule, ImplId, ItemContainerId, LocalFieldId, Lookup, StaticId, StructId, TraitId,
33+
TypeAliasId, TypeOrConstParamId, TypeParamId, UnionId, VariantId,
3134
};
32-
use hir_def::{ConstParamId, TypeOrConstParamId, TypeParamId};
3335
use hir_expand::{name::Name, ExpandResult};
3436
use itertools::Either;
3537
use la_arena::ArenaMap;
@@ -38,20 +40,19 @@ use smallvec::SmallVec;
3840
use stdx::{impl_from, never};
3941
use syntax::{ast, SmolStr};
4042

41-
use crate::consteval::{
42-
intern_scalar_const, path_to_const, unknown_const, unknown_const_as_generic,
43-
};
44-
use crate::utils::Generics;
45-
use crate::{all_super_traits, make_binders, Const, GenericArgData, ParamKind};
4643
use crate::{
44+
all_super_traits,
45+
consteval::{intern_scalar_const, path_to_const, unknown_const, unknown_const_as_generic},
4746
db::HirDatabase,
47+
make_binders,
4848
mapping::ToChalk,
4949
static_lifetime, to_assoc_type_id, to_chalk_trait_id, to_placeholder_idx,
50+
utils::Generics,
5051
utils::{all_super_trait_refs, associated_type_by_name_including_super_traits, generics},
51-
AliasEq, AliasTy, Binders, BoundVar, CallableSig, DebruijnIndex, DynTy, FnPointer, FnSig,
52-
FnSubst, ImplTraitId, Interner, PolyFnSig, ProjectionTy, QuantifiedWhereClause,
53-
QuantifiedWhereClauses, ReturnTypeImplTrait, ReturnTypeImplTraits, Substitution,
54-
TraitEnvironment, TraitRef, TraitRefExt, Ty, TyBuilder, TyKind, WhereClause,
52+
AliasEq, AliasTy, Binders, BoundVar, CallableSig, Const, DebruijnIndex, DynTy, FnPointer,
53+
FnSig, FnSubst, GenericArgData, ImplTraitId, Interner, ParamKind, PolyFnSig, ProjectionTy,
54+
QuantifiedWhereClause, QuantifiedWhereClauses, ReturnTypeImplTrait, ReturnTypeImplTraits,
55+
Substitution, TraitEnvironment, TraitRef, TraitRefExt, Ty, TyBuilder, TyKind, WhereClause,
5556
};
5657

5758
#[derive(Debug)]

crates/hir-ty/src/utils.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@ use hir_def::{
1212
WherePredicateTypeTarget,
1313
},
1414
intern::Interned,
15-
path::Path,
1615
resolver::{HasResolver, TypeNs},
1716
type_ref::{TraitBoundModifier, TypeRef},
1817
ConstParamId, FunctionId, GenericDefId, ItemContainerId, Lookup, TraitId, TypeAliasId,
1918
TypeOrConstParamId, TypeParamId,
2019
};
21-
use hir_expand::name::{known, name, Name};
20+
use hir_expand::name::{known, Name};
2221
use itertools::Either;
2322
use rustc_hash::FxHashSet;
2423
use smallvec::{smallvec, SmallVec};
@@ -53,25 +52,25 @@ fn direct_super_traits(db: &dyn DefDatabase, trait_: TraitId) -> SmallVec<[Trait
5352
.iter()
5453
.filter_map(|pred| match pred {
5554
WherePredicate::ForLifetime { target, bound, .. }
56-
| WherePredicate::TypeBound { target, bound } => match target {
57-
WherePredicateTypeTarget::TypeRef(type_ref) => match &**type_ref {
58-
TypeRef::Path(p) if p == &Path::from(name![Self]) => bound.as_path(),
59-
_ => None,
60-
},
61-
WherePredicateTypeTarget::TypeOrConstParam(local_id)
62-
if Some(*local_id) == trait_self =>
63-
{
64-
bound.as_path()
55+
| WherePredicate::TypeBound { target, bound } => {
56+
let is_trait = match target {
57+
WherePredicateTypeTarget::TypeRef(type_ref) => match &**type_ref {
58+
TypeRef::Path(p) => p.is_self_type(),
59+
_ => false,
60+
},
61+
WherePredicateTypeTarget::TypeOrConstParam(local_id) => {
62+
Some(*local_id) == trait_self
63+
}
64+
};
65+
match is_trait {
66+
true => bound.as_path(),
67+
false => None,
6568
}
66-
_ => None,
67-
},
69+
}
6870
WherePredicate::Lifetime { .. } => None,
6971
})
70-
.filter_map(|(path, bound_modifier)| match bound_modifier {
71-
TraitBoundModifier::None => Some(path),
72-
TraitBoundModifier::Maybe => None,
73-
})
74-
.filter_map(|path| match resolver.resolve_path_in_type_ns_fully(db, path.mod_path()) {
72+
.filter(|(_, bound_modifier)| matches!(bound_modifier, TraitBoundModifier::None))
73+
.filter_map(|(path, _)| match resolver.resolve_path_in_type_ns_fully(db, path.mod_path()) {
7574
Some(TypeNs::TraitId(t)) => Some(t),
7675
_ => None,
7776
})

0 commit comments

Comments
 (0)