mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 21:35:20 +00:00
Simplify
This commit is contained in:
parent
f5e0a31eaf
commit
434f108ada
8 changed files with 17 additions and 33 deletions
|
@ -109,7 +109,7 @@ pub fn run(
|
||||||
}
|
}
|
||||||
let body = f.body(db);
|
let body = f.body(db);
|
||||||
let inference_result = f.infer(db);
|
let inference_result = f.infer(db);
|
||||||
for (expr_id, _) in body.exprs() {
|
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;
|
||||||
if let Ty::Unknown = ty {
|
if let Ty::Unknown = ty {
|
||||||
|
|
|
@ -44,15 +44,15 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
|
||||||
pub(crate) fn validate_body(&mut self, db: &impl HirDatabase) {
|
pub(crate) fn validate_body(&mut self, db: &impl HirDatabase) {
|
||||||
let body = self.func.body(db);
|
let body = self.func.body(db);
|
||||||
|
|
||||||
for e in body.exprs() {
|
for e in body.exprs.iter() {
|
||||||
if let (id, Expr::RecordLit { path, fields, spread }) = e {
|
if let (id, Expr::RecordLit { path, fields, spread }) = e {
|
||||||
self.validate_record_literal(id, path, fields, *spread, db);
|
self.validate_record_literal(id, path, fields, *spread, db);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let body_expr = &body[body.body_expr()];
|
let body_expr = &body[body.body_expr];
|
||||||
if let Expr::Block { statements: _, tail: Some(t) } = body_expr {
|
if let Expr::Block { statements: _, tail: Some(t) } = body_expr {
|
||||||
self.validate_results_in_tail_expr(body.body_expr(), *t, db);
|
self.validate_results_in_tail_expr(body.body_expr, *t, db);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -80,7 +80,7 @@ impl TestDB {
|
||||||
let crate_graph = self.crate_graph();
|
let crate_graph = self.crate_graph();
|
||||||
for krate in crate_graph.iter().next() {
|
for krate in crate_graph.iter().next() {
|
||||||
let crate_def_map = self.crate_def_map(krate);
|
let crate_def_map = self.crate_def_map(krate);
|
||||||
for module_id in crate_def_map.modules() {
|
for (module_id, _) in crate_def_map.modules.iter() {
|
||||||
let module_id = ModuleId { krate, module_id };
|
let module_id = ModuleId { krate, module_id };
|
||||||
let module = crate::Module::from(module_id);
|
let module = crate::Module::from(module_id);
|
||||||
module.diagnostics(
|
module.diagnostics(
|
||||||
|
|
|
@ -565,7 +565,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
||||||
|
|
||||||
fn collect_fn(&mut self, data: &FunctionData) {
|
fn collect_fn(&mut self, data: &FunctionData) {
|
||||||
let body = Arc::clone(&self.body); // avoid borrow checker problem
|
let body = Arc::clone(&self.body); // avoid borrow checker problem
|
||||||
for (type_ref, pat) in data.params.iter().zip(body.params()) {
|
for (type_ref, pat) in data.params.iter().zip(body.params.iter()) {
|
||||||
let ty = self.make_ty(type_ref);
|
let ty = self.make_ty(type_ref);
|
||||||
|
|
||||||
self.infer_pat(*pat, &ty, BindingMode::default());
|
self.infer_pat(*pat, &ty, BindingMode::default());
|
||||||
|
@ -574,7 +574,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn infer_body(&mut self) {
|
fn infer_body(&mut self) {
|
||||||
self.infer_expr(self.body.body_expr(), &Expectation::has_type(self.return_ty.clone()));
|
self.infer_expr(self.body.body_expr, &Expectation::has_type(self.return_ty.clone()));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn resolve_into_iter_item(&self) -> Option<TypeAlias> {
|
fn resolve_into_iter_item(&self) -> Option<TypeAlias> {
|
||||||
|
|
|
@ -20,7 +20,7 @@ use crate::{
|
||||||
DefWithBodyId, HasModule, HasSource, Lookup, ModuleId,
|
DefWithBodyId, HasModule, HasSource, Lookup, ModuleId,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct Expander {
|
struct Expander {
|
||||||
crate_def_map: Arc<CrateDefMap>,
|
crate_def_map: Arc<CrateDefMap>,
|
||||||
current_file_id: HirFileId,
|
current_file_id: HirFileId,
|
||||||
hygiene: Hygiene,
|
hygiene: Hygiene,
|
||||||
|
@ -28,7 +28,7 @@ pub struct Expander {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Expander {
|
impl Expander {
|
||||||
pub fn new(db: &impl DefDatabase, current_file_id: HirFileId, module: ModuleId) -> Expander {
|
fn new(db: &impl DefDatabase, current_file_id: HirFileId, module: ModuleId) -> Expander {
|
||||||
let crate_def_map = db.crate_def_map(module.krate);
|
let crate_def_map = db.crate_def_map(module.krate);
|
||||||
let hygiene = Hygiene::new(db, current_file_id);
|
let hygiene = Hygiene::new(db, current_file_id);
|
||||||
Expander { crate_def_map, current_file_id, hygiene, module }
|
Expander { crate_def_map, current_file_id, hygiene, module }
|
||||||
|
@ -101,17 +101,17 @@ impl Drop for Mark {
|
||||||
/// The body of an item (function, const etc.).
|
/// The body of an item (function, const etc.).
|
||||||
#[derive(Debug, Eq, PartialEq)]
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
pub struct Body {
|
pub struct Body {
|
||||||
exprs: Arena<ExprId, Expr>,
|
pub exprs: Arena<ExprId, Expr>,
|
||||||
pats: Arena<PatId, Pat>,
|
pub pats: Arena<PatId, Pat>,
|
||||||
/// The patterns for the function's parameters. While the parameter types are
|
/// The patterns for the function's parameters. While the parameter types are
|
||||||
/// part of the function signature, the patterns are not (they don't change
|
/// part of the function signature, the patterns are not (they don't change
|
||||||
/// the external type of the function).
|
/// the external type of the function).
|
||||||
///
|
///
|
||||||
/// If this `Body` is for the body of a constant, this will just be
|
/// If this `Body` is for the body of a constant, this will just be
|
||||||
/// empty.
|
/// empty.
|
||||||
params: Vec<PatId>,
|
pub params: Vec<PatId>,
|
||||||
/// The `ExprId` of the actual body expression.
|
/// The `ExprId` of the actual body expression.
|
||||||
body_expr: ExprId,
|
pub body_expr: ExprId,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type ExprPtr = Either<AstPtr<ast::Expr>, AstPtr<ast::RecordField>>;
|
pub type ExprPtr = Either<AstPtr<ast::Expr>, AstPtr<ast::RecordField>>;
|
||||||
|
@ -182,22 +182,6 @@ impl Body {
|
||||||
) -> (Body, BodySourceMap) {
|
) -> (Body, BodySourceMap) {
|
||||||
lower::lower(db, expander, params, body)
|
lower::lower(db, expander, params, body)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn params(&self) -> &[PatId] {
|
|
||||||
&self.params
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn body_expr(&self) -> ExprId {
|
|
||||||
self.body_expr
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn exprs(&self) -> impl Iterator<Item = (ExprId, &Expr)> {
|
|
||||||
self.exprs.iter()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn pats(&self) -> impl Iterator<Item = (PatId, &Pat)> {
|
|
||||||
self.pats.iter()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Index<ExprId> for Body {
|
impl Index<ExprId> for Body {
|
||||||
|
|
|
@ -54,8 +54,8 @@ impl ExprScopes {
|
||||||
let mut scopes =
|
let mut scopes =
|
||||||
ExprScopes { scopes: Arena::default(), scope_by_expr: FxHashMap::default() };
|
ExprScopes { scopes: Arena::default(), scope_by_expr: FxHashMap::default() };
|
||||||
let root = scopes.root_scope();
|
let root = scopes.root_scope();
|
||||||
scopes.add_params_bindings(body, root, body.params());
|
scopes.add_params_bindings(body, root, &body.params);
|
||||||
compute_expr_scopes(body.body_expr(), body, &mut scopes, root);
|
compute_expr_scopes(body.body_expr, body, &mut scopes, root);
|
||||||
scopes
|
scopes
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -81,13 +81,13 @@ use crate::{
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub struct CrateDefMap {
|
pub struct CrateDefMap {
|
||||||
pub root: LocalModuleId,
|
pub root: LocalModuleId,
|
||||||
|
pub modules: Arena<LocalModuleId, ModuleData>,
|
||||||
pub(crate) krate: CrateId,
|
pub(crate) krate: CrateId,
|
||||||
/// The prelude module for this crate. This either comes from an import
|
/// The prelude module for this crate. This either comes from an import
|
||||||
/// marked with the `prelude_import` attribute, or (in the normal case) from
|
/// marked with the `prelude_import` attribute, or (in the normal case) from
|
||||||
/// a dependency (`std` or `core`).
|
/// a dependency (`std` or `core`).
|
||||||
pub(crate) prelude: Option<ModuleId>,
|
pub(crate) prelude: Option<ModuleId>,
|
||||||
pub(crate) extern_prelude: FxHashMap<Name, ModuleDefId>,
|
pub(crate) extern_prelude: FxHashMap<Name, ModuleDefId>,
|
||||||
pub(crate) modules: Arena<LocalModuleId, ModuleData>,
|
|
||||||
|
|
||||||
edition: Edition,
|
edition: Edition,
|
||||||
diagnostics: Vec<DefDiagnostic>,
|
diagnostics: Vec<DefDiagnostic>,
|
||||||
|
|
|
@ -25,7 +25,7 @@ fn compute_crate_def_map(fixture: &str) -> Arc<CrateDefMap> {
|
||||||
|
|
||||||
fn render_crate_def_map(map: &CrateDefMap) -> String {
|
fn render_crate_def_map(map: &CrateDefMap) -> String {
|
||||||
let mut buf = String::new();
|
let mut buf = String::new();
|
||||||
go(&mut buf, map, "\ncrate", map.root());
|
go(&mut buf, map, "\ncrate", map.root);
|
||||||
return buf.trim().to_string();
|
return buf.trim().to_string();
|
||||||
|
|
||||||
fn go(buf: &mut String, map: &CrateDefMap, path: &str, module: LocalModuleId) {
|
fn go(buf: &mut String, map: &CrateDefMap, path: &str, module: LocalModuleId) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue