From 97158f5c8a6dadf3bcf28601f739ab6b7c4464aa Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 May 2019 15:03:58 +0300 Subject: add built-in types to scopes --- Cargo.lock | 1 + crates/ra_hir/Cargo.toml | 1 + crates/ra_hir/src/code_model.rs | 2 +- crates/ra_hir/src/name.rs | 30 +++++++++++++++++++++++++- crates/ra_hir/src/nameres.rs | 19 ++++++++++++---- crates/ra_hir/src/nameres/tests/incremental.rs | 4 ++-- crates/ra_hir/src/ty/lower.rs | 16 -------------- 7 files changed, 49 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b56586ac1..e38fa0ff0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", diff --git a/crates/ra_hir/Cargo.toml b/crates/ra_hir/Cargo.toml index a86dfa6b2..78808e72f 100644 --- a/crates/ra_hir/Cargo.toml +++ b/crates/ra_hir/Cargo.toml @@ -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" } diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 3053f5488..e3c765674 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -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 })), ]; } diff --git a/crates/ra_hir/src/name.rs b/crates/ra_hir/src/name.rs index e3a82cf03..e9003e00b 100644 --- a/crates/ra_hir/src/name.rs +++ b/crates/ra_hir/src/name.rs @@ -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()) + } +} diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs index 51a7b8b95..aa26345b2 100644 --- a/crates/ra_hir/src/nameres.rs +++ b/crates/ra_hir/src/nameres.rs @@ -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, } +static BUILTIN_SCOPE: Lazy> = 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 + '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 + '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 { - 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, diff --git a/crates/ra_hir/src/nameres/tests/incremental.rs b/crates/ra_hir/src/nameres/tests/incremental.rs index 001f76ac3..bc721f6e0 100644 --- a/crates/ra_hir/src/nameres/tests/incremental.rs +++ b/crates/ra_hir/src/nameres/tests/incremental.rs @@ -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) } diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs index c4cef2d7c..d2ba01826 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs @@ -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(); -- cgit v1.2.3