aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/code_model_api.rs
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-04-02 20:01:54 +0100
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-04-02 20:01:54 +0100
commitfdbebccd71d38c4dffffe918b036bbfa39355c5f (patch)
tree541c13ce234023bbfc640a38e55e860c029fd52c /crates/ra_hir/src/code_model_api.rs
parent01a54f094ba7c17a6788ec706f12b07d8d60af4b (diff)
parentb9d2c2c21fe8880fe1ca29b70b03db1d3faac554 (diff)
Merge #1076
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 <[email protected]>
Diffstat (limited to 'crates/ra_hir/src/code_model_api.rs')
-rw-r--r--crates/ra_hir/src/code_model_api.rs65
1 files changed, 60 insertions, 5 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs
index 87d81f4a4..9e6170440 100644
--- a/crates/ra_hir/src/code_model_api.rs
+++ b/crates/ra_hir/src/code_model_api.rs
@@ -429,6 +429,45 @@ impl Docs for EnumVariant {
429 } 429 }
430} 430}
431 431
432/// The defs which have a body.
433#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
434pub enum DefWithBody {
435 Function(Function),
436 Const(Const),
437 Static(Static),
438}
439
440impl_froms!(DefWithBody: Function, Const, Static);
441
442impl DefWithBody {
443 pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> {
444 db.infer(*self)
445 }
446
447 pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> {
448 db.body_with_source_map(*self).1
449 }
450
451 pub fn body(&self, db: &impl HirDatabase) -> Arc<Body> {
452 db.body_hir(*self)
453 }
454
455 /// Builds a resolver for code inside this item.
456 pub fn resolver(&self, db: &impl HirDatabase) -> Resolver {
457 match *self {
458 DefWithBody::Const(ref c) => c.resolver(db),
459 DefWithBody::Function(ref f) => f.resolver(db),
460 DefWithBody::Static(ref s) => s.resolver(db),
461 }
462 }
463
464 pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSourceMap {
465 let scopes = db.expr_scopes(*self);
466 let source_map = db.body_with_source_map(*self).1;
467 ScopesWithSourceMap { scopes, source_map }
468 }
469}
470
432#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 471#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
433pub struct Function { 472pub struct Function {
434 pub(crate) id: FunctionId, 473 pub(crate) id: FunctionId,
@@ -479,11 +518,11 @@ impl Function {
479 } 518 }
480 519
481 pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> { 520 pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> {
482 db.body_with_source_map(*self).1 521 db.body_with_source_map((*self).into()).1
483 } 522 }
484 523
485 pub fn body(&self, db: &impl HirDatabase) -> Arc<Body> { 524 pub fn body(&self, db: &impl HirDatabase) -> Arc<Body> {
486 db.body_hir(*self) 525 db.body_hir((*self).into())
487 } 526 }
488 527
489 pub fn ty(&self, db: &impl HirDatabase) -> Ty { 528 pub fn ty(&self, db: &impl HirDatabase) -> Ty {
@@ -491,8 +530,8 @@ impl Function {
491 } 530 }
492 531
493 pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSourceMap { 532 pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSourceMap {
494 let scopes = db.expr_scopes(*self); 533 let scopes = db.expr_scopes((*self).into());
495 let source_map = db.body_with_source_map(*self).1; 534 let source_map = db.body_with_source_map((*self).into()).1;
496 ScopesWithSourceMap { scopes, source_map } 535 ScopesWithSourceMap { scopes, source_map }
497 } 536 }
498 537
@@ -501,7 +540,7 @@ impl Function {
501 } 540 }
502 541
503 pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> { 542 pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> {
504 db.infer(*self) 543 db.infer((*self).into())
505 } 544 }
506 545
507 pub fn generic_params(&self, db: &impl DefDatabase) -> Arc<GenericParams> { 546 pub fn generic_params(&self, db: &impl DefDatabase) -> Arc<GenericParams> {
@@ -557,6 +596,14 @@ impl Const {
557 db.const_signature(*self) 596 db.const_signature(*self)
558 } 597 }
559 598
599 pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> {
600 db.infer((*self).into())
601 }
602
603 pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> {
604 db.body_with_source_map((*self).into()).1
605 }
606
560 /// The containing impl block, if this is a method. 607 /// The containing impl block, if this is a method.
561 pub fn impl_block(&self, db: &impl DefDatabase) -> Option<ImplBlock> { 608 pub fn impl_block(&self, db: &impl DefDatabase) -> Option<ImplBlock> {
562 let module_impls = db.impls_in_module(self.module(db)); 609 let module_impls = db.impls_in_module(self.module(db));
@@ -621,6 +668,14 @@ impl Static {
621 // take the outer scope... 668 // take the outer scope...
622 self.module(db).resolver(db) 669 self.module(db).resolver(db)
623 } 670 }
671
672 pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> {
673 db.infer((*self).into())
674 }
675
676 pub fn body_source_map(&self, db: &impl HirDatabase) -> Arc<BodySourceMap> {
677 db.body_with_source_map((*self).into()).1
678 }
624} 679}
625 680
626impl Docs for Static { 681impl Docs for Static {