Skip to content

Commit fbdbfba

Browse files
committed
Add some basic type checking
1 parent af28a27 commit fbdbfba

File tree

9 files changed

+835
-63
lines changed

9 files changed

+835
-63
lines changed

crates/formality-check/src/fns.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ use formality_rust::{
55
};
66
use formality_types::grammar::{Fallible, Wcs};
77

8-
use crate::Check;
8+
use crate::{Check, CrateItem};
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, all_fn: &Vec<CrateItem>) -> Fallible<()> {
13+
self.check_fn(&Env::default(), Wcs::t(), f, all_fn)
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+
all_fn: &Vec<CrateItem>,
2829
) -> Fallible<()> {
2930
let in_assumptions = in_assumptions.to_wcs();
3031

@@ -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-
self.check_body(&env, &output_ty, &fn_assumptions, body)?;
75+
self.check_body(&env, &output_ty, &fn_assumptions, body, all_fn, input_tys)?;
7576
}
76-
}
77+
},
7778
}
7879

7980
Ok(())

crates/formality-check/src/impls.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use anyhow::bail;
22

3+
use crate::CrateItem;
34
use fn_error_context::context;
45
use formality_core::Downcasted;
56
use formality_prove::{Env, Safety};
@@ -18,7 +19,11 @@ use formality_types::{
1819

1920
impl super::Check<'_> {
2021
#[context("check_trait_impl({trait_impl:?})")]
21-
pub(super) fn check_trait_impl(&self, trait_impl: &TraitImpl) -> Fallible<()> {
22+
pub(super) fn check_trait_impl(
23+
&self,
24+
trait_impl: &TraitImpl,
25+
all_fn: &Vec<CrateItem>,
26+
) -> Fallible<()> {
2227
let TraitImpl { binder, safety: _ } = trait_impl;
2328

2429
let mut env = Env::default();
@@ -48,7 +53,7 @@ impl super::Check<'_> {
4853
self.check_safety_matches(trait_decl, trait_impl)?;
4954

5055
for impl_item in &impl_items {
51-
self.check_trait_impl_item(&env, &where_clauses, &trait_items, impl_item)?;
56+
self.check_trait_impl_item(&env, &where_clauses, &trait_items, impl_item, all_fn)?;
5257
}
5358

5459
Ok(())
@@ -101,14 +106,15 @@ impl super::Check<'_> {
101106
assumptions: impl ToWcs,
102107
trait_items: &[TraitItem],
103108
impl_item: &ImplItem,
109+
all_fn: &Vec<CrateItem>,
104110
) -> Fallible<()> {
105111
let assumptions: Wcs = assumptions.to_wcs();
106112
assert!(
107113
env.only_universal_variables() && env.encloses((&assumptions, trait_items, impl_item))
108114
);
109115

110116
match impl_item {
111-
ImplItem::Fn(v) => self.check_fn_in_impl(env, &assumptions, trait_items, v),
117+
ImplItem::Fn(v) => self.check_fn_in_impl(env, &assumptions, trait_items, v, all_fn),
112118
ImplItem::AssociatedTyValue(v) => {
113119
self.check_associated_ty_value(env, assumptions, trait_items, v)
114120
}
@@ -121,6 +127,7 @@ impl super::Check<'_> {
121127
impl_assumptions: impl ToWcs,
122128
trait_items: &[TraitItem],
123129
ii_fn: &Fn,
130+
all_fn: &Vec<CrateItem>,
124131
) -> Fallible<()> {
125132
let impl_assumptions: Wcs = impl_assumptions.to_wcs();
126133
assert!(
@@ -139,7 +146,7 @@ impl super::Check<'_> {
139146

140147
tracing::debug!(?ti_fn);
141148

142-
self.check_fn(env, &impl_assumptions, ii_fn)?;
149+
self.check_fn(env, &impl_assumptions, ii_fn, all_fn)?;
143150

144151
let mut env = env.clone();
145152
let (

crates/formality-check/src/lib.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,18 @@ impl Check<'_> {
6161

6262
fn check_current_crate(&self, c: &Crate) -> Fallible<()> {
6363
let Crate { id: _, items } = c;
64+
// Collect Fn item from current crate.
65+
let all_fn: Vec<CrateItem> = c
66+
.items
67+
.clone()
68+
.into_iter()
69+
.filter(|item| matches!(item, CrateItem::Fn(_)))
70+
.collect();
6471

6572
self.check_for_duplicate_items()?;
6673

6774
for item in items {
68-
self.check_crate_item(item)?;
75+
self.check_crate_item(item, &all_fn)?;
6976
}
7077

7178
self.check_coherence(c)?;
@@ -109,13 +116,13 @@ impl Check<'_> {
109116
Ok(())
110117
}
111118

112-
fn check_crate_item(&self, c: &CrateItem) -> Fallible<()> {
119+
fn check_crate_item(&self, c: &CrateItem, all_fn: &Vec<CrateItem>) -> Fallible<()> {
113120
match c {
114-
CrateItem::Trait(v) => self.check_trait(v),
115-
CrateItem::TraitImpl(v) => self.check_trait_impl(v),
121+
CrateItem::Trait(v) => self.check_trait(v, all_fn),
122+
CrateItem::TraitImpl(v) => self.check_trait_impl(v, all_fn),
116123
CrateItem::Struct(s) => self.check_adt(&s.to_adt()),
117124
CrateItem::Enum(e) => self.check_adt(&e.to_adt()),
118-
CrateItem::Fn(f) => self.check_free_fn(f),
125+
CrateItem::Fn(f) => self.check_free_fn(f, all_fn),
119126
CrateItem::NegTraitImpl(i) => self.check_neg_trait_impl(i),
120127
CrateItem::Test(t) => self.check_test(t),
121128
}

0 commit comments

Comments
 (0)