From 3ac605e6876056fa56098231cc2f96553faab8f0 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Thu, 20 Dec 2018 21:56:28 +0100 Subject: Add beginnings of type infrastructure --- crates/ra_hir/src/ty/primitive.rs | 98 +++++++++++++++++++++++++++++++++++++++ crates/ra_hir/src/ty/tests.rs | 45 ++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 crates/ra_hir/src/ty/primitive.rs create mode 100644 crates/ra_hir/src/ty/tests.rs (limited to 'crates/ra_hir/src/ty') diff --git a/crates/ra_hir/src/ty/primitive.rs b/crates/ra_hir/src/ty/primitive.rs new file mode 100644 index 000000000..4a5ce5a97 --- /dev/null +++ b/crates/ra_hir/src/ty/primitive.rs @@ -0,0 +1,98 @@ +use std::fmt; + +#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)] +pub enum IntTy { + Isize, + I8, + I16, + I32, + I64, + I128, +} + +impl fmt::Debug for IntTy { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Display::fmt(self, f) + } +} + +impl fmt::Display for IntTy { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.ty_to_string()) + } +} + +impl IntTy { + pub fn ty_to_string(&self) -> &'static str { + match *self { + IntTy::Isize => "isize", + IntTy::I8 => "i8", + IntTy::I16 => "i16", + IntTy::I32 => "i32", + IntTy::I64 => "i64", + IntTy::I128 => "i128", + } + } +} + +#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)] +pub enum UintTy { + Usize, + U8, + U16, + U32, + U64, + U128, +} + +impl UintTy { + pub fn ty_to_string(&self) -> &'static str { + match *self { + UintTy::Usize => "usize", + UintTy::U8 => "u8", + UintTy::U16 => "u16", + UintTy::U32 => "u32", + UintTy::U64 => "u64", + UintTy::U128 => "u128", + } + } +} + +impl fmt::Debug for UintTy { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Display::fmt(self, f) + } +} + +impl fmt::Display for UintTy { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.ty_to_string()) + } +} + +#[derive(Clone, PartialEq, Eq, Hash, Copy, PartialOrd, Ord)] +pub enum FloatTy { + F32, + F64, +} + +impl fmt::Debug for FloatTy { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Display::fmt(self, f) + } +} + +impl fmt::Display for FloatTy { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.ty_to_string()) + } +} + +impl FloatTy { + pub fn ty_to_string(self) -> &'static str { + match self { + FloatTy::F32 => "f32", + FloatTy::F64 => "f64", + } + } +} diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs new file mode 100644 index 000000000..f2466dd51 --- /dev/null +++ b/crates/ra_hir/src/ty/tests.rs @@ -0,0 +1,45 @@ +use std::sync::Arc; + +use salsa::Database; +use ra_db::{FilesDatabase, CrateGraph, SyntaxDatabase}; +use ra_syntax::{SmolStr, algo::visit::{visitor, Visitor}, ast::{self, AstNode}}; +use relative_path::RelativePath; + +use crate::{source_binder, mock::WORKSPACE, module::ModuleSourceNode}; + +use crate::{ + self as hir, + db::HirDatabase, + mock::MockDatabase, +}; + +fn infer_all_fns(fixture: &str) -> () { + let (db, source_root) = MockDatabase::with_files(fixture); + for &file_id in source_root.files.values() { + let source_file = db.source_file(file_id); + for fn_def in source_file.syntax().descendants().filter_map(ast::FnDef::cast) { + let func = source_binder::function_from_source(&db, file_id, fn_def).unwrap().unwrap(); + let inference_result = func.infer(&db); + for (syntax_ptr, ty) in &inference_result.type_for { + let node = syntax_ptr.resolve(&source_file); + eprintln!("{} '{}': {:?}", syntax_ptr.range(), node.text(), ty); + } + } + } +} + +#[test] +fn infer_smoke_test() { + let text = " + //- /lib.rs + fn foo(x: u32, y: !) -> i128 { + x; + y; + return 1; + \"hello\"; + 0 + } + "; + + infer_all_fns(text); +} -- cgit v1.2.3