mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 05:15:04 +00:00
Simplify
This commit is contained in:
parent
d157812cd1
commit
e48430cbae
8 changed files with 30 additions and 104 deletions
|
@ -3,7 +3,7 @@
|
||||||
use std::{collections::HashSet, fmt::Write, path::Path, time::Instant};
|
use std::{collections::HashSet, fmt::Write, path::Path, time::Instant};
|
||||||
|
|
||||||
use ra_db::SourceDatabaseExt;
|
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 ra_syntax::AstNode;
|
||||||
|
|
||||||
use crate::{Result, Verbosity};
|
use crate::{Result, Verbosity};
|
||||||
|
@ -128,15 +128,16 @@ 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.expr_source(db, expr_id);
|
let src = f.body_source_map(db).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);
|
||||||
let path = db.file_relative_path(original_file);
|
let path = db.file_relative_path(original_file);
|
||||||
let line_index = host.analysis().file_line_index(original_file).unwrap();
|
let line_index = host.analysis().file_line_index(original_file).unwrap();
|
||||||
let text_range = src
|
let text_range = src.value.either(
|
||||||
.value
|
|it| it.syntax_node_ptr().range(),
|
||||||
.either(|it| it.syntax().text_range(), |it| it.syntax().text_range());
|
|it| it.syntax_node_ptr().range(),
|
||||||
|
);
|
||||||
let (start, end) = (
|
let (start, end) = (
|
||||||
line_index.line_col(text_range.start()),
|
line_index.line_col(text_range.start()),
|
||||||
line_index.line_col(text_range.end()),
|
line_index.line_col(text_range.end()),
|
||||||
|
|
|
@ -6,7 +6,6 @@ use std::sync::Arc;
|
||||||
|
|
||||||
use hir_def::{
|
use hir_def::{
|
||||||
adt::VariantData,
|
adt::VariantData,
|
||||||
body::scope::ExprScopes,
|
|
||||||
builtin_type::BuiltinType,
|
builtin_type::BuiltinType,
|
||||||
docs::Documentation,
|
docs::Documentation,
|
||||||
per_ns::PerNs,
|
per_ns::PerNs,
|
||||||
|
@ -28,7 +27,7 @@ use crate::{
|
||||||
db::{DefDatabase, HirDatabase},
|
db::{DefDatabase, HirDatabase},
|
||||||
expr::{BindingAnnotation, Body, BodySourceMap, ExprValidator, Pat, PatId},
|
expr::{BindingAnnotation, Body, BodySourceMap, ExprValidator, Pat, PatId},
|
||||||
ty::{InferenceResult, Namespace, TraitRef},
|
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
|
/// hir::Crate describes a single crate. It's the main interface with which
|
||||||
|
@ -560,52 +559,6 @@ impl DefWithBody {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
pub struct Function {
|
pub struct Function {
|
||||||
pub(crate) id: FunctionId,
|
pub(crate) id: FunctionId,
|
||||||
|
@ -632,7 +585,7 @@ impl Function {
|
||||||
db.function_data(self.id).params.clone()
|
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
|
db.body_with_source_map(self.id.into()).1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -966,7 +919,7 @@ pub struct Local {
|
||||||
|
|
||||||
impl Local {
|
impl Local {
|
||||||
pub fn name(self, db: &impl HirDatabase) -> Option<Name> {
|
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] {
|
match &body[self.pat_id] {
|
||||||
Pat::Bind { name, .. } => Some(name.clone()),
|
Pat::Bind { name, .. } => Some(name.clone()),
|
||||||
_ => None,
|
_ => None,
|
||||||
|
@ -978,7 +931,7 @@ impl Local {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_mut(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] {
|
match &body[self.pat_id] {
|
||||||
Pat::Bind { mode, .. } => match mode {
|
Pat::Bind { mode, .. } => match mode {
|
||||||
BindingAnnotation::Mutable | BindingAnnotation::RefMut => true,
|
BindingAnnotation::Mutable | BindingAnnotation::RefMut => true,
|
||||||
|
@ -1002,7 +955,7 @@ impl Local {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn source(self, db: &impl HirDatabase) -> Source<Either<ast::BindPat, ast::SelfParam>> {
|
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 src = source_map.pat_syntax(self.pat_id).unwrap(); // Hmm...
|
||||||
let root = src.file_syntax(db);
|
let root = src.file_syntax(db);
|
||||||
src.map(|ast| ast.map(|it| it.cast().unwrap().to_node(&root), |it| it.to_node(&root)))
|
src.map(|ast| ast.map(|it| it.cast().unwrap().to_node(&root), |it| it.to_node(&root)))
|
||||||
|
|
|
@ -2,11 +2,10 @@
|
||||||
|
|
||||||
use hir_def::{AstItemDef, HasChildSource, HasSource as _, Lookup, VariantId};
|
use hir_def::{AstItemDef, HasChildSource, HasSource as _, Lookup, VariantId};
|
||||||
use hir_expand::either::Either;
|
use hir_expand::either::Either;
|
||||||
use ra_syntax::ast::{self, AstNode};
|
use ra_syntax::ast;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
db::{DefDatabase, HirDatabase},
|
db::DefDatabase, Const, Enum, EnumVariant, FieldSource, Function, Import, MacroDef, Module,
|
||||||
Const, Enum, EnumVariant, FieldSource, Function, HasBody, Import, MacroDef, Module,
|
|
||||||
ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union,
|
ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -121,27 +120,3 @@ impl HasSource for Import {
|
||||||
src.with_value(ptr.map(|it| it.to_node(&root), |it| it.to_node(&root)))
|
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,
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
|
@ -9,9 +9,9 @@ use ra_syntax::{
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
db::{AstDatabase, DefDatabase, HirDatabase},
|
db::{AstDatabase, DefDatabase, HirDatabase},
|
||||||
AssocItem, Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, HasBody, HasSource,
|
AssocItem, Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, HasSource, ImplBlock,
|
||||||
ImplBlock, Local, MacroDef, Module, ModuleDef, ModuleSource, Source, Static, Struct,
|
Local, MacroDef, Module, ModuleDef, ModuleSource, Source, Static, Struct, StructField, Trait,
|
||||||
StructField, Trait, TypeAlias, Union, VariantDef,
|
TypeAlias, Union, VariantDef,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub trait FromSource: Sized {
|
pub trait FromSource: Sized {
|
||||||
|
@ -221,7 +221,7 @@ impl Local {
|
||||||
};
|
};
|
||||||
Some(res)
|
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 src = src.map(ast::Pat::from);
|
||||||
let pat_id = source_map.node_pat(src.as_ref())?;
|
let pat_id = source_map.node_pat(src.as_ref())?;
|
||||||
Some(Local { parent, pat_id })
|
Some(Local { parent, pat_id })
|
||||||
|
|
|
@ -49,11 +49,10 @@ mod marks;
|
||||||
|
|
||||||
pub use crate::{
|
pub use crate::{
|
||||||
code_model::{
|
code_model::{
|
||||||
src::{HasBodySource, HasSource},
|
src::HasSource, Adt, AssocItem, AttrDef, Const, Container, Crate, CrateDependency,
|
||||||
Adt, AssocItem, AttrDef, Const, Container, Crate, CrateDependency, DefWithBody, Docs, Enum,
|
DefWithBody, Docs, Enum, EnumVariant, FieldSource, Function, GenericDef, GenericParam,
|
||||||
EnumVariant, FieldSource, Function, GenericDef, GenericParam, HasAttrs, HasBody, ImplBlock,
|
HasAttrs, ImplBlock, Import, Local, MacroDef, Module, ModuleDef, ModuleSource, ScopeDef,
|
||||||
Import, Local, MacroDef, Module, ModuleDef, ModuleSource, ScopeDef, Static, Struct,
|
Static, Struct, StructField, Trait, TypeAlias, Union, VariantDef,
|
||||||
StructField, Trait, TypeAlias, Union, VariantDef,
|
|
||||||
},
|
},
|
||||||
expr::ExprScopes,
|
expr::ExprScopes,
|
||||||
from_source::FromSource,
|
from_source::FromSource,
|
||||||
|
|
|
@ -28,8 +28,7 @@ use crate::{
|
||||||
expr::{BodySourceMap, ExprScopes, ScopeId},
|
expr::{BodySourceMap, ExprScopes, ScopeId},
|
||||||
ty::method_resolution::{self, implements_trait},
|
ty::method_resolution::{self, implements_trait},
|
||||||
Adt, AssocItem, Const, DefWithBody, Either, Enum, EnumVariant, FromSource, Function,
|
Adt, AssocItem, Const, DefWithBody, Either, Enum, EnumVariant, FromSource, Function,
|
||||||
GenericParam, HasBody, Local, MacroDef, Name, Path, ScopeDef, Static, Struct, Trait, Ty,
|
GenericParam, Local, MacroDef, Name, Path, ScopeDef, Static, Struct, Trait, Ty, TypeAlias,
|
||||||
TypeAlias,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
fn try_get_resolver_for_node(db: &impl HirDatabase, node: Source<&SyntaxNode>) -> Option<Resolver> {
|
fn try_get_resolver_for_node(db: &impl HirDatabase, node: Source<&SyntaxNode>) -> Option<Resolver> {
|
||||||
|
@ -155,8 +154,8 @@ impl SourceAnalyzer {
|
||||||
) -> SourceAnalyzer {
|
) -> SourceAnalyzer {
|
||||||
let def_with_body = def_with_body_from_child_node(db, node);
|
let def_with_body = def_with_body_from_child_node(db, node);
|
||||||
if let Some(def) = def_with_body {
|
if let Some(def) = def_with_body {
|
||||||
let source_map = def.body_source_map(db);
|
let (_body, source_map) = db.body_with_source_map(def.into());
|
||||||
let scopes = def.expr_scopes(db);
|
let scopes = db.expr_scopes(def.into());
|
||||||
let scope = match offset {
|
let scope = match offset {
|
||||||
None => scope_for(&scopes, &source_map, node),
|
None => scope_for(&scopes, &source_map, node),
|
||||||
Some(offset) => scope_for_offset(&scopes, &source_map, node.with_value(offset)),
|
Some(offset) => scope_for_offset(&scopes, &source_map, node.with_value(offset)),
|
||||||
|
@ -166,7 +165,7 @@ impl SourceAnalyzer {
|
||||||
resolver,
|
resolver,
|
||||||
body_owner: Some(def),
|
body_owner: Some(def),
|
||||||
body_source_map: Some(source_map),
|
body_source_map: Some(source_map),
|
||||||
infer: Some(def.infer(db)),
|
infer: Some(db.infer(def)),
|
||||||
scopes: Some(scopes),
|
scopes: Some(scopes),
|
||||||
file_id: node.file_id,
|
file_id: node.file_id,
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,8 +44,7 @@ use crate::{
|
||||||
db::HirDatabase,
|
db::HirDatabase,
|
||||||
expr::{BindingAnnotation, Body, ExprId, PatId},
|
expr::{BindingAnnotation, Body, ExprId, PatId},
|
||||||
ty::infer::diagnostics::InferenceDiagnostic,
|
ty::infer::diagnostics::InferenceDiagnostic,
|
||||||
Adt, AssocItem, DefWithBody, FloatTy, Function, HasBody, IntTy, Path, StructField, Trait,
|
Adt, AssocItem, DefWithBody, FloatTy, Function, IntTy, Path, StructField, Trait, VariantDef,
|
||||||
VariantDef,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
macro_rules! ty_app {
|
macro_rules! ty_app {
|
||||||
|
@ -221,7 +220,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
||||||
coerce_unsized_map: Self::init_coerce_unsized_map(db, &resolver),
|
coerce_unsized_map: Self::init_coerce_unsized_map(db, &resolver),
|
||||||
db,
|
db,
|
||||||
owner,
|
owner,
|
||||||
body: owner.body(db),
|
body: db.body(owner.into()),
|
||||||
resolver,
|
resolver,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ use crate::{
|
||||||
db::HirDatabase,
|
db::HirDatabase,
|
||||||
ty::display::HirDisplay,
|
ty::display::HirDisplay,
|
||||||
ty::{ApplicationTy, GenericPredicate, ProjectionTy, Substs, TraitRef, Ty, TypeCtor, TypeWalk},
|
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.
|
/// 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)?;
|
let fn_once_trait = get_fn_trait(db, krate, super::FnTrait::FnOnce)?;
|
||||||
fn_once_trait.associated_type_by_name(db, &name::OUTPUT_TYPE)?;
|
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,
|
crate::expr::Expr::Lambda { args, .. } => args.len() as u16,
|
||||||
_ => {
|
_ => {
|
||||||
log::warn!("closure for closure type {:?} not found", data);
|
log::warn!("closure for closure type {:?} not found", data);
|
||||||
|
@ -805,7 +805,7 @@ fn closure_fn_trait_output_assoc_ty_value(
|
||||||
) -> Arc<AssociatedTyValue<ChalkIr>> {
|
) -> Arc<AssociatedTyValue<ChalkIr>> {
|
||||||
let impl_id = Impl::ClosureFnTraitImpl(data.clone()).to_chalk(db);
|
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,
|
crate::expr::Expr::Lambda { args, .. } => args.len() as u16,
|
||||||
_ => {
|
_ => {
|
||||||
log::warn!("closure for closure type {:?} not found", data);
|
log::warn!("closure for closure type {:?} not found", data);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue