2390: Simplify r=matklad a=matklad



Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2019-11-24 17:54:14 +00:00 committed by GitHub
commit 64fc9ac965
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 34 additions and 106 deletions

View File

@ -3,7 +3,7 @@
use std::{collections::HashSet, fmt::Write, path::Path, time::Instant};
use ra_db::SourceDatabaseExt;
use ra_hir::{AssocItem, Crate, HasBodySource, HasSource, HirDisplay, ModuleDef, Ty, TypeWalk};
use ra_hir::{AssocItem, Crate, HasSource, HirDisplay, ModuleDef, Ty, TypeWalk};
use ra_syntax::AstNode;
use crate::{Result, Verbosity};
@ -128,15 +128,16 @@ pub fn run(
if let Some(mismatch) = inference_result.type_mismatch_for_expr(expr_id) {
num_type_mismatches += 1;
if verbosity.is_verbose() {
let src = f.expr_source(db, expr_id);
let src = f.body_source_map(db).expr_syntax(expr_id);
if let Some(src) = src {
// 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 path = db.file_relative_path(original_file);
let line_index = host.analysis().file_line_index(original_file).unwrap();
let text_range = src
.value
.either(|it| it.syntax().text_range(), |it| it.syntax().text_range());
let text_range = src.value.either(
|it| it.syntax_node_ptr().range(),
|it| it.syntax_node_ptr().range(),
);
let (start, end) = (
line_index.line_col(text_range.start()),
line_index.line_col(text_range.end()),

View File

@ -6,7 +6,6 @@
use hir_def::{
adt::VariantData,
body::scope::ExprScopes,
builtin_type::BuiltinType,
docs::Documentation,
per_ns::PerNs,
@ -28,7 +27,7 @@
db::{DefDatabase, HirDatabase},
expr::{BindingAnnotation, Body, BodySourceMap, ExprValidator, Pat, PatId},
ty::{InferenceResult, Namespace, TraitRef},
Either, HasSource, Name, Source, Ty,
Either, Name, Source, Ty,
};
/// hir::Crate describes a single crate. It's the main interface with which
@ -560,52 +559,6 @@ pub fn module(self, db: &impl HirDatabase) -> Module {
}
}
pub trait HasBody: Copy {
fn infer(self, db: &impl HirDatabase) -> Arc<InferenceResult>;
fn body(self, db: &impl HirDatabase) -> Arc<Body>;
fn body_source_map(self, db: &impl HirDatabase) -> Arc<BodySourceMap>;
fn expr_scopes(self, db: &impl HirDatabase) -> Arc<ExprScopes>;
}
impl<T> HasBody for T
where
T: Into<DefWithBody> + Copy + HasSource,
{
fn infer(self, db: &impl HirDatabase) -> Arc<InferenceResult> {
db.infer(self.into())
}
fn body(self, db: &impl HirDatabase) -> Arc<Body> {
self.into().body(db)
}
fn body_source_map(self, db: &impl HirDatabase) -> Arc<BodySourceMap> {
self.into().body_source_map(db)
}
fn expr_scopes(self, db: &impl HirDatabase) -> Arc<ExprScopes> {
self.into().expr_scopes(db)
}
}
impl HasBody for DefWithBody {
fn infer(self, db: &impl HirDatabase) -> Arc<InferenceResult> {
db.infer(self)
}
fn body(self, db: &impl HirDatabase) -> Arc<Body> {
db.body(self.into())
}
fn body_source_map(self, db: &impl HirDatabase) -> Arc<BodySourceMap> {
db.body_with_source_map(self.into()).1
}
fn expr_scopes(self, db: &impl HirDatabase) -> Arc<ExprScopes> {
db.expr_scopes(self.into())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Function {
pub(crate) id: FunctionId,
@ -632,7 +585,7 @@ pub fn params(self, db: &impl HirDatabase) -> Vec<TypeRef> {
db.function_data(self.id).params.clone()
}
pub(crate) fn body_source_map(self, db: &impl HirDatabase) -> Arc<BodySourceMap> {
pub fn body_source_map(self, db: &impl HirDatabase) -> Arc<BodySourceMap> {
db.body_with_source_map(self.id.into()).1
}
@ -966,7 +919,7 @@ pub struct Local {
impl Local {
pub fn name(self, db: &impl HirDatabase) -> Option<Name> {
let body = self.parent.body(db);
let body = db.body(self.parent.into());
match &body[self.pat_id] {
Pat::Bind { name, .. } => Some(name.clone()),
_ => None,
@ -978,7 +931,7 @@ pub fn is_self(self, db: &impl HirDatabase) -> bool {
}
pub fn is_mut(self, db: &impl HirDatabase) -> bool {
let body = self.parent.body(db);
let body = db.body(self.parent.into());
match &body[self.pat_id] {
Pat::Bind { mode, .. } => match mode {
BindingAnnotation::Mutable | BindingAnnotation::RefMut => true,
@ -1002,7 +955,7 @@ pub fn ty(self, db: &impl HirDatabase) -> Ty {
}
pub fn source(self, db: &impl HirDatabase) -> Source<Either<ast::BindPat, ast::SelfParam>> {
let source_map = self.parent.body_source_map(db);
let (_body, source_map) = db.body_with_source_map(self.parent.into());
let src = source_map.pat_syntax(self.pat_id).unwrap(); // Hmm...
let root = src.file_syntax(db);
src.map(|ast| ast.map(|it| it.cast().unwrap().to_node(&root), |it| it.to_node(&root)))

View File

@ -2,11 +2,10 @@
use hir_def::{AstItemDef, HasChildSource, HasSource as _, Lookup, VariantId};
use hir_expand::either::Either;
use ra_syntax::ast::{self, AstNode};
use ra_syntax::ast;
use crate::{
db::{DefDatabase, HirDatabase},
Const, Enum, EnumVariant, FieldSource, Function, HasBody, Import, MacroDef, Module,
db::DefDatabase, Const, Enum, EnumVariant, FieldSource, Function, Import, MacroDef, Module,
ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union,
};
@ -121,27 +120,3 @@ fn source(self, db: &impl DefDatabase) -> Source<Self::Ast> {
src.with_value(ptr.map(|it| it.to_node(&root), |it| it.to_node(&root)))
}
}
pub trait HasBodySource: HasBody + HasSource
where
Self::Ast: AstNode,
{
fn expr_source(
self,
db: &impl HirDatabase,
expr_id: crate::expr::ExprId,
) -> Option<Source<Either<ast::Expr, ast::RecordField>>> {
let source_map = self.body_source_map(db);
let source_ptr = source_map.expr_syntax(expr_id)?;
let root = source_ptr.file_syntax(db);
let source = source_ptr.map(|ast| ast.map(|it| it.to_node(&root), |it| it.to_node(&root)));
Some(source)
}
}
impl<T> HasBodySource for T
where
T: HasBody + HasSource,
T::Ast: AstNode,
{
}

View File

@ -9,9 +9,9 @@
use crate::{
db::{AstDatabase, DefDatabase, HirDatabase},
AssocItem, Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, HasBody, HasSource,
ImplBlock, Local, MacroDef, Module, ModuleDef, ModuleSource, Source, Static, Struct,
StructField, Trait, TypeAlias, Union, VariantDef,
AssocItem, Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, HasSource, ImplBlock,
Local, MacroDef, Module, ModuleDef, ModuleSource, Source, Static, Struct, StructField, Trait,
TypeAlias, Union, VariantDef,
};
pub trait FromSource: Sized {
@ -221,7 +221,7 @@ pub fn from_source(db: &impl HirDatabase, src: Source<ast::BindPat>) -> Option<S
};
Some(res)
})?;
let source_map = parent.body_source_map(db);
let (_body, source_map) = db.body_with_source_map(parent.into());
let src = src.map(ast::Pat::from);
let pat_id = source_map.node_pat(src.as_ref())?;
Some(Local { parent, pat_id })

View File

@ -49,11 +49,10 @@ fn from(it: $sv) -> $e {
pub use crate::{
code_model::{
src::{HasBodySource, HasSource},
Adt, AssocItem, AttrDef, Const, Container, Crate, CrateDependency, DefWithBody, Docs, Enum,
EnumVariant, FieldSource, Function, GenericDef, GenericParam, HasAttrs, HasBody, ImplBlock,
Import, Local, MacroDef, Module, ModuleDef, ModuleSource, ScopeDef, Static, Struct,
StructField, Trait, TypeAlias, Union, VariantDef,
src::HasSource, Adt, AssocItem, AttrDef, Const, Container, Crate, CrateDependency,
DefWithBody, Docs, Enum, EnumVariant, FieldSource, Function, GenericDef, GenericParam,
HasAttrs, ImplBlock, Import, Local, MacroDef, Module, ModuleDef, ModuleSource, ScopeDef,
Static, Struct, StructField, Trait, TypeAlias, Union, VariantDef,
},
expr::ExprScopes,
from_source::FromSource,

View File

@ -28,8 +28,7 @@
expr::{BodySourceMap, ExprScopes, ScopeId},
ty::method_resolution::{self, implements_trait},
Adt, AssocItem, Const, DefWithBody, Either, Enum, EnumVariant, FromSource, Function,
GenericParam, HasBody, Local, MacroDef, Name, Path, ScopeDef, Static, Struct, Trait, Ty,
TypeAlias,
GenericParam, Local, MacroDef, Name, Path, ScopeDef, Static, Struct, Trait, Ty, TypeAlias,
};
fn try_get_resolver_for_node(db: &impl HirDatabase, node: Source<&SyntaxNode>) -> Option<Resolver> {
@ -155,8 +154,8 @@ pub fn new(
) -> SourceAnalyzer {
let def_with_body = def_with_body_from_child_node(db, node);
if let Some(def) = def_with_body {
let source_map = def.body_source_map(db);
let scopes = def.expr_scopes(db);
let (_body, source_map) = db.body_with_source_map(def.into());
let scopes = db.expr_scopes(def.into());
let scope = match offset {
None => scope_for(&scopes, &source_map, node),
Some(offset) => scope_for_offset(&scopes, &source_map, node.with_value(offset)),
@ -166,7 +165,7 @@ pub fn new(
resolver,
body_owner: Some(def),
body_source_map: Some(source_map),
infer: Some(def.infer(db)),
infer: Some(db.infer(def)),
scopes: Some(scopes),
file_id: node.file_id,
}

View File

@ -44,8 +44,7 @@
db::HirDatabase,
expr::{BindingAnnotation, Body, ExprId, PatId},
ty::infer::diagnostics::InferenceDiagnostic,
Adt, AssocItem, DefWithBody, FloatTy, Function, HasBody, IntTy, Path, StructField, Trait,
VariantDef,
Adt, AssocItem, DefWithBody, FloatTy, Function, IntTy, Path, StructField, Trait, VariantDef,
};
macro_rules! ty_app {
@ -221,7 +220,7 @@ fn new(db: &'a D, owner: DefWithBody, resolver: Resolver) -> Self {
coerce_unsized_map: Self::init_coerce_unsized_map(db, &resolver),
db,
owner,
body: owner.body(db),
body: db.body(owner.into()),
resolver,
}
}

View File

@ -19,7 +19,7 @@
db::HirDatabase,
ty::display::HirDisplay,
ty::{ApplicationTy, GenericPredicate, ProjectionTy, Substs, TraitRef, Ty, TypeCtor, TypeWalk},
Crate, GenericDef, HasBody, ImplBlock, Trait, TypeAlias,
Crate, GenericDef, ImplBlock, Trait, TypeAlias,
};
/// This represents a trait whose name we could not resolve.
@ -715,7 +715,7 @@ fn closure_fn_trait_impl_datum(
let fn_once_trait = get_fn_trait(db, krate, super::FnTrait::FnOnce)?;
fn_once_trait.associated_type_by_name(db, &name::OUTPUT_TYPE)?;
let num_args: u16 = match &data.def.body(db)[data.expr] {
let num_args: u16 = match &db.body(data.def.into())[data.expr] {
crate::expr::Expr::Lambda { args, .. } => args.len() as u16,
_ => {
log::warn!("closure for closure type {:?} not found", data);
@ -805,7 +805,7 @@ fn closure_fn_trait_output_assoc_ty_value(
) -> Arc<AssociatedTyValue<ChalkIr>> {
let impl_id = Impl::ClosureFnTraitImpl(data.clone()).to_chalk(db);
let num_args: u16 = match &data.def.body(db)[data.expr] {
let num_args: u16 = match &db.body(data.def.into())[data.expr] {
crate::expr::Expr::Lambda { args, .. } => args.len() as u16,
_ => {
log::warn!("closure for closure type {:?} not found", data);

View File

@ -1,4 +1,5 @@
//! FIXME: write short doc here
//! Defines `Body`: a lowered representation of bodies of functions, statics and
//! consts.
mod lower;
pub mod scope;

View File

@ -1,4 +1,5 @@
//! FIXME: write short doc here
//! Transforms `ast::Expr` into an equivalent `hir_def::expr::Expr`
//! representation.
use hir_expand::{
either::Either,