Skip to content

Commit d463efb

Browse files
authored
Merge pull request #195 from tiif/mir-fn-body
Typeck mir fn bodies
2 parents 07beba5 + 62e7f9d commit d463efb

File tree

14 files changed

+1339
-72
lines changed

14 files changed

+1339
-72
lines changed

crates/formality-check/src/fns.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ use formality_rust::{
33
grammar::{Fn, FnBoundData, MaybeFnBody},
44
prove::ToWcs,
55
};
6-
use formality_types::grammar::{Fallible, Wcs};
6+
use formality_types::grammar::{CrateId, Fallible, Wcs};
77

8-
use crate::{mini_rust_check, Check};
8+
use crate::Check;
99

1010
impl Check<'_> {
1111
/// A "free function" is a free-standing function that is not part of an impl.
12-
pub(crate) fn check_free_fn(&self, f: &Fn) -> Fallible<()> {
13-
self.check_fn(&Env::default(), Wcs::t(), f)
12+
pub(crate) fn check_free_fn(&self, f: &Fn, crate_id: &CrateId) -> Fallible<()> {
13+
self.check_fn(&Env::default(), Wcs::t(), f, crate_id)
1414
}
1515

1616
/// Invoked for both free functions and methods.
17-
///
17+
///
1818
/// # Parameters
19-
///
19+
///
2020
/// * `in_env` -- the environment from the enclosing impl (if any)
2121
/// * `in_assumptions` -- where-clauses from the enclosing impl (if any)
2222
/// * `f` -- the function definition
@@ -25,6 +25,7 @@ impl Check<'_> {
2525
in_env: &Env,
2626
in_assumptions: impl ToWcs,
2727
f: &Fn,
28+
crate_id: &CrateId,
2829
) -> Fallible<()> {
2930
let in_assumptions = in_assumptions.to_wcs();
3031

@@ -59,7 +60,7 @@ impl Check<'_> {
5960
for input_ty in &input_tys {
6061
self.prove_goal(&env, &fn_assumptions, input_ty.well_formed())?;
6162
}
62-
self.prove_goal(&env, &fn_assumptions, output_ty.well_formed())?;
63+
self.prove_goal(&env, &fn_assumptions, &output_ty.well_formed())?;
6364

6465
// Type-check the function body, if present.
6566
match body {
@@ -71,9 +72,9 @@ impl Check<'_> {
7172
// A trusted function body is assumed to be valid, all set.
7273
}
7374
formality_rust::grammar::FnBody::MiniRust(body) => {
74-
mini_rust_check::check_body(&env, &fn_assumptions, body)?;
75+
self.check_body(&env, &output_ty, &fn_assumptions, body, input_tys, crate_id)?;
7576
}
76-
}
77+
},
7778
}
7879

7980
Ok(())

crates/formality-check/src/impls.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@ use formality_rust::{
1212
prove::ToWcs,
1313
};
1414
use formality_types::{
15-
grammar::{Binder, Fallible, Relation, Substitution, Wcs},
15+
grammar::{Binder, CrateId, Fallible, Relation, Substitution, Wcs},
1616
rust::Term,
1717
};
1818

