aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def
diff options
context:
space:
mode:
authorBenjamin Coenen <[email protected]>2020-04-18 16:14:24 +0100
committerBenjamin Coenen <[email protected]>2020-04-18 16:14:24 +0100
commitaf3c19e85f55db9277ce9ad5b784df2ccfe3c9e4 (patch)
tree7a777f22f5fa79704b8d2c9aa8d67255d12123be /crates/ra_hir_def
parent379787858bbfb2691e134942d94fcbc70c5d1d7f (diff)
parent84e3304a9bf0d68e30d58b1e37a6db2e9ec97525 (diff)
Merge branch 'master' of github.com:rust-analyzer/rust-analyzer
Diffstat (limited to 'crates/ra_hir_def')
-rw-r--r--crates/ra_hir_def/src/body.rs4
-rw-r--r--crates/ra_hir_def/src/body/lower.rs16
-rw-r--r--crates/ra_hir_def/src/builtin_type.rs54
-rw-r--r--crates/ra_hir_def/src/diagnostics.rs2
-rw-r--r--crates/ra_hir_def/src/find_path.rs21
5 files changed, 65 insertions, 32 deletions
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 {
210 expr_map_back: ArenaMap<ExprId, Result<ExprSource, SyntheticSyntax>>, 210 expr_map_back: ArenaMap<ExprId, Result<ExprSource, SyntheticSyntax>>,
211 pat_map: FxHashMap<PatSource, PatId>, 211 pat_map: FxHashMap<PatSource, PatId>,
212 pat_map_back: ArenaMap<PatId, Result<PatSource, SyntheticSyntax>>, 212 pat_map_back: ArenaMap<PatId, Result<PatSource, SyntheticSyntax>>,
213 field_map: FxHashMap<(ExprId, usize), AstPtr<ast::RecordField>>, 213 field_map: FxHashMap<(ExprId, usize), InFile<AstPtr<ast::RecordField>>>,
214 expansions: FxHashMap<InFile<AstPtr<ast::MacroCall>>, HirFileId>, 214 expansions: FxHashMap<InFile<AstPtr<ast::MacroCall>>, HirFileId>,
215} 215}
216 216
@@ -303,7 +303,7 @@ impl BodySourceMap {
303 self.pat_map.get(&src).cloned() 303 self.pat_map.get(&src).cloned()
304 } 304 }
305 305
306 pub fn field_syntax(&self, expr: ExprId, field: usize) -> AstPtr<ast::RecordField> { 306 pub fn field_syntax(&self, expr: ExprId, field: usize) -> InFile<AstPtr<ast::RecordField>> {
307 self.field_map[&(expr, field)].clone() 307 self.field_map[&(expr, field)].clone()
308 } 308 }
309} 309}
diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs
index 79abe55ce..82a52804d 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<'_> {
320 320
321 let res = self.alloc_expr(record_lit, syntax_ptr); 321 let res = self.alloc_expr(record_lit, syntax_ptr);
322 for (i, ptr) in field_ptrs.into_iter().enumerate() { 322 for (i, ptr) in field_ptrs.into_iter().enumerate() {
323 self.source_map.field_map.insert((res, i), ptr); 323 let src = self.expander.to_source(ptr);
324 self.source_map.field_map.insert((res, i), src);
324 } 325 }
325 res 326 res
326 } 327 }
@@ -650,6 +651,7 @@ impl ExprCollector<'_> {
650 ast::Pat::SlicePat(p) => { 651 ast::Pat::SlicePat(p) => {
651 let SlicePatComponents { prefix, slice, suffix } = p.components(); 652 let SlicePatComponents { prefix, slice, suffix } = p.components();
652 653
654 // FIXME properly handle `DotDotPat`
653 Pat::Slice { 655 Pat::Slice {
654 prefix: prefix.into_iter().map(|p| self.collect_pat(p)).collect(), 656 prefix: prefix.into_iter().map(|p| self.collect_pat(p)).collect(),
655 slice: slice.map(|p| self.collect_pat(p)), 657 slice: slice.map(|p| self.collect_pat(p)),
@@ -666,9 +668,15 @@ impl ExprCollector<'_> {
666 Pat::Missing 668 Pat::Missing
667 } 669 }
668 } 670 }
669 ast::Pat::DotDotPat(_) => unreachable!( 671 ast::Pat::DotDotPat(_) => {
670 "`DotDotPat` requires special handling and should not be mapped to a Pat." 672 // `DotDotPat` requires special handling and should not be mapped
671 ), 673 // to a Pat. Here we are using `Pat::Missing` as a fallback for
674 // when `DotDotPat` is mapped to `Pat`, which can easily happen
675 // when the source code being analyzed has a malformed pattern
676 // which includes `..` in a place where it isn't valid.
677
678 Pat::Missing
679 }
672 // FIXME: implement 680 // FIXME: implement
673 ast::Pat::BoxPat(_) | ast::Pat::RangePat(_) | ast::Pat::MacroPat(_) => Pat::Missing, 681 ast::Pat::BoxPat(_) | ast::Pat::RangePat(_) | ast::Pat::MacroPat(_) => Pat::Missing,
674 }; 682 };
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 @@
5 5
6use std::fmt; 6use std::fmt;
7 7
8use hir_expand::name::{name, Name}; 8use hir_expand::name::{name, AsName, Name};
9 9
10#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] 10#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
11pub enum Signedness { 11pub enum Signedness {
@@ -75,33 +75,39 @@ impl BuiltinType {
75 ]; 75 ];
76} 76}
77 77
78impl fmt::Display for BuiltinType { 78impl AsName for BuiltinType {
79 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 79 fn as_name(&self) -> Name {
80 let type_name = match self { 80 match self {
81 BuiltinType::Char => "char", 81 BuiltinType::Char => name![char],
82 BuiltinType::Bool => "bool", 82 BuiltinType::Bool => name![bool],
83 BuiltinType::Str => "str", 83 BuiltinType::Str => name![str],
84 BuiltinType::Int(BuiltinInt { signedness, bitness }) => match (signedness, bitness) { 84 BuiltinType::Int(BuiltinInt { signedness, bitness }) => match (signedness, bitness) {
85 (Signedness::Signed, IntBitness::Xsize) => "isize", 85 (Signedness::Signed, IntBitness::Xsize) => name![isize],
86 (Signedness::Signed, IntBitness::X8) => "i8", 86 (Signedness::Signed, IntBitness::X8) => name![i8],
87 (Signedness::Signed, IntBitness::X16) => "i16", 87 (Signedness::Signed, IntBitness::X16) => name![i16],
88 (Signedness::Signed, IntBitness::X32) => "i32", 88 (Signedness::Signed, IntBitness::X32) => name![i32],
89 (Signedness::Signed, IntBitness::X64) => "i64", 89 (Signedness::Signed, IntBitness::X64) => name![i64],
90 (Signedness::Signed, IntBitness::X128) => "i128", 90 (Signedness::Signed, IntBitness::X128) => name![i128],
91 91
92 (Signedness::Unsigned, IntBitness::Xsize) => "usize", 92 (Signedness::Unsigned, IntBitness::Xsize) => name![usize],
93 (Signedness::Unsigned, IntBitness::X8) => "u8", 93 (Signedness::Unsigned, IntBitness::X8) => name![u8],
94 (Signedness::Unsigned, IntBitness::X16) => "u16", 94 (Signedness::Unsigned, IntBitness::X16) => name![u16],
95 (Signedness::Unsigned, IntBitness::X32) => "u32", 95 (Signedness::Unsigned, IntBitness::X32) => name![u32],
96 (Signedness::Unsigned, IntBitness::X64) => "u64", 96 (Signedness::Unsigned, IntBitness::X64) => name![u64],
97 (Signedness::Unsigned, IntBitness::X128) => "u128", 97 (Signedness::Unsigned, IntBitness::X128) => name![u128],
98 }, 98 },
99 BuiltinType::Float(BuiltinFloat { bitness }) => match bitness { 99 BuiltinType::Float(BuiltinFloat { bitness }) => match bitness {
100 FloatBitness::X32 => "f32", 100 FloatBitness::X32 => name![f32],
101 FloatBitness::X64 => "f64", 101 FloatBitness::X64 => name![f64],
102 }, 102 },
103 }; 103 }
104 f.write_str(type_name) 104 }
105}
106
107impl fmt::Display for BuiltinType {
108 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
109 let type_name = self.as_name();
110 type_name.fmt(f)
105 } 111 }
106} 112}
107 113
diff --git a/crates/ra_hir_def/src/diagnostics.rs b/crates/ra_hir_def/src/diagnostics.rs
index cfa0f2f76..510c5e064 100644
--- a/crates/ra_hir_def/src/diagnostics.rs
+++ b/crates/ra_hir_def/src/diagnostics.rs
@@ -20,7 +20,7 @@ impl Diagnostic for UnresolvedModule {
20 "unresolved module".to_string() 20 "unresolved module".to_string()
21 } 21 }
22 fn source(&self) -> InFile<SyntaxNodePtr> { 22 fn source(&self) -> InFile<SyntaxNodePtr> {
23 InFile { file_id: self.file, value: self.decl.clone().into() } 23 InFile::new(self.file, self.decl.clone().into())
24 } 24 }
25 fn as_any(&self) -> &(dyn Any + Send + 'static) { 25 fn as_any(&self) -> &(dyn Any + Send + 'static) {
26 self 26 self
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::{
7 visibility::Visibility, 7 visibility::Visibility,
8 CrateId, ModuleDefId, ModuleId, 8 CrateId, ModuleDefId, ModuleId,
9}; 9};
10use hir_expand::name::{known, Name}; 10use hir_expand::name::{known, AsName, Name};
11use test_utils::tested_by; 11use test_utils::tested_by;
12 12
13const MAX_PATH_LEN: usize = 15; 13const MAX_PATH_LEN: usize = 15;
@@ -113,6 +113,11 @@ fn find_path_inner(
113 } 113 }
114 } 114 }
115 115
116 // - if the item is a builtin, it's in scope
117 if let ItemInNs::Types(ModuleDefId::BuiltinType(builtin)) = item {
118 return Some(ModPath::from_segments(PathKind::Plain, vec![builtin.as_name()]));
119 }
120
116 // Recursive case: 121 // Recursive case:
117 // - if the item is an enum variant, refer to it via the enum 122 // - if the item is an enum variant, refer to it via the enum
118 if let Some(ModuleDefId::EnumVariantId(variant)) = item.as_module_def_id() { 123 if let Some(ModuleDefId::EnumVariantId(variant)) = item.as_module_def_id() {
@@ -523,4 +528,18 @@ mod tests {
523 "#; 528 "#;
524 check_found_path(code, "megaalloc::Arc"); 529 check_found_path(code, "megaalloc::Arc");
525 } 530 }
531
532 #[test]
533 fn builtins_are_in_scope() {
534 let code = r#"
535 //- /main.rs
536 <|>
537
538 pub mod primitive {
539 pub use u8;
540 }
541 "#;
542 check_found_path(code, "u8");
543 check_found_path(code, "u16");
544 }
526} 545}