Skip to content

Commit 1324d13

Browse files
committed
Add documentation
1 parent 7a925de commit 1324d13

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

crates/formality-check/src/impls.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,46 @@ impl super::Check<'_> {
120120
}
121121
}
122122

123+
/// Check that function `ii_fn` that appears in an impl is valid.
124+
/// This includes the core check from [`Self::check_fn`],
125+
/// but also additional checks to ensure that the signature in the impl
126+
/// matches what is declared in the trait.
127+
///
128+
/// # Example
129+
///
130+
/// Suppose we are checking `<Cup<L> as Potable>::drink` in this example...
131+
///
132+
/// ```rust,ignore
133+
/// trait Potable {
134+
/// fn drink(&self) // `trait_items` includes both fn drink and fn smell
135+
/// where Self: Copy; // <-- ti_where_clauses
136+
/// fn smell(&self);
137+
/// }
138+
///
139+
/// struct Water;
140+
/// impl Potable for Water {
141+
/// fn drink(&self) {}
142+
/// fn smell(&self) {}
143+
/// }
144+
///
145+
/// struct Cup<L>;
146+
/// impl<L> Potable for Cup<L> // <-- env has `L` in scope
147+
/// where
148+
/// L: Potable, // <-- `impl_assumptions`
149+
/// {
150+
/// fn drink(&self) {} // <-- `ii_fn`
151+
/// where (Cup<L>,): Copy; // <-- ii_where_clauses, implied by ti_where_clauses, Cup<L>: Copy, ok
152+
/// fn smell(&self) {} // not currently being checked
153+
/// }
154+
/// ```
155+
///
156+
/// # Parameters
157+
///
158+
/// * `env`, the environment from the impl header
159+
/// * `impl_assumptions`, where-clauses declared on the impl
160+
/// * `trait_items`, items declared in the trait that is being implemented
161+
/// (we search this to find the corresponding declaration of the method)
162+
/// * `ii_fn`, the fn as declared in the impl
123163
fn check_fn_in_impl(
124164
&self,
125165
env: &Env,
@@ -149,12 +189,14 @@ impl super::Check<'_> {
149189

150190
let mut env = env.clone();
151191
let (
192+
// ii_: the signature of the function as found in the impl item (ii)
152193
FnBoundData {
153194
input_tys: ii_input_tys,
154195
output_ty: ii_output_ty,
155196
where_clauses: ii_where_clauses,
156197
body: _,
157198
},
199+
// ti_: the signature of the function as found in the trait item (ti)
158200
FnBoundData {
159201
input_tys: ti_input_tys,
160202
output_ty: ti_output_ty,
@@ -169,6 +211,7 @@ impl super::Check<'_> {
169211
&ii_where_clauses,
170212
)?;
171213

214+
// Must have same number of arguments as declared in the trait
172215
if ii_input_tys.len() != ti_input_tys.len() {
173216
bail!(
174217
"impl has {} function arguments but trait has {} function arguments",

crates/formality-prove/src/prove/prove_eq.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,21 @@ judgment_fn! {
6464
(prove_eq(decls, env, assumptions, Variable::ExistentialVar(v), r) => c)
6565
)
6666

67+
// Example: we are trying to prove `x` (which equals `<SomeX<?V> as Iterator>::Item`)
68+
// is equal to some type `z`.
6769
(
70+
// Normalize `x` will find alternative "spellings" that it is equivalent to.
71+
// For example, if there is an impl like
72+
// `impl Iterator for SomeX<i32> { type Item = u32; ... }`
73+
// then `prove_normalize` would yield `(c, u32)` where `c` are any constraints
74+
// needed to show that it normalized (in this case, `c` would include the
75+
// substitution `?V = i32`).
6876
(prove_normalize(&decls, env, &assumptions, &x) => (c, y))
77+
78+
// Now that we know that `x` is equivalent to `y`, we try to prove
79+
// that `y` is equivalent to `z` (our original goal).
80+
// We do that with `prove_after` so that the constraints `c` are considered
81+
// (e.g., if `z` includes `?V`, it will be replaced with `i32`).
6982
(prove_after(&decls, c, &assumptions, eq(y, &z)) => c)
7083
----------------------------- ("normalize-l")
7184
(prove_eq(decls, env, assumptions, x, z) => c)

crates/formality-types/src/grammar/wc.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ pub enum WcData {
148148
#[grammar(for $v0)]
149149
ForAll(Binder<Wc>),
150150

151+
// An *implication* `if $v0 $v1` says "assuming v0 is true, v1 is true".
152+
// These are useful to express hypothetical syntax like
153+
// `for<'a: 'b, 'b>` or as part of an implied bounds scheme
154+
// where you might make the Rust syntax `for<'a, 'b> T: Something<'a, 'b>`
155+
// expand to `for<'a, 'b> if ('a: 'b) (T: Something<'a, 'b>)`
156+
// (given `trait Something<'a, 'b> where 'a: 'b`).
151157
#[grammar(if $v0 $v1)]
152158
Implies(Wcs, Wc),
153159
}

0 commit comments

Comments
 (0)