From 619a8185a607b216c64b58d230c3949ccef98a37 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 7 Sep 2019 21:03:03 +0200 Subject: Give closures types --- crates/ra_hir/src/ty.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'crates/ra_hir/src/ty.rs') diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index 36bfb10ce..e6ecbe1ea 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -16,7 +16,10 @@ use std::ops::Deref; use std::sync::Arc; use std::{fmt, mem}; -use crate::{db::HirDatabase, type_ref::Mutability, Adt, GenericParams, Name, Trait, TypeAlias}; +use crate::{ + db::HirDatabase, expr::ExprId, type_ref::Mutability, Adt, DefWithBody, GenericParams, Name, + Trait, TypeAlias, +}; use display::{HirDisplay, HirFormatter}; pub(crate) use autoderef::autoderef; @@ -100,6 +103,12 @@ pub enum TypeCtor { /// couldn't find a better representation. In that case, we generate /// an **application type** like `(Iterator::Item)`. AssociatedType(TypeAlias), + + /// The type of a specific closure. + /// + /// The closure signature is stored in a `FnPtr` type in the first type + /// parameter. + Closure { def: DefWithBody, expr: ExprId }, } /// A nominal type with (maybe 0) type parameters. This might be a primitive @@ -481,6 +490,10 @@ impl Ty { let sig = db.callable_item_signature(def); Some(sig.subst(&a_ty.parameters)) } + TypeCtor::Closure { .. } => { + let sig_param = &a_ty.parameters[0]; + sig_param.callable_sig(db) + } _ => None, }, _ => None, @@ -720,6 +733,14 @@ impl HirDisplay for ApplicationTy { write!(f, ">")?; } } + TypeCtor::Closure { .. } => { + let sig = self.parameters[0] + .callable_sig(f.db) + .expect("first closure parameter should contain signature"); + write!(f, "|")?; + f.write_joined(sig.params(), ", ")?; + write!(f, "| -> {}", sig.ret().display(f.db))?; + } } Ok(()) } -- cgit v1.2.3