Remove ty from code_model

This commit is contained in:
Aleksey Kladov 2019-12-08 12:44:14 +01:00
parent b77d7c24aa
commit b37c6a746b
9 changed files with 53 additions and 55 deletions

2
Cargo.lock generated
View file

@ -919,6 +919,8 @@ dependencies = [
"ra_batch 0.1.0", "ra_batch 0.1.0",
"ra_db 0.1.0", "ra_db 0.1.0",
"ra_hir 0.1.0", "ra_hir 0.1.0",
"ra_hir_def 0.1.0",
"ra_hir_ty 0.1.0",
"ra_ide 0.1.0", "ra_ide 0.1.0",
"ra_prof 0.1.0", "ra_prof 0.1.0",
"ra_syntax 0.1.0", "ra_syntax 0.1.0",

View file

@ -12,7 +12,9 @@ env_logger = { version = "0.7.1", default-features = false, features = ["humanti
ra_syntax = { path = "../ra_syntax" } ra_syntax = { path = "../ra_syntax" }
ra_ide = { path = "../ra_ide" } ra_ide = { path = "../ra_ide" }
ra_batch = { path = "../ra_batch" } ra_batch = { path = "../ra_batch" }
ra_hir = { path = "../ra_hir" } hir = { path = "../ra_hir", package = "ra_hir" }
hir_ty = { path = "../ra_hir_ty", package = "ra_hir_ty" }
hir_def = { path = "../ra_hir_def", package = "ra_hir_def" }
ra_db = { path = "../ra_db" } ra_db = { path = "../ra_db" }
[dependencies.ra_prof] [dependencies.ra_prof]

View file

@ -2,8 +2,13 @@
use std::{collections::HashSet, fmt::Write, path::Path, time::Instant}; use std::{collections::HashSet, fmt::Write, path::Path, time::Instant};
use hir::{
db::{DefDatabase, HirDatabase},
AssocItem, Crate, HasSource, HirDisplay, ModuleDef,
};
use hir_def::FunctionId;
use hir_ty::{Ty, TypeWalk};
use ra_db::SourceDatabaseExt; use ra_db::SourceDatabaseExt;
use ra_hir::{AssocItem, Crate, HasSource, HirDisplay, ModuleDef, Ty, TypeWalk};
use ra_syntax::AstNode; use ra_syntax::AstNode;
use crate::{progress_report::ProgressReport, Result, Verbosity}; use crate::{progress_report::ProgressReport, Result, Verbosity};
@ -101,8 +106,9 @@ pub fn run(
continue; continue;
} }
} }
let body = f.body(db); let f_id = FunctionId::from(f);
let inference_result = f.infer(db); let body = db.body(f_id.into());
let inference_result = db.infer(f_id.into());
for (expr_id, _) in body.exprs.iter() { for (expr_id, _) in body.exprs.iter() {
let ty = &inference_result[expr_id]; let ty = &inference_result[expr_id];
num_exprs += 1; num_exprs += 1;
@ -122,7 +128,8 @@ pub fn run(
if let Some(mismatch) = inference_result.type_mismatch_for_expr(expr_id) { if let Some(mismatch) = inference_result.type_mismatch_for_expr(expr_id) {
num_type_mismatches += 1; num_type_mismatches += 1;
if verbosity.is_verbose() { if verbosity.is_verbose() {
let src = f.body_source_map(db).expr_syntax(expr_id); let (_, sm) = db.body_with_source_map(f_id.into());
let src = sm.expr_syntax(expr_id);
if let Some(src) = src { if let Some(src) = src {
// FIXME: it might be nice to have a function (on Analysis?) that goes from Source<T> -> (LineCol, LineCol) directly // FIXME: it might be nice to have a function (on Analysis?) that goes from Source<T> -> (LineCol, LineCol) directly
let original_file = src.file_id.original_file(db); let original_file = src.file_id.original_file(db);

View file

@ -23,14 +23,15 @@ use hir_expand::{
name::{self, AsName}, name::{self, AsName},
MacroDefId, MacroDefId,
}; };
use hir_ty::expr::ExprValidator; use hir_ty::{
autoderef, display::HirFormatter, expr::ExprValidator, ApplicationTy, Canonical, InEnvironment,
TraitEnvironment, Ty, TyDefId, TypeCtor, TypeWalk,
};
use ra_db::{CrateId, Edition, FileId}; use ra_db::{CrateId, Edition, FileId};
use ra_syntax::ast; use ra_syntax::ast;
use crate::{ use crate::{
db::{DefDatabase, HirDatabase}, db::{DefDatabase, HirDatabase},
ty::display::HirFormatter,
ty::{self, InEnvironment, TraitEnvironment, Ty, TyDefId, TypeCtor, TypeWalk},
CallableDef, HirDisplay, InFile, Name, CallableDef, HirDisplay, InFile, Name,
}; };
@ -87,10 +88,6 @@ impl Crate {
pub fn all(db: &impl DefDatabase) -> Vec<Crate> { pub fn all(db: &impl DefDatabase) -> Vec<Crate> {
db.crate_graph().iter().map(|id| Crate { id }).collect() db.crate_graph().iter().map(|id| Crate { id }).collect()
} }
pub fn crate_id(self) -> CrateId {
self.id
}
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@ -937,7 +934,7 @@ impl Type {
pub fn fields(&self, db: &impl HirDatabase) -> Vec<(StructField, Type)> { pub fn fields(&self, db: &impl HirDatabase) -> Vec<(StructField, Type)> {
if let Ty::Apply(a_ty) = &self.ty.value { if let Ty::Apply(a_ty) = &self.ty.value {
match a_ty.ctor { match a_ty.ctor {
ty::TypeCtor::Adt(AdtId::StructId(s)) => { TypeCtor::Adt(AdtId::StructId(s)) => {
let var_def = s.into(); let var_def = s.into();
return db return db
.field_types(var_def) .field_types(var_def)
@ -959,7 +956,7 @@ impl Type {
let mut res = Vec::new(); let mut res = Vec::new();
if let Ty::Apply(a_ty) = &self.ty.value { if let Ty::Apply(a_ty) = &self.ty.value {
match a_ty.ctor { match a_ty.ctor {
ty::TypeCtor::Tuple { .. } => { TypeCtor::Tuple { .. } => {
for ty in a_ty.parameters.iter() { for ty in a_ty.parameters.iter() {
let ty = ty.clone().subst(&a_ty.parameters); let ty = ty.clone().subst(&a_ty.parameters);
res.push(self.derived(ty)); res.push(self.derived(ty));
@ -995,10 +992,10 @@ impl Type {
pub fn autoderef<'a>(&'a self, db: &'a impl HirDatabase) -> impl Iterator<Item = Type> + 'a { pub fn autoderef<'a>(&'a self, db: &'a impl HirDatabase) -> impl Iterator<Item = Type> + 'a {
// There should be no inference vars in types passed here // There should be no inference vars in types passed here
// FIXME check that? // FIXME check that?
let canonical = crate::ty::Canonical { value: self.ty.value.clone(), num_vars: 0 }; let canonical = Canonical { value: self.ty.value.clone(), num_vars: 0 };
let environment = self.ty.environment.clone(); let environment = self.ty.environment.clone();
let ty = InEnvironment { value: canonical, environment: environment.clone() }; let ty = InEnvironment { value: canonical, environment: environment.clone() };
ty::autoderef(db, Some(self.krate), ty) autoderef(db, Some(self.krate), ty)
.map(|canonical| canonical.value) .map(|canonical| canonical.value)
.map(move |ty| self.derived(ty)) .map(move |ty| self.derived(ty))
} }
@ -1038,15 +1035,14 @@ impl Type {
// FIXME: provide required accessors such that it becomes implementable from outside. // FIXME: provide required accessors such that it becomes implementable from outside.
pub fn is_equal_for_find_impls(&self, other: &Type) -> bool { pub fn is_equal_for_find_impls(&self, other: &Type) -> bool {
match (&self.ty.value, &other.ty.value) { match (&self.ty.value, &other.ty.value) {
(Ty::Apply(a_original_ty), Ty::Apply(ty::ApplicationTy { ctor, parameters })) => { (Ty::Apply(a_original_ty), Ty::Apply(ApplicationTy { ctor, parameters })) => match ctor
match ctor { {
TypeCtor::Ref(..) => match parameters.as_single() { TypeCtor::Ref(..) => match parameters.as_single() {
Ty::Apply(a_ty) => a_original_ty.ctor == a_ty.ctor, Ty::Apply(a_ty) => a_original_ty.ctor == a_ty.ctor,
_ => false, _ => false,
}, },
_ => a_original_ty.ctor == *ctor, _ => a_original_ty.ctor == *ctor,
} },
}
_ => false, _ => false,
} }
} }

View file

@ -9,16 +9,10 @@ use hir_def::{
}; };
use crate::{ use crate::{
Adt, AssocItem, AttrDef, Crate, DefWithBody, EnumVariant, GenericDef, ModuleDef, StructField, Adt, AssocItem, AttrDef, DefWithBody, EnumVariant, GenericDef, ModuleDef, StructField,
VariantDef, VariantDef,
}; };
impl From<ra_db::CrateId> for Crate {
fn from(id: ra_db::CrateId) -> Self {
Crate { id }
}
}
macro_rules! from_id { macro_rules! from_id {
($(($id:path, $ty:path)),*) => {$( ($(($id:path, $ty:path)),*) => {$(
impl From<$id> for $ty { impl From<$id> for $ty {
@ -26,10 +20,16 @@ macro_rules! from_id {
$ty { id } $ty { id }
} }
} }
impl From<$ty> for $id {
fn from(ty: $ty) -> $id {
ty.id
}
}
)*} )*}
} }
from_id![ from_id![
(ra_db::CrateId, crate::Crate),
(hir_def::ModuleId, crate::Module), (hir_def::ModuleId, crate::Module),
(hir_def::StructId, crate::Struct), (hir_def::StructId, crate::Struct),
(hir_def::UnionId, crate::Union), (hir_def::UnionId, crate::Union),

View file

@ -31,7 +31,6 @@ pub mod debug;
pub mod db; pub mod db;
pub mod source_binder; pub mod source_binder;
mod ty;
pub mod diagnostics; pub mod diagnostics;
mod from_id; mod from_id;
@ -48,11 +47,6 @@ pub use crate::{
}, },
from_source::FromSource, from_source::FromSource,
source_binder::{PathResolution, ScopeEntryWithSyntax, SourceAnalyzer}, source_binder::{PathResolution, ScopeEntryWithSyntax, SourceAnalyzer},
ty::{
display::HirDisplay,
primitive::{FloatBitness, FloatTy, IntBitness, IntTy, Signedness, Uncertain},
ApplicationTy, CallableDef, Substs, TraitRef, Ty, TypeCtor, TypeWalk,
},
}; };
pub use hir_def::{ pub use hir_def::{
@ -66,3 +60,4 @@ pub use hir_def::{
pub use hir_expand::{ pub use hir_expand::{
name::Name, HirFileId, InFile, MacroCallId, MacroCallLoc, MacroDefId, MacroFile, name::Name, HirFileId, InFile, MacroCallId, MacroCallLoc, MacroDefId, MacroFile,
}; };
pub use hir_ty::{display::HirDisplay, CallableDef};

View file

@ -22,6 +22,10 @@ use hir_def::{
use hir_expand::{ use hir_expand::{
hygiene::Hygiene, name::AsName, AstId, HirFileId, InFile, MacroCallId, MacroCallKind, hygiene::Hygiene, name::AsName, AstId, HirFileId, InFile, MacroCallId, MacroCallKind,
}; };
use hir_ty::{
method_resolution::{self, implements_trait},
Canonical, InEnvironment, InferenceResult, TraitEnvironment, Ty,
};
use ra_syntax::{ use ra_syntax::{
ast::{self, AstNode}, ast::{self, AstNode},
match_ast, AstPtr, match_ast, AstPtr,
@ -30,13 +34,9 @@ use ra_syntax::{
}; };
use crate::{ use crate::{
db::HirDatabase, db::HirDatabase, Adt, AssocItem, Const, DefWithBody, Enum, EnumVariant, FromSource, Function,
ty::{ ImplBlock, Local, MacroDef, Name, Path, ScopeDef, Static, Struct, Trait, Type, TypeAlias,
method_resolution::{self, implements_trait}, TypeParam,
InEnvironment, TraitEnvironment, Ty,
},
Adt, AssocItem, Const, DefWithBody, Enum, EnumVariant, FromSource, Function, ImplBlock, Local,
MacroDef, Name, Path, ScopeDef, Static, Struct, Trait, Type, TypeAlias, TypeParam,
}; };
fn try_get_resolver_for_node(db: &impl HirDatabase, node: InFile<&SyntaxNode>) -> Option<Resolver> { fn try_get_resolver_for_node(db: &impl HirDatabase, node: InFile<&SyntaxNode>) -> Option<Resolver> {
@ -100,7 +100,7 @@ pub struct SourceAnalyzer {
resolver: Resolver, resolver: Resolver,
body_owner: Option<DefWithBody>, body_owner: Option<DefWithBody>,
body_source_map: Option<Arc<BodySourceMap>>, body_source_map: Option<Arc<BodySourceMap>>,
infer: Option<Arc<crate::ty::InferenceResult>>, infer: Option<Arc<InferenceResult>>,
scopes: Option<Arc<ExprScopes>>, scopes: Option<Arc<ExprScopes>>,
} }
@ -376,7 +376,7 @@ impl SourceAnalyzer {
// There should be no inference vars in types passed here // There should be no inference vars in types passed here
// FIXME check that? // FIXME check that?
// FIXME replace Unknown by bound vars here // FIXME replace Unknown by bound vars here
let canonical = crate::ty::Canonical { value: ty.ty.value.clone(), num_vars: 0 }; let canonical = Canonical { value: ty.ty.value.clone(), num_vars: 0 };
method_resolution::iterate_method_candidates( method_resolution::iterate_method_candidates(
&canonical, &canonical,
db, db,
@ -400,7 +400,7 @@ impl SourceAnalyzer {
// There should be no inference vars in types passed here // There should be no inference vars in types passed here
// FIXME check that? // FIXME check that?
// FIXME replace Unknown by bound vars here // FIXME replace Unknown by bound vars here
let canonical = crate::ty::Canonical { value: ty.ty.value.clone(), num_vars: 0 }; let canonical = Canonical { value: ty.ty.value.clone(), num_vars: 0 };
method_resolution::iterate_method_candidates( method_resolution::iterate_method_candidates(
&canonical, &canonical,
db, db,
@ -418,7 +418,7 @@ impl SourceAnalyzer {
// ) -> impl Iterator<Item = Ty> + 'a { // ) -> impl Iterator<Item = Ty> + 'a {
// // There should be no inference vars in types passed here // // There should be no inference vars in types passed here
// // FIXME check that? // // FIXME check that?
// let canonical = crate::ty::Canonical { value: ty, num_vars: 0 }; // let canonical = Canonical { value: ty, num_vars: 0 };
// let krate = self.resolver.krate(); // let krate = self.resolver.krate();
// let environment = TraitEnvironment::lower(db, &self.resolver); // let environment = TraitEnvironment::lower(db, &self.resolver);
// let ty = crate::ty::InEnvironment { value: canonical, environment }; // let ty = crate::ty::InEnvironment { value: canonical, environment };
@ -440,7 +440,7 @@ impl SourceAnalyzer {
_ => return false, _ => return false,
}; };
let canonical_ty = crate::ty::Canonical { value: ty, num_vars: 0 }; let canonical_ty = Canonical { value: ty, num_vars: 0 };
implements_trait(&canonical_ty, db, &self.resolver, krate.into(), std_future_trait) implements_trait(&canonical_ty, db, &self.resolver, krate.into(), std_future_trait)
} }

View file

@ -1,4 +0,0 @@
//! The type system. We currently use this to infer types for completion, hover
//! information and various assists.
pub use hir_ty::*;

View file

@ -30,7 +30,7 @@ pub(crate) fn crate_for(db: &RootDatabase, file_id: FileId) -> Vec<CrateId> {
None => return Vec::new(), None => return Vec::new(),
}; };
let krate = module.krate(); let krate = module.krate();
vec![krate.crate_id()] vec![krate.into()]
} }
#[cfg(test)] #[cfg(test)]