From 5ffddc4b921685ebd5f9525f7e33913a1d08c04e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 6 Mar 2020 15:11:05 +0100 Subject: Explicitly remember desugard exprs --- crates/ra_hir_def/src/body.rs | 6 +++--- crates/ra_hir_def/src/body/lower.rs | 31 ++++++++++++++++--------------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs index cd9e22c42..e60adc146 100644 --- a/crates/ra_hir_def/src/body.rs +++ b/crates/ra_hir_def/src/body.rs @@ -149,14 +149,14 @@ pub type PatSource = InFile; #[derive(Default, Debug, Eq, PartialEq)] pub struct BodySourceMap { expr_map: FxHashMap, - expr_map_back: ArenaMap, + expr_map_back: ArenaMap>, pat_map: FxHashMap, pat_map_back: ArenaMap, field_map: FxHashMap<(ExprId, usize), AstPtr>, expansions: FxHashMap>, HirFileId>, } -#[derive(Debug)] +#[derive(Default, Debug, Eq, PartialEq, Clone, Copy)] pub struct SyntheticSyntax; impl Body { @@ -223,7 +223,7 @@ impl Index for Body { impl BodySourceMap { pub fn expr_syntax(&self, expr: ExprId) -> Result { - self.expr_map_back.get(expr).copied().ok_or(SyntheticSyntax) + self.expr_map_back[expr] } pub fn node_expr(&self, node: InFile<&ast::Expr>) -> Option { diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index c18e6879b..209907700 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs @@ -14,9 +14,10 @@ use ra_syntax::{ }; use test_utils::tested_by; +use super::ExprSource; use crate::{ adt::StructKind, - body::{Body, BodySourceMap, Expander, PatPtr}, + body::{Body, BodySourceMap, Expander, PatPtr, SyntheticSyntax}, builtin_type::{BuiltinFloat, BuiltinInt}, db::DefDatabase, expr::{ @@ -102,25 +103,35 @@ where fn alloc_expr(&mut self, expr: Expr, ptr: AstPtr) -> 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) -> 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) -> 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); @@ -128,16 +139,6 @@ where 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) } -- cgit v1.2.3