Skip to content

Commit 55388d3

Browse files
committed
Pass crate_id instead of all fn item from current crate
1 parent fbdbfba commit 55388d3

File tree

5 files changed

+62
-59
lines changed

5 files changed

+62
-59
lines changed

crates/formality-check/src/fns.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ 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::{Check, CrateItem};
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, all_fn: &Vec<CrateItem>) -> Fallible<()> {
13-
self.check_fn(&Env::default(), Wcs::t(), f, all_fn)
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.
@@ -25,7 +25,7 @@ impl Check<'_> {
2525
in_env: &Env,
2626
in_assumptions: impl ToWcs,
2727
f: &Fn,
28-
all_fn: &Vec<CrateItem>,
28+
crate_id: &CrateId
2929
) -> Fallible<()> {
3030
let in_assumptions = in_assumptions.to_wcs();
3131

@@ -72,7 +72,7 @@ impl Check<'_> {
7272
// A trusted function body is assumed to be valid, all set.
7373
}
7474
formality_rust::grammar::FnBody::MiniRust(body) => {
75-
self.check_body(&env, &output_ty, &fn_assumptions, body, all_fn, input_tys)?;
75+
self.check_body(&env, &output_ty, &fn_assumptions, body, input_tys, crate_id)?;
7676
}
7777
},
7878
}

crates/formality-check/src/impls.rs

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

3-
use crate::CrateItem;
43
use fn_error_context::context;
54
use formality_core::Downcasted;
65
use formality_prove::{Env, Safety};
@@ -13,7 +12,7 @@ use formality_rust::{
1312
prove::ToWcs,
1413
};
1514
use formality_types::{
16-
grammar::{Binder, Fallible, Relation, Substitution, Wcs},
15+
grammar::{Binder, CrateId, Fallible, Relation, Substitution, Wcs},
1716
rust::Term,
1817
};
1918

