Skip to content

Commit 88e22e9

Browse files
committed
Added const bodies and static body to the ast
and added inference the inference test reduce code duplication
1 parent 7f3bf7c commit 88e22e9

File tree

10 files changed

+180
-91
lines changed

10 files changed

+180
-91
lines changed

crates/ra_hir/src/code_model_api.rs

+35-52
Original file line numberDiff line numberDiff line change
@@ -436,73 +436,40 @@ impl Docs for EnumVariant {
436436
/// The defs which have a body.
437437
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
438438
pub enum DefWithBody {
439-
Func(Function),
439+
Function(Function),
440440
Const(Const),
441441
Static(Static),
442442
}
443443

444-
impl DefWithBody {
445-
pub fn get_funct(&self) -> &Function {
446-
match *self {
447-
DefWithBody::Func(ref f) => f,
448-
_ => unreachable!()
449-
}
450-
}
451-
452-
pub fn const_source(&self, db: &impl DefDatabase) -> (HirFileId, TreeArc<ast::ConstDef>) {
453-
match *self {
454-
DefWithBody::Const(ref c) => c.source(db),
455-
_ => unreachable!()
456-
}
457-
}
458-
459-
pub fn func_source(&self, db: &impl DefDatabase) -> (HirFileId, TreeArc<ast::FnDef>) {
460-
match *self {
461-
DefWithBody::Func(ref f) => f.source(db),
462-
_ => unreachable!()
463-
}
464-
}
465-
466-
pub fn static_source(&self, db: &impl DefDatabase) -> (HirFileId, TreeArc<ast::StaticDef>) {
467-
match *self {
468-
DefWithBody::Static(ref s) => s.source(db),
469-
_ => unreachable!()
470-
}
471-
}
444+
impl_froms!(DefWithBody: Function, Const, Static);
472445

446+
impl DefWithBody {
473447
pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> {
474448
db.infer(*self)
475449
}
476450

451+
pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> {
452+
db.body_with_source_map(*self).1
453+
}
454+
477455
pub fn body(&self, db: &impl HirDatabase) -> Arc<Body> {
478456
db.body_hir(*self)
479457
}
480-
458+
481459
/// Builds a resolver for code inside this item.
482460
pub fn resolver(&self, db: &impl HirDatabase) -> Resolver {
483-
// // take the outer scope...
484-
// let r = self
485-
// .impl_block(db)
486-
// .map(|ib| ib.resolver(db))
487-
// .unwrap_or_else(|| self.module(db).resolver(db));
488-
// // ...and add generic params, if present
489-
// let p = self.generic_params(db);
490-
// let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r };
491-
// r
492-
unimplemented!()
493-
}
494-
495-
pub fn signature(&self, db: &impl HirDatabase) -> Arc<FnSignature> {
496-
// db.fn_signature(*self)
497-
unimplemented!()
461+
match *self {
462+
DefWithBody::Const(ref c) => c.resolver(db),
463+
DefWithBody::Function(ref f) => f.resolver(db),
464+
DefWithBody::Static(ref s) => s.resolver(db),
465+
}
498466
}
499467

500468
pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSourceMap {
501469
let scopes = db.expr_scopes(*self);
502470
let source_map = db.body_with_source_map(*self).1;
503471
ScopesWithSourceMap { scopes, source_map }
504472
}
505-
506473
}
507474

508475
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -555,20 +522,20 @@ impl Function {
555522
}
556523

557524
pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> {
558-
db.body_with_source_map(DefWithBody::Func(*self)).1
525+
db.body_with_source_map((*self).into()).1
559526
}
560527

561528
pub fn body(&self, db: &impl HirDatabase) -> Arc<Body> {
562-
db.body_hir(DefWithBody::Func(*self))
529+
db.body_hir((*self).into())
563530
}
564531

565532
pub fn ty(&self, db: &impl HirDatabase) -> Ty {
566533
db.type_for_def((*self).into(), Namespace::Values)
567534
}
568535

569536
pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSourceMap {
570-
let scopes = db.expr_scopes( DefWithBody::Func(*self));
571-
let source_map = db.body_with_source_map(DefWithBody::Func(*self)).1;
537+
let scopes = db.expr_scopes((*self).into());
538+
let source_map = db.body_with_source_map((*self).into()).1;
572539
ScopesWithSourceMap { scopes, source_map }
573540
}
574541

@@ -577,7 +544,7 @@ impl Function {
577544
}
578545

579546
pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> {
580-
db.infer(DefWithBody::Func(*self))
547+
db.infer((*self).into())
581548
}
582549

583550
pub fn generic_params(&self, db: &impl DefDatabase) -> Arc<GenericParams> {
@@ -633,6 +600,14 @@ impl Const {
633600
db.const_signature(*self)
634601
}
635602

603+
pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> {
604+
db.infer((*self).into())
605+
}
606+
607+
pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> {
608+
db.body_with_source_map((*self).into()).1
609+
}
610+
636611
/// The containing impl block, if this is a method.
637612
pub fn impl_block(&self, db: &impl DefDatabase) -> Option<ImplBlock> {
638613
let module_impls = db.impls_in_module(self.module(db));
@@ -697,6 +672,14 @@ impl Static {
697672
// take the outer scope...
698673
self.module(db).resolver(db)
699674
}
675+
676+
pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> {
677+
db.infer((*self).into())
678+
}
679+
680+
pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> {
681+
db.body_with_source_map((*self).into()).1
682+
}
700683
}
701684

702685
impl Docs for Static {
@@ -788,4 +771,4 @@ impl Docs for TypeAlias {
788771
fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
789772
docs_from_ast(&*self.source(db).1)
790773
}
791-
}
774+
}

crates/ra_hir/src/db.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pub trait HirDatabase: DefDatabase {
8787
fn expr_scopes(&self, def: DefWithBody) -> Arc<ExprScopes>;
8888

8989
#[salsa::invoke(crate::ty::infer)]
90-
fn infer(&self, def:DefWithBody) -> Arc<InferenceResult>;
90+
fn infer(&self, def: DefWithBody) -> Arc<InferenceResult>;
9191

9292
#[salsa::invoke(crate::ty::type_for_def)]
9393
fn type_for_def(&self, def: TypableDef, ns: Namespace) -> Ty;

crates/ra_hir/src/expr.rs

+16-20
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use ra_syntax::{
1010
};
1111

1212
use crate::{
13-
Path, Name, HirDatabase, Function, Resolver,DefWithBody,
13+
Path, Name, HirDatabase, Resolver,DefWithBody,
1414
name::AsName,
1515
type_ref::{Mutability, TypeRef},
1616
};
@@ -27,8 +27,7 @@ impl_arena_id!(ExprId);
2727
/// The body of an item (function, const etc.).
2828
#[derive(Debug, Eq, PartialEq)]
2929
pub struct Body {
30-
// FIXME: this should be more general, consts & statics also have bodies
31-
/// The Function of the item this body belongs to
30+
/// The def of the item this body belongs to
3231
owner: DefWithBody,
3332
exprs: Arena<ExprId, Expr>,
3433
pats: Arena<PatId, Pat>,
@@ -503,9 +502,6 @@ impl ExprCollector {
503502
self.exprs.alloc(block)
504503
}
505504

506-
507-
508-
509505
fn collect_expr(&mut self, expr: &ast::Expr) -> ExprId {
510506
let syntax_ptr = SyntaxNodePtr::new(expr.syntax());
511507
match expr.kind() {
@@ -874,13 +870,14 @@ impl ExprCollector {
874870
}
875871
}
876872

877-
878-
fn collect_const_body(&mut self,node:&ast::ConstDef) {
879-
873+
fn collect_const_body(&mut self, node: &ast::ConstDef) {
874+
let body = self.collect_expr_opt(node.body());
875+
self.body_expr = Some(body);
880876
}
881877

882-
fn collect_static_body(&mut self,node:&ast::StaticDef) {
883-
878+
fn collect_static_body(&mut self, node: &ast::StaticDef) {
879+
let body = self.collect_expr_opt(node.body());
880+
self.body_expr = Some(body);
884881
}
885882

886883
fn collect_fn_body(&mut self, node: &ast::FnDef) {
@@ -931,28 +928,27 @@ pub(crate) fn body_with_source_map_query(
931928
db: &impl HirDatabase,
932929
def: DefWithBody,
933930
) -> (Arc<Body>, Arc<BodySourceMap>) {
934-
935931
let mut collector = ExprCollector::new(def);
936932

937-
// FIXME: do can this be turned into a method
938-
939933
match def {
940-
DefWithBody::Const(ref c) => collector.collect_const_body(&def.const_source(db).1),
941-
DefWithBody::Func(ref f) => collector.collect_fn_body(&def.func_source(db).1),
942-
DefWithBody::Static(ref s) => collector.collect_static_body(&def.static_source(db).1)
934+
DefWithBody::Const(ref c) => collector.collect_const_body(&c.source(db).1),
935+
DefWithBody::Function(ref f) => collector.collect_fn_body(&f.source(db).1),
936+
DefWithBody::Static(ref s) => collector.collect_static_body(&s.source(db).1),
943937
}
944-
938+
945939
let (body, source_map) = collector.finish();
946940
(Arc::new(body), Arc::new(source_map))
947941
}
948942

949943
pub(crate) fn body_hir_query(db: &impl HirDatabase, def: DefWithBody) -> Arc<Body> {
950-
db.body_with_source_map(def).0
944+
db.body_with_source_map(def).0
951945
}
952946

947+
#[cfg(test)]
948+
use crate::{Function};
953949
#[cfg(test)]
954950
fn collect_fn_body_syntax(function: Function, node: &ast::FnDef) -> (Body, BodySourceMap) {
955-
let mut collector = ExprCollector::new(function);
951+
let mut collector = ExprCollector::new(DefWithBody::Function(function));
956952
collector.collect_fn_body(node);
957953
collector.finish()
958954
}

crates/ra_hir/src/expr/scope.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use ra_syntax::{
1010
use ra_arena::{Arena, RawId, impl_arena_id};
1111

1212
use crate::{
13-
Name, AsName, Function,DefWithBody,
13+
Name, AsName,DefWithBody,
1414
expr::{PatId, ExprId, Pat, Expr, Body, Statement, BodySourceMap},
1515
HirDatabase,
1616
};
@@ -297,6 +297,7 @@ mod tests {
297297
use ra_syntax::{SourceFile, algo::find_node_at_offset};
298298
use test_utils::{extract_offset, assert_eq_text};
299299
use ra_arena::ArenaId;
300+
use crate::{Function};
300301

301302
use crate::expr;
302303

crates/ra_hir/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,4 @@ pub use self::code_model_api::{
7474
StructField, FieldSource,
7575
Static, Const, ConstSignature,
7676
Trait, TypeAlias,
77-
7877
};

crates/ra_hir/src/source_binder.rs

+43-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use ra_syntax::{
1313
};
1414

1515
use crate::{
16-
HirDatabase, Function, Struct, Enum,
16+
HirDatabase, Function, Struct, Enum,Const,Static,
1717
AsName, Module, HirFileId, Crate, Trait, Resolver,
1818
ids::LocationCtx,
1919
expr, AstId
@@ -87,6 +87,27 @@ fn module_from_source(
8787
)
8888
}
8989

90+
pub fn const_from_source(
91+
db: &impl HirDatabase,
92+
file_id: FileId,
93+
const_def: &ast::ConstDef,
94+
) -> Option<Const> {
95+
let module = module_from_child_node(db, file_id, const_def.syntax())?;
96+
let res = const_from_module(db, module, const_def);
97+
Some(res)
98+
}
99+
100+
pub fn const_from_module(
101+
db: &impl HirDatabase,
102+
module: Module,
103+
const_def: &ast::ConstDef,
104+
) -> Const {
105+
let (file_id, _) = module.definition_source(db);
106+
let file_id = file_id.into();
107+
let ctx = LocationCtx::new(db, module, file_id);
108+
Const { id: ctx.to_def(const_def) }
109+
}
110+
90111
pub fn function_from_position(db: &impl HirDatabase, position: FilePosition) -> Option<Function> {
91112
let file = db.parse(position.file_id);
92113
let fn_def = find_node_at_offset::<ast::FnDef>(file.syntax(), position.offset)?;
@@ -134,6 +155,27 @@ pub fn struct_from_module(
134155
Struct { id: ctx.to_def(struct_def) }
135156
}
136157

158+
pub fn static_from_source(
159+
db: &impl HirDatabase,
160+
file_id: FileId,
161+
static_def: &ast::StaticDef,
162+
) -> Option<Static> {
163+
let module = module_from_child_node(db, file_id, static_def.syntax())?;
164+
let res = static_from_module(db, module, static_def);
165+
Some(res)
166+
}
167+
168+
pub fn static_from_module(
169+
db: &impl HirDatabase,
170+
module: Module,
171+
static_def: &ast::StaticDef,
172+
) -> Static {
173+
let (file_id, _) = module.definition_source(db);
174+
let file_id = file_id.into();
175+
let ctx = LocationCtx::new(db, module, file_id);
176+
Static { id: ctx.to_def(static_def) }
177+
}
178+
137179
pub fn enum_from_module(db: &impl HirDatabase, module: Module, enum_def: &ast::EnumDef) -> Enum {
138180
let (file_id, _) = module.definition_source(db);
139181
let file_id = file_id.into();

crates/ra_hir/src/ty/infer.rs

+14-6
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ use test_utils::tested_by;
2727

2828
use crate::{
2929
Function, StructField, Path, Name,
30-
FnSignature, AdtDef,
30+
FnSignature, AdtDef,ConstSignature,
3131
HirDatabase,
32+
DefWithBody,
3233
ImplItem,
3334
type_ref::{TypeRef, Mutability},
3435
expr::{Body, Expr, BindingAnnotation, Literal, ExprId, Pat, PatId, UnaryOp, BinaryOp, Statement, FieldPat, self},
@@ -43,14 +44,17 @@ use crate::{
4344
use super::{Ty, TypableDef, Substs, primitive, op, FnSig, ApplicationTy, TypeCtor};
4445

4546
/// The entry point of type inference.
46-
pub fn infer(db: &impl HirDatabase, func: Function) -> Arc<InferenceResult> {
47+
pub fn infer(db: &impl HirDatabase, def: DefWithBody) -> Arc<InferenceResult> {
4748
db.check_canceled();
48-
let body = func.body(db);
49-
let resolver = func.resolver(db);
49+
let body = def.body(db);
50+
let resolver = def.resolver(db);
5051
let mut ctx = InferenceContext::new(db, body, resolver);
5152

52-
let signature = func.signature(db);
53-
ctx.collect_fn_signature(&signature);
53+
match def {
54+
DefWithBody::Const(ref c) => ctx.collect_const_signature(&c.signature(db)),
55+
DefWithBody::Function(ref f) => ctx.collect_fn_signature(&f.signature(db)),
56+
DefWithBody::Static(ref s) => ctx.collect_const_signature(&s.signature(db)),
57+
}
5458

5559
ctx.infer_body();
5660

@@ -1142,6 +1146,10 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
11421146
ty
11431147
}
11441148

1149+
fn collect_const_signature(&mut self, signature: &ConstSignature) {
1150+
self.return_ty = self.make_ty(signature.type_ref());
1151+
}
1152+
11451153
fn collect_fn_signature(&mut self, signature: &FnSignature) {
11461154
let body = Arc::clone(&self.body); // avoid borrow checker problem
11471155
for (type_ref, pat) in signature.params().iter().zip(body.params()) {

0 commit comments

Comments
 (0)