897: Add basic const/static type inference r=flodiebold a=vipentti

This adds basic const/static type inference discussed in #887.

Currently the inference does not work for const/static declared inside a block. In addition the inference does not work inside the bodies of const/static.

Co-authored-by: Ville Penttinen <villem.penttinen@gmail.com>
This commit is contained in:
bors[bot] 2019-02-25 12:03:57 +00:00
commit 7c9acf2f83
11 changed files with 206 additions and 20 deletions

View file

@ -554,11 +554,26 @@ impl Const {
self.id.module(db)
}
pub fn signature(&self, db: &impl HirDatabase) -> Arc<ConstSignature> {
db.const_signature(*self)
}
/// The containing impl block, if this is a method.
pub fn impl_block(&self, db: &impl PersistentHirDatabase) -> Option<ImplBlock> {
let module_impls = db.impls_in_module(self.module(db));
ImplBlock::containing(module_impls, (*self).into())
}
// TODO: move to a more general type for 'body-having' items
/// Builds a resolver for code inside this item.
pub fn resolver(&self, db: &impl HirDatabase) -> Resolver {
// take the outer scope...
let r = self
.impl_block(db)
.map(|ib| ib.resolver(db))
.unwrap_or_else(|| self.module(db).resolver(db));
r
}
}
impl Docs for Const {
@ -567,6 +582,23 @@ impl Docs for Const {
}
}
/// The declared signature of a const.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConstSignature {
pub(crate) name: Name,
pub(crate) type_ref: TypeRef,
}
impl ConstSignature {
pub fn name(&self) -> &Name {
&self.name
}
pub fn type_ref(&self) -> &TypeRef {
&self.type_ref
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Static {
pub(crate) id: StaticId,
@ -580,6 +612,16 @@ impl Static {
pub fn module(&self, db: &impl PersistentHirDatabase) -> Module {
self.id.module(db)
}
pub fn signature(&self, db: &impl HirDatabase) -> Arc<ConstSignature> {
db.static_signature(*self)
}
/// Builds a resolver for code inside this item.
pub fn resolver(&self, db: &impl HirDatabase) -> Resolver {
// take the outer scope...
self.module(db).resolver(db)
}
}
impl Docs for Static {