2330: Remove hir/adt.rs r=matklad a=matklad



Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2019-11-20 18:38:00 +00:00 committed by GitHub
commit 6983afdea9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 81 additions and 105 deletions

View file

@ -1,54 +0,0 @@
//! This module contains the implementation details of the HIR for ADTs, i.e.
//! structs and enums (and unions).
use std::sync::Arc;
use hir_def::adt::VariantData;
use crate::{
db::{DefDatabase, HirDatabase},
EnumVariant, Module, Name, Struct, StructField,
};
impl Struct {
pub(crate) fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> {
db.struct_data(self.id.into()).variant_data.clone()
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum VariantDef {
Struct(Struct),
EnumVariant(EnumVariant),
}
impl_froms!(VariantDef: Struct, EnumVariant);
impl VariantDef {
pub fn fields(self, db: &impl HirDatabase) -> Vec<StructField> {
match self {
VariantDef::Struct(it) => it.fields(db),
VariantDef::EnumVariant(it) => it.fields(db),
}
}
pub fn field(self, db: &impl HirDatabase, name: &Name) -> Option<StructField> {
match self {
VariantDef::Struct(it) => it.field(db, name),
VariantDef::EnumVariant(it) => it.field(db, name),
}
}
pub fn module(self, db: &impl HirDatabase) -> Module {
match self {
VariantDef::Struct(it) => it.module(db),
VariantDef::EnumVariant(it) => it.module(db),
}
}
pub(crate) fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> {
match self {
VariantDef::Struct(it) => it.variant_data(db),
VariantDef::EnumVariant(it) => it.variant_data(db),
}
}
}

View file

@ -23,7 +23,6 @@ use ra_db::{CrateId, Edition};
use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner};
use crate::{
adt::VariantDef,
db::{AstDatabase, DefDatabase, HirDatabase},
expr::{BindingAnnotation, Body, BodySourceMap, ExprValidator, Pat, PatId},
generics::{GenericDef, HasGenericParams},
@ -322,9 +321,11 @@ impl Struct {
// take the outer scope...
let r = self.module(db).resolver(db);
// ...and add generic params, if present
let p = self.generic_params(db);
let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r };
r
r.push_generic_params_scope(db, self.into())
}
fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> {
db.struct_data(self.id.into()).variant_data.clone()
}
}
@ -352,9 +353,7 @@ impl Union {
// take the outer scope...
let r = self.module(db).resolver(db);
// ...and add generic params, if present
let p = self.generic_params(db);
let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r };
r
r.push_generic_params_scope(db, self.into())
}
}
@ -402,8 +401,7 @@ impl Enum {
// take the outer scope...
let r = self.module(db).resolver(db);
// ...and add generic params, if present
let p = self.generic_params(db);
let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r };
let r = r.push_generic_params_scope(db, self.into());
r.push_scope(Scope::AdtScope(self.into()))
}
}
@ -487,6 +485,43 @@ impl Adt {
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum VariantDef {
Struct(Struct),
EnumVariant(EnumVariant),
}
impl_froms!(VariantDef: Struct, EnumVariant);
impl VariantDef {
pub fn fields(self, db: &impl HirDatabase) -> Vec<StructField> {
match self {
VariantDef::Struct(it) => it.fields(db),
VariantDef::EnumVariant(it) => it.fields(db),
}
}
pub fn field(self, db: &impl HirDatabase, name: &Name) -> Option<StructField> {
match self {
VariantDef::Struct(it) => it.field(db, name),
VariantDef::EnumVariant(it) => it.field(db, name),
}
}
pub fn module(self, db: &impl HirDatabase) -> Module {
match self {
VariantDef::Struct(it) => it.module(db),
VariantDef::EnumVariant(it) => it.module(db),
}
}
pub(crate) fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> {
match self {
VariantDef::Struct(it) => it.variant_data(db),
VariantDef::EnumVariant(it) => it.variant_data(db),
}
}
}
/// The defs which have a body.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DefWithBody {
@ -709,9 +744,7 @@ impl Function {
// take the outer scope...
let r = self.container(db).map_or_else(|| self.module(db).resolver(db), |c| c.resolver(db));
// ...and add generic params, if present
let p = self.generic_params(db);
let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r };
r
r.push_generic_params_scope(db, self.into())
}
pub fn diagnostics(self, db: &impl HirDatabase, sink: &mut DiagnosticSink) {
@ -946,9 +979,7 @@ impl Trait {
pub(crate) fn resolver(self, db: &impl DefDatabase) -> Resolver {
let r = self.module(db).resolver(db);
// add generic params, if present
let p = self.generic_params(db);
let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r };
r
r.push_generic_params_scope(db, self.into())
}
}
@ -1010,9 +1041,7 @@ impl TypeAlias {
.map(|ib| ib.resolver(db))
.unwrap_or_else(|| self.module(db).resolver(db));
// ...and add generic params, if present
let p = self.generic_params(db);
let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r };
r
r.push_generic_params_scope(db, self.into())
}
}

