aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_def')
-rw-r--r--crates/ra_hir_def/src/builtin_type.rs54
-rw-r--r--crates/ra_hir_def/src/find_path.rs21
2 files changed, 50 insertions, 25 deletions
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/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}