mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 13:51:31 +00:00
parent
0948932145
commit
b49ecafd40
2 changed files with 49 additions and 24 deletions
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
use hir_expand::name::{name, Name};
|
use hir_expand::name::{name, AsName, Name};
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
|
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
|
||||||
pub enum Signedness {
|
pub enum Signedness {
|
||||||
|
@ -75,33 +75,39 @@ impl BuiltinType {
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for BuiltinType {
|
impl AsName for BuiltinType {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn as_name(&self) -> Name {
|
||||||
let type_name = match self {
|
match self {
|
||||||
BuiltinType::Char => "char",
|
BuiltinType::Char => name![char],
|
||||||
BuiltinType::Bool => "bool",
|
BuiltinType::Bool => name![bool],
|
||||||
BuiltinType::Str => "str",
|
BuiltinType::Str => name![str],
|
||||||
BuiltinType::Int(BuiltinInt { signedness, bitness }) => match (signedness, bitness) {
|
BuiltinType::Int(BuiltinInt { signedness, bitness }) => match (signedness, bitness) {
|
||||||
(Signedness::Signed, IntBitness::Xsize) => "isize",
|
(Signedness::Signed, IntBitness::Xsize) => name![isize],
|
||||||
(Signedness::Signed, IntBitness::X8) => "i8",
|
(Signedness::Signed, IntBitness::X8) => name![i8],
|
||||||
(Signedness::Signed, IntBitness::X16) => "i16",
|
(Signedness::Signed, IntBitness::X16) => name![i16],
|
||||||
(Signedness::Signed, IntBitness::X32) => "i32",
|
(Signedness::Signed, IntBitness::X32) => name![i32],
|
||||||
(Signedness::Signed, IntBitness::X64) => "i64",
|
(Signedness::Signed, IntBitness::X64) => name![i64],
|
||||||
(Signedness::Signed, IntBitness::X128) => "i128",
|
(Signedness::Signed, IntBitness::X128) => name![i128],
|
||||||
|
|
||||||
(Signedness::Unsigned, IntBitness::Xsize) => "usize",
|
(Signedness::Unsigned, IntBitness::Xsize) => name![usize],
|
||||||
(Signedness::Unsigned, IntBitness::X8) => "u8",
|
(Signedness::Unsigned, IntBitness::X8) => name![u8],
|
||||||
(Signedness::Unsigned, IntBitness::X16) => "u16",
|
(Signedness::Unsigned, IntBitness::X16) => name![u16],
|
||||||
(Signedness::Unsigned, IntBitness::X32) => "u32",
|
(Signedness::Unsigned, IntBitness::X32) => name![u32],
|
||||||
(Signedness::Unsigned, IntBitness::X64) => "u64",
|
(Signedness::Unsigned, IntBitness::X64) => name![u64],
|
||||||
(Signedness::Unsigned, IntBitness::X128) => "u128",
|
(Signedness::Unsigned, IntBitness::X128) => name![u128],
|
||||||
},
|
},
|
||||||
BuiltinType::Float(BuiltinFloat { bitness }) => match bitness {
|
BuiltinType::Float(BuiltinFloat { bitness }) => match bitness {
|
||||||
FloatBitness::X32 => "f32",
|
FloatBitness::X32 => name![f32],
|
||||||
FloatBitness::X64 => "f64",
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@ use crate::{
|
||||||
visibility::Visibility,
|
visibility::Visibility,
|
||||||
CrateId, ModuleDefId, ModuleId,
|
CrateId, ModuleDefId, ModuleId,
|
||||||
};
|
};
|
||||||
use hir_expand::name::{known, Name};
|
use hir_expand::name::{known, AsName, Name};
|
||||||
use test_utils::tested_by;
|
use test_utils::tested_by;
|
||||||
|
|
||||||
const MAX_PATH_LEN: usize = 15;
|
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:
|
// Recursive case:
|
||||||
// - if the item is an enum variant, refer to it via the enum
|
// - if the item is an enum variant, refer to it via the enum
|
||||||
if let Some(ModuleDefId::EnumVariantId(variant)) = item.as_module_def_id() {
|
if let Some(ModuleDefId::EnumVariantId(variant)) = item.as_module_def_id() {
|
||||||
|
@ -523,4 +528,18 @@ mod tests {
|
||||||
"#;
|
"#;
|
||||||
check_found_path(code, "megaalloc::Arc");
|
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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue