aboutsummaryrefslogtreecommitdiff
path: root/crates/hir
diff options
context:
space:
mode:
authorArnaud <[email protected]>2021-01-04 17:58:42 +0000
committerArnaud <[email protected]>2021-01-04 18:14:44 +0000
commita6dc7cf36de40f6b2a5b1dfee86430848f94ce42 (patch)
treeca9c21a110a7723761ad4807000beb96dc122766 /crates/hir
parent0708bfeb7270923be5a2059ad5b99de183e667ba (diff)
Make it possible to retrieve `hir::Function`'s return type
This is done by adding a `ret_type` method to `hir::Function`. I followed `assoc_fn_params` convention by creating a new `RetType` type, that contains the actual return type accessible via a `ty` method.
Diffstat (limited to 'crates/hir')
-rw-r--r--crates/hir/src/code_model.rs23
1 files changed, 23 insertions, 0 deletions
diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs
index 071e553a8..e783e0aba 100644
--- a/crates/hir/src/code_model.rs
+++ b/crates/hir/src/code_model.rs
@@ -743,6 +743,18 @@ impl Function {
743 db.function_data(self.id).name.clone() 743 db.function_data(self.id).name.clone()
744 } 744 }
745 745
746 pub fn ret_type(self, db: &dyn HirDatabase) -> RetType {
747 let resolver = self.id.resolver(db.upcast());
748 let ret_type = &db.function_data(self.id).ret_type;
749 let ctx = hir_ty::TyLoweringContext::new(db, &resolver);
750 let environment = TraitEnvironment::lower(db, &resolver);
751 let ty = Type {
752 krate: self.id.lookup(db.upcast()).container.module(db.upcast()).krate,
753 ty: InEnvironment { value: Ty::from_hir_ext(&ctx, ret_type).0, environment },
754 };
755 RetType { ty }
756 }
757
746 pub fn self_param(self, db: &dyn HirDatabase) -> Option<SelfParam> { 758 pub fn self_param(self, db: &dyn HirDatabase) -> Option<SelfParam> {
747 if !db.function_data(self.id).has_self_param { 759 if !db.function_data(self.id).has_self_param {
748 return None; 760 return None;
@@ -827,6 +839,17 @@ impl From<Mutability> for Access {
827} 839}
828 840
829#[derive(Debug)] 841#[derive(Debug)]
842pub struct RetType {
843 ty: Type,
844}
845
846impl RetType {
847 pub fn ty(&self) -> &Type {
848 &self.ty
849 }
850}
851
852#[derive(Debug)]
830pub struct Param { 853pub struct Param {
831 ty: Type, 854 ty: Type,
832} 855}