1919
impl super::Check<'_> {
2020
#[context("check_trait_impl({trait_impl:?})")]
21-
pub(super) fn check_trait_impl(&self, trait_impl: &TraitImpl) -> Fallible<()> {
21+
pub(super) fn check_trait_impl(
22+
&self,
23+
trait_impl: &TraitImpl,
24+
crate_id: &CrateId,
25+
) -> Fallible<()> {
2226
let TraitImpl { binder, safety: _ } = trait_impl;
2327

2428
let mut env = Env::default();
@@ -48,7 +52,7 @@ impl super::Check<'_> {
4852
self.check_safety_matches(trait_decl, trait_impl)?;
4953

5054
for impl_item in &impl_items {
51-
self.check_trait_impl_item(&env, &where_clauses, &trait_items, impl_item)?;
55+
self.check_trait_impl_item(&env, &where_clauses, &trait_items, impl_item, crate_id)?;
5256
}
5357

5458
Ok(())
@@ -101,14 +105,15 @@ impl super::Check<'_> {
101105
assumptions: impl ToWcs,
102106
trait_items: &[TraitItem],
103107
impl_item: &ImplItem,
108+
crate_id: &CrateId,
104109
) -> Fallible<()> {
105110
let assumptions: Wcs = assumptions.to_wcs();
106111
assert!(
107112
env.only_universal_variables() && env.encloses((&assumptions, trait_items, impl_item))
108113
);
109114

110115
match impl_item {
111-
ImplItem::Fn(v) => self.check_fn_in_impl(env, &assumptions, trait_items, v),
116+
ImplItem::Fn(v) => self.check_fn_in_impl(env, &assumptions, trait_items, v, crate_id),
112117
ImplItem::AssociatedTyValue(v) => {
113118
self.check_associated_ty_value(env, assumptions, trait_items, v)
114119
}
@@ -121,6 +126,7 @@ impl super::Check<'_> {
121126
impl_assumptions: impl ToWcs,
122127
trait_items: &[TraitItem],
123128
ii_fn: &Fn,
129+
crate_id: &CrateId,
124130
) -> Fallible<()> {
125131
let impl_assumptions: Wcs = impl_assumptions.to_wcs();
126132
assert!(
@@ -139,7 +145,7 @@ impl super::Check<'_> {
139145

140146
tracing::debug!(?ti_fn);
141147

142-
self.check_fn(env, &impl_assumptions, ii_fn)?;
148+
self.check_fn(env, &impl_assumptions, ii_fn, crate_id)?;
143149

144150
let mut env = env.clone();
145151
let (

crates/formality-check/src/lib.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#![allow(dead_code)]
22

3-
use formality_types::rust::FormalityLang;
4-
53
use std::{collections::VecDeque, fmt::Debug};
64

75
use anyhow::bail;
@@ -11,9 +9,9 @@ use formality_rust::{
119
grammar::{Crate, CrateItem, Program, Test, TestBoundData},
1210
prove::ToWcs,
1311
};
14-
use formality_types::grammar::{Fallible, Wcs};
12+
use formality_types::grammar::{CrateId, Fallible, Wcs};
1513

16-
mod mini_rust_check;;
14+
mod mini_rust_check;
1715

1816
/// Check all crates in the program. The crates must be in dependency order
1917
/// such that any prefix of the crates is a complete program.
@@ -62,12 +60,12 @@ impl Check<'_> {
6260
}
6361

6462
fn check_current_crate(&self, c: &Crate) -> Fallible<()> {
65-
let Crate { id: _, items } = c;
63+
let Crate { id, items } = c;
6664

6765
self.check_for_duplicate_items()?;
6866

6967
for item in items {
70-
self.check_crate_item(item)?;
68+
self.check_crate_item(item, &id)?;
7169
}
7270

7371
self.check_coherence(c)?;
@@ -111,13 +109,13 @@ impl Check<'_> {
111109
Ok(())
112110
}
113111

114-
fn check_crate_item(&self, c: &CrateItem) -> Fallible<()> {
112+
fn check_crate_item(&self, c: &CrateItem, crate_id: &CrateId) -> Fallible<()> {
115113
match c {
116-
CrateItem::Trait(v) => self.check_trait(v),
117-
CrateItem::TraitImpl(v) => self.check_trait_impl(v),
114+
CrateItem::Trait(v) => self.check_trait(v, crate_id),
115+
CrateItem::TraitImpl(v) => self.check_trait_impl(v, crate_id),
118116
CrateItem::Struct(s) => self.check_adt(&s.to_adt()),
119117
CrateItem::Enum(e) => self.check_adt(&e.to_adt()),
120-
CrateItem::Fn(f) => self.check_free_fn(f),
118+
CrateItem::Fn(f) => self.check_free_fn(f, crate_id),
121119
CrateItem::NegTraitImpl(i) => self.check_neg_trait_impl(i),
122120
CrateItem::Test(t) => self.check_test(t),
123121
}

0 commit comments

Comments
 (0)