@@ -22,7 +21,7 @@ impl super::Check<'_> {
2221
pub(super) fn check_trait_impl(
2322
&self,
2423
trait_impl: &TraitImpl,
25-
all_fn: &Vec<CrateItem>,
24+
crate_id: &CrateId
2625
) -> Fallible<()> {
2726
let TraitImpl { binder, safety: _ } = trait_impl;
2827

@@ -53,7 +52,7 @@ impl super::Check<'_> {
5352
self.check_safety_matches(trait_decl, trait_impl)?;
5453

5554
for impl_item in &impl_items {
56-
self.check_trait_impl_item(&env, &where_clauses, &trait_items, impl_item, all_fn)?;
55+
self.check_trait_impl_item(&env, &where_clauses, &trait_items, impl_item, crate_id)?;
5756
}
5857

5958
Ok(())
@@ -106,15 +105,15 @@ impl super::Check<'_> {
106105
assumptions: impl ToWcs,
107106
trait_items: &[TraitItem],
108107
impl_item: &ImplItem,
109-
all_fn: &Vec<CrateItem>,
108+
crate_id: &CrateId,
110109
) -> Fallible<()> {
111110
let assumptions: Wcs = assumptions.to_wcs();
112111
assert!(
113112
env.only_universal_variables() && env.encloses((&assumptions, trait_items, impl_item))
114113
);
115114

116115
match impl_item {
117-
ImplItem::Fn(v) => self.check_fn_in_impl(env, &assumptions, trait_items, v, all_fn),
116+
ImplItem::Fn(v) => self.check_fn_in_impl(env, &assumptions, trait_items, v, crate_id),
118117
ImplItem::AssociatedTyValue(v) => {
119118
self.check_associated_ty_value(env, assumptions, trait_items, v)
120119
}
@@ -127,7 +126,7 @@ impl super::Check<'_> {
127126
impl_assumptions: impl ToWcs,
128127
trait_items: &[TraitItem],
129128
ii_fn: &Fn,
130-
all_fn: &Vec<CrateItem>,
129+
crate_id: &CrateId
131130
) -> Fallible<()> {
132131
let impl_assumptions: Wcs = impl_assumptions.to_wcs();
133132
assert!(
@@ -146,7 +145,7 @@ impl super::Check<'_> {
146145

147146
tracing::debug!(?ti_fn);
148147

149-
self.check_fn(env, &impl_assumptions, ii_fn, all_fn)?;
148+
self.check_fn(env, &impl_assumptions, ii_fn, crate_id)?;
150149

151150
let mut env = env.clone();
152151
let (

crates/formality-check/src/lib.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use formality_rust::{
99
grammar::{Crate, CrateItem, Program, Test, TestBoundData},
1010
prove::ToWcs,
1111
};
12-
use formality_types::grammar::{Fallible, Wcs};
12+
use formality_types::grammar::{CrateId, Fallible, Wcs};
1313

1414
mod mini_rust_check;
1515

@@ -60,19 +60,12 @@ impl Check<'_> {
6060
}
6161

6262
fn check_current_crate(&self, c: &Crate) -> Fallible<()> {
63-
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();
63+
let Crate { id, items } = c;
7164

7265
self.check_for_duplicate_items()?;
7366

7467
for item in items {
75-
self.check_crate_item(item, &all_fn)?;
68+
self.check_crate_item(item, &id)?;
7669
}
7770

7871
self.check_coherence(c)?;
@@ -116,13 +109,13 @@ impl Check<'_> {
116109
Ok(())
117110
}
118111

119-
fn check_crate_item(&self, c: &CrateItem, all_fn: &Vec<CrateItem>) -> Fallible<()> {
112+
fn check_crate_item(&self, c: &CrateItem, crate_id: &CrateId) -> Fallible<()> {
120113
match c {
121-
CrateItem::Trait(v) => self.check_trait(v, all_fn),
122-
CrateItem::TraitImpl(v) => self.check_trait_impl(v, all_fn),
114+
CrateItem::Trait(v) => self.check_trait(v, crate_id),
115+
CrateItem::TraitImpl(v) => self.check_trait_impl(v, crate_id),
123116
CrateItem::Struct(s) => self.check_adt(&s.to_adt()),
124117
CrateItem::Enum(e) => self.check_adt(&e.to_adt()),
125-
CrateItem::Fn(f) => self.check_free_fn(f, all_fn),
118+
CrateItem::Fn(f) => self.check_free_fn(f, crate_id),
126119
CrateItem::NegTraitImpl(i) => self.check_neg_trait_impl(i),
127120
CrateItem::Test(t) => self.check_test(t),
128121
}

crates/formality-check/src/mini_rust_check.rs

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use formality_rust::grammar::minirust::ValueExpression::{Fn, Load};
88
use formality_rust::grammar::minirust::{
99
self, ArgumentExpression, BasicBlock, BbId, LocalId, PlaceExpression, ValueExpression,
1010
};
11-
use formality_rust::grammar::FnBoundData;
12-
use formality_types::grammar::FnId;
11+
use formality_rust::grammar::{FnBoundData};
12+
use formality_types::grammar::{CrateId, FnId};
1313
use formality_types::grammar::{Relation, Ty, Wcs};
1414

1515
use crate::{Check, CrateItem};
@@ -22,8 +22,8 @@ impl Check<'_> {
2222
output_ty: impl Upcast<Ty>,
2323
fn_assumptions: &Wcs,
2424
body: minirust::Body,
25-
declared_fn: &Vec<CrateItem>,
2625
declared_input_tys: Vec<Ty>,
26+
crate_id: &CrateId
2727
) -> Fallible<()> {
2828
// Type-check:
2929
//
@@ -91,8 +91,8 @@ impl Check<'_> {
9191
ret_id: body.ret,
9292
ret_place_is_initialised: false,
9393
declared_input_tys,
94-
declared_fn: declared_fn.to_vec(),
9594
callee_input_tys: Map::new(),
95+
crate_id: crate_id.clone(),
9696
};
9797

9898
// (4) Check statements in body are valid
@@ -255,24 +255,34 @@ impl Check<'_> {
255255
}
256256
Fn(fn_id) => {
257257
// Check if the function called is in declared in current crate.
258-
let item = typeck_env.declared_fn.iter().find(|&item| {
259-
match item {
260-
CrateItem::Fn(fn_declared) => {
261-
if fn_declared.id == *fn_id {
262-
let fn_bound_data =
263-
typeck_env.env.instantiate_universally(&fn_declared.binder);
264-
// Store the callee information in typeck_env, we will need this when type checking Terminator::Call.
265-
typeck_env
266-
.callee_input_tys
267-
.insert(fn_declared.id.clone(), fn_bound_data);
268-
return true;
258+
259+
// Find the crate that is currently being typeck.
260+
let curr_crate = self.program.crates.iter().find(|c| c.id == typeck_env.crate_id ).unwrap();
261+
262+
// Find the callee from current crate.
263+
let callee = curr_crate
264+
.items
265+
.iter()
266+
.find(|item| {
267+
match item {
268+
CrateItem::Fn(fn_declared) => {
269+
if fn_declared.id == *fn_id {
270+
let fn_bound_data =
271+
typeck_env.env.instantiate_universally(&fn_declared.binder);
272+
// Store the callee information in typeck_env, we will need this when type checking Terminator::Call.
273+
typeck_env
274+
.callee_input_tys
275+
.insert(fn_declared.id.clone(), fn_bound_data);
276+
return true;
277+
}
278+
false
269279
}
270-
false
280+
_ => false,
271281
}
272-
_ => false,
273-
}
274-
});
275-
if item == None {
282+
});
283+
284+
// If the callee is not found, return error.
285+
if callee.is_none() {
276286
bail!("The function called is not declared in current crate")
277287
}
278288
value_ty = typeck_env.output_ty.clone();
@@ -329,9 +339,11 @@ struct TypeckEnv {
329339
/// All declared argument type of current function.
330340
declared_input_tys: Vec<Ty>,
331341

332-
/// All declared fn in current crate.
333-
declared_fn: Vec<CrateItem>,
334-
335342
/// All information of callee.
336343
callee_input_tys: Map<FnId, FnBoundData>,
344+
345+
/// The id of the crate where this function resides.
346+
/// We need this to access information about other functions
347+
/// declared in the current crate.
348+
crate_id: CrateId,
337349
}

crates/formality-check/src/traits.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
use crate::CrateItem;
21
use anyhow::bail;
32
use fn_error_context::context;
43
use formality_core::Set;
54
use formality_prove::Env;
65
use formality_rust::grammar::{
76
AssociatedTy, AssociatedTyBoundData, Fn, Trait, TraitBoundData, TraitItem, WhereClause,
87
};
9-
use formality_types::grammar::Fallible;
8+
use formality_types::grammar::{CrateId, Fallible};
109

1110
impl super::Check<'_> {
1211
#[context("check_trait({:?})", t.id)]
13-
pub(super) fn check_trait(&self, t: &Trait, all_fn: &Vec<CrateItem>) -> Fallible<()> {
12+
pub(super) fn check_trait(&self, t: &Trait, crate_id: &CrateId) -> Fallible<()> {
1413
let Trait {
1514
safety: _,
1615
id: _,
@@ -28,7 +27,7 @@ impl super::Check<'_> {
2827
self.prove_where_clauses_well_formed(&env, &where_clauses, &where_clauses)?;
2928

3029
for trait_item in &trait_items {
31-
self.check_trait_item(&env, &where_clauses, trait_item, all_fn)?;
30+
self.check_trait_item(&env, &where_clauses, trait_item, crate_id)?;
3231
}
3332

3433
Ok(())
@@ -63,10 +62,10 @@ impl super::Check<'_> {
6362
env: &Env,
6463
where_clauses: &[WhereClause],
6564
trait_item: &TraitItem,
66-
all_fn: &Vec<CrateItem>,
65+
crate_id: &CrateId,
6766
) -> Fallible<()> {
6867
match trait_item {
69-
TraitItem::Fn(v) => self.check_fn_in_trait(env, where_clauses, v, all_fn),
68+
TraitItem::Fn(v) => self.check_fn_in_trait(env, where_clauses, v, crate_id),
7069
TraitItem::AssociatedTy(v) => self.check_associated_ty(env, where_clauses, v),
7170
}
7271
}
@@ -76,9 +75,9 @@ impl super::Check<'_> {
7675
env: &Env,
7776
where_clauses: &[WhereClause],
7877
f: &Fn,
79-
all_fn: &Vec<CrateItem>,
78+
crate_id: &CrateId
8079
) -> Fallible<()> {
81-
self.check_fn(env, where_clauses, f, all_fn)
80+
self.check_fn(env, where_clauses, f, crate_id)
8281
}
8382

8483
fn check_associated_ty(

0 commit comments

Comments
 (0)