Skip to content

Commit af52685

Browse files
committed
Add documentation
1 parent 7a925de commit af52685

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

crates/formality-check/src/impls.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,44 @@ 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 these fns
135+
/// fn smell(&self); //
136+
/// }
137+
///
138+
/// struct Water;
139+
/// impl Potable for Water {
140+
/// fn drink(&self) {}
141+
/// fn smell(&self) {}
142+
/// }
143+
///
144+
/// struct Cup<L>;
145+
/// impl<L> Potable for Cup<L> // <-- env has `L` in scope
146+
/// where
147+
/// L: Potable, // <-- `impl_assumptions`
148+
/// {
149+
/// fn drink(&self) {} // <-- `ii_fn`
150+
/// fn smell(&self) {} // not currently being checked
151+
/// }
152+
/// ```
153+
///
154+
/// # Parameters
155+
///
156+
/// * `env`, the environment from the impl header
157+
/// * `impl_assumptions`, where-clauses declared on the impl
158+
/// * `trait_items`, items declared in the trait that is being implemented
159+
/// (we search this to find the corresponding declaration of the method)
160+
/// * `ii_fn`, the fn as declared in the impl
123161
fn check_fn_in_impl(
124162
&self,
125163
env: &Env,
@@ -149,12 +187,14 @@ impl super::Check<'_> {
149187

150188
let mut env = env.clone();
151189
let (
190+
// ii_: the signature of the function as found in the impl item (ii)
152191
FnBoundData {
153192
input_tys: ii_input_tys,
154193
output_ty: ii_output_ty,
155194
where_clauses: ii_where_clauses,
156195
body: _,
157196
},
197+
// ti_: the signature of the function as found in the trait item (ti)
158198
FnBoundData {
159199
input_tys: ti_input_tys,
160200
output_ty: ti_output_ty,
@@ -169,6 +209,7 @@ impl super::Check<'_> {
169209
&ii_where_clauses,
170210
)?;
171211

212+
// Must have same number of arguments as declared in the trait
172213
if ii_input_tys.len() != ti_input_tys.len() {
173214
bail!(
174215
"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)