7160: Get `hir::Function` return type r=flodiebold a=arnaudgolfouse

Hello !

As said in #7158, I noticed that `hir::Function` has no direct way of getting the return type, so this PR adds this functionality.

Co-authored-by: Arnaud <arnaud.golfouse@free.fr>
This commit is contained in:
bors[bot] 2021-01-04 21:07:50 +00:00 committed by GitHub
commit b99b14311c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -743,6 +743,18 @@ impl Function {
db.function_data(self.id).name.clone()
}
/// Get this function's return type
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);
Type {
krate: self.id.lookup(db.upcast()).container.module(db.upcast()).krate,
ty: InEnvironment { value: Ty::from_hir_ext(&ctx, ret_type).0, environment },
}
}
pub fn self_param(self, db: &dyn HirDatabase) -> Option<SelfParam> {
if !db.function_data(self.id).has_self_param {
return None;