From 111126ed3c4f6358e0c833f80226e5192778f749 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 29 Dec 2018 21:32:07 +0100 Subject: Type the self parameter --- crates/ra_hir/src/mock.rs | 4 +++ crates/ra_hir/src/ty.rs | 38 ++++++++++++++++++++++----- crates/ra_hir/src/ty/tests/data/0007_self.txt | 2 ++ 3 files changed, 37 insertions(+), 7 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/mock.rs b/crates/ra_hir/src/mock.rs index ef245ec7a..2419d256a 100644 --- a/crates/ra_hir/src/mock.rs +++ b/crates/ra_hir/src/mock.rs @@ -30,6 +30,10 @@ impl MockDatabase { let file_id = db.add_file(&mut source_root, "/main.rs", text); db.query_mut(ra_db::SourceRootQuery) .set(WORKSPACE, Arc::new(source_root.clone())); + + let mut crate_graph = CrateGraph::default(); + crate_graph.add_crate_root(file_id); + db.set_crate_graph(crate_graph); (db, source_root, file_id) } diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index 719b3f7cd..c762ec606 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -918,22 +918,46 @@ pub fn infer(db: &impl HirDatabase, def_id: DefId) -> Cancelable ty, + ast::SelfParamFlavor::Ref => Ty::Ref(Arc::new(ty), Mutability::Shared), + ast::SelfParamFlavor::MutRef => Ty::Ref(Arc::new(ty), Mutability::Mut), + }; + ctx.insert_type_vars(ty) + } + } else { + log::debug!( + "No impl block found, but self param for function {:?}", + def_id + ); + ctx.new_type_var() + }; + if let Some(self_kw) = self_param.self_kw() { + ctx.type_of + .insert(LocalSyntaxPtr::new(self_kw.syntax()), self_type); + } + } for param in param_list.params() { let pat = if let Some(pat) = param.pat() { pat } else { continue; }; - if let Some(type_ref) = param.type_ref() { + let ty = if let Some(type_ref) = param.type_ref() { let ty = Ty::from_ast(db, &ctx.module, type_ref)?; - let ty = ctx.insert_type_vars(ty); - ctx.type_of.insert(LocalSyntaxPtr::new(pat.syntax()), ty); + ctx.insert_type_vars(ty) } else { - // TODO self param - let type_var = ctx.new_type_var(); - ctx.type_of - .insert(LocalSyntaxPtr::new(pat.syntax()), type_var); + // missing type annotation + ctx.new_type_var() }; + ctx.type_of.insert(LocalSyntaxPtr::new(pat.syntax()), ty); } } diff --git a/crates/ra_hir/src/ty/tests/data/0007_self.txt b/crates/ra_hir/src/ty/tests/data/0007_self.txt index 18cd796c2..3d3c8b260 100644 --- a/crates/ra_hir/src/ty/tests/data/0007_self.txt +++ b/crates/ra_hir/src/ty/tests/data/0007_self.txt @@ -1,4 +1,6 @@ [50; 54) 'self': [unknown] +[34; 38) 'self': &S [40; 61) '{ ... }': () [88; 109) '{ ... }': () [98; 102) 'self': [unknown] +[75; 79) 'self': &[unknown] -- cgit v1.2.3