Merge #3489
3489: More robust expression lowering r=matklad a=matklad Closes #2236 Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
1cc6879576
@ -21,9 +21,9 @@ pub trait WithFixture: Default + SourceDatabaseExt + 'static {
|
||||
(db, file_id)
|
||||
}
|
||||
|
||||
fn with_files(fixture: &str) -> Self {
|
||||
fn with_files(ra_fixture: &str) -> Self {
|
||||
let mut db = Self::default();
|
||||
let pos = with_files(&mut db, fixture);
|
||||
let pos = with_files(&mut db, ra_fixture);
|
||||
assert!(pos.is_none());
|
||||
db
|
||||
}
|
||||
|
@ -261,7 +261,7 @@ fn scope_for_offset(
|
||||
.scope_by_expr()
|
||||
.iter()
|
||||
.filter_map(|(id, scope)| {
|
||||
let source = source_map.expr_syntax(*id)?;
|
||||
let source = source_map.expr_syntax(*id).ok()?;
|
||||
// FIXME: correctly handle macro expansion
|
||||
if source.file_id != offset.file_id {
|
||||
return None;
|
||||
@ -337,7 +337,7 @@ fn adjust(
|
||||
.scope_by_expr()
|
||||
.iter()
|
||||
.filter_map(|(id, scope)| {
|
||||
let source = source_map.expr_syntax(*id)?;
|
||||
let source = source_map.expr_syntax(*id).ok()?;
|
||||
// FIXME: correctly handle macro expansion
|
||||
if source.file_id != file_id {
|
||||
return None;
|
||||
|
@ -149,13 +149,16 @@ pub type PatSource = InFile<PatPtr>;
|
||||
#[derive(Default, Debug, Eq, PartialEq)]
|
||||
pub struct BodySourceMap {
|
||||
expr_map: FxHashMap<ExprSource, ExprId>,
|
||||
expr_map_back: ArenaMap<ExprId, ExprSource>,
|
||||
expr_map_back: ArenaMap<ExprId, Result<ExprSource, SyntheticSyntax>>,
|
||||
pat_map: FxHashMap<PatSource, PatId>,
|
||||
pat_map_back: ArenaMap<PatId, PatSource>,
|
||||
pat_map_back: ArenaMap<PatId, Result<PatSource, SyntheticSyntax>>,
|
||||
field_map: FxHashMap<(ExprId, usize), AstPtr<ast::RecordField>>,
|
||||
expansions: FxHashMap<InFile<AstPtr<ast::MacroCall>>, HirFileId>,
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Eq, PartialEq, Clone, Copy)]
|
||||
pub struct SyntheticSyntax;
|
||||
|
||||
impl Body {
|
||||
pub(crate) fn body_with_source_map_query(
|
||||
db: &impl DefDatabase,
|
||||
@ -219,8 +222,8 @@ impl Index<PatId> for Body {
|
||||
}
|
||||
|
||||
impl BodySourceMap {
|
||||
pub fn expr_syntax(&self, expr: ExprId) -> Option<ExprSource> {
|
||||
self.expr_map_back.get(expr).copied()
|
||||
pub fn expr_syntax(&self, expr: ExprId) -> Result<ExprSource, SyntheticSyntax> {
|
||||
self.expr_map_back[expr]
|
||||
}
|
||||
|
||||
pub fn node_expr(&self, node: InFile<&ast::Expr>) -> Option<ExprId> {
|
||||
@ -238,8 +241,8 @@ impl BodySourceMap {
|
||||
self.expr_map.get(&src).cloned()
|
||||
}
|
||||
|
||||
pub fn pat_syntax(&self, pat: PatId) -> Option<PatSource> {
|
||||
self.pat_map_back.get(pat).copied()
|
||||
pub fn pat_syntax(&self, pat: PatId) -> Result<PatSource, SyntheticSyntax> {
|
||||
self.pat_map_back[pat]
|
||||
}
|
||||
|
||||
pub fn node_pat(&self, node: InFile<&ast::Pat>) -> Option<PatId> {
|
||||
|
@ -14,9 +14,10 @@ use ra_syntax::{
|
||||
};
|
||||
use test_utils::tested_by;
|
||||
|
||||
use super::{ExprSource, PatSource};
|
||||
use crate::{
|
||||
adt::StructKind,
|
||||
body::{Body, BodySourceMap, Expander, PatPtr},
|
||||
body::{Body, BodySourceMap, Expander, PatPtr, SyntheticSyntax},
|
||||
builtin_type::{BuiltinFloat, BuiltinInt},
|
||||
db::DefDatabase,
|
||||
expr::{
|
||||
@ -102,44 +103,48 @@ where
|
||||
|
||||
fn alloc_expr(&mut self, expr: Expr, ptr: AstPtr<ast::Expr>) -> ExprId {
|
||||
let ptr = Either::Left(ptr);
|
||||
let id = self.body.exprs.alloc(expr);
|
||||
let src = self.expander.to_source(ptr);
|
||||
let id = self.make_expr(expr, Ok(src));
|
||||
self.source_map.expr_map.insert(src, id);
|
||||
self.source_map.expr_map_back.insert(id, src);
|
||||
id
|
||||
}
|
||||
// desugared exprs don't have ptr, that's wrong and should be fixed
|
||||
// somehow.
|
||||
fn alloc_expr_desugared(&mut self, expr: Expr) -> ExprId {
|
||||
self.body.exprs.alloc(expr)
|
||||
self.make_expr(expr, Err(SyntheticSyntax))
|
||||
}
|
||||
fn alloc_expr_field_shorthand(&mut self, expr: Expr, ptr: AstPtr<ast::RecordField>) -> ExprId {
|
||||
let ptr = Either::Right(ptr);
|
||||
let id = self.body.exprs.alloc(expr);
|
||||
let src = self.expander.to_source(ptr);
|
||||
let id = self.make_expr(expr, Ok(src));
|
||||
self.source_map.expr_map.insert(src, id);
|
||||
id
|
||||
}
|
||||
fn empty_block(&mut self) -> ExprId {
|
||||
self.alloc_expr_desugared(Expr::Block { statements: Vec::new(), tail: None })
|
||||
}
|
||||
fn missing_expr(&mut self) -> ExprId {
|
||||
self.alloc_expr_desugared(Expr::Missing)
|
||||
}
|
||||
fn make_expr(&mut self, expr: Expr, src: Result<ExprSource, SyntheticSyntax>) -> ExprId {
|
||||
let id = self.body.exprs.alloc(expr);
|
||||
self.source_map.expr_map_back.insert(id, src);
|
||||
id
|
||||
}
|
||||
|
||||
fn alloc_pat(&mut self, pat: Pat, ptr: PatPtr) -> PatId {
|
||||
let id = self.body.pats.alloc(pat);
|
||||
let src = self.expander.to_source(ptr);
|
||||
let id = self.make_pat(pat, Ok(src));
|
||||
self.source_map.pat_map.insert(src, id);
|
||||
self.source_map.pat_map_back.insert(id, src);
|
||||
id
|
||||
}
|
||||
|
||||
fn empty_block(&mut self) -> ExprId {
|
||||
let block = Expr::Block { statements: Vec::new(), tail: None };
|
||||
self.body.exprs.alloc(block)
|
||||
}
|
||||
|
||||
fn missing_expr(&mut self) -> ExprId {
|
||||
self.body.exprs.alloc(Expr::Missing)
|
||||
}
|
||||
|
||||
fn missing_pat(&mut self) -> PatId {
|
||||
self.body.pats.alloc(Pat::Missing)
|
||||
self.make_pat(Pat::Missing, Err(SyntheticSyntax))
|
||||
}
|
||||
fn make_pat(&mut self, pat: Pat, src: Result<PatSource, SyntheticSyntax>) -> PatId {
|
||||
let id = self.body.pats.alloc(pat);
|
||||
self.source_map.pat_map_back.insert(id, src);
|
||||
id
|
||||
}
|
||||
|
||||
fn collect_expr(&mut self, expr: ast::Expr) -> ExprId {
|
||||
|
@ -100,7 +100,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
|
||||
}
|
||||
let (_, source_map) = db.body_with_source_map(self.func.into());
|
||||
|
||||
if let Some(source_ptr) = source_map.expr_syntax(id) {
|
||||
if let Ok(source_ptr) = source_map.expr_syntax(id) {
|
||||
if let Some(expr) = source_ptr.value.left() {
|
||||
let root = source_ptr.file_syntax(db);
|
||||
if let ast::Expr::RecordLit(record_lit) = expr.to_node(&root) {
|
||||
@ -145,7 +145,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
|
||||
if params.len() == 2 && params[0] == mismatch.actual {
|
||||
let (_, source_map) = db.body_with_source_map(self.func.into());
|
||||
|
||||
if let Some(source_ptr) = source_map.expr_syntax(id) {
|
||||
if let Ok(source_ptr) = source_map.expr_syntax(id) {
|
||||
if let Some(expr) = source_ptr.value.left() {
|
||||
self.sink.push(MissingOkInTailExpr { file: source_ptr.file_id, expr });
|
||||
}
|
||||
|
@ -11,8 +11,13 @@ use std::fmt::Write;
|
||||
use std::sync::Arc;
|
||||
|
||||
use hir_def::{
|
||||
body::BodySourceMap, child_by_source::ChildBySource, db::DefDatabase, item_scope::ItemScope,
|
||||
keys, nameres::CrateDefMap, AssocItemId, DefWithBodyId, LocalModuleId, Lookup, ModuleDefId,
|
||||
body::{BodySourceMap, SyntheticSyntax},
|
||||
child_by_source::ChildBySource,
|
||||
db::DefDatabase,
|
||||
item_scope::ItemScope,
|
||||
keys,
|
||||
nameres::CrateDefMap,
|
||||
AssocItemId, DefWithBodyId, LocalModuleId, Lookup, ModuleDefId,
|
||||
};
|
||||
use hir_expand::InFile;
|
||||
use insta::assert_snapshot;
|
||||
@ -67,20 +72,20 @@ fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String {
|
||||
|
||||
for (pat, ty) in inference_result.type_of_pat.iter() {
|
||||
let syntax_ptr = match body_source_map.pat_syntax(pat) {
|
||||
Some(sp) => {
|
||||
Ok(sp) => {
|
||||
sp.map(|ast| ast.either(|it| it.syntax_node_ptr(), |it| it.syntax_node_ptr()))
|
||||
}
|
||||
None => continue,
|
||||
Err(SyntheticSyntax) => continue,
|
||||
};
|
||||
types.push((syntax_ptr, ty));
|
||||
}
|
||||
|
||||
for (expr, ty) in inference_result.type_of_expr.iter() {
|
||||
let syntax_ptr = match body_source_map.expr_syntax(expr) {
|
||||
Some(sp) => {
|
||||
Ok(sp) => {
|
||||
sp.map(|ast| ast.either(|it| it.syntax_node_ptr(), |it| it.syntax_node_ptr()))
|
||||
}
|
||||
None => continue,
|
||||
Err(SyntheticSyntax) => continue,
|
||||
};
|
||||
types.push((syntax_ptr, ty));
|
||||
if let Some(mismatch) = inference_result.type_mismatch_for_expr(expr) {
|
||||
|
@ -158,7 +158,7 @@ pub fn analysis_stats(
|
||||
// in super-verbose mode for just one function, we print every single expression
|
||||
let (_, sm) = db.body_with_source_map(f_id.into());
|
||||
let src = sm.expr_syntax(expr_id);
|
||||
if let Some(src) = src {
|
||||
if let Ok(src) = src {
|
||||
let original_file = src.file_id.original_file(db);
|
||||
let line_index = host.analysis().file_line_index(original_file).unwrap();
|
||||
let text_range = src.value.either(
|
||||
@ -186,7 +186,7 @@ pub fn analysis_stats(
|
||||
if verbosity.is_verbose() {
|
||||
let (_, sm) = db.body_with_source_map(f_id.into());
|
||||
let src = sm.expr_syntax(expr_id);
|
||||
if let Some(src) = src {
|
||||
if let Ok(src) = src {
|
||||
// FIXME: it might be nice to have a function (on Analysis?) that goes from Source<T> -> (LineCol, LineCol) directly
|
||||
// But also, we should just turn the type mismatches into diagnostics and provide these
|
||||
let root = db.parse_or_expand(src.file_id).unwrap();
|
||||
|
Loading…
x
Reference in New Issue
Block a user