internal: Simplify

This commit is contained in:
Lukas Wirth 2022-06-23 20:08:29 +02:00
parent 312ac83caf
commit 2642f64570
6 changed files with 75 additions and 94 deletions

View File

@ -146,6 +146,15 @@ pub(crate) fn generic_params_query(
) -> Interned<GenericParams> {
let _p = profile::span("generic_params_query");
macro_rules! id_to_generics {
($id:ident) => {{
let id = $id.lookup(db).id;
let tree = id.item_tree(db);
let item = &tree[id.value];
item.generic_params.clone()
}};
}
match def {
GenericDefId::FunctionId(id) => {
let loc = id.lookup(db);
@ -166,42 +175,12 @@ pub(crate) fn generic_params_query(
Interned::new(generic_params)
}
GenericDefId::AdtId(AdtId::StructId(id)) => {
let id = id.lookup(db).id;
let tree = id.item_tree(db);
let item = &tree[id.value];
item.generic_params.clone()
}
GenericDefId::AdtId(AdtId::EnumId(id)) => {
let id = id.lookup(db).id;
let tree = id.item_tree(db);
let item = &tree[id.value];
item.generic_params.clone()
}
GenericDefId::AdtId(AdtId::UnionId(id)) => {
let id = id.lookup(db).id;
let tree = id.item_tree(db);
let item = &tree[id.value];
item.generic_params.clone()
}
GenericDefId::TraitId(id) => {
let id = id.lookup(db).id;
let tree = id.item_tree(db);
let item = &tree[id.value];
item.generic_params.clone()
}
GenericDefId::TypeAliasId(id) => {
let id = id.lookup(db).id;
let tree = id.item_tree(db);
let item = &tree[id.value];
item.generic_params.clone()
}
GenericDefId::ImplId(id) => {
let id = id.lookup(db).id;
let tree = id.item_tree(db);
let item = &tree[id.value];
item.generic_params.clone()
}
GenericDefId::AdtId(AdtId::StructId(id)) => id_to_generics!(id),
GenericDefId::AdtId(AdtId::EnumId(id)) => id_to_generics!(id),
GenericDefId::AdtId(AdtId::UnionId(id)) => id_to_generics!(id),
GenericDefId::TraitId(id) => id_to_generics!(id),
GenericDefId::TypeAliasId(id) => id_to_generics!(id),
GenericDefId::ImplId(id) => id_to_generics!(id),
GenericDefId::EnumVariantId(_) | GenericDefId::ConstId(_) => {
Interned::new(GenericParams::default())
}
@ -393,15 +372,14 @@ pub fn find_const_by_name(&self, name: &Name, parent: GenericDefId) -> Option<Co
pub fn find_trait_self_param(&self) -> Option<LocalTypeOrConstParamId> {
self.type_or_consts.iter().find_map(|(id, p)| {
if let TypeOrConstParamData::TypeParamData(p) = p {
if p.provenance == TypeParamProvenance::TraitSelf {
Some(id)
} else {
None
}
} else {
None
}
matches!(
p,
TypeOrConstParamData::TypeParamData(TypeParamData {
provenance: TypeParamProvenance::TraitSelf,
..
})
)
.then(|| id)
})
}
}

View File

@ -11,7 +11,7 @@
intern::Interned,
type_ref::{ConstScalarOrPath, LifetimeRef},
};
use hir_expand::name::{name, Name};
use hir_expand::name::Name;
use syntax::ast;
use crate::type_ref::{TypeBound, TypeRef};
@ -134,9 +134,7 @@ pub fn qualifier(&self) -> Option<Path> {
}
pub fn is_self_type(&self) -> bool {
self.type_anchor.is_none()
&& *self.generic_args == [None]
&& self.mod_path.as_ident() == Some(&name!(Self))
self.type_anchor.is_none() && *self.generic_args == [None] && self.mod_path.is_Self()
}
}

View File

