From 302bf97bbf1855e3c7def9ab4f9f3d338be5e3b7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 17 Apr 2020 11:38:51 +0200 Subject: Don't expose impl details of SyntaxPtr --- crates/ra_hir_def/src/body.rs | 4 ++-- crates/ra_hir_def/src/body/lower.rs | 3 ++- crates/ra_hir_def/src/diagnostics.rs | 6 +++++- crates/ra_hir_def/src/nameres.rs | 3 ++- 4 files changed, 11 insertions(+), 5 deletions(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs index eafaf48c1..3b169440a 100644 --- a/crates/ra_hir_def/src/body.rs +++ b/crates/ra_hir_def/src/body.rs @@ -210,7 +210,7 @@ pub struct BodySourceMap { expr_map_back: ArenaMap>, pat_map: FxHashMap, pat_map_back: ArenaMap>, - field_map: FxHashMap<(ExprId, usize), AstPtr>, + field_map: FxHashMap<(ExprId, usize), InFile>>, expansions: FxHashMap>, HirFileId>, } @@ -303,7 +303,7 @@ impl BodySourceMap { self.pat_map.get(&src).cloned() } - pub fn field_syntax(&self, expr: ExprId, field: usize) -> AstPtr { + pub fn field_syntax(&self, expr: ExprId, field: usize) -> InFile> { self.field_map[&(expr, field)].clone() } } diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index 79abe55ce..10a1ba714 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs @@ -320,7 +320,8 @@ impl ExprCollector<'_> { let res = self.alloc_expr(record_lit, syntax_ptr); for (i, ptr) in field_ptrs.into_iter().enumerate() { - self.source_map.field_map.insert((res, i), ptr); + let src = self.expander.to_source(ptr); + self.source_map.field_map.insert((res, i), src); } res } diff --git a/crates/ra_hir_def/src/diagnostics.rs b/crates/ra_hir_def/src/diagnostics.rs index cfa0f2f76..dbaf4deef 100644 --- a/crates/ra_hir_def/src/diagnostics.rs +++ b/crates/ra_hir_def/src/diagnostics.rs @@ -4,7 +4,7 @@ use std::any::Any; use hir_expand::diagnostics::Diagnostic; use ra_db::RelativePathBuf; -use ra_syntax::{ast, AstPtr, SyntaxNodePtr}; +use ra_syntax::{ast, AstPtr, SyntaxNodePtr, TextRange}; use hir_expand::{HirFileId, InFile}; @@ -12,6 +12,7 @@ use hir_expand::{HirFileId, InFile}; pub struct UnresolvedModule { pub file: HirFileId, pub decl: AstPtr, + pub highlight_range: TextRange, pub candidate: RelativePathBuf, } @@ -19,6 +20,9 @@ impl Diagnostic for UnresolvedModule { fn message(&self) -> String { "unresolved module".to_string() } + fn highlight_range(&self) -> TextRange { + self.highlight_range + } fn source(&self) -> InFile { InFile { file_id: self.file, value: self.decl.clone().into() } } diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs index f279c2ad4..4a5a93dad 100644 --- a/crates/ra_hir_def/src/nameres.rs +++ b/crates/ra_hir_def/src/nameres.rs @@ -297,7 +297,7 @@ pub enum ModuleSource { mod diagnostics { use hir_expand::diagnostics::DiagnosticSink; use ra_db::RelativePathBuf; - use ra_syntax::{ast, AstPtr}; + use ra_syntax::{ast, AstNode, AstPtr}; use crate::{db::DefDatabase, diagnostics::UnresolvedModule, nameres::LocalModuleId, AstId}; @@ -326,6 +326,7 @@ mod diagnostics { sink.push(UnresolvedModule { file: declaration.file_id, decl: AstPtr::new(&decl), + highlight_range: decl.syntax().text_range(), candidate: candidate.clone(), }) } -- cgit v1.2.3 From a8196ffe8466aa60dec56e77c2da717793c0debe Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 17 Apr 2020 13:06:02 +0200 Subject: Correctly highlight ranges of diagnostics from macros closes #2799 --- crates/ra_hir_def/src/diagnostics.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/diagnostics.rs b/crates/ra_hir_def/src/diagnostics.rs index dbaf4deef..2ee28fbaa 100644 --- a/crates/ra_hir_def/src/diagnostics.rs +++ b/crates/ra_hir_def/src/diagnostics.rs @@ -20,11 +20,11 @@ impl Diagnostic for UnresolvedModule { fn message(&self) -> String { "unresolved module".to_string() } - fn highlight_range(&self) -> TextRange { - self.highlight_range + fn highlight_range(&self) -> InFile { + InFile::new(self.file, self.highlight_range) } fn source(&self) -> InFile { - InFile { file_id: self.file, value: self.decl.clone().into() } + InFile::new(self.file, self.decl.clone().into()) } fn as_any(&self) -> &(dyn Any + Send + 'static) { self -- cgit v1.2.3 From 146f6f5a45a4bfd98ab0eb54bb30610d784433c9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 17 Apr 2020 13:55:05 +0200 Subject: Simplify Diagnostic structure It's not entirely clear what subnode ranges should mean in the presence of macros, so let's leave them out for now. We are not using them heavily anyway. --- crates/ra_hir_def/src/diagnostics.rs | 6 +----- crates/ra_hir_def/src/nameres.rs | 3 +-- 2 files changed, 2 insertions(+), 7 deletions(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/diagnostics.rs b/crates/ra_hir_def/src/diagnostics.rs index 2ee28fbaa..510c5e064 100644 --- a/crates/ra_hir_def/src/diagnostics.rs +++ b/crates/ra_hir_def/src/diagnostics.rs @@ -4,7 +4,7 @@ use std::any::Any; use hir_expand::diagnostics::Diagnostic; use ra_db::RelativePathBuf; -use ra_syntax::{ast, AstPtr, SyntaxNodePtr, TextRange}; +use ra_syntax::{ast, AstPtr, SyntaxNodePtr}; use hir_expand::{HirFileId, InFile}; @@ -12,7 +12,6 @@ use hir_expand::{HirFileId, InFile}; pub struct UnresolvedModule { pub file: HirFileId, pub decl: AstPtr, - pub highlight_range: TextRange, pub candidate: RelativePathBuf, } @@ -20,9 +19,6 @@ impl Diagnostic for UnresolvedModule { fn message(&self) -> String { "unresolved module".to_string() } - fn highlight_range(&self) -> InFile { - InFile::new(self.file, self.highlight_range) - } fn source(&self) -> InFile { InFile::new(self.file, self.decl.clone().into()) } diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs index 4a5a93dad..f279c2ad4 100644 --- a/crates/ra_hir_def/src/nameres.rs +++ b/crates/ra_hir_def/src/nameres.rs @@ -297,7 +297,7 @@ pub enum ModuleSource { mod diagnostics { use hir_expand::diagnostics::DiagnosticSink; use ra_db::RelativePathBuf; - use ra_syntax::{ast, AstNode, AstPtr}; + use ra_syntax::{ast, AstPtr}; use crate::{db::DefDatabase, diagnostics::UnresolvedModule, nameres::LocalModuleId, AstId}; @@ -326,7 +326,6 @@ mod diagnostics { sink.push(UnresolvedModule { file: declaration.file_id, decl: AstPtr::new(&decl), - highlight_range: decl.syntax().text_range(), candidate: candidate.clone(), }) } -- cgit v1.2.3 From 408f914bf4d6719ae68582ae43e2de9d3cb362b0 Mon Sep 17 00:00:00 2001 From: Josh Mcguigan Date: Fri, 17 Apr 2020 05:36:44 -0700 Subject: fix panic on ellipsis in pattern --- crates/ra_hir_def/src/body/lower.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index 79abe55ce..0679ffa1e 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs @@ -650,6 +650,7 @@ impl ExprCollector<'_> { ast::Pat::SlicePat(p) => { let SlicePatComponents { prefix, slice, suffix } = p.components(); + // FIXME properly handle `DotDotPat` Pat::Slice { prefix: prefix.into_iter().map(|p| self.collect_pat(p)).collect(), slice: slice.map(|p| self.collect_pat(p)), @@ -666,9 +667,15 @@ impl ExprCollector<'_> { Pat::Missing } } - ast::Pat::DotDotPat(_) => unreachable!( - "`DotDotPat` requires special handling and should not be mapped to a Pat." - ), + ast::Pat::DotDotPat(_) => { + // `DotDotPat` requires special handling and should not be mapped + // to a Pat. Here we are using `Pat::Missing` as a fallback for + // when `DotDotPat` is mapped to `Pat`, which can easily happen + // when the source code being analyzed has a malformed pattern + // which includes `..` in a place where it isn't valid. + + Pat::Missing + } // FIXME: implement ast::Pat::BoxPat(_) | ast::Pat::RangePat(_) | ast::Pat::MacroPat(_) => Pat::Missing, }; -- cgit v1.2.3 From b49ecafd40f3dd6c9c55d14c392b8c10ce682b84 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 18 Apr 2020 11:38:58 +0200 Subject: find_path: Builtins are always in scope Fixes #3977. --- crates/ra_hir_def/src/builtin_type.rs | 54 +++++++++++++++++++---------------- crates/ra_hir_def/src/find_path.rs | 21 +++++++++++++- 2 files changed, 50 insertions(+), 25 deletions(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/builtin_type.rs b/crates/ra_hir_def/src/builtin_type.rs index d14901a9b..0f872b5c0 100644 --- a/crates/ra_hir_def/src/builtin_type.rs +++ b/crates/ra_hir_def/src/builtin_type.rs @@ -5,7 +5,7 @@ use std::fmt; -use hir_expand::name::{name, Name}; +use hir_expand::name::{name, AsName, Name}; #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] pub enum Signedness { @@ -75,33 +75,39 @@ impl BuiltinType { ]; } -impl fmt::Display for BuiltinType { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let type_name = match self { - BuiltinType::Char => "char", - BuiltinType::Bool => "bool", - BuiltinType::Str => "str", +impl AsName for BuiltinType { + fn as_name(&self) -> Name { + match self { + BuiltinType::Char => name![char], + BuiltinType::Bool => name![bool], + BuiltinType::Str => name![str], BuiltinType::Int(BuiltinInt { signedness, bitness }) => match (signedness, bitness) { - (Signedness::Signed, IntBitness::Xsize) => "isize", - (Signedness::Signed, IntBitness::X8) => "i8", - (Signedness::Signed, IntBitness::X16) => "i16", - (Signedness::Signed, IntBitness::X32) => "i32", - (Signedness::Signed, IntBitness::X64) => "i64", - (Signedness::Signed, IntBitness::X128) => "i128", - - (Signedness::Unsigned, IntBitness::Xsize) => "usize", - (Signedness::Unsigned, IntBitness::X8) => "u8", - (Signedness::Unsigned, IntBitness::X16) => "u16", - (Signedness::Unsigned, IntBitness::X32) => "u32", - (Signedness::Unsigned, IntBitness::X64) => "u64", - (Signedness::Unsigned, IntBitness::X128) => "u128", + (Signedness::Signed, IntBitness::Xsize) => name![isize], + (Signedness::Signed, IntBitness::X8) => name![i8], + (Signedness::Signed, IntBitness::X16) => name![i16], + (Signedness::Signed, IntBitness::X32) => name![i32], + (Signedness::Signed, IntBitness::X64) => name![i64], + (Signedness::Signed, IntBitness::X128) => name![i128], + + (Signedness::Unsigned, IntBitness::Xsize) => name![usize], + (Signedness::Unsigned, IntBitness::X8) => name![u8], + (Signedness::Unsigned, IntBitness::X16) => name![u16], + (Signedness::Unsigned, IntBitness::X32) => name![u32], + (Signedness::Unsigned, IntBitness::X64) => name![u64], + (Signedness::Unsigned, IntBitness::X128) => name![u128], }, BuiltinType::Float(BuiltinFloat { bitness }) => match bitness { - FloatBitness::X32 => "f32", - FloatBitness::X64 => "f64", + FloatBitness::X32 => name![f32], + FloatBitness::X64 => name![f64], }, - }; - f.write_str(type_name) + } + } +} + +impl fmt::Display for BuiltinType { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let type_name = self.as_name(); + type_name.fmt(f) } } diff --git a/crates/ra_hir_def/src/find_path.rs b/crates/ra_hir_def/src/find_path.rs index d58ac6ba5..81eff5bfe 100644 --- a/crates/ra_hir_def/src/find_path.rs +++ b/crates/ra_hir_def/src/find_path.rs @@ -7,7 +7,7 @@ use crate::{ visibility::Visibility, CrateId, ModuleDefId, ModuleId, }; -use hir_expand::name::{known, Name}; +use hir_expand::name::{known, AsName, Name}; use test_utils::tested_by; const MAX_PATH_LEN: usize = 15; @@ -113,6 +113,11 @@ fn find_path_inner( } } + // - if the item is a builtin, it's in scope + if let ItemInNs::Types(ModuleDefId::BuiltinType(builtin)) = item { + return Some(ModPath::from_segments(PathKind::Plain, vec![builtin.as_name()])); + } + // Recursive case: // - if the item is an enum variant, refer to it via the enum if let Some(ModuleDefId::EnumVariantId(variant)) = item.as_module_def_id() { @@ -523,4 +528,18 @@ mod tests { "#; check_found_path(code, "megaalloc::Arc"); } + + #[test] + fn builtins_are_in_scope() { + let code = r#" + //- /main.rs + <|> + + pub mod primitive { + pub use u8; + } + "#; + check_found_path(code, "u8"); + check_found_path(code, "u16"); + } } -- cgit v1.2.3