Skip to content

Commit 84e3304

Browse files
Merge #4027
4027: find_path: Builtins are always in scope r=matklad a=flodiebold Fixes #3977. Co-authored-by: Florian Diebold <[email protected]>
2 parents 0948932 + b49ecaf commit 84e3304

File tree

2 files changed

+50
-25
lines changed

2 files changed

+50
-25
lines changed

crates/ra_hir_def/src/builtin_type.rs

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
use std::fmt;
77

8-
use hir_expand::name::{name, Name};
8+
use hir_expand::name::{name, AsName, Name};
99

1010
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
1111
pub enum Signedness {
@@ -75,33 +75,39 @@ impl BuiltinType {
7575
];
7676
}
7777

78-
impl fmt::Display for BuiltinType {
79-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
80-
let type_name = match self {
81-
BuiltinType::Char => "char",
82-
BuiltinType::Bool => "bool",
83-
BuiltinType::Str => "str",
78+
impl AsName for BuiltinType {
79+
fn as_name(&self) -> Name {
80+
match self {
81+
BuiltinType::Char => name![char],
82+
BuiltinType::Bool => name![bool],
83+
BuiltinType::Str => name![str],
8484
BuiltinType::Int(BuiltinInt { signedness, bitness }) => match (signedness, bitness) {
85-
(Signedness::Signed, IntBitness::Xsize) => "isize",
86-
(Signedness::Signed, IntBitness::X8) => "i8",
87-
(Signedness::Signed, IntBitness::X16) => "i16",
88-
(Signedness::Signed, IntBitness::X32) => "i32",
89-
(Signedness::Signed, IntBitness::X64) => "i64",
90-
(Signedness::Signed, IntBitness::X128) => "i128",
91-
92-
(Signedness::Unsigned, IntBitness::Xsize) => "usize",
93-
(Signedness::Unsigned, IntBitness::X8) => "u8",
94-
(Signedness::Unsigned, IntBitness::X16) => "u16",
95-
(Signedness::Unsigned, IntBitness::X32) => "u32",
96-
(Signedness::Unsigned, IntBitness::X64) => "u64",
97-
(Signedness::Unsigned, IntBitness::X128) => "u128",
85+
(Signedness::Signed, IntBitness::Xsize) => name![isize],
86+
(Signedness::Signed, IntBitness::X8) => name![i8],
87+
(Signedness::Signed, IntBitness::X16) => name![i16],
88+
(Signedness::Signed, IntBitness::X32) => name![i32],
89+
(Signedness::Signed, IntBitness::X64) => name![i64],
90+
(Signedness::Signed, IntBitness::X128) => name![i128],
91+
92+
(Signedness::Unsigned, IntBitness::Xsize) => name![usize],
93+
(Signedness::Unsigned, IntBitness::X8) => name![u8],
94+
(Signedness::Unsigned, IntBitness::X16) => name![u16],
95+
(Signedness::Unsigned, IntBitness::X32) => name![u32],
96+
(Signedness::Unsigned, IntBitness::X64) => name![u64],
97+
(Signedness::Unsigned, IntBitness::X128) => name![u128],
9898
},
9999
BuiltinType::Float(BuiltinFloat { bitness }) => match bitness {
100-
FloatBitness::X32 => "f32",
101-
FloatBitness::X64 => "f64",
100+
FloatBitness::X32 => name![f32],
101+
FloatBitness::X64 => name![f64],
102102
},
103-
};
104-
f.write_str(type_name)
103+
}
104+
}
105+
}
106+
107+
impl 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)
105111
}
106112
}
107113

crates/ra_hir_def/src/find_path.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
visibility::Visibility,
88
CrateId, ModuleDefId, ModuleId,
99
};
10-
use hir_expand::name::{known, Name};
10+
use hir_expand::name::{known, AsName, Name};
1111
use test_utils::tested_by;
1212

1313
const MAX_PATH_LEN: usize = 15;
@@ -113,6 +113,11 @@ fn find_path_inner(
113113
}
114114
}
115115

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+
116121
// Recursive case:
117122
// - if the item is an enum variant, refer to it via the enum
118123
if let Some(ModuleDefId::EnumVariantId(variant)) = item.as_module_def_id() {
@@ -523,4 +528,18 @@ mod tests {
523528
"#;
524529
check_found_path(code, "megaalloc::Arc");
525530
}
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+
}
526545
}

0 commit comments

Comments
 (0)