Parameter names
This is the design of #411 and of the warning for swapped names. How the checker does it is described in Names of positional parameters.
A positional parameter can be passed by name, like f(y: 2, x: 1) for
fn f(x: i32, y: i32), so a function type has names: fn(x: i32, y: i32).
This page is about what those names promise once a function is a value:
given to another function, kept in a variable or a struct, or returned.
The problem
Before, a name in a function type was a contract. A written type asked for
names, and only a function with those names fit it. apply(|x| x) worked,
because a lambda took the names of the type it was checked against, but
almost any other way of passing a function failed:
fn apply(k: fn(item: i32) -> i32) -> i32 = k(item: 1);
fn apply_v(k: fn(value: i32) -> i32) -> i32 = k(value: 1);
fn double(n: i32) -> i32 = n * 2;
fn mk() = |x: i32| x;
let f = |x: i32| x;
apply(f); // expected fn(item: i32) -> i32, found fn(x: i32) -> i32
apply(id(|x: i32| x)); // the same: the `T` of `id` hides what's expected
apply(mk()); // the same: the names of `mk` are settled with its type
apply(double); // expected fn(item: i32) -> i32, found fn(n: i32) -> i32
let g = |_: i32| 0;
apply(g); // a function without names doesn't fit one with names
apply(f) + apply_v(f); // `f` can't be both `item` and `value`
fn q(k) = k(item: 2) + apply(k);
// `k(item: 2)` made `item` keyword-only, which `apply` didn't take
None of these can go wrong at runtime. A name only says which position an
argument goes to, and the checker and MIR both find it from the type of the
callee at the call (place_args). Inside apply, k(item: 1) passes its
argument first, whatever the function it was given calls its parameter.
Monomorphization already drops the names of positional parameters
(MonoTy::Fn).
The rule
Only a call by name checks a name.
A name in a function type says how whoever holds a value of that type can call it by name. Where a function is given for a written function type, it takes the written type's names: the type renames its parameters. Every example above checks then. What still doesn't is a call by a name that the callee can't be called by:
let f = |x: i32| x;
f(y: 1); // error: no parameter y
let h: fn(i32) -> i32 = f;
h(x: 1); // error: the type of `h` has no names
Calls through a class already work like this: they use the class's names, whatever an impl calls its parameters.
Name slots as sets
Each positional parameter of a function type has a name slot in
Params::names. A slot stands for the set of names that the parameter can
be called by:
| Slot | Set | Examples |
|---|---|---|
| a written name | {x} | fn f(x: i32), |x| x, fn(x: i32) -> i32 |
| written without a name | ∅ | fn(i32) -> i32, |_| 0, |(a, b)| a |
| open | any name | the type of k in fn p(k) = apply(k) |
A lambda's slots are always the names of its patterns: it no longer takes those of the type it's checked against, since that type renames them anyway.
The rules:
- Names never make unification fail. Two function types unify by their shapes, like other types.
- Functions that flow into a variable give it the intersection of their
slots. Like
fandgintokinlet k = if c { f } else { g }, into the elements of an array, or into a type parameter. A written slot receives nothing: that's the renaming. - A call by name needs its name in the callee's slot. If the slot is open, the call claims it, and so it claims the open slots of the functions that flowed into it: what's given there must work when called by that name.
- A call by name of an unknown function makes positional parameters, one for each argument, in the order they're written, with open slots that the call then claims.
- At the end, a slot with one name has that name. Otherwise it has none.
Examples
These are cases in tests/checker/types/params.txt.
let f = |x: i32| x;
let g = |y: i32| y;
let k = if c { f } else { g }; // {x} ∩ {y} = ∅
k(1); // ok
k(x: 1); // error: no parameter x
let g2 = |x: i32| x + 1;
let k2 = if c { f } else { g2 }; // {x} ∩ {x} = {x}
k2(x: 1); // ok
A call narrows every open function that flowed into the callee:
fn pick(c, f, g) = { let k = if c { f } else { g }; k(x: 1) };
// pick: fn(c: Bool, f: fn(x: i32) -> i32, g: fn(x: i32) -> i32) -> i32
fn mix(c, f) = { let k = if c { f } else { |x: i32| x }; k(x: 1) }; // f: fn(x: i32) -> i32
fn bad(c, f) = { let k = if c { f } else { |x: i32| x }; k(y: 1) }; // error: no parameter y
A parameter without an annotation gets the names it's called by, whatever order the calls come in:
fn p(k) = apply(k) + k(item: 2); // p: fn(k: fn(item: i32) -> i32) -> i32
fn q(k) = k(item: 2) + apply(k); // the same
fn s(k) = apply(k) + k(x: 2); // s: fn(k: fn(x: i32) -> i32) -> i32
fn r(k) = k(item: 2); // r: fn(k: fn(item: i32) -> a) -> a
fn t(k) = apply(k); // t: fn(k: fn(i32) -> i32) -> i32
fn u(k) = { k(x: 1); k(y: 1) }; // error: no parameter y
t has no names, since nothing calls k by name: callers can give the same
functions either way. A slot can only have one name, so u fails.
Order
All the functions that flow into the result of an if, a match or an
array literal come before anything can call it. A function can only flow
into a variable after a call when it's assigned to it later. If it doesn't
have the name the call used, that's an error at the assignment:
let mut k = |x: i32| x;
k(x: 1);
k = |y: i32| y; // error: expected fn(x: i32) -> i32, found fn(y: i32) -> i32
Writes into containers
A function written into a mut array, struct or tuple flows into what's
stored there, however it's written:
fn put[T](xs: mut [T], x: T) = { xs[0] = x; };
let ks = mut [|x: i32| x];
ks[0] = |y: i32| y; // directly
put(ks, |y: i32| y); // through a generic function
let ys = ks;
ys[0] = |y: i32| y; // through another name for the same array
ks[0](x: 1); // error after any of them: no parameter x
Names normally flow one way, from what's read into what reads it. Inside a
mut value they flow both ways: both sides share the value, and either can
write to it, which is why mutable containers are invariant in languages
with subtyping. A place, like ks[0] or c.v, shares its slot with what
it denotes, so a write through it reaches the storage. A container that
isn't mut keeps one-way flows, since nothing can write to it: in
pick(fs, |z: i32| z) for fn pick[T](xs: [T], d: T) -> T, the elements
of fs keep their names. A function that was copied into a container
isn't changed by writes into it either.
What changed in the checker
NameEdgemeans "flows into": a variablelothat the slothiflowed into.Unifier::relate_namesrecords it only whenlois a variable, and never fails, except for a function that comes after a call it doesn't fit. The order of names, its solving and the tags that the solver used to report names that didn't fit are gone.- A slot's set is found when it's needed (
Unifier::name_set): the name a call claimed, the intersection of its sources, or any name. Unifier::require_nameclaims (Unifier::claim), andUnifier::slots_nowtells a call which slots are open.Unifier::settle_namesgives each slot its name.apply_generalgives a named argument for no name the first open slot after the positional arguments, and makes an unknown callee's parameters positional, with open slots.infer_lambdatakes its names from its patterns only.- A field access and an associated type, like
Index.Item, share their slots with what they denote (Solver::equal_shared), and a pattern takes the names of what it matches (Solver::equal_into). What's in amutarray, struct or tuple shares its slots on both sides (Unifier::share_if_mut), also when a variable copies the value (Unifier::bind_freshened).
Left to do: a clearer error for a function that comes after a call it doesn't fit, naming the call (#434).
Swapped names
Renaming makes one mistake easy to write:
fn copy(f: fn(src: Path, dst: Path)) = f(src: a, dst: b);
copy(|dst, src| ...); // `src` arrives in the lambda's `dst`
fn apply2(k: fn(a: i32, b: i32) -> i32) -> i32 = k(a: 1, b: 2);
fn w(k) = apply2(k) + k(b: 1, a: 2); // rule 4 makes k fn(b: i32, a: i32)
The checker warns when a function is given for a function type that has
one of its parameter names at another position (W0005). Once the names
are settled, it compares the names of the two function types at each
place where one was given for the other. Names that differ but aren't
swapped, like |x| x for fn(item: i32) -> i32, don't warn, and neither
do two functions that meet in a variable, like the branches of an if:
neither is given for the other.
What this doesn't cover
- Keyword-only parameters
(#432). Their names have
no position to fall back on, so they stay part of the type's structure
(
Params::keyword), and a lambda, which only has positional parameters, can't be checked against them. With rule 4,fn r(k) = k(item: 2)can no longer be given a function whoseitemis keyword-only; an annotation says so. - Adapting a named function with defaults
(#407), like giving
fn fmt(x: i32, *, width: i32 = 0)wherefn(i32) -> Stringis expected. - Checking impl methods against their class (#154), where a renamed parameter could get the same warning as a swapped one.
Prior art
- OCaml has labels in function types, and a function with other labels
doesn't fit:
~x:int -> intisn't~item:int -> int. That's what Bolt did before this design. - Swift took argument labels out of function types (SE-0111), so a function value can't be called by name at all. This design keeps names in the types, for calls through a value.
- Kotlin warns when an override renames a parameter, because calls by name through the supertype use the supertype's names. That's the same idea as the warning for swapped names.
- Python checks names at each call by keyword, at runtime, so passing a
function whose parameter isn't called
itemto code that calls it byitemfails when that call runs.