From fb5891c4330e577f5d6891be529141695ce36d4f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 6 Mar 2020 14:44:44 +0100 Subject: Source map returns a result cc #2236 --- crates/ra_hir_def/src/body.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'crates/ra_hir_def/src') diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs index 010d35e55..cd9e22c42 100644 --- a/crates/ra_hir_def/src/body.rs +++ b/crates/ra_hir_def/src/body.rs @@ -156,6 +156,9 @@ pub struct BodySourceMap { expansions: FxHashMap>, HirFileId>, } +#[derive(Debug)] +pub struct SyntheticSyntax; + impl Body { pub(crate) fn body_with_source_map_query( db: &impl DefDatabase, @@ -219,8 +222,8 @@ impl Index for Body { } impl BodySourceMap { - pub fn expr_syntax(&self, expr: ExprId) -> Option { - self.expr_map_back.get(expr).copied() + pub fn expr_syntax(&self, expr: ExprId) -> Result { + self.expr_map_back.get(expr).copied().ok_or(SyntheticSyntax) } pub fn node_expr(&self, node: InFile<&ast::Expr>) -> Option { @@ -238,8 +241,8 @@ impl BodySourceMap { self.expr_map.get(&src).cloned() } - pub fn pat_syntax(&self, pat: PatId) -> Option { - self.pat_map_back.get(pat).copied() + pub fn pat_syntax(&self, pat: PatId) -> Result { + self.pat_map_back.get(pat).copied().ok_or(SyntheticSyntax) } pub fn node_pat(&self, node: InFile<&ast::Pat>) -> Option { -- cgit v1.2.3 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(-) (limited to 'crates/ra_hir_def/src') 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 From 57da3df99a52ccedd7a684d2053db766e5681a61 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 6 Mar 2020 15:17:48 +0100 Subject: Explicitly remember desugard pats --- crates/ra_hir_def/src/body.rs | 4 ++-- crates/ra_hir_def/src/body/lower.rs | 12 ++++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) (limited to 'crates/ra_hir_def/src') diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs index e60adc146..57ba45b45 100644 --- a/crates/ra_hir_def/src/body.rs +++ b/crates/ra_hir_def/src/body.rs @@ -151,7 +151,7 @@ pub struct BodySourceMap { expr_map: FxHashMap, expr_map_back: ArenaMap>, pat_map: FxHashMap, - pat_map_back: ArenaMap, + pat_map_back: ArenaMap>, field_map: FxHashMap<(ExprId, usize), AstPtr>, expansions: FxHashMap>, HirFileId>, } @@ -242,7 +242,7 @@ impl BodySourceMap { } pub fn pat_syntax(&self, pat: PatId) -> Result { - self.pat_map_back.get(pat).copied().ok_or(SyntheticSyntax) + self.pat_map_back[pat] } pub fn node_pat(&self, node: InFile<&ast::Pat>) -> Option { diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index 209907700..ec1b0c2e7 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs @@ -14,7 +14,7 @@ use ra_syntax::{ }; use test_utils::tested_by; -use super::ExprSource; +use super::{ExprSource, PatSource}; use crate::{ adt::StructKind, body::{Body, BodySourceMap, Expander, PatPtr, SyntheticSyntax}, @@ -133,14 +133,18 @@ where } 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 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) -> 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 { -- cgit v1.2.3