1076: Const body inference r=flodiebold a=Lapz

This is the second part of #887. I've added type inference on const bodies and introduced the DefWithBody containing Function, Const and Static. I want to add tests but im unsure on how I would go about testing that completions work.


Co-authored-by: Lenard Pratt <l3np27@gmail.com>
This commit is contained in:
bors[bot] 2019-04-02 19:01:54 +00:00
commit fdbebccd71
10 changed files with 231 additions and 49 deletions

View file

@ -429,6 +429,45 @@ impl Docs for EnumVariant {
}
}
/// The defs which have a body.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DefWithBody {
Function(Function),
Const(Const),
Static(Static),
}
impl_froms!(DefWithBody: Function, Const, Static);
impl DefWithBody {
pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> {
db.infer(*self)
}
pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> {
db.body_with_source_map(*self).1
}
pub fn body(&self, db: &impl HirDatabase) -> Arc<Body> {
db.body_hir(*self)
}
/// Builds a resolver for code inside this item.
pub fn resolver(&self, db: &impl HirDatabase) -> Resolver {
match *self {
DefWithBody::Const(ref c) => c.resolver(db),
DefWithBody::Function(ref f) => f.resolver(db),
DefWithBody::Static(ref s) => s.resolver(db),
}
}
pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSourceMap {
let scopes = db.expr_scopes(*self);
let source_map = db.body_with_source_map(*self).1;
ScopesWithSourceMap { scopes, source_map }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Function {
pub(crate) id: FunctionId,
@ -479,11 +518,11 @@ impl Function {
}
pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> {
db.body_with_source_map(*self).1
db.body_with_source_map((*self).into()).1
}
pub fn body(&self, db: &impl HirDatabase) -> Arc<Body> {
db.body_hir(*self)
db.body_hir((*self).into())
}
pub fn ty(&self, db: &impl HirDatabase) -> Ty {
@ -491,8 +530,8 @@ impl Function {
}
pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSourceMap {
let scopes = db.expr_scopes(*self);
let source_map = db.body_with_source_map(*self).1;
let scopes = db.expr_scopes((*self).into());
let source_map = db.body_with_source_map((*self).into()).1;
ScopesWithSourceMap { scopes, source_map }
}
@ -501,7 +540,7 @@ impl Function {
}
pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> {
db.infer(*self)
db.infer((*self).into())
}
pub fn generic_params(&self, db: &impl DefDatabase) -> Arc<GenericParams> {
@ -557,6 +596,14 @@ impl Const {
db.const_signature(*self)
}
pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> {
db.infer((*self).into())
}
pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> {
db.body_with_source_map((*self).into()).1
}
/// The containing impl block, if this is a method.
pub fn impl_block(&self, db: &impl DefDatabase) -> Option<ImplBlock> {
let module_impls = db.impls_in_module(self.module(db));
@ -621,6 +668,14 @@ impl Static {
// take the outer scope...
self.module(db).resolver(db)
}
pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> {
db.infer((*self).into())
}
pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> {
db.body_with_source_map((*self).into()).1
}
}
impl Docs for Static {