@ -109,7 +109,6 @@ pub enum TypeRef {
Slice(Box<TypeRef>),
/// A fn pointer. Last element of the vector is the return type.
Fn(Vec<(Option<Name>, TypeRef)>, bool /*varargs*/),
// For
ImplTrait(Vec<Interned<TypeBound>>),
DynTrait(Vec<Interned<TypeBound>>),
Macro(AstId<ast::MacroCall>),

View File

@ -80,6 +80,12 @@ pub fn is_self(&self) -> bool {
self.kind == PathKind::Super(0) && self.segments.is_empty()
}
#[allow(non_snake_case)]
pub fn is_Self(&self) -> bool {
self.kind == PathKind::Plain
&& matches!(&*self.segments, [name] if *name == known::SELF_TYPE)
}
/// If this path is a single identifier, like `foo`, return its name.
pub fn as_ident(&self) -> Option<&Name> {
if self.kind != PathKind::Plain {

View File

@ -5,31 +5,33 @@
//! - Building the type for an item: This happens through the `type_for_def` query.
//!
//! This usually involves resolving names, collecting generic arguments etc.
use std::cell::{Cell, RefCell};
use std::{iter, sync::Arc};
use std::{
cell::{Cell, RefCell},
iter,
sync::Arc,
};
use base_db::CrateId;
use chalk_ir::fold::Fold;
use chalk_ir::interner::HasInterner;
use chalk_ir::{cast::Cast, fold::Shift, Mutability, Safety};
use hir_def::generics::TypeOrConstParamData;
use hir_def::intern::Interned;
use hir_def::lang_item::lang_attr;
use hir_def::path::{ModPath, PathKind};
use hir_def::type_ref::ConstScalarOrPath;
use chalk_ir::{cast::Cast, fold::Fold, fold::Shift, interner::HasInterner, Mutability, Safety};
use hir_def::{
adt::StructKind,
body::{Expander, LowerCtx},
builtin_type::BuiltinType,
generics::{TypeParamProvenance, WherePredicate, WherePredicateTypeTarget},
path::{GenericArg, Path, PathSegment, PathSegments},
generics::{
TypeOrConstParamData, TypeParamProvenance, WherePredicate, WherePredicateTypeTarget,
},
intern::Interned,
lang_item::lang_attr,
path::{GenericArg, ModPath, Path, PathKind, PathSegment, PathSegments},
resolver::{HasResolver, Resolver, TypeNs},
type_ref::{TraitBoundModifier, TraitRef as HirTraitRef, TypeBound, TypeRef},
AdtId, AssocItemId, ConstId, EnumId, EnumVariantId, FunctionId, GenericDefId, HasModule,
ImplId, ItemContainerId, LocalFieldId, Lookup, StaticId, StructId, TraitId, TypeAliasId,
UnionId, VariantId,
type_ref::{
ConstScalarOrPath, TraitBoundModifier, TraitRef as HirTraitRef, TypeBound, TypeRef,
},
AdtId, AssocItemId, ConstId, ConstParamId, EnumId, EnumVariantId, FunctionId, GenericDefId,
HasModule, ImplId, ItemContainerId, LocalFieldId, Lookup, StaticId, StructId, TraitId,
TypeAliasId, TypeOrConstParamId, TypeParamId, UnionId, VariantId,
};
use hir_def::{ConstParamId, TypeOrConstParamId, TypeParamId};
use hir_expand::{name::Name, ExpandResult};
use itertools::Either;
use la_arena::ArenaMap;
@ -38,20 +40,19 @@
use stdx::{impl_from, never};
use syntax::{ast, SmolStr};
use crate::consteval::{
intern_scalar_const, path_to_const, unknown_const, unknown_const_as_generic,
};
use crate::utils::Generics;
use crate::{all_super_traits, make_binders, Const, GenericArgData, ParamKind};
use crate::{
all_super_traits,
consteval::{intern_scalar_const, path_to_const, unknown_const, unknown_const_as_generic},
db::HirDatabase,
make_binders,
mapping::ToChalk,
static_lifetime, to_assoc_type_id, to_chalk_trait_id, to_placeholder_idx,
utils::Generics,
utils::{all_super_trait_refs, associated_type_by_name_including_super_traits, generics},
AliasEq, AliasTy, Binders, BoundVar, CallableSig, DebruijnIndex, DynTy, FnPointer, FnSig,
FnSubst, ImplTraitId, Interner, PolyFnSig, ProjectionTy, QuantifiedWhereClause,
QuantifiedWhereClauses, ReturnTypeImplTrait, ReturnTypeImplTraits, Substitution,
TraitEnvironment, TraitRef, TraitRefExt, Ty, TyBuilder, TyKind, WhereClause,
AliasEq, AliasTy, Binders, BoundVar, CallableSig, Const, DebruijnIndex, DynTy, FnPointer,
FnSig, FnSubst, GenericArgData, ImplTraitId, Interner, ParamKind, PolyFnSig, ProjectionTy,
QuantifiedWhereClause, QuantifiedWhereClauses, ReturnTypeImplTrait, ReturnTypeImplTraits,
Substitution, TraitEnvironment, TraitRef, TraitRefExt, Ty, TyBuilder, TyKind, WhereClause,
};
#[derive(Debug)]

View File

@ -12,13 +12,12 @@
WherePredicateTypeTarget,
},
intern::Interned,
path::Path,
resolver::{HasResolver, TypeNs},
type_ref::{TraitBoundModifier, TypeRef},
ConstParamId, FunctionId, GenericDefId, ItemContainerId, Lookup, TraitId, TypeAliasId,
TypeOrConstParamId, TypeParamId,
};
use hir_expand::name::{known, name, Name};
use hir_expand::name::{known, Name};
use itertools::Either;
use rustc_hash::FxHashSet;
use smallvec::{smallvec, SmallVec};
@ -53,25 +52,25 @@ pub(crate) fn fn_traits(db: &dyn DefDatabase, krate: CrateId) -> impl Iterator<I
.iter()
.filter_map(|pred| match pred {
WherePredicate::ForLifetime { target, bound, .. }
| WherePredicate::TypeBound { target, bound } => match target {
WherePredicateTypeTarget::TypeRef(type_ref) => match &**type_ref {
TypeRef::Path(p) if p == &Path::from(name![Self]) => bound.as_path(),
_ => None,
},
WherePredicateTypeTarget::TypeOrConstParam(local_id)
if Some(*local_id) == trait_self =>
{
bound.as_path()
| WherePredicate::TypeBound { target, bound } => {
let is_trait = match target {
WherePredicateTypeTarget::TypeRef(type_ref) => match &**type_ref {
TypeRef::Path(p) => p.is_self_type(),
_ => false,
},
WherePredicateTypeTarget::TypeOrConstParam(local_id) => {
Some(*local_id) == trait_self
}
};
match is_trait {
true => bound.as_path(),
false => None,
}
_ => None,
},
}
WherePredicate::Lifetime { .. } => None,
})
.filter_map(|(path, bound_modifier)| match bound_modifier {
TraitBoundModifier::None => Some(path),
TraitBoundModifier::Maybe => None,
})
.filter_map(|path| match resolver.resolve_path_in_type_ns_fully(db, path.mod_path()) {
.filter(|(_, bound_modifier)| matches!(bound_modifier, TraitBoundModifier::None))
.filter_map(|(path, _)| match resolver.resolve_path_in_type_ns_fully(db, path.mod_path()) {
Some(TypeNs::TraitId(t)) => Some(t),
_ => None,
})