mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-26 13:03:31 +00:00
add built-in types to scopes
This commit is contained in:
parent
4e5b02966b
commit
97158f5c8a
7 changed files with 49 additions and 24 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -1084,6 +1084,7 @@ dependencies = [
|
|||
"insta 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"join_to_string 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"once_cell 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"parking_lot 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"ra_arena 0.1.0",
|
||||
"ra_db 0.1.0",
|
||||
|
|
|
@ -13,6 +13,7 @@ parking_lot = "0.8.0"
|
|||
ena = "0.11"
|
||||
join_to_string = "0.1.3"
|
||||
either = "1.5.2"
|
||||
once_cell = "0.2"
|
||||
|
||||
ra_syntax = { path = "../ra_syntax" }
|
||||
ra_arena = { path = "../ra_arena" }
|
||||
|
|
|
@ -106,7 +106,7 @@ impl BuiltinType {
|
|||
(KnownName::U128, BuiltinType::Int(IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X128 })),
|
||||
|
||||
(KnownName::F32, BuiltinType::Float(FloatTy { bitness: FloatBitness::X32 })),
|
||||
(KnownName::F64, BuiltinType::Float(FloatTy { bitness: FloatBitness::X32 })),
|
||||
(KnownName::F64, BuiltinType::Float(FloatTy { bitness: FloatBitness::X64 })),
|
||||
];
|
||||
}
|
||||
|
||||
|
|
|
@ -123,7 +123,7 @@ impl AsName for ra_db::Dependency {
|
|||
// const ISIZE: Name = Name::new("isize")
|
||||
// ```
|
||||
// but const-fn is not that powerful yet.
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub(crate) enum KnownName {
|
||||
Isize,
|
||||
I8,
|
||||
|
@ -151,3 +151,31 @@ pub(crate) enum KnownName {
|
|||
|
||||
MacroRules,
|
||||
}
|
||||
|
||||
impl AsName for KnownName {
|
||||
fn as_name(&self) -> Name {
|
||||
let s = match self {
|
||||
KnownName::Isize => "isize",
|
||||
KnownName::I8 => "i8",
|
||||
KnownName::I16 => "i16",
|
||||
KnownName::I32 => "i32",
|
||||
KnownName::I64 => "i64",
|
||||
KnownName::I128 => "i128",
|
||||
KnownName::Usize => "usize",
|
||||
KnownName::U8 => "u8",
|
||||
KnownName::U16 => "u16",
|
||||
KnownName::U32 => "u32",
|
||||
KnownName::U64 => "u64",
|
||||
KnownName::U128 => "u128",
|
||||
KnownName::F32 => "f32",
|
||||
KnownName::F64 => "f64",
|
||||
KnownName::Bool => "bool",
|
||||
KnownName::Char => "char",
|
||||
KnownName::Str => "str",
|
||||
KnownName::SelfType => "Self",
|
||||
KnownName::SelfParam => "self",
|
||||
KnownName::MacroRules => "macro_rules",
|
||||
};
|
||||
Name::new(s.into())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -62,9 +62,10 @@ use ra_db::{FileId, Edition};
|
|||
use test_utils::tested_by;
|
||||
use ra_syntax::ast;
|
||||
use ra_prof::profile;
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
use crate::{
|
||||
ModuleDef, Name, Crate, Module, MacroDef, KnownName, BuiltinType,
|
||||
ModuleDef, Name, Crate, Module, MacroDef, AsName, BuiltinType,
|
||||
DefDatabase, Path, PathKind, HirFileId, Trait,
|
||||
ids::MacroDefId,
|
||||
diagnostics::DiagnosticSink,
|
||||
|
@ -140,12 +141,22 @@ pub struct ModuleScope {
|
|||
macros: FxHashMap<Name, MacroDef>,
|
||||
}
|
||||
|
||||
static BUILTIN_SCOPE: Lazy<FxHashMap<Name, Resolution>> = Lazy::new(|| {
|
||||
BuiltinType::ALL
|
||||
.iter()
|
||||
.map(|&(known_name, ty)| {
|
||||
(known_name.as_name(), Resolution { def: PerNs::types(ty.into()), import: None })
|
||||
})
|
||||
.collect()
|
||||
});
|
||||
|
||||
impl ModuleScope {
|
||||
pub fn entries<'a>(&'a self) -> impl Iterator<Item = (&'a Name, &'a Resolution)> + 'a {
|
||||
self.items.iter()
|
||||
//FIXME: shadowing
|
||||
self.items.iter().chain(BUILTIN_SCOPE.iter())
|
||||
}
|
||||
pub fn get(&self, name: &Name) -> Option<&Resolution> {
|
||||
self.items.get(name)
|
||||
self.items.get(name).or_else(|| BUILTIN_SCOPE.get(name))
|
||||
}
|
||||
pub fn traits<'a>(&'a self) -> impl Iterator<Item = Trait> + 'a {
|
||||
self.items.values().filter_map(|r| match r.def.take_types() {
|
||||
|
@ -154,7 +165,7 @@ impl ModuleScope {
|
|||
})
|
||||
}
|
||||
fn get_item_or_macro(&self, name: &Name) -> Option<ItemOrMacro> {
|
||||
match (self.items.get(name), self.macros.get(name)) {
|
||||
match (self.get(name), self.macros.get(name)) {
|
||||
(Some(item), _) if !item.def.is_none() => Some(Either::Left(item.def)),
|
||||
(_, Some(macro_)) => Some(Either::Right(*macro_)),
|
||||
_ => None,
|
||||
|
|
|
@ -116,7 +116,7 @@ fn typing_inside_a_macro_should_not_invalidate_def_map() {
|
|||
let events = db.log_executed(|| {
|
||||
let module = crate::source_binder::module_from_file_id(&db, pos.file_id).unwrap();
|
||||
let decls = module.declarations(&db);
|
||||
assert_eq!(decls.len(), 1);
|
||||
assert_eq!(decls.len(), 18);
|
||||
});
|
||||
assert!(format!("{:?}", events).contains("crate_def_map"), "{:#?}", events)
|
||||
}
|
||||
|
@ -126,7 +126,7 @@ fn typing_inside_a_macro_should_not_invalidate_def_map() {
|
|||
let events = db.log_executed(|| {
|
||||
let module = crate::source_binder::module_from_file_id(&db, pos.file_id).unwrap();
|
||||
let decls = module.declarations(&db);
|
||||
assert_eq!(decls.len(), 1);
|
||||
assert_eq!(decls.len(), 18);
|
||||
});
|
||||
assert!(!format!("{:?}", events).contains("crate_def_map"), "{:#?}", events)
|
||||
}
|
||||
|
|
|
@ -65,22 +65,6 @@ impl Ty {
|
|||
}
|
||||
|
||||
pub(crate) fn from_hir_path(db: &impl HirDatabase, resolver: &Resolver, path: &Path) -> Self {
|
||||
if let Some(name) = path.as_ident() {
|
||||
// TODO: remove this
|
||||
if let Some(int_ty) = primitive::IntTy::from_type_name(name) {
|
||||
return Ty::simple(TypeCtor::Int(primitive::UncertainIntTy::Known(int_ty)));
|
||||
} else if let Some(float_ty) = primitive::FloatTy::from_type_name(name) {
|
||||
return Ty::simple(TypeCtor::Float(primitive::UncertainFloatTy::Known(float_ty)));
|
||||
} else if let Some(known) = name.as_known_name() {
|
||||
match known {
|
||||
KnownName::Bool => return Ty::simple(TypeCtor::Bool),
|
||||
KnownName::Char => return Ty::simple(TypeCtor::Char),
|
||||
KnownName::Str => return Ty::simple(TypeCtor::Str),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Resolve the path (in type namespace)
|
||||
let resolution = resolver.resolve_path(db, path).take_types();
|
||||
|
||||
|
|
Loading…
Reference in a new issue