View file

@ -4,11 +4,10 @@ use hir_def::{HasSource as _, Lookup};
use ra_syntax::ast::{self, AstNode};
use crate::{
adt::VariantDef,
db::{AstDatabase, DefDatabase, HirDatabase},
ids::AstItemDef,
Const, Either, Enum, EnumVariant, FieldSource, Function, HasBody, HirFileId, MacroDef, Module,
ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union,
ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, VariantDef,
};
pub use hir_expand::Source;

View file

@ -5,7 +5,6 @@ use ra_syntax::ast::{self};
use crate::{
db::{AstDatabase, DefDatabase, HirDatabase},
generics::HasGenericParams,
resolve::Resolver,
ty::Ty,
AssocItem, Crate, HasSource, ImplBlock, Module, Source, TraitRef,
@ -52,12 +51,11 @@ impl ImplBlock {
Crate { crate_id: self.module(db).id.krate }
}
pub(crate) fn resolver(&self, db: &impl DefDatabase) -> Resolver {
pub(crate) fn resolver(self, db: &impl DefDatabase) -> Resolver {
let r = self.module(db).resolver(db);
// add generic params, if present
let p = self.generic_params(db);
let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r };
let r = r.push_impl_block_scope(self.clone());
let r = r.push_generic_params_scope(db, self.into());
let r = r.push_impl_block_scope(self);
r
}
}

View file

@ -32,7 +32,6 @@ pub mod db;
pub mod source_binder;
mod ids;
mod adt;
mod type_alias;
mod ty;
mod impl_block;
@ -56,15 +55,14 @@ mod marks;
use crate::resolve::Resolver;
pub use crate::{
adt::VariantDef,
code_model::ImplBlock,
code_model::{
attrs::{AttrDef, Attrs},
docs::{DocDef, Docs, Documentation},
src::{HasBodySource, HasSource},
Adt, AssocItem, Const, ConstData, Container, Crate, CrateDependency, DefWithBody, Enum,
EnumVariant, FieldSource, FnData, Function, GenericParam, HasBody, Local, MacroDef, Module,
ModuleDef, ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union,
EnumVariant, FieldSource, FnData, Function, GenericParam, HasBody, ImplBlock, Local,
MacroDef, Module, ModuleDef, ModuleSource, Static, Struct, StructField, Trait, TypeAlias,
Union, VariantDef,
},
expr::ExprScopes,
from_source::FromSource,

View file

@ -14,9 +14,9 @@ use crate::{
code_model::Crate,
db::{DefDatabase, HirDatabase},
expr::{ExprScopes, PatId, ScopeId},
generics::GenericParams,
Adt, Const, DefWithBody, Enum, EnumVariant, Function, ImplBlock, Local, MacroDef, ModuleDef,
PerNs, Static, Struct, Trait, TypeAlias,
generics::{GenericParams, HasGenericParams},
Adt, Const, DefWithBody, Enum, EnumVariant, Function, GenericDef, ImplBlock, Local, MacroDef,
ModuleDef, PerNs, Static, Struct, Trait, TypeAlias,
};
#[derive(Debug, Clone, Default)]
@ -43,7 +43,7 @@ pub(crate) enum Scope {
/// All the items and imported names of a module
ModuleScope(ModuleItemMap),
/// Brings the generic parameters of an item into scope
GenericParams(Arc<GenericParams>),
GenericParams { def: GenericDef, params: Arc<GenericParams> },
/// Brings `Self` in `impl` block into scope
ImplBlockScope(ImplBlock),
/// Brings `Self` in enum, struct and union definitions into scope
@ -141,9 +141,9 @@ impl Resolver {
for scope in self.scopes.iter().rev() {
match scope {
Scope::ExprScope(_) => continue,
Scope::GenericParams(_) | Scope::ImplBlockScope(_) if skip_to_mod => continue,
Scope::GenericParams { .. } | Scope::ImplBlockScope(_) if skip_to_mod => continue,
Scope::GenericParams(params) => {
Scope::GenericParams { params, .. } => {
if let Some(param) = params.find_by_name(first_name) {
let idx = if path.segments.len() == 1 { None } else { Some(1) };
return Some((TypeNs::GenericParam(param.idx), idx));
@ -212,7 +212,7 @@ impl Resolver {
match scope {
Scope::AdtScope(_)
| Scope::ExprScope(_)
| Scope::GenericParams(_)
| Scope::GenericParams { .. }
| Scope::ImplBlockScope(_)
if skip_to_mod =>
{
@ -232,13 +232,13 @@ impl Resolver {
}
Scope::ExprScope(_) => continue,
Scope::GenericParams(params) if n_segments > 1 => {
Scope::GenericParams { params, .. } if n_segments > 1 => {
if let Some(param) = params.find_by_name(first_name) {
let ty = TypeNs::GenericParam(param.idx);
return Some(ResolveValueResult::Partial(ty, 1));
}
}
Scope::GenericParams(_) => continue,
Scope::GenericParams { .. } => continue,
Scope::ImplBlockScope(impl_) if n_segments > 1 => {
if first_name == &name::SELF_TYPE {
@ -361,7 +361,7 @@ impl Resolver {
self.scopes
.iter()
.filter_map(|scope| match scope {
Scope::GenericParams(params) => Some(params),
Scope::GenericParams { params, .. } => Some(params),
_ => None,
})
.flat_map(|params| params.where_predicates.iter())
@ -369,7 +369,7 @@ impl Resolver {
pub(crate) fn generic_def(&self) -> Option<crate::generics::GenericDef> {
self.scopes.iter().find_map(|scope| match scope {
Scope::GenericParams(params) => Some(params.def.into()),
Scope::GenericParams { def, .. } => Some(*def),
_ => None,
})
}
@ -381,8 +381,17 @@ impl Resolver {
self
}
pub(crate) fn push_generic_params_scope(self, params: Arc<GenericParams>) -> Resolver {
self.push_scope(Scope::GenericParams(params))
pub(crate) fn push_generic_params_scope(
self,
db: &impl DefDatabase,
def: GenericDef,
) -> Resolver {
let params = def.generic_params(db);
if params.params.is_empty() {
self
} else {
self.push_scope(Scope::GenericParams { def, params })
}
}
pub(crate) fn push_impl_block_scope(self, impl_block: ImplBlock) -> Resolver {
@ -457,8 +466,8 @@ impl Scope {
});
}
}
Scope::GenericParams(gp) => {
for param in &gp.params {
Scope::GenericParams { params, .. } => {
for param in params.params.iter() {
f(param.name.clone(), ScopeDef::GenericParam(param.idx))
}
}

View file

@ -37,14 +37,13 @@ use super::{
TypeCtor, TypeWalk, Uncertain,
};
use crate::{
adt::VariantDef,
code_model::TypeAlias,
db::HirDatabase,
expr::{BindingAnnotation, Body, ExprId, PatId},
resolve::{Resolver, TypeNs},
ty::infer::diagnostics::InferenceDiagnostic,
Adt, AssocItem, ConstData, DefWithBody, FloatTy, FnData, Function, HasBody, IntTy, Path,
StructField,
StructField, VariantDef,
};
macro_rules! ty_app {

View file

@ -19,7 +19,6 @@ use super::{
TypeWalk,
};
use crate::{
adt::VariantDef,
db::HirDatabase,
generics::HasGenericParams,
generics::{GenericDef, WherePredicate},
@ -30,7 +29,7 @@ use crate::{
},
util::make_mut_slice,
Const, Enum, EnumVariant, Function, ModuleDef, Path, Static, Struct, StructField, Trait,
TypeAlias, Union,
TypeAlias, Union, VariantDef,
};
// FIXME: this is only really used in `type_for_def`, which contains a bunch of

View file

@ -25,7 +25,6 @@ pub struct GenericParam {
/// Data about the generic parameters of a function, struct, impl, etc.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct GenericParams {
pub def: GenericDefId,
pub parent_params: Option<Arc<GenericParams>>,
pub params: Vec<GenericParam>,
pub where_predicates: Vec<WherePredicate>,
@ -56,7 +55,7 @@ impl GenericParams {
parent_params: Option<Arc<GenericParams>>,
) -> GenericParams {
let mut generics =
GenericParams { def, params: Vec::new(), parent_params, where_predicates: Vec::new() };
GenericParams { params: Vec::new(), parent_params, where_predicates: Vec::new() };
let start = generics.parent_params.as_ref().map(|p| p.params.len()).unwrap_or(0) as u32;
// FIXME: add `: Sized` bound for everything except for `Self` in traits
match def {