From a6dc7cf36de40f6b2a5b1dfee86430848f94ce42 Mon Sep 17 00:00:00 2001 From: Arnaud Date: Mon, 4 Jan 2021 18:58:42 +0100 Subject: 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. --- crates/hir/src/code_model.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) 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 { db.function_data(self.id).name.clone() } + pub fn ret_type(self, db: &dyn HirDatabase) -> RetType { + let resolver = self.id.resolver(db.upcast()); + let ret_type = &db.function_data(self.id).ret_type; + let ctx = hir_ty::TyLoweringContext::new(db, &resolver); + let environment = TraitEnvironment::lower(db, &resolver); + let ty = Type { + krate: self.id.lookup(db.upcast()).container.module(db.upcast()).krate, + ty: InEnvironment { value: Ty::from_hir_ext(&ctx, ret_type).0, environment }, + }; + RetType { ty } + } + pub fn self_param(self, db: &dyn HirDatabase) -> Option { if !db.function_data(self.id).has_self_param { return None; @@ -826,6 +838,17 @@ impl From for Access { } } +#[derive(Debug)] +pub struct RetType { + ty: Type, +} + +impl RetType { + pub fn ty(&self) -> &Type { + &self.ty + } +} + #[derive(Debug)] pub struct Param { ty: Type, -- cgit v1.2.3 From 2f0969b873b1d28a578e1f61862f4a369ec9471f Mon Sep 17 00:00:00 2001 From: Arnaud Date: Mon, 4 Jan 2021 18:59:37 +0100 Subject: Document `hir::Function::ret_type` This adds documentation for the newly added function. It might be a bit too detailed, but I like it that way :) --- crates/hir/src/code_model.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index e783e0aba..a8b67da20 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs @@ -743,6 +743,10 @@ impl Function { db.function_data(self.id).name.clone() } + /// Get this function's return type + /// + /// The returned type can be converted to a [`Type`] via its `ty` + /// method. pub fn ret_type(self, db: &dyn HirDatabase) -> RetType { let resolver = self.id.resolver(db.upcast()); let ret_type = &db.function_data(self.id).ret_type; -- cgit v1.2.3 From 052404565ea495b32956373e18d807a8cde68ceb Mon Sep 17 00:00:00 2001 From: Arnaud Date: Mon, 4 Jan 2021 20:34:23 +0100 Subject: Remove `RetType` --- crates/hir/src/code_model.rs | 21 +++------------------ 1 file changed, 3 insertions(+), 18 deletions(-) diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index a8b67da20..1a4aa78fb 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs @@ -744,19 +744,15 @@ impl Function { } /// Get this function's return type - /// - /// The returned type can be converted to a [`Type`] via its `ty` - /// method. - pub fn ret_type(self, db: &dyn HirDatabase) -> RetType { + pub fn ret_type(self, db: &dyn HirDatabase) -> Type { let resolver = self.id.resolver(db.upcast()); let ret_type = &db.function_data(self.id).ret_type; let ctx = hir_ty::TyLoweringContext::new(db, &resolver); let environment = TraitEnvironment::lower(db, &resolver); - let ty = Type { + Type { krate: self.id.lookup(db.upcast()).container.module(db.upcast()).krate, ty: InEnvironment { value: Ty::from_hir_ext(&ctx, ret_type).0, environment }, - }; - RetType { ty } + } } pub fn self_param(self, db: &dyn HirDatabase) -> Option { @@ -842,17 +838,6 @@ impl From for Access { } } -#[derive(Debug)] -pub struct RetType { - ty: Type, -} - -impl RetType { - pub fn ty(&self) -> &Type { - &self.ty - } -} - #[derive(Debug)] pub struct Param { ty: Type, -- cgit v1.2.3