Files
sakurac/tests/analyzer/checker.sakura
T
2026-12-06 13:52:57 -06:00

3565 lines
146 KiB
Plaintext

#! @file checker.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the tests of the checker: what each part of the language means, what
#! it refuses, and the notes it makes.
import std::testing;
module sakura::analyzer;
# Programs here are checked without the standard library, unless a test gives
# a small `std` of its own as another file.
@test def a_valid_program_checks_clean(t: &mut tester): void {
t.expects(checks("""
def add(a: i64, b: i64): i64 { return a + b; }
def main(): void {
val x := add(1, 2);
var y: i64 = x * 3;
y += 1;
if y > 4 { y = 0; } else { y--; }
while y < 10 { y++; }
for i in 0..3 { y += i; }
}
"""));
}
@test def locals_take_the_types_of_their_values(t: &mut tester): void {
t.expects(local_type("x", "def f(): void { val x := 1; }").span == "int");
t.expects(local_type("x", "def f(): void { val x := 1u; }").span == "uint");
t.expects(local_type("x", "def f(): void { val x := 2.5; }").span == "float");
t.expects(local_type("x", "def f(): void { val x := 2.5f; }").span == "f32");
t.expects(local_type("x", "def f(): void { val x := 'a'; }").span == "u32");
t.expects(local_type("x", "def f(): void { val x := true; }").span == "bool");
t.expects(local_type("x", "def f(): void { val x: u8 = 7; }").span == "u8");
t.expects(local_type("x", "def f(): void { val x := [1, 2, 3]; }").span == "int[3]");
t.expects(local_type("x", "def f(): void { var a := 1; val x := &mut a; }").span == "&mut int");
}
@test def names_must_be_declared(t: &mut tester): void {
t.expects(check_noted(1, 26, ec::undefined, "def f(): void { val x := y; }"));
t.expects(check_noted(1, 10, ec::undefined, "def f(): nope { }"));
}
@test def a_name_is_declared_once_where_it_is(t: &mut tester): void {
val made := check_program("", "val a := 1;\nval a := 2;");
t.expects(made.notes.length == 1);
t.expects(made.notes.length == 1 && made.notes.at(0).line == 2 && made.notes.at(0).code == ec::duplicate);
}
@test def values_convert_only_where_they_fit(t: &mut tester): void {
t.expects(check_noted(1, 30, ec::mismatch, "def f(): void { val x: i32 = true; }"));
t.expects(check_noted(1, 29, ec::overflow, "def f(): void { val x: u8 = 256; }"));
t.expects(check_noted(1, 30, ec::mismatch, "def f(): void { val x: i32 = 1u; }"));
t.expects(checks("def f(): void { val x: u64 = 7u; val y: i64 = x as i64; val z: u16 = 3; val w: u32 = z; }"));
t.expects(check_noted(1, 46, ec::mismatch, "def f(): void { val x: u32 = 3; val y: u16 = x; }"));
}
@test def operators_take_what_they_apply_to(t: &mut tester): void {
t.expects(check_noted(1, 42, ec::operands, "def f(a: i64, b: u64): void { val c := a + b; }"));
t.expects(checks("def f(a: i64, b: u64): bool { return a < b; }"));
t.expects(checks("def f(a: f64, b: u8): f64 { return a * 2 + b; }"));
t.expects(check_noted(1, 26, ec::overflow, "def f(): void { val c := -1u; }"));
t.expects(check_noted(1, 26, ec::operands, "def f(): void { val c := !5; }"));
t.expects(checks("def f(p: *u8, q: *u8): i64 { val r := p + 3; return q - p; }"));
}
@test def operators_call_the_methods_that_give_them(t: &mut tester): void {
# An operator on what is no number is a call of its method, on the left
# operand's type: the overload the right operand fits is chosen, a constant
# taking its parameter's type, and the operator gives what the method gives. A
# number on the left asks its own type, and a compound assignment writes what
# the method gives.
t.expects(checks("""
import std;
struct length { var ns: i64; }
def length::add(&this, other: &length): length { return length{ this.ns + other.ns }; }
def length::negate(&this): length { return length{ -this.ns }; }
def length::mul(&this, by: &i64): length { return length{ this.ns * by }; }
def length::mul(&this, by: &f64): length { return length{ this.ns }; }
def length::div(&this, by: &length): f64 { return 1.0; }
struct moment { var ns: i64; }
def moment::add(&this, by: &length): moment { return moment{ this.ns + by.ns }; }
def moment::sub(&this, by: &length): moment { return moment{ this.ns - by.ns }; }
def moment::sub(&this, since: &moment): length { return length{ this.ns - since.ns }; }
def i64::mul(&this, by: &length): length { return by * this; }
struct holder { var kept: length; }
var holder::all: length { get => this.kept; set => this.kept = value; }
def main(): void {
var began := moment{ 1 };
val waited: length = moment{ 5 } - began;
val later: moment = began + waited * 2 + -waited;
val scaled: length = 3 * waited * 0.5;
val ratio: f64 = waited / scaled;
began += waited;
began -= waited * 2;
var h := holder{ waited };
h.all += waited;
val still: i64 = 2 * 3 + 4.mul(&5);
}
""", mini_std));
# Generic code has an operator from any bound asking for its method, composed
# traits included, and what a bound composes gives its own operators too.
t.expects(checks("""
import std;
trait scales<S> : additive { def mul(&this, by: &S): self; }
def lerp<V, S>(a: V, b: V, t: S): V where V: scales<S> { return a == b ? a : a + (b - a) * t; }
def total<T>(a: T, b: T): T where T: addable { var t := a; t += b; return t + b; }
def flip<T>(a: T): T where T: negatable { return -a; }
struct vec2 : additive { var x: f64; }
def vec2::equals(&this, o: &vec2): bool { return this.x == o.x; }
def vec2::add(&this, o: &vec2): vec2 { return vec2{ this.x + o.x }; }
def vec2::sub(&this, o: &vec2): vec2 { return vec2{ this.x - o.x }; }
def vec2::negate(&this): vec2 { return vec2{ -this.x }; }
def vec2::mul(&this, by: &f64): vec2 { return vec2{ this.x * by }; }
def main(): void {
val a := lerp(1.0, 3.0, 0.5) + total(1, 2) as f64;
val b := flip(total(lerp(vec2{ 0.0 }, vec2{ 2.0 }, 0.5), vec2{ 1.0 }));
val c := 1 is additive && 1.5 is multiplicative && b is additive && !(b is numeric);
}
""", mini_std));
# Each operator needs its own method, taking the other operand by reference
# and nothing more, with no type parameters of its own. What `+=` calls gives
# the type it writes over. Comparisons and bitwise operators are as they were.
val wrong := """
import std;
struct p { var x: i64; }
def p::add(&this, o: &p): p { return p{ this.x + o.x }; }
def p::mul(&this, by: i64): p { return p{ this.x * by }; }
def p::div<T>(&this, by: &T): p { return p{ this.x }; }
struct q { var y: i64; }
def q::sub(&this, o: &q): p { return p{ this.y - o.y }; }
def q::rem(&this, by: &i32): q { return q{ this.y }; }
def q::rem(&this, by: &u8): q { return q{ this.y }; }
def both<T>(a: T, b: T): T where T: equatable { return a + b; }
def main(): void {
var a := p{ 1 };
var m := q{ 1 };
val c := a - a;
val d := -a;
val e := a * 2;
val f := a / a;
val g := a + 1;
val h := 2 + a;
val i := a & a;
val j := a << 1;
val k := a == a;
val n := m + a;
m -= m;
val o := m % 1;
}
""";
t.expects(note_count(wrong, mini_std) == 13);
t.expects(check_noted(10, 58, ec::operands, wrong, mini_std));
for line in 14u..24u {
t.expects(check_noted(line as u32, line == 15 ? 12 : 14, ec::operands, wrong, mini_std));
}
t.expects(check_noted(24, 5, ec::operands, wrong, mini_std));
t.expects(check_noted(25, 14, ec::ambiguous, wrong, mini_std));
}
@test def a_programs_methods_on_numbers_stand_beside_the_compilers(t: &mut tester): void {
# With `f64::mul` declared for a vector, `f64` is still `numeric`, and its own
# `mul` is still called where the argument is a number.
t.expects(checks("""
import std;
struct vec2 { var x: f64; }
def vec2::mul(&this, by: &f64): vec2 { return vec2{ this.x * by }; }
def f64::mul(&this, v: &vec2): vec2 { return v * this; }
def twice<T>(x: T): T where T: numeric { return x.mul(&x) + x; }
def main(): void {
val a: f64 = 2.5.mul(&2.0) + twice(1.5);
val b: vec2 = 2.0 * vec2{ 1.0 } * 2.0;
val c := 2.5 is numeric && 2.5 is multipliable;
}
""", mini_std));
t.expects(check_noted(5, 20, ec::mismatch, """
import std;
struct vec2 { var x: f64; }
def f64::mul(&this, v: &vec2): vec2 { return vec2{ this }; }
def main(): void {
val a := 2.5.mul(&true);
}
""", mini_std));
}
@test def members_a_module_declares_on_anothers_type_are_seen_where_it_is_imported(t: &mut tester): void {
# `core`'s members are seen everywhere, and a type's own wherever it is. One a
# module declares on another's type, or on a built-in one, is seen within
# that module and where a file imports it, whatever another file of the
# program imports.
val core := """
module std; module core;
pub struct lanes { pub var x: f64; }
pub def lanes::doubled(&this): f64 { return this.x * 2.0; }
pub def u8::twice(&this): u8 { return this * 2; }
""";
val math := """
module math;
pub def lanes::root(&this): f64 { return this.x; }
pub def u8::halved(&this): u8 { return this / 2; }
def within(v: lanes): f64 { return v.root(); }
""";
t.expects(checks("""
import math;
def f(v: lanes, b: u8): f64 { return v.root() + v.doubled() + (b.halved() + b.twice()) as f64; }
""", core, math));
t.expects(check_noted(1, 47, ec::member, """
def g(v: lanes): f64 { return v.doubled() + v.root(); }
""", core, math, """
import math;
def f(v: lanes): f64 { return v.root(); }
"""));
t.expects(check_noted(2, 33, ec::member, "\n def g(b: u8): u8 { return b.halved(); }\n", core, math));
}
@test def constants_fold_where_they_are_known(t: &mut tester): void {
t.expects(local_type("x", "const n := 2 * 3; def f(): void { var x: i64[n + 1]; }").span == "int[7]");
t.expects(check_noted(1, 33, ec::overflow, "def f(): void { val x: u8 = 200 + 100; }"));
t.expects(check_noted(1, 45, ec::overflow, "def f(): void { val x := 0x7FFFFFFFFFFFFFFF * 4; }"));
t.expects(check_noted(1, 33, ec::constant, "def f(n: u64): void { var x: u8[n]; }"));
}
@test def local_constants_are_worked_out_as_global_ones_are(t: &mut tester): void {
# A table is run while checking, as a global constant's is.
t.expects(checks(mini_std, """
import std;
def f(): f64 {
const reals: span<f64> = [-0.0, 2.5, 1e-7];
const whole: i64[2] = [1, -2];
const n := 3 * 4;
var a: i64[n];
return reals[1];
}
"""));
t.expects(check_noted(1, 38, ec::constant, "def f(n: i64): void { const m: i64 = n; }"));
}
@test def what_is_written_can_be_written(t: &mut tester): void {
t.expects(check_noted(1, 29, ec::assignable, "def f(): void { val a := 1; a = 2; }"));
t.expects(check_noted(2, 40, ec::assignable,
"struct p { val x: i64; var y: i64; }\ndef f(): void { var v := p{ x = 1 }; v.x = 2; v.y = 3; }"));
t.expects(check_noted(1, 36, ec::assignable, "def f(r: &i64): void { var a := 1; r = 2; }"));
t.expects(checks("def f(r: &mut i64): void { r = 2; r += 1; }"));
t.expects(checks("def f(): void { var a := 1; var r := &mut a; r = 5; var b := 2; r = &mut b; }"));
}
@test def an_index_is_the_reference_at_gives(t: &mut tester): void {
# `d[key]` is `d.at(key)`, the reference `at` gives, `&mut` where `d` can be
# written. A binding takes the value it reaches, and `&` or `&mut` written
# binds the reference.
val table := """
struct table { var items: i64[4]; }
def table::at(&this, key: u64): &i64 { return &this.items[key]; }
def table::at(&mut this, key: u64): &mut i64 { return &mut this.items[key]; }
def f(): void {
val read := table { items = [1, 2, 3, 4] };
var written := table { items = [1, 2, 3, 4] };
val r := read[0];
val w := written[1];
val c: i64 = written[2];
val again := &written[3];
val lent := &read[0];
val writes := &mut written[1];
}
""";
t.expects(local_type("r", table).span == "int");
t.expects(local_type("w", table).span == "int");
t.expects(local_type("c", table).span == "int");
t.expects(local_type("again", table).span == "&int");
t.expects(local_type("lent", table).span == "&int");
t.expects(local_type("writes", table).span == "&mut int");
# What gives only `at(&this)` is read through `&` however it is held, so
# nothing writes through it, nor takes `&mut` of it to write a copy.
val frozen := """
struct frozen { var items: i64[4]; }
def frozen::at(&this, key: u64): &i64 { return &this.items[key]; }
def f(): void {
var b := frozen { items = [1, 2, 3, 4] };
b[0] = 1;
b[1]++;
val m := &mut b[2];
val k := &mut b.at(3);
}
""";
t.expects(check_noted(5, 4, ec::assignable, frozen));
t.expects(check_noted(6, 4, ec::assignable, frozen));
t.expects(check_noted(7, 12, ec::assignable, frozen));
t.expects(check_noted(8, 12, ec::assignable, frozen));
# Through a bound, `x[k]` is read by the `at` a trait asks for, and written
# only where a trait the bound composes asks for the one that writes, as
# core's `assignable` adds to its `indexable`.
t.expects(checks("""
trait reads<V> { def at(&this, key: u64): &V; }
trait writes<V> : reads<V> { def at(&mut this, key: u64): &mut V; }
def read<C>(c: &C): i64 where C: writes<i64> { return c[0]; }
def write<C>(c: &mut C): i64 where C: writes<i64> { c[0] = 1; c[1] += 2; val k: i64 = c[2]; return k; }
"""));
t.expects(check_noted(4, 4, ec::assignable, """
trait reads<V> { def at(&this, key: u64): &V; }
trait writes<V> : reads<V> { def at(&mut this, key: u64): &mut V; }
def write<C>(c: &mut C): void where C: reads<i64> {
c[0] = 1;
}
"""));
}
@test def contracts_check_what_they_are_given(t: &mut tester): void {
t.expects(checks("""
def f(n: i64): i64 {
expects(n > 0);
ensures(r: r > old(n));
asserts(n != 3);
return n + 1;
}
def g(n: i64): void {
for i in 0..n {
ensures(i < n);
continue if i == 1;
}
}
"""));
# A condition is a `bool`, which changes nothing it names.
t.expects(check_noted(1, 31, ec::mismatch, "def f(n: i64): void { expects(n); }"));
t.expects(check_noted(1, 44, ec::invalid, "def f(n: i64): void { var k := n; asserts(k++ > 0); }"));
t.expects(check_noted(1, 46, ec::invalid, "def f(n: i64): void { var k := n; asserts((k = 2) == 2); }"));
# Only a function's own block names what it returns, of a function returning
# something.
t.expects(check_noted(1, 25, ec::invalid, "def f(): void { ensures(r: true); }"));
t.expects(check_noted(1, 41, ec::invalid, "def f(n: i64): i64 { if n > 0 { ensures(r: r > 0); return n; } return 1; }"));
# The name is the `ensures`'s own.
t.expects(check_noted(1, 50, ec::undefined, "def f(n: i64): i64 { ensures(r: r > 0); val k := r; return n; }"));
}
@test def variables_left_undefined_are_vars_of_what_closes_nothing(t: &mut tester): void {
t.expects(checks("""
def f(n: u64): u64 {
var buffer: u64[8] = undefined;
var at: *u8 = undefined;
buffer[0] = n;
return buffer[0];
}
def g(): u64 { val undefined: u64 = 2; return undefined + 1; }
"""));
# Only a `var` with a type written, of what has a zero value and closes
# nothing, and only a local.
t.expects(check_noted(1, 30, ec::invalid, "def f(): void { val n: u64 = undefined; }"));
t.expects(check_noted(1, 26, ec::invalid, "def f(): void { var n := undefined; }"));
t.expects(check_noted(1, 38, ec::invalid, "def f(x: &u64): void { var r: &u64 = undefined; }"));
t.expects(check_noted(1, 116, ec::invalid, mini_std,
"import std; struct held : owning { var n: i64; } def held::close(&mut this): void {} def f(): void { var h: held = undefined; }"));
t.expects(check_noted(1, 14, ec::invalid, "var g: u64 = undefined;"));
}
@test def functions_return_on_every_path(t: &mut tester): void {
t.expects(check_noted(1, 5, ec::control, "def f(c: bool): i64 { if c { return 1; } }"));
t.expects(checks("def f(c: bool): i64 { if c { return 1; } else { return 2; } }"));
t.expects(checks("def f(c: bool): i64 { if c { return 1; } return 2; }"));
t.expects(check_noted(1, 17, ec::control, "def f(): void { break; }"));
t.expects(check_noted(1, 24, ec::mismatch, "def f(): void { return 1; }"));
}
@test def calls_choose_the_overload_that_fits_best(t: &mut tester): void {
t.expects(checks("""
def show(n: i64): i64 { return 1; }
def show(n: f64): i64 { return 2; }
def use(): void { val a := show(1); val b := show(1.5); }
"""));
t.expects(check_noted(3, 19, ec::arguments, """
def show(n: i64): void {}
def show(n: f64): void {}
def use(): void { show(true); }
"""));
t.expects(check_noted(2, 19, ec::arguments, "def two(a: i64, b: i64): void {}\ndef use(): void { two(1); }"));
t.expects(checks("def pad(a: i64, b: i64 = 2): void {}\ndef use(): void { pad(1); pad(1, 3); }"));
t.expects(checks("def sum(xs: i64...): void {}\ndef use(): void { sum(); sum(1, 2, 3); }"));
}
@test def references_are_taken_for_reference_parameters(t: &mut tester): void {
t.expects(checks("def grow(n: &mut i64): void { n += 1; }\ndef use(): void { var a := 1; grow(a); grow(&mut a); }"));
t.expects(check_noted(2, 36, ec::assignable,
"def grow(n: &mut i64): void { n += 1; }\ndef use(): void { val a := 1; grow(a); }"));
t.expects(checks("def read(n: &u8): u8 { return n; }\ndef use(): void { val a := read(200); }"));
# A property's value is copied to be referred to, as any value is, but has no
# place for `&mut` to write.
val sized := "struct r { var w: i64; }\nval r::width: i64 { get => this.w; }\ndef read(n: &i64): i64 { return n; }\n";
t.expects(checks(sized, "def f(): i64 { var v := r{ w = 1 }; return read(&v.width); }"));
t.expects(check_noted(1, 47, ec::invalid, sized, "def f(): void { var v := r{ w = 1 }; val m := &mut v.width; }"));
}
@test def structs_are_made_of_their_fields(t: &mut tester): void {
val shape := "struct v { var x: f64; var y := 0.0; }\n";
t.expects(checks(shape, "def f(): void { val a := v{ x = 1.0 }; val b := v{1.0, 2.0}; val c := v{ y = 2.0, x = 1.0 }; }"));
t.expects(note_count("struct v { var x: f64; var y := 0.0; }\ndef f(): void { val a := v{ z = 1.0 }; }") == 1);
t.expects(check_noted(2, 38, ec::duplicate, "struct v { var x: f64; }\ndef f(): void { val a := v{ x = 1.0, x = 2.0 }; }"));
t.expects(check_noted(2, 27, ec::zero_value, "struct v { val r: &i64; }\ndef f(): void { val a := v{}; }"));
}
@test def methods_belong_to_their_types(t: &mut tester): void {
t.expects(checks("""
struct v { var x: f64; }
def v::length(&this): f64 { return this.x; }
def v::grow(&mut this): void { this.x += 1.0; }
def v::of(x: f64): v { return v{ x = x }; }
val v::zero := v{ x = 0.0 };
def u8::upper(&this): bool { return this >= 'A' && this <= 'Z'; }
def f(): void {
var a := v::of(1.0);
a.grow();
val b := a.length() + v::zero.x;
val d := (65 as u8).upper();
}
"""));
t.expects(check_noted(3, 40, ec::assignable, """
struct v { var x: f64; }
def v::grow(&mut this): void { this.x += 1.0; }
def f(): void { val a := v{ x = 1.0 }; a.grow(); }
"""));
}
@test def a_constant_settles_where_its_members_are_reached(t: &mut tester): void {
# A literal is a `strview` and a number an `int`, as either would be alone, and
# only interpolation builds a `string`.
val text := """
module std; module core;
pub struct span<T> { var data: *T; var count: u64; }
pub type strview = span<u8>;
pub val strview::length: u64 { get => this.count; }
pub def strview::trim(&this): strview { return this; }
pub struct string { var count: u64; }
pub val string::capacity: u64 { get => this.count; }
""";
val program := """
import std;
def f(): u64 {
val plain := "abc";
val built := $"abc{1}";
return "abc".trim().length + plain.length + built.capacity + 5.hash();
}
""";
t.expects(checks(program, text));
t.expects(check_program("plain", program, text).asked.span == "strview");
t.expects(check_noted(2, 29, ec::member, "import std;\ndef f(): u64 { return \"abc\".capacity; }", text));
}
@test def enums_have_members_and_values(t: &mut tester): void {
t.expects(checks("""
enum color: u8 { red, green = 4, blue }
def f(c: color): u8 {
val n: u8 = c;
val m := color::blue as u8 + 1;
return match c { red => 1, green => 2, blue => 3 };
}
"""));
t.expects(check_noted(1, 24, ec::duplicate, "enum e { a = 1, b = 0, c }"));
t.expects(check_noted(1, 23, ec::overflow, "enum e: u8 { a = 255, b }"));
t.expects(check_noted(3, 3, ec::arms, """
enum color { red, green, blue }
def f(c: color): void {
match c { red => {} green => {} }
}
"""));
}
@test def unions_hold_one_of_their_fields(t: &mut tester): void {
t.expects(checks("""
struct pair { var a: i64; var b: i64; }
union shape { var circle: f64; var rect: pair; }
def area(s: &shape): f64 {
match s {
circle(r) => return r;
rect(p) => return (p.a * p.b) as f64;
}
}
def f(): void {
var s: shape = 2.5;
s = pair{ a = 1, b = 2 };
val t := shape{ circle = 1.0 };
if s is circle(r) { val x := r + 1.0; }
if s is not rect(p) { return; }
val y := p.a;
}
"""));
}
@test def generics_are_checked_against_their_bounds(t: &mut tester): void {
t.expects(checks(mini_std, """
import std;
def max<T>(a: T, b: T): T where T: ordered { return a > b ? a : b; }
struct box<T> { var item: T; }
def box<T>::get(&this): T { return this.item; }
def f(): void {
val a := max(1, 2);
val b := max::<u8>(1, 2);
val c := box{ item = 5 }.get();
val d: box<f64> = box{ item = 1.5 };
}
"""));
t.expects(check_noted(5, 3, ec::bound, mini_std, """
import std;
struct p {}
def max<T>(a: T, b: T): T where T: ordered { return a; }
def f(): void {
max(p{}, p{});
}
"""));
t.expects(check_noted(2, 26, ec::generic, "def make<T>(): T { return make::<T>(); }\ndef f(): void { val x := make(); }"));
}
# A program keeping references past what they refer to, each line a warning or
# none, as the checker is sure of it.
const lifetimes_program: strview = """
import std;
struct point { var x: i64; }
struct holder { var p: &point; }
def returns_local(): &point { var p := point { x = 1 }; return &p; }
def returns_temp(): &point { return &point { x = 2 }; }
def returns_alias(): &point { var p := point { x = 3 }; val r := &p; return r; }
def returns_field(): &i64 { var p := point { x = 4 }; return &p.x; }
def returns_closure(): def(): i64 { var n: i64 = 5; return def(): i64 { return n; }; }
def returns_holder(): holder { var p := point { x = 6 }; return holder { p = &p }; }
def returns_by_value(p: point): &point { return &p; }
def into_param(h: &mut holder): void { var p := point { x = 7 }; h.p = &p; }
var global: maybe<&point> = nothing {};
def into_global(): void { var p := point { x = 8 }; global = &p; }
def into_outer(): void {
var r: maybe<&point> = nothing {};
{ var p := point { x = 9 }; r = &p; }
for i in 0..3 { var q := point { x = i }; r = &q; }
}
def temporaries(h: &mut holder): void {
h.p = &point { x = 10 };
val bound: &point = &point { x = 11 };
val held := holder { p = &point { x = 12 } };
}
def returns_param(p: &point): &point { return p; }
def returns_through(p: &point): &i64 { return &p.x; }
def returns_element(items: span<i64>): maybe<&i64> { for &x in items { return &x; } return nothing {}; }
def in_scope(): i64 {
var p := point { x = 13 };
val r := &p;
var h := holder { p = &p };
h.p = &p;
val f := def(): i64 { return p.x; };
val g := def(): &point { var inner := point { x = 1 }; return &p; };
return f() + r.x + h.p.x + g().x;
}
def lambda_own(): void { val g := def(): &point { var inner := point { x = 1 }; return &inner; }; }
@allow def allowed(): &point { var p := point { x = 14 }; return &p; }
""";
# What `allow` needs of std beside the mini one.
const allow_std: strview = "module std; module core;\npub trait attribute {}\npub struct allow : attribute {}\n";
@test def references_kept_past_what_they_refer_to_are_warned_of(t: &mut tester): void {
# A reference, view, or closure returned from the function declaring what it
# refers to, or written into a global, what a parameter refers to, or a local
# of a scope around the local's, outlives it, as one to a value its statement
# makes does where it is kept past the statement. These are warnings, so they
# stop nothing, and `@allow` quiets those of a function.
val made := check_program("", lifetimes_program, mini_std, allow_std);
var lines := vector::<u32>::new();
var others: u64 = 0;
for &each in made.notes {
if each.code == ec::lifetime {
lines.append(each.line);
} else {
others++;
}
}
val wanted: u32[14] = [4, 5, 6, 7, 8, 9, 10, 11, 13, 16, 17, 20, 22, 36];
t.expects(others == 0 && lines.length == 14);
for i in 0..14u {
t.expects(i < lines.length && lines[i] == wanted[i]);
}
}
# A program keeping anonymous functions past the locals they name through calls,
# each line a warning or none: a call keeps what it is given where its
# parameter is `@kept`, or of a type parameter its result's type holds.
const kept_program: strview = """
import std;
struct handle { var n: i64; }
struct holding<F> { var f: F; }
def hold<F>(f: F): holding<F> { return holding::<F> { f = f }; }
def pass<F>(f: F): F { return f; }
def start(@kept f: def(): i64): handle { return handle { n = 0 }; }
def try_start(@kept f: def(): i64): maybe<handle> { return handle { n = 0 }; }
def run(f: def(): i64): i64 { return f(); }
def through_holder(): holding<def(): i64> { var n: i64 = 1; return hold(def(): i64 { return n; }); }
def through_pass(): def(): i64 { var n: i64 = 2; return pass(def(): i64 { return n; }); }
def through_kept(): handle { var n: i64 = 3; return start(def(): i64 { return n; }); }
def through_var(): handle { var n: i64 = 4; var h := start(def(): i64 { return n; }); return h; }
def through_else(): handle { var n: i64 = 5; val h := try_start(def(): i64 { return n; }) else { return handle { n = 0 }; }; return h; }
def var_changed(): handle { var n: i64 = 6; var h := start(def(): i64 { return n; }); h = handle { n = 0 }; return h; }
def not_kept(): i64 { var n: i64 = 7; return run(def(): i64 { return n; }); }
def kept_in_scope(): i64 { var n: i64 = 8; val h := start(def(): i64 { return n; }); return h.n; }
def names_nothing(): handle { return start(def(): i64 { return 9; }); }
""";
# What `kept` needs of std beside the mini one.
const kept_std: strview = "module std; module core;\npub trait attribute {}\npub struct kept : attribute {}\n";
@test def anonymous_functions_kept_through_calls_are_warned_of(t: &mut tester): void {
# A call's result outliving the locals an anonymous function it keeps names is
# warned of, as the function itself returned would be: through a type
# parameter of the call's own its result holds, or a `@kept` parameter, and
# through a `val`, a `var` no assignment changes, and `else`.
val made := check_program("", kept_program, mini_std, kept_std);
var lines := vector::<u32>::new();
var others: u64 = 0;
for &each in made.notes {
if each.code == ec::lifetime {
lines.append(each.line);
} else {
others++;
}
}
val wanted: u32[5] = [9, 10, 11, 12, 13];
t.expects(others == 0 && lines.length == 5);
for i in 0..5u {
t.expects(i < lines.length && lines[i] == wanted[i]);
}
}
@test def a_librarys_declarations_are_checked_where_the_program_reaches_them(t: &mut tester): void {
# What the program's own files declare is checked whole; what a library's do
# is checked once the program reaches it, or where everything is asked for.
val library := "pub def used(): i64 { return 1; }\npub def unused(): i64 { return nope; }\npub struct kept { var n: i64 = nope; }\n";
t.expects(library_notes(false, "def f(): i64 { return used(); }", library) == 0);
t.expects(library_notes(false, "def f(): i64 { return unused(); }", library) == 1);
t.expects(library_notes(false, "def f(): void { val k: kept; }", library) == 1);
t.expects(library_notes(true, "def f(): i64 { return used(); }", library) == 2);
t.expects(library_notes(false, "def f(): i64 { return 1; }\ndef g(): i64 { return nope; }", library) == 1);
}
@test def references_satisfy_what_they_refer_to(t: &mut tester): void {
# A reference or a pointer satisfies what its referent does, so a generic given
# one borrows what it would otherwise own. An instance refers to an argument as
# its template did, so `take(to)` takes the reference itself where `S` is one.
t.expects(checks(mini_std, """
import std;
trait sink { def put(&mut this, n: i64): void; }
trait sized { def size(&this): i64; }
struct tally : sink, owning { var n: i64; }
def tally::put(&mut this, n: i64): void { this.n += n; }
def tally::size(&this): i64 { return this.n; }
def tally::close(&mut this): void {}
struct held<S> where S: sink { var to: S; }
def held<S>::put(&mut this, n: i64): void { this.to.put(n); val r: &mut sink = &mut this.to; r.put(n); }
def take<T>(x: &mut T): T { var made: T = x; return made; }
def hold<S>(to: S): held<S> where S: sink { return held::<S>{ to = take(to) }; }
def measure<T>(x: T): i64 where T: sized { return x.size(); }
def f(): void {
var t := tally{ n = 0 };
var a := hold(&mut t);
var b := hold(&mut (*((&mut t) as *mut tally)));
val dyn: &mut sink = &mut t;
var c := hold(dyn);
a.put(1); b.put(2); c.put(3);
val n := measure(&t) + measure(&(*((&t) as *tally)));
}
"""));
# Not where it only reads and the trait writes, where the trait asks for what is
# not called through it, or where the trait says what a value itself is.
val refused := """
import std;
trait sink { def put(&mut this, n: i64): void; }
trait made { def make(): i64; }
struct tally { var n: i64; }
def tally::put(&mut this, n: i64): void { this.n += n; }
def tally::make(): i64 { return 1; }
def tally::close(&mut this): void {}
def sinks<S>(to: S): void where S: sink {}
def makes<S>(to: S): void where S: made {}
def closes<S>(to: S): void where S: owning {}
def f(): void {
var t := tally{ n = 0 };
sinks(&t);
makes(&mut t);
closes(&mut t);
}
""";
t.expects(check_noted(13, 3, ec::bound, mini_std, refused));
t.expects(check_noted(14, 3, ec::bound, mini_std, refused));
t.expects(check_noted(15, 3, ec::bound, mini_std, refused));
}
@test def references_compare_as_what_they_refer_to(t: &mut tester): void {
# A reference is compared and ordered as what it refers to, so it takes a trait
# naming `self` where `self` is only taken by reference. A pointer is compared as
# its address, so its `equals` and `compare` are its own.
t.expects(checks(mini_std, """
import std;
trait near { def near(&this, other: &self): bool; }
struct tally : ordered, near { var n: i64; }
def tally::equals(&this, other: &tally): bool { return this.n == other.n; }
def tally::compare(&this, other: &tally): i64 { return this.n - other.n; }
def tally::near(&this, other: &tally): bool { return true; }
def least<T>(a: T, b: T): T where T: ordered { if b < a { return b; } return a; }
def same<T>(a: T, b: T): bool where T: equatable { return a == b && a.equals(&b); }
def nears<T>(a: T, b: T): bool where T: near { return a.near(&b); }
def f(): void {
var t := tally{ n = 1 };
var x: i64 = 2;
val l := least(&t, &t);
val s := same(&t, &mut t) && same(&x, &x) && nears(&t, &t);
val p := (&t) as *tally;
val q := p.equals(&p) && p.compare(&p) == 0 && same(p, p);
}
"""));
# Not where `self` is given or taken by value, nor through a pointer.
val refused := """
import std;
trait near { def near(&this, other: &self): bool; }
trait merges { def merge(&mut this, other: self): void; }
struct tally : near, merges { var n: i64; }
def tally::near(&this, other: &tally): bool { return true; }
def tally::merge(&mut this, other: tally): void {}
def adds<T>(a: T): void where T: numeric {}
def nears<T>(a: T): void where T: near {}
def merging<T>(a: T): void where T: merges {}
def f(): void {
var t := tally{ n = 1 };
var x: i64 = 1;
adds(&x);
nears((&t) as *tally);
merging(&mut t);
val p := (&t) as *tally;
val b := p.near(&p);
}
""";
t.expects(check_noted(13, 3, ec::bound, mini_std, refused));
t.expects(check_noted(14, 3, ec::bound, mini_std, refused));
t.expects(check_noted(15, 3, ec::bound, mini_std, refused));
t.expects(check_noted(17, 14, ec::member, mini_std, refused));
}
@test def pointers_are_addresses(t: &mut tester): void {
# A pointer is compared and ordered as an address and moves by whole elements, as
# does a type parameter bounded by `pointer`. Nothing of what it points at is
# reached through it, but by dereferencing it.
val shared := "module std; module core;\npub trait pointer {}";
t.expects(checks(mini_std, shared, """
import std;
def gap<P>(a: P, b: P): i64 where P: pointer { var at := a; at += 1; return b < at ? 0 : b - at; }
def none<P>(): P where P: pointer { var made: P; return made; }
struct node { var next: *node; }
def f(n: &node): void {
val p := n as *node;
val g := gap(p, p + 2);
val q := none::<*node>();
val next := (*p).next;
val also := p[0].next;
}
"""));
t.expects(check_noted(3, 36, ec::member, mini_std, shared, "import std;\nstruct node { var next: *node; }\ndef f(p: *node): void { val x := p.next; }"));
t.expects(check_noted(3, 17, ec::bound, mini_std, shared, "import std;\ndef take<P>(p: P): void where P: pointer {}\ndef f(): void { take(5); }"));
}
@test def traits_give_their_default_methods(t: &mut tester): void {
# A method a trait writes a body for is its default: what implements the trait
# may leave it out, and a type naming the trait has it as its own. What a pointer
# points at is reached by dereferencing it, through a reference to it too.
t.expects(checks(mini_std, """
import std;
trait sink { def put(&mut this, n: i64): void; def twice(&mut this, n: i64): void { this.put(n); this.put(n); } }
struct tally : sink { var n: i64; }
def tally::put(&mut this, n: i64): void { this.n += n; }
def fill<S>(s: &mut S): void where S: sink { s.twice(1); }
def f(): void {
var t := tally{ n = 0 };
t.twice(2);
fill(&mut t);
var p := (&mut t) as *mut tally;
(*p).twice(3);
val r: &mut *mut tally = &mut p;
(*r).put(4);
(*r).n = 5;
}
"""));
# A type satisfying the trait without naming it has its defaults only through a
# reference to the trait.
t.expects(check_noted(7, 5, ec::member, mini_std, """
import std;
trait sink { def put(&mut this, n: i64): void; def twice(&mut this, n: i64): void { this.put(n); } }
struct plain { var n: i64; }
def plain::put(&mut this, n: i64): void {}
def f(): void {
var p := plain{ n = 0 };
p.twice(1);
val s: &mut sink = &mut p;
s.twice(1);
}
"""));
}
@test def traits_have_functions_of_their_own(t: &mut tester): void {
# A function declared through a trait, outside it, is the trait's own: named
# through the trait, and generic as any function is, its type arguments written
# or inferred.
t.expects(checks("""
trait parses {}
def parses::id<T>(x: T): T { return x; }
def parses::one(): i64 { return 1; }
def f(): void {
val x: f64 = parses::id::<f64>(2.0);
val b: u8 = 3;
val y: u8 = parses::id(b);
val z: i64 = parses::one();
}
"""));
# What a trait asks for is declared within it, so a method taking `this`, an
# associated variable, or a nested type is not declared through it, and a
# function of its own takes a name it does not ask for.
val asking := """
trait made {
def make(): i64 { return 1; }
def pick<T>(x: T): T;
def size(&this): i64;
def build(): i64;
}
def made::grow(&this): i64 { return 1; }
val made::count: i64 = 1;
def made::size(): i64 { return 2; }
def made::own(): i64 { return 3; }
struct made::inner {}
struct thing {}
def f(): void {
val a := made::build();
val c := thing::own();
}
""";
t.expects(check_noted(7, 5, ec::invalid, asking));
t.expects(check_noted(8, 5, ec::invalid, asking));
t.expects(check_noted(9, 11, ec::duplicate, asking));
t.expects(check_noted(11, 8, ec::invalid, asking));
# What it asks for each implementer gives: a static method has no default,
# since nothing within the trait names the type implementing it, no method has
# type parameters, since each implementation is called through the trait, and
# a static one is called through a type parameter, not the trait's own name.
t.expects(check_noted(2, 7, ec::invalid, asking));
t.expects(check_noted(3, 12, ec::generic, asking));
t.expects(check_noted(14, 18, ec::invalid, asking));
# What implements the trait has none of its own functions.
t.expects(check_noted(15, 19, ec::member, asking));
}
@test def fallbacks_keep_one_owner(t: &mut tester): void {
# A value that closes has one owner. `else` hands over what a call made, but a
# variable tested keeps what it holds, a result keeps the error it holds, and a
# union keeps the field it holds, even where it is only a call's result, so
# none of them is copied out.
val taken := """
import std;
struct held : owning { var n: i64; }
def held::close(&mut this): void {}
def make(): result<held, held> { return result::<held, held> { value = held { n = 1 } }; }
def find(): maybe<held> { return held { n = 2 }; }
def f(): void {
val kept := make();
val a := kept else held { n = 3 };
val b := make() else (e) { return; };
val c := find() else kept.value;
val d := make().value;
val e := find() else held { n = 4 };
val g := make() else (_) { return; };
}
""";
t.expects(check_noted(8, 12, ec::owned, mini_std, taken));
t.expects(check_noted(9, 25, ec::owned, mini_std, taken));
t.expects(check_noted(10, 29, ec::owned, mini_std, taken));
t.expects(check_noted(11, 19, ec::owned, mini_std, taken));
t.expects(note_count(mini_std, taken) == 4);
}
@test def for_const_counts_through_ranges_known_while_checking(t: &mut tester): void {
# Each number is a constant in its copy of the body, which `if const` decides
# by, and a fixed array may take its length from.
t.expects(checks("""
const count: u64 = 3;
def f(): void {
for const i in 0..count + 1 {
if const i == 0 { continue; }
var held: u8[i];
}
}
"""));
# A range of an enum's members counts through each member.
t.expects(checks("""
enum gap { a, b = 4, c }
def f(): void {
for const m in gap::a..=gap::c {
if const m == gap::a { continue; }
var held: u8[m as u64];
}
}
"""));
# What the program running gives is not counted through, nor read by `if
# const`, and a range gives numbers, as many as `for const` unrolls.
val wrong := """
def f(n: u64): void {
val local := n + 1;
for const i in 0..n { }
for const &j in 0..3 { }
for const k in 0..2000 { }
if const local > 2 { }
for const m in 0..3 { m = 2; }
}
""";
t.expects(check_noted(3, 21, ec::constant, wrong));
t.expects(check_noted(4, 14, ec::invalid, wrong));
t.expects(check_noted(5, 13, ec::limit, wrong));
t.expects(check_noted(6, 12, ec::constant, wrong));
t.expects(check_noted(7, 25, ec::assignable, wrong));
}
@test def loops_own_what_iterators_give_by_value(t: &mut tester): void {
# A loop variable takes the value of each element. An element an iterator gives
# by value is the loop's own, which `for x` takes. One it gives by reference
# belongs to what it walks, as an array's elements do, so `for x` copies it,
# which a value that closes refuses, and `for &x` refers to it.
t.expects(checks(mini_std, """
import std;
struct held : owning { var n: i64; }
def held::close(&mut this): void {}
struct gives : iter<held> { var n: i64; }
def gives::next(&mut this): maybe<held> { return nothing {}; }
struct lends : iter<&held> { var n: i64; }
def lends::next(&mut this): maybe<&held> { return nothing {}; }
def f(): void {
for x in (gives{ n = 0 }) { val n := x.n; }
for &x in (lends{ n = 0 }) { val n := x.n; }
for &x in (lends{ n = 0 }) { val r: &held = x; }
var hs: held[2];
for &h in hs { val r: &held = h; }
}
"""));
val copied := """
import std;
struct held : owning { var n: i64; }
def held::close(&mut this): void {}
struct lends : iter<&held> { var n: i64; }
def lends::next(&mut this): maybe<&held> { return nothing {}; }
def f(): void {
for x in (lends{ n = 0 }) { val n := x.n; }
}
""";
t.expects(check_noted(7, 7, ec::owned, copied, mini_std));
}
@test def union_fields_that_close_are_taken_from_what_a_call_made(t: &mut tester): void {
# A union a call made owns nothing past the statement, so naming a field of it
# that closes takes the field over, and an arm used as a value may give it. A
# variable keeps what it holds, so its field is named by `&` or `&mut`. `_`
# names nothing.
t.expects(checks(mini_std, """
import std;
struct held : owning { var n: i64; }
def held::close(&mut this): void {}
def make(): maybe<held> { return nothing {}; }
def f(): held {
while make() is some(h) { break; }
match make() {
some(h) => {}
else => {}
}
val given: held = match make() { some(h) => h, else => held {} };
val kept: maybe<held> = held {};
if kept is some(&h) {}
val tested := kept is some(_) || false;
if make() is not some(h) { return held {}; }
return h;
}
"""));
t.expects(check_noted(6, 19, ec::owned, mini_std, """
import std;
struct held : owning { var n: i64; }
def held::close(&mut this): void {}
def f(): void {
val kept: maybe<held> = held {};
if kept is some(h) {}
}
"""));
}
@test def names_are_given_where_their_test_holds(t: &mut tester): void {
# Wherever the test is known to hold: the right of `&&`, the right of `||` for
# `is not`, through `!`, the branches of an `if` or a `while`, and below a guard,
# with or without an `else`.
t.expects(checks(mini_std, """
import std;
def f(a: maybe<i64>, b: maybe<i64>): i64 {
if a is some(x) && x > 3 { return x; }
if a is some(x) && b is some(y) { return x + y; }
while a is some(x) && b is not some(y) { return x; }
val big := b is some(v) && v > 3;
if a is not some(x) || x < 2 { return 0; } else { val y := x; }
if !(b is some(z)) { return x; } else { return z; }
}
"""));
t.expects(check_noted(3, 22, ec::undefined, mini_std, """
import std;
def f(a: maybe<i64>): i64 {
if a is some(x) || x > 3 { return x; }
return 0;
}
"""));
t.expects(check_noted(4, 10, ec::undefined, mini_std, """
import std;
def f(a: maybe<i64>): i64 {
if a is some(x) { return 0; }
return x;
}
"""));
}
@test def bindings_of_values_can_be_changed(t: &mut tester): void {
# A binding holding a value of its own can be changed and moved from, as a `var`
# can, and so can the variable of a loop.
t.expects(checks(mini_std, """
import std;
struct handle : owning { var id: i64; }
def handle::close(&mut this): void { this.id = 0; }
def make(): maybe<handle> { return handle { id = 1 }; }
def f(): void {
if make() is some(h) { h.id = 2; }
match make() {
some(h) => h.id = 2;
else => {}
}
for n in 0..3 { n += 1; }
}
"""));
}
@test def branches_copy_nothing_that_closes_out_of_its_place(t: &mut tester): void {
t.expects(check_noted(6, 19, ec::owned, mini_std, """
import std;
struct held : owning { var n: i64; }
def held::close(&mut this): void {}
def f(flag: bool): void {
val a := held {};
val b := flag ? a : held {};
}
"""));
t.expects(check_noted(6, 50, ec::owned, mini_std, """
import std;
struct held : owning { var n: i64; }
def held::close(&mut this): void {}
def f(flag: bool): void {
val a := held {};
val c := match flag { true => held {}, else => a };
}
"""));
}
@test def conditions_and_branches_read_through_references(t: &mut tester): void {
t.expects(checks(mini_std, """
import std;
def f(flag: &mut bool, seen: &bool, count: &u32): u32 {
if flag { }
while seen { break; }
val n := flag ? count : 0;
return seen ? n : count;
}
"""));
}
@test def lockfree_is_decided_by_the_compiler(t: &mut tester): void {
# Integers, `bool`, enums, and pointers are words or less every processor reads
# and writes whole, and a type parameter is where its bounds say so.
t.expects(checks(mini_std, """
import std;
enum level: u8 { low, high }
def take<T>(value: T): T where T: lockfree { return value; }
def pass<T>(value: T): T where T: lockfree { return take::<T>(value); }
def f(p: *mut i64): void {
val a := take::<u8>(1);
val b := take::<i64>(-1);
val c := take::<bool>(true);
val d := take::<level>(level::high);
val e := take::<*mut i64>(p);
val g := pass::<u32>(7);
}
"""));
# Nothing else is: not a struct, a real, a reference, or a parameter its bounds
# do not say is.
val made := check_program("", """
import std;
struct pair { var a: i64; var b: i64; }
def take<T>(value: T): T where T: lockfree { return value; }
def bare<T>(value: T): T { return take::<T>(value); }
def f(r: &i64): void {
val a := take::<pair>(pair { a = 1, b = 2 });
val b := take::<f64>(1.0);
val c := take::<&i64>(r);
}
""", mini_std);
t.expects(made.notes.length == 4);
for &each in made.notes {
t.expects(each.code == ec::bound);
}
}
@test def zeroable_is_decided_by_the_compiler(t: &mut tester): void {
t.expects(checks(mini_std, """
import std;
enum lonely: u8 { one = 1 }
enum level: u8 { low, high }
struct holds { var r: &i64; }
struct plain { var n: i64 = 5; var l: level; var p: *i64; }
def make<T>(): T where T: zeroable { var made: T; return made; }
def passes<T>(): T where T: zeroable { return make::<T>(); }
def zeroes<T>(): bool { if const T is zeroable { var made: T; return true; } return false; }
def guarded<T>(): bool { if const T is not zeroable { return false; } var made: T; return true; }
def counted<T>(): T where T: integer { var made: T; return make::<T>() + made; }
def f(): void {
val p := passes::<plain[2]>();
val a := zeroes::<holds>() || zeroes::<lonely>() || guarded::<&i64>();
val c := counted::<u8>();
}
"""));
# A type argument without a zero value is reported where it is given, with
# what in it has none.
val made := check_program("", """
import std;
enum lonely: u8 { one = 1 }
struct holds { var r: &i64; }
struct outer { var inner: holds; }
def make<T>(): T where T: zeroable { var made: T; return made; }
def bare<T>(): T { var made: T; return made; }
def passes<T>(): T { return make::<T>(); }
def f(): void {
val a := make::<lonely>();
val b := make::<outer>();
val c := make::<&i64>();
}
""", mini_std);
t.expects(made.notes.length == 5);
t.expects(made.notes.length == 5 && made.notes.at(0).line == 6 && made.notes.at(0).code == ec::zero_value);
t.expects(made.notes.length == 5 && made.notes.at(1).line == 7 && made.notes.at(1).code == ec::bound);
t.expects(made.notes.length == 5 && made.notes.at(2).detail.span
== "`lonely` is not `zeroable`, which `T` of `make` requires: no member of `lonely` has the value 0.");
t.expects(made.notes.length == 5 && made.notes.at(3).detail.span
== "`outer` is not `zeroable`, which `T` of `make` requires: its field `inner.r` holds `&int`, which has no zero value.");
t.expects(made.notes.length == 5 && made.notes.at(4).detail.span
== "`&int` is not `zeroable`, which `T` of `make` requires: a reference has no zero value.");
# `equatable` and `ordinal` give no zero value: a struct holding a reference
# may be equatable, and an enum without a member of value 0 is ordinal.
t.expects(check_noted(2, 44, ec::zero_value, mini_std, "import std;\ndef equal<T>(): T where T: equatable { var made: T; return made; }"));
t.expects(check_noted(2, 48, ec::zero_value, mini_std, "import std;\ndef ordered_one<T>(): T where T: ordered { var made: T; return made; }"));
t.expects(check_noted(2, 48, ec::zero_value, mini_std, "import std;\ndef ordinal_one<T>(): T where T: ordinal { var made: T; return made; }"));
}
@test def traits_nest_in_types(t: &mut tester): void {
t.expects(checks(mini_std, """
import std;
def shown<T>(x: &T): i64 where T: ns::shows { return x.show(); }
struct point : ns::shows, ns::ranked { var x: i64; }
def point::show(&this): i64 { return this.x; }
def point::compare(&this, other: &point): i64 { return this.x - other.x; }
struct ns {}
trait ns::shows { def show(&this): i64; }
trait ns::ranked { def compare(&this, other: &self): i64; }
struct bag<T> { var items: T[2]; }
trait bag<T>::visitor { def visit(&mut this, item: &T): void; }
def bag<T>::walk(&this, v: &mut bag<T>::visitor): void { v.visit(&this.items[0]); }
struct summer : bag<i64>::visitor { var total: i64; }
def summer::visit(&mut this, item: &i64): void { this.total += item; }
def use(p: &point): i64 {
val r: &ns::shows = p;
val b := bag::<i64>{ items = [1, 2] };
var s := summer{ total = 0 };
b.walk(&mut s);
return shown(p) + r.show();
}
"""));
t.expects(check_noted(4, 8, ec::bound, mini_std, """
import std;
struct ns {}
trait ns::shows { def show(&this): i64; }
struct quiet : ns::shows { var x: i64; }
def need<T>(x: &T): void where T: ns::nope {}
"""));
t.expects(check_noted(5, 39, ec::undefined, mini_std, """
import std;
struct ns {}
trait ns::shows { def show(&this): i64; }
struct quiet : ns::shows { var x: i64; }
def need<T>(x: &T): void where T: ns::nope {}
"""));
}
@test def members_reached_through_bounds_need_no_pub(t: &mut tester): void {
val lib := """
import std;
module lib;
pub trait sink { def text(&mut this, value: span<u8>): void; }
pub def through_bound<W>(out: &mut W): void where W: sink { out.text("bound"); }
pub struct quiet { pub var said: u64; }
def quiet::text(&mut this, value: span<u8>): void { this.said += value.length; }
""";
t.expects(checks("""
import std;
import lib;
struct shout { var said: u64; }
def shout::text(&mut this, value: span<u8>): void { this.said += value.length; }
def f(): void {
var s := shout{ said = 0 };
var q := quiet{ said = 0 };
through_bound(&mut s);
through_bound(&mut q);
}
""", mini_std, lib));
# Naming the method from another module still needs `pub`.
t.expects(check_noted(3, 47, ec::hidden, """
import std;
import lib;
def f(): void { var q := quiet{ said = 0 }; q.text("x"); }
""", mini_std, lib));
}
@test def a_module_comes_before_a_type_of_its_name(t: &mut tester): void {
# `kit::make` names the module where `kit` is also a type the module declares,
# as `serial::encode` does.
t.expects(checks("""
import std;
import kit;
def f(): i64 { val made: kit::count = kit::make(); return made + kit::make(); }
""", mini_std, """
module kit;
pub struct kit { var n: i64; }
pub type count = i64;
pub def make(): count { return 1; }
"""));
}
@test def a_type_comes_after_a_module_of_its_name_declaring_nothing_named(t: &mut tester): void {
# `shape::make` and `shape::corner` name the type where the module `shape`
# declares neither, and `shape::helper` the module's function.
t.expects(checks("""
import std;
import shape;
def f(): u64 {
val s := shape::make(3);
val c: shape::corner = shape::corner{ angle = 90 };
return s.sides + c.angle + shape::most + shape::helper();
}
""", mini_std, """
module shape;
pub struct shape { pub var sides: u64; }
pub struct shape::corner { pub var angle: u64; }
pub def shape::make(sides: u64): shape { return shape{ sides = sides }; }
pub const shape::most: u64 = 12;
pub def helper(): u64 { return 7; }
"""));
}
@test def a_guard_that_ends_in_compile_error_narrows_what_follows(t: &mut tester): void {
val reports := """
module std; module core;
pub trait attribute {}
pub struct intrinsic : attribute {}
@intrinsic pub def compile_error(text: span<u8>): void;
""";
# What follows the guard has the bound it tests, given its arguments.
t.expects(checks(mini_std, reports, """
import std;
trait shows<T> { def show(&this): T; }
def first<K>(k: &K): i64 {
if const !(K is shows<i64>) {
compile_error("A key shows itself.");
}
return k.show();
}
def second<K>(k: &K): i64 {
if const K is shows<i64> {
return k.show();
}
return 0;
}
"""));
}
@test def an_alias_satisfies_traits_through_what_it_aliases(t: &mut tester): void {
# `boxed` is `box<u8>`, whose `equals` takes a `&box<u8>`, which is a `&boxed`.
t.expects(checks(mini_std, """
import std;
struct box<T> { var item: T; }
def box<T>::equals(&this, other: &box<T>): bool { return true; }
type boxed = box<u8>;
def same<T>(a: &T, b: &T): bool where T: equatable { return a == b; }
def f(x: &boxed): bool { return same(x, x); }
"""));
}
@test def a_slice_writes_its_elements_however_it_is_held(t: &mut tester): void {
# A slice made where it is indexed is no place, but what it views is written
# through it, as through a `*mut` pointer.
t.expects(checks(mini_std, """
module std; module core;
pub struct slice<T> { var data: *mut T; var count: u64; }
pub def slice<T>::over(data: *mut T, count: u64): slice<T> { return slice { data = data, count = count }; }
pub def slice<T>::at(&this, index: u64): &T { return &this.data[index]; }
pub def slice<T>::at(&mut this, index: u64): &mut T { return &mut this.data[index]; }
""", """
import std;
def f(p: *mut i64): &mut i64 {
slice::<i64>::over(p, 1)[0] = 5;
return &mut slice::<i64>::over(p, 1)[0];
}
"""));
}
@test def each_instance_is_checked_with_its_types(t: &mut tester): void {
val made := check_program("", """
import std;
def small<T>(x: T): T where T: numeric { return x + 300; }
def f(): void {
val a := small(1);
val b := small::<u8>(1);
}
""", mini_std);
t.expects(made.notes.length == 1);
t.expects(made.notes.length == 1 && made.notes.at(0).code == ec::overflow);
# A note made in an instance says where what is written made it, and refers to
# that place.
t.expects(made.notes.length == 1 && made.notes.at(0).instance.span == "In `small<u8>`, made at test.sakura:5:12");
t.expects(made.notes.length == 1 && made.notes.at(0).related == 5);
}
@test def types_satisfy_traits_by_their_shape(t: &mut tester): void {
t.expects(checks("""
trait shown { def show(&this): i64; }
struct a {}
def a::show(&this): i64 { return 1; }
def print(s: &shown): i64 { return s.show(); }
def f(): void { val x := print(a{}); }
"""));
t.expects(check_noted(2, 8, ec::bound, """
trait shown { def show(&this): i64; }
struct b : shown {}
def b::show(&this): bool { return true; }
"""));
t.expects(checks(mini_std, """
import std;
struct v : equatable { var x: i64; }
def v::equals(&this, other: &v): bool { return this.x == other.x; }
def f(a: v, b: v): bool { return a == b; }
"""));
t.expects(check_noted(3, 36, ec::operands, mini_std, """
import std;
struct v { var x: i64; }
def f(a: v, b: v): bool { return a == b; }
"""));
}
@test def if_const_proves_bounds_for_its_branch(t: &mut tester): void {
t.expects(checks(mini_std, """
import std;
def finish<T>(x: &mut T): void {
if const T is owning {
x.close();
}
}
def tell<T>(x: &T): i64 {
return 0 if const T is not owning;
return 1;
}
"""));
}
@test def loops_walk_ranges_arrays_and_iterators(t: &mut tester): void {
t.expects(local_type("x", "def f(): void { for i in 0..3u { val x := i; } }").span == "uint");
t.expects(local_type("x", "def f(a: i64[4]): void { for &e in a { val x := &e; } }").span == "&int");
t.expects(local_type("x", "def f(a: i64[4]): void { for e in a { val x := e; } }").span == "int");
t.expects(local_type("x", "def f(a: i64[4]): void { for i, e in a { val x := i; } }").span == "uint");
t.expects(checks(mini_std, "import std;\ndef f(s: span<u8>): void { for b in s { val c: u8 = b; } }"));
t.expects(check_noted(1, 32, ec::member, "def f(n: i64): void { for x in n { } }"));
}
@test def fallbacks_take_what_a_result_or_maybe_holds(t: &mut tester): void {
t.expects(checks(mini_std, """
import std;
def parse(): result<i64, u8> { return 5; }
def find(): maybe<i64> { return 1; }
def f(): i64 {
val a := parse() else 0;
val b := find() else { return 0; };
val c := parse() else (e) { return e as i64; };
return a + b + c;
}
"""));
t.expects(check_noted(3, 38, ec::control, mini_std, """
import std;
def find(): maybe<i64> { return 1; }
def f(): void { val b := find() else { val x := 1; }; }
"""));
}
@test def modules_see_their_own_and_what_is_pub(t: &mut tester): void {
val geometry := """
module geometry;
pub struct rect { pub var w: f64; }
def hidden(): i64 { return 1; }
pub def area(r: &rect): f64 { return r.w; }
""";
t.expects(checks("import geometry;\ndef f(): void { val r := rect{ w = 1.0 }; val a := area(r); }", geometry));
t.expects(checks("def f(): void { val r := geometry::rect{ w = 1.0 }; val a := geometry::area(r); }", geometry));
t.expects(check_noted(2, 26, ec::hidden, "import geometry;\ndef f(): void { val h := hidden(); }", geometry));
t.expects(check_noted(1, 1, ec::undefined, "import nowhere;"));
t.expects(checks("""
module inner;
def helper(): i64 { return 1; }
module inner::deeper;
def use(): i64 { return helper(); }
"""));
}
@test def no_module_is_within_the_program(t: &mut tester): void {
# The files declaring no module are the program, which holds no module, so
# what a module names comes from it, the modules holding it, and its imports.
val tools := "module tools;\npub def scratch(): u32 { return 1; }";
t.expects(checks(
"import tools;\nmodule user;\ndef f(): u32 { return scratch(); }",
"struct scratch { var at: u32; }",
tools));
t.expects(checks(
"import tools;\nmodule user::inner;\ndef f(): u32 { return scratch(); }",
"struct scratch { var at: u32; }",
tools));
t.expects(check_noted(2, 23, ec::undefined,
"module user;\ndef f(): u32 { return rooted(); }",
"def rooted(): u32 { return 2; }"));
}
@test def fields_are_private_unless_pub(t: &mut tester): void {
val geometry := """
module geometry;
pub struct point { pub var x: i64; var y: i64; }
pub union shape { pub var dot: point; var blob: u8; }
pub struct base { pub var w: i64; }
pub struct framed { pub var b: ...base; }
pub struct hidden { var b: ...base; }
pub def origin(): point { return point { x = 0, y = 0 }; }
pub def y_of(p: &point): i64 { return p.y; }
module geometry::inner;
def deeper(p: &point): i64 { return p.y; }
""";
t.expects(checks("""
import geometry;
def f(): i64 {
val p := point { x = 1 };
val q := origin();
val r := framed {};
return p.x + q.x + r.w + y_of(&q);
}
""", geometry));
t.expects(check_noted(2, 44, ec::hidden, "import geometry;\ndef f(): i64 { val p := origin(); return p.y; }", geometry));
t.expects(check_noted(2, 41, ec::hidden, "import geometry;\ndef f(): void { val p := point { x = 1, y = 2 }; }", geometry));
t.expects(check_noted(2, 37, ec::hidden, "import geometry;\ndef f(): void { val p := point { 1, 2 }; }", geometry));
t.expects(check_noted(2, 37, ec::hidden, "import geometry;\ndef f(s: shape): bool { return s is blob; }", geometry));
t.expects(check_noted(2, 35, ec::hidden, "import geometry;\ndef f(h: &hidden): i64 { return h.w; }", geometry));
}
@test def a_field_and_a_type_through_its_type_share_a_name(t: &mut tester): void {
t.expects(checks("""
struct token { var kind: token::kind; }
enum token::kind { word, number }
def f(t: &token): bool { return t.kind == token::kind::word; }
"""));
t.expects(checks("""
enum token::kind { word, number }
struct token { val kind: token::kind; }
def f(): token { return token { kind = token::kind::number }; }
"""));
# Only a field shares its name with a type, since a method is reached through
# a value as a field is.
t.expects(check_noted(3, 13, ec::duplicate, """
struct token { var at: u64; }
def token::kind(&this): u64 { return this.at; }
enum token::kind { word }
"""));
t.expects(check_noted(3, 37, ec::member, """
struct token { var at: u64; }
enum token::kind { word }
def f(t: &token): void { val k := t.kind; }
"""));
}
@test def a_type_names_its_own_without_them(t: &mut tester): void {
t.expects(checks("""
struct token { var genus: kind; var at: span; }
enum token::kind { word, number }
struct token::span { var start: u64; var end: u64; }
val token::none: kind = kind::word;
def token::matches(&this, k: kind): bool { return this.genus == k && k != none; }
def token::kind::is_word(&this): bool { return this == word; }
def token::width(&this): u64 { val s: span = this.at; return s.end - s.start; }
def f(t: &token): bool { return t.matches(token::kind::number) && t.genus.is_word(); }
"""));
# A generic type gives what is nested in it its arguments.
t.expects(local_type("n", """
struct list<T> { var head: *node; }
struct list<T>::node { var value: T; }
def list<T>::first(&this): T { val n: node = this.head[0]; return n.value; }
def f(l: &list<i64>): i64 { return l.first(); }
""").span == "node<T>");
# Only types are meant in a type, so an enum's member `u8` leaves `u8` the type.
t.expects(checks("""
enum width { u8, u16 }
def width::bytes(&this): u8 { val one: u8 = 1; return this == u8 ? one : one + one; }
"""));
# A type's functions are named through it, since they are often named for the
# free functions they implement.
t.expects(check_noted(3, 36, ec::undefined, """
struct token { var at: u64; }
def token::make(): token { return token {}; }
def token::again(): token { return make(); }
"""));
}
@test def a_name_a_type_and_its_module_both_give_is_ambiguous(t: &mut tester): void {
t.expects(check_noted(2, 27, ec::ambiguous, """
enum kind { a }
struct token { var genus: kind; }
enum token::kind { b }
"""));
t.expects(check_noted(3, 21, ec::ambiguous, """
module shapes;
enum kind { a }
struct box { var k: kind; }
enum box::kind { b }
"""));
t.expects(checks("""
module shapes;
enum kind { a }
struct box { var k: box::kind; var other: shapes::kind; }
enum box::kind { b }
"""));
# A name of the module shadows those imports give, and a type's does too.
t.expects(checks("""
import lib;
struct token { var genus: kind; }
enum token::kind { b }
def f(t: &token): bool { return t.genus == token::kind::b; }
""", "module lib;\npub enum kind { a }"));
}
@test def when_keeps_what_the_target_takes(t: &mut tester): void {
t.expects(checks("""
when os == linux { def home(): i64 { return 1; } }
else { def home(): bool { return true; } }
def f(): void { val h: i64 = home(); }
"""));
t.expects(check_noted(1, 12, ec::invalid, "when os == lynx { }"));
t.expects(checks("def f(): i64 { when backend == vm { return 1; } else { return nope; } }"));
}
@test def when_compares_the_compilers_version(t: &mut tester): void {
# The compiler is 1.0 or later, and never 4000, and a branch not taken is
# not checked, wherever the `when` is.
t.expects(checks("""
when compiler >= 1.0 { def era(): i64 { return 1; } }
else { def era(): bool { return true; } }
when compiler > 4000.0.0 || compiler < 0.1 { def f(): i64 { return nope; } }
def g(): void { val e: i64 = era(); }
"""));
t.expects(checks(
"def f(): i64 { when compiler <= 0.9 { return nope; } else { return 1; } }"
));
# Whatever its version is, it is that version.
val own := compiler_version.text();
var text := string::from("when compiler == ", nothing {});
text.append(own.span);
text.append(" { def f(): i64 { return 1; } }\ndef g(): i64 { return f(); }");
t.expects(checks(text.span));
# Only `compiler` is ordered, and only by a version.
t.expects(check_noted(1, 9, ec::invalid, "when os >= linux { }"));
t.expects(check_noted(1, 18, ec::invalid, "when compiler == linux { }"));
t.expects(check_noted(1, 18, ec::invalid, "when compiler >= 1.2.3.4 { }"));
t.expects(check_noted(1, 6, ec::invalid, "when flavor == sweet { }"));
}
@test def a_core_for_another_release_is_refused(t: &mut tester): void {
t.expects(check_noted(2, 11, ec::invalid, "def f(): void {}",
"module core::toolchain;\npub const release := \"0.0.1\";"));
# One for this release is read, and one that says nothing is let be.
val own := compiler_version.text();
var text := string::from("module core::toolchain;\n", nothing {});
text.append("pub const release := \"");
text.append(own.span);
text.append("\";");
t.expects(checks("def f(): void {}", text.span));
t.expects(checks(
"def f(): void {}",
"module core;\npub const other: u32 = 1;"
));
}
@test def versions_are_read_as_when_writes_them(t: &mut tester): void {
t.expects(
read_version("1.2.3") is some(v) && v == version_number { 1, 2, 3 }
);
t.expects(read_version("1.2") is some(v) && v == version_number { 1, 2, 0 });
t.expects(read_version("7") is some(v) && v == version_number { 7, 0, 0 });
t.expects(read_version("1.10") is some(v) && v > version_number { 1, 9, 0 });
t.expects(read_version("") is none);
t.expects(read_version("1.") is none);
t.expects(read_version(".1") is none);
t.expects(read_version("1.2.3.4") is none);
t.expects(read_version("1.x") is none);
t.expects(read_version("99999999999") is none);
t.expects(compiler_compares(tk::moreeq, &version_number { 0, 1, 0 }));
t.expects(!compiler_compares(tk::less, &version_number { 0, 1, 0 }));
t.expects(compiler_compares(tk::equals, &compiler_version));
}
#! A standard library giving what C's declarations use: `cfn`.
const c_std: strview = """
module std; module core;
pub trait attribute {}
pub struct intrinsic : attribute {}
@intrinsic pub struct cfn<F> {}
""";
@test def extern_sections_declare_what_c_gives(t: &mut tester): void {
# A section's functions with no body, structs, and unions are C's, and the
# rest is the module's own. A function of the program is given where C's
# address of one is expected.
t.expects(checks("""
import std;
extern import "libc.so.6" when os == linux;
def compare(a: *u8, b: *u8): i32 { return 0; }
def sorted(xs: *mut u8): i32 {
c::qsort(xs, 1, 4, compare);
val order: cfn<def(*u8, *u8): i32> = compare;
val q := c::div(7, 2);
return c::twice(q.quot);
}
extern module c when os == linux;
pub struct quotient { pub var quot: i32; pub var rem: i32; }
pub union bits { pub var whole: u32; pub var real: f32; }
pub def div(a: i32, b: i32): quotient;
pub def qsort(base: *mut u8, count: u64, size: u64, order: cfn<def(*u8, *u8): i32>): void;
pub def twice(x: i32): i32 { return x * 2; }
""", c_std));
# A section says which systems it is for.
t.expects(check_noted(1, 1, ec::invalid, "extern module c;\npub def f(): void;", c_std));
t.expects(check_noted(1, 1, ec::invalid, "extern module c when backend == native;\npub def f(): void;", c_std));
# A function with no body is C's only in a section, and never generic there.
t.expects(check_noted(1, 5, ec::invalid, "def f(): void;", c_std));
t.expects(check_noted(2, 9, ec::invalid, "extern module c when os == linux;\npub def f<T>(x: T): T;", c_std));
# A section's struct holds only what crosses to C.
t.expects(check_noted(2, 24, ec::invalid, "extern module c when os == linux;\npub struct s { pub var r: &i32; }", c_std));
# A C union holds no tag, so `is` can't ask which field it holds.
t.expects(check_noted(1, 39, ec::invalid,
"def f(b: c::bits): bool { return b is whole; }\nextern module c when os == linux;\npub union bits { pub var whole: u32; pub var real: f32; }", c_std));
# C calls back a function by its name, not a lambda.
t.expects(check_noted(2, 47, ec::invalid,
"import std;\ndef f(xs: *mut u8): void { c::qsort(xs, 1, 4, def(a: *u8, b: *u8): i32 => 0); }\nextern module c when os == linux;\npub def qsort(base: *mut u8, count: u64, size: u64, order: cfn<def(*u8, *u8): i32>): void;", c_std));
# A library nothing in its file uses is warned of.
t.expects(check_noted(1, 1, ec::invalid, "extern import \"libm.so.6\";\ndef f(): void {}", c_std));
# The attributes C was declared with before say where it is declared now, once.
t.expects(check_noted(1, 1, ec::invalid, "@foreign def f(): void {}", c_std));
t.expects(note_count("@foreign def f(): void {}", c_std) == 1);
}
@test def declarations_that_depend_on_themselves_are_reported(t: &mut tester): void {
t.expects(check_noted(1, 7, ec::cycle, "const a: i64 = b;\nconst b: i64 = a;"));
t.expects(check_noted(1, 6, ec::cycle, "type loop = loop;"));
t.expects(checks("enum e { a = 1, b = e::a }"));
}
@test def notes_point_at_what_they_relate_to(t: &mut tester): void {
val made := check_program("", "val a := 1;\n\nval a := 2;");
t.expects(made.notes.length == 1 && made.notes.at(0).line == 3 && made.notes.at(0).related == 1);
}
@test def what_is_not_checked_yet_is_counted(t: &mut tester): void {
val made := check_program("", "def f(): void { val x: type_of(f) = 1; }");
t.expects(made.notes.length == 0 && made.skipped == 1);
}
@test def traits_that_name_self_are_only_bounds(t: &mut tester): void {
t.expects(check_noted(2, 11, ec::not_type, "trait cloneable { def clone(&this): self; }\ndef f(c: &cloneable): void {}"));
t.expects(checks("trait shown { def show(&this): i64; }\ndef f(s: &shown): i64 { return s.show(); }"));
t.expects(check_noted(2, 24, ec::not_type, "trait shown { def show(&this): i64; }\ndef f(): void { val s: shown; }"));
# `void` is a value's type, unlike a trait, of its one value.
t.expects(checks("def f(): void { val v: void; }"));
}
@test def defaults_of_a_bound_are_compiled_for_each_type(t: &mut tester): void {
# A trait only a bound gives defaults compiled for each type implementing it,
# generic ones too, and a default may ask more of the trait's parameters or of
# `self`, which only the types meeting it have.
t.expects(checks(mini_std, ranges_std, """
import std;
trait shelf<T> {
def get(&this, at: u64): T;
def first(&this): T { return this.get(0); }
def pick<U, F>(&this, at: u64, f: F): U where F: invokable<def(T): U> { return f(this.get(at)); }
def total(&this): T where T: numeric { return this.get(0) + this.get(1); }
def tagged(&this): u64 where self: labeled { return this.label(); }
}
trait labeled { def label(&this): u64; }
struct trio : shelf<i64>, labeled { var items: i64[3]; }
def trio::get(&this, at: u64): i64 { return this.items[at]; }
def trio::label(&this): u64 { return 3; }
def sum<S>(s: &S): i64 where S: shelf<i64> { return s.first() + s.total(); }
def f(): void {
val t := trio { items = [1, 2, 3] };
val a: i64 = t.first() + t.total() + sum(&t);
val b: u64 = t.pick(1, def(x) => x as u64) + t.tagged();
}
"""));
# A type not meeting what a default asks does not have it, and the trait,
# having one compiled for each type, is no type of a reference.
val unmet := """
import std;
trait shelf<T> {
def get(&this, at: u64): T;
def total(&this): T where T: numeric { return this.get(0) + this.get(1); }
def tagged(&this): u64 where self: labeled { return this.label(); }
}
trait labeled { def label(&this): u64; }
struct words : shelf<strview> { var a: strview; }
def words::get(&this, at: u64): strview { return this.a; }
def use(s: &shelf<i64>): void {}
def main(): void {
val w := words { a = "x" };
val a := w.total();
val b := w.tagged();
}
""";
t.expects(check_noted(10, 13, ec::not_type, mini_std, ranges_std, unmet));
t.expects(check_noted(13, 14, ec::bound, mini_std, ranges_std, unmet));
t.expects(check_noted(14, 14, ec::bound, mini_std, ranges_std, unmet));
}
@test def methods_exist_where_their_bounds_hold(t: &mut tester): void {
# A method bounding its type's parameters belongs to the instances meeting
# them, which then satisfy a trait asking for it.
t.expects(checks(mini_std, ranges_std, """
import std;
struct holder<T> { var held: T; }
def holder<T>::doubled(&this): T where T: numeric { return this.held + this.held; }
def holder<T>::get(&this): T { return this.held; }
trait doubles<T> { def doubled(&this): T; }
def twice<H>(h: &H): i64 where H: doubles<i64> { return h.doubled(); }
def f(): void {
val h := holder::<i64> { held = 2 };
val a: i64 = h.doubled() + twice(&h);
val w := holder::<strview> { held = "w" };
val b: strview = w.get();
}
"""));
val unmet := """
import std;
struct holder<T> { var held: T; }
def holder<T>::doubled(&this): T where T: numeric { return this.held + this.held; }
trait doubles<T> { def doubled(&this): T; }
def twice<H>(h: &H): strview where H: doubles<strview> { return h.doubled(); }
def f(): void {
val w := holder::<strview> { held = "w" };
val a := w.doubled();
val b := twice(&w);
}
""";
t.expects(check_noted(8, 14, ec::bound, mini_std, ranges_std, unmet));
t.expects(check_noted(9, 12, ec::bound, mini_std, ranges_std, unmet));
}
@test def lambdas_take_their_types_from_bounds(t: &mut tester): void {
# An anonymous function given for a parameter bounded by `invokable` takes
# the types it leaves out from the bound.
t.expects(checks(mini_std, ranges_std, """
import std;
def apply<F>(x: i64, f: F): i64 where F: invokable<def(i64): i64> { return f(x); }
def both<F>(f: F): bool where F: invokable<def(&strview, u8): bool> { val s: strview = "ab"; return f(&s, 2); }
def f(): void {
val a: i64 = apply(3, def(x) => x + 1);
val b: bool = both(def(s, n) => s.length == n as u64);
val c: i64 = apply(4, def(x) { return x * 2; });
}
"""));
# Where nothing gives a parameter's type, it is written.
val untyped := """
import std;
def apply<F>(x: i64, f: F): i64 where F: invokable<def(i64): i64> { return f(x); }
def keep<F>(f: F): void {}
def f(): void {
keep(def(x) => x);
val a := apply(3, def(x) => x.length);
val b := apply(3, def(x, y) => x);
}
""";
t.expects(check_noted(5, 12, ec::mismatch, mini_std, ranges_std, untyped));
t.expects(check_noted(6, 33, ec::member, mini_std, ranges_std, untyped));
t.expects(check_noted(7, 28, ec::mismatch, mini_std, ranges_std, untyped));
# Within a generic, its own parameters are known, so the bound gives them too.
t.expects(checks(mini_std, ranges_std, """
import std;
struct box<T> { var held: T; }
def apply<U, F>(b: &box<U>, f: F): U where F: invokable<def(&box<U>): U> { return f(b); }
def f<T>(b: &box<T>): T { return apply(b, def(x) => x.held); }
"""));
}
@test def properties_and_methods_of_generic_types_are_values(t: &mut tester): void {
# A property named as a value is its `get`, a function of `&this`. A method or
# property of a generic type named without its type arguments takes them from
# the function type expected, or a parameter's `invokable` bound.
t.expects(checks(mini_std, ranges_std, """
import std;
struct rect { var w: f32; var h: f32; }
val rect::area: f32 { get => this.w * this.h; }
var rect::width: f32 { get => this.w; set => this.w = value; }
struct box<T> { var held: T; }
val box<T>::inner: T { get => this.held; }
def box<T>::peek(&this): T { return this.held; }
def apply<F>(r: &rect, f: F): f32 where F: invokable<def(&rect): f32> { return f(r); }
def look<F>(b: &box<i64>, f: F): i64 where F: invokable<def(&box<i64>): i64> { return f(b); }
def f(): i64 {
val r := rect { w = 3, h = 4 };
val area: def(&rect): f32 = rect::area;
val width: def(&rect): f32 = rect::width;
val a := apply(&r, rect::area) + area(&r) + width(&r);
val b := box::<i64> { held = 5 };
val inner: def(&box<i64>): i64 = box::inner;
val peek: def(&box<i64>): i64 = box::peek;
return look(&b, box::inner) + look(&b, box::peek) + look(&b, box::<i64>::inner) + inner(&b) + peek(&b);
}
def g<T>(b: &box<T>): T {
val inner: def(&box<T>): T = box::inner;
return inner(b);
}
"""));
# A generic function whose type arguments neither the function type expected
# nor anything else gives is named with them.
val unwritten := """
import std;
struct maker {}
def maker::make<T>(x: i64): i64 { return x; }
def apply<F>(f: F): i64 where F: invokable<def(i64): i64> { return f(1); }
def f(): i64 { return apply(maker::make); }
""";
t.expects(check_noted(5, 29, ec::generic, mini_std, ranges_std, unwritten));
}
@test def if_const_names_the_type_a_parameter_is(t: &mut tester): void {
# Where `if const` says a type parameter is a type, the branch names that type
# by the parameter's name, and is checked with the parameter that type, a value
# of it one of that type, within the generic and in each instance taking it.
t.expects(checks(mini_std, ranges_std, """
import std;
struct box<T> { var held: T; }
def pick<T>(x: T): i64 {
if const T is i64 {
val n: i64 = x;
val b: box<T> = box::<i64> { held = n };
return b.held;
}
return 0 if const T is not bool;
return 1;
}
def f(): i64 { return pick(5) + pick(true) + pick(2u); }
"""));
# What is wrong there is noted within the generic, and by the instances taking
# it.
val wrong := """
import std;
def pick<T>(x: T): i64 {
if const T is bool {
val n: i64 = x;
return n;
}
return 0;
}
def f(): i64 { return pick(true) + pick(3); }
""";
val made := check_program("", mini_std, ranges_std, wrong);
t.expects(made.notes.length == 2 && made.notes.at(0).line == 4 && made.notes.at(0).code == ec::mismatch
&& made.notes.at(1).line == 4 && made.notes.at(1).code == ec::mismatch);
}
@test def references_never_stack_and_bindings_take_values(t: &mut tester): void {
# `&` of a reference is that reference, wherever it is held, and `&mut` of one
# writes where it does. Inference, like a binding, takes the value a reference
# reaches unless `&` is written, and a reference to a trait is taken as it is.
val lending := """
struct point { var x: i64; }
def larger<T>(a: T, b: T): T { return a; }
def f(r: &mut point, s: &point, v: &mut i64[4]): void {
val again := &r;
val writes := &mut r;
val read := &s;
val copied := r;
val big := larger(v[0], v[1]);
val lent := &larger(&v[0], &v[1]);
val given := larger(&v[0], &v[1]);
}
""";
t.expects(local_type("again", lending).span == "&point");
t.expects(local_type("writes", lending).span == "&mut point");
t.expects(local_type("read", lending).span == "&point");
t.expects(local_type("copied", lending).span == "point");
t.expects(local_type("big", lending).span == "int");
t.expects(local_type("lent", lending).span == "&int");
t.expects(local_type("given", lending).span == "int");
t.expects(check_noted(1, 33, ec::assignable, "def f(s: &i64): void { val w := &mut s; }"));
t.expects(checks(mini_std, """
import std;
trait shown { def show(&this): i64; }
struct dot : shown { var n: i64; }
def dot::show(&this): i64 { return this.n; }
def f(d: &mut dot, m: maybe<&mut shown>): i64 {
val as_trait: &shown = d;
val kept := as_trait;
if m is some(held) { return held.show() + kept.show(); }
return kept.show();
}
"""));
# A pattern copies what a field holding a reference reaches, and `&` or
# `&mut` names the reference.
val payload := """
union slot { var none: u8; var some: &mut i64; }
def f(m: slot): void {
if m is some(n) { val copy := n; }
if m is some(&mut w) { val lent := &mut w; }
}
""";
t.expects(local_type("copy", payload).span == "int");
t.expects(local_type("lent", payload).span == "&mut int");
# A reference written as one is a pointer where one is expected, but nothing
# else is, and a pointer keeps no value alive past its statement.
t.expects(checks("def look(p: *i64): i64 { return p[0]; }\ndef f(): void { var x: i64 = 1; val p: *mut i64 = &mut x; val q: *i64 = &x; val n := look(&x) + look(&2); }"));
t.expects(check_noted(2, 38, ec::mismatch, "def look(p: *i64): i64 { return p[0]; }\ndef f(): void { var x: i64 = 1; look(x); }"));
t.expects(check_noted(1, 31, ec::invalid, "def f(): void { val p: *i64 = &10; }"));
t.expects(check_noted(1, 51, ec::mismatch, "def f(): void { val x: i64 = 1; val p: *mut i64 = &x; }"));
}
@test def named_functions_are_called_as_the_type_asks(t: &mut tester): void {
# A named function given where a function type it doesn't match is expected
# is called as that type asks: each parameter given as a call gives it, the
# rest taking their defaults, and among overloads the one such a call chooses.
t.expects(checks("""
def doubled(x: &i64): i64 { return x * 2; }
def tripled(x: i64): i64 { return x * 3; }
def scaled(x: i64, by: i64 = 10): i64 { return x * by; }
def shown(x: &i64): i64 { return x; }
def shown(x: &f64): i64 { return 0; }
def f(): void {
val a: def(i64): i64 = doubled;
val b: def(&i64): i64 = tripled;
val d: def(i64): i64 = scaled;
val e: def(i64): i64 = shown;
}
"""));
# A value that closes is never copied out of what a reference reaches, so a
# function taking one is not called with what a `&` parameter gives.
val owned := """
import std;
struct held : owning { var n: i64; }
def held::close(&mut this): void {}
def keep(h: held): i64 { return h.n; }
def f(): void { val g: def(&held): i64 = keep; }
""";
t.expects(check_noted(5, 42, ec::mismatch, owned, mini_std));
# Between `&` and `&mut` parameters, what can be written is lent to `&mut`.
val picked := """
def pick(v: &mut i64): i64 { return 1; }
def pick(v: &i64): bool { return true; }
def f(): void { var a: i64 = 1; val b: i64 = 2; val x := pick(a); val y := pick(b); }
""";
t.expects(local_type("x", picked).span == "int");
t.expects(local_type("y", picked).span == "bool");
}
@test def ranges_are_values(t: &mut tester): void {
# `a..b` and `a..=b` are std's `range` of the integers their ends are, or of
# what is expected of them, as an `iter<u64>` bound expects a
# `range<u64>`. A local of the name is no type.
t.expects(checks(mini_std, ranges_std, """
import std;
def total(r: range<u64>): u64 { var t: u64 = 0; for x in r { t += x; } return t; }
def count<I>(it: I): u64 where I: iter<u64> { return 0; }
def f(): void {
val r := 0..10;
val s: range<u8> = 1..=255;
val a: u64 = total(1..=4) + count(2..6);
for i in 0..=3 { val j: int = i; }
val range: maybe<i64> = 3;
if range is some(x) { val y: i64 = x; }
}
"""));
# Members of one enum make a range of its members, and a type parameter
# bounded by `integer` or `ordinal` a range of what it is.
t.expects(checks(mini_std, ranges_std, """
import std;
enum color { red, green = 4, blue }
def walk<T>(a: T, b: T): void where T: integer { for i in a..b { val j: T = i; } val r := a..=b; }
def step<T>(a: T, b: T): void where T: ordinal { for i in a..b { val j: T = i; } val r: range<T> = a..=b; }
def f(): void {
val r := color::red..=color::blue;
val s: range<color> = color::red..color::green;
for c in color::red..color::blue { val d: color = c; }
walk(1, 4);
step(color::green, color::blue);
}
"""));
val mixed := """
import std;
enum color { red, green }
enum level { low, high }
def f(): void {
val a := color::red..level::high;
val b := color::red..1;
}
""";
t.expects(check_noted(5, 12, ec::operands, mini_std, ranges_std, mixed));
t.expects(check_noted(6, 12, ec::operands, mini_std, ranges_std, mixed));
val wrong := """
import std;
def f(): void {
val a := 1.5..2.5;
val s: strview = "x";
val b := 0..s;
for i in 0..=true {}
val c: range<u8> = 0..300;
}
""";
t.expects(check_noted(3, 12, ec::operands, mini_std, ranges_std, wrong));
t.expects(check_noted(5, 12, ec::operands, mini_std, ranges_std, wrong));
t.expects(check_noted(6, 12, ec::operands, mini_std, ranges_std, wrong));
t.expects(check_noted(7, 25, ec::overflow, mini_std, ranges_std, wrong));
}
@test def a_struct_holds_itself_only_through_a_pointer(t: &mut tester): void {
t.expects(check_noted(1, 8, ec::recursive, "struct node { var next: node; }"));
t.expects(check_noted(1, 7, ec::recursive, "union tree { var leaf: i64; var both: tree[2]; }"));
t.expects(checks("struct node { var next: *node; }\nunion tree { var leaf: i64; var both: &tree; }"));
}
@test def overloads_differ_in_their_parameters(t: &mut tester): void {
t.expects(check_noted(2, 5, ec::duplicate, "def f(a: i64): void {}\ndef f(b: i64): void {}"));
t.expects(checks("def f(a: i64): void {}\ndef f(a: u8): void {}\nstruct v {}\ndef v::g(&this): void {}\ndef v::g(&mut this): void {}"));
}
@test def what_is_nodiscard_is_handled(t: &mut tester): void {
val shared := "module std; module core;\npub trait attribute {}\npub struct nodiscard : attribute {}";
t.expects(check_noted(3, 21, ec::unhandled, shared, "import std;\n@nodiscard def must(): i64 { return 1; }\ndef f(): void { must(); }"));
t.expects(checks(shared, "import std;\n@nodiscard def must(): i64 { return 1; }\ndef f(): void { _ = must(); val x := must(); }"));
}
@test def what_is_inline_has_a_body_to_copy(t: &mut tester): void {
val shared := """
module std; module core;
pub trait attribute {}
pub enum target : u32 { functions = 1, methods = 2, structs = 32, requirements = 0x400, properties = 0x1000, all = 0xFFFF }
pub struct usage : attribute { pub val on: u32 = target::all; pub val many: bool = false; }
pub struct intrinsic : attribute {}
@usage{ on = target::functions | target::methods | target::properties }
pub struct inline : attribute {}
""";
# A function, a method, and a property, whose accessors it marks, are copied
# where they are called.
val program := """
import std;
struct p { var x: i64; }
@inline def twice(x: i64): i64 { return x * 2; }
@inline def p::sum(&this): i64 { return this.x; }
@inline val p::doubled: i64 { get => this.x * 2; }
def f(q: p): i64 { return twice(q.sum()) + q.doubled; }
""";
t.expects(checks(shared, program));
# What C or the compiler gives has no body to copy, and neither a struct nor
# what a trait asks for is a function of its own.
t.expects(check_noted(2, 1, ec::invalid, shared, "import std;\n@inline @intrinsic def given(): u64;"));
t.expects(check_noted(2, 1, ec::invalid, shared, "extern module c when os == linux;\n@inline pub def puts(text: *u8): i32;"));
t.expects(checks(shared, "extern module c when os == linux;\n@inline pub def twice(x: i32): i32 { return x * 2; }"));
t.expects(check_noted(2, 1, ec::invalid, shared, "import std;\n@inline struct s {}"));
t.expects(check_noted(2, 15, ec::invalid, shared, "import std;\ntrait shape { @inline def area(&this): i64; }"));
}
@test def a_value_that_closes_has_one_owner(t: &mut tester): void {
val shared := """
import std;
struct file : owning { var fd: i64; }
def file::close(&mut this): void {}
def take(f: file): void {}
def open(): file { val made := file{ fd = 1 }; return made; }
""";
t.expects(checks(mini_std, shared, "import std;\ndef f(): void { var a := open(); take(open()); take(move_out(&mut a)); }\ndef move_out(f: &mut file): file { return file{ fd = f.fd }; }"));
t.expects(check_noted(2, 43, ec::owned, mini_std, shared, "import std;\ndef f(): void { var a := open(); val b := a; }"));
t.expects(check_noted(2, 39, ec::owned, mini_std, shared, "import std;\ndef f(): void { var a := open(); take(a); }"));
# What a reference refers to still has its owner, and so does a field. A `return`
# hands over a local it names, and what a pointer points at.
t.expects(check_noted(2, 30, ec::owned, mini_std, shared, "import std;\ndef f(r: &file): void { take(r); }"));
t.expects(check_noted(2, 32, ec::owned, mini_std, shared, "import std;\ndef f(r: &file): file { return r; }"));
t.expects(check_noted(3, 42, ec::owned, mini_std, shared, "import std;\nstruct box { var f: file; }\ndef box::take(&this): file { return this.f; }"));
t.expects(checks(mini_std, shared, "import std;\ndef f(p: *file): file { return p[0]; }\ndef g(r: &file): &file { return r; }"));
}
@test def a_loop_warns_where_its_body_may_move_what_it_walks(t: &mut tester): void {
# What a loop walks is changed in place, an element or a field of it, and read
# through `&this`, as anything is. A fixed array holds its elements where it
# is, so nothing moves them.
t.expects(checks("""
struct bag { var items: i64[3]; var n: i64; }
def bag::iter(&this): span<i64>::iterator { return span::<i64> { data = (&this.items) as *i64, count = 3 }.iter(); }
def bag::look(&this): i64 { return this.n; }
def f(): void {
var b := bag {};
for x in b {
b.items[0] = x;
b.n += 1;
val l := b.look();
}
var xs: i64[3];
for x in xs { xs[0] = x; xs = [1, 2, 3]; }
for &mut x in xs { x += 1; }
}
""", mini_std));
# A method taking `&mut this` of it, or assigning it whole, may move what the
# loop reads next, which is warned of, never refused, as it is through what a
# property gives of it.
val moving := """
struct bag { var items: i64[3]; var n: i64; }
def bag::iter(&this): span<i64>::iterator { return this.all.iter(); }
val bag::all: span<i64> { get => span::<i64> { data = (&this.items) as *i64, count = 3 }; }
def bag::grow(&mut this): void { this.n++; }
def f(): void {
var b := bag {};
for x in b {
b.grow();
}
for x in b.all {
b = bag {};
}
}
""";
t.expects(check_noted(8, 5, ec::lifetime, moving, mini_std));
t.expects(check_noted(11, 5, ec::lifetime, moving, mini_std));
}
@test def attributes_are_checked_where_they_are_written(t: &mut tester): void {
val shared := """
module std; module core;
pub trait attribute {}
pub enum target : u32 { functions = 1, methods = 2, constants = 4, structs = 32, all = 0xFFFF }
pub struct usage : attribute { pub val on: u32 = target::all; pub val many: bool = false; }
""";
val program := """
import std;
@usage{ on = target::functions | target::constants }
struct test : attribute { val skip: bool = false; }
struct plain { var x: i64 = 0; }
@test def a(): void {}
@test{ skip = true } def b(): void {}
@test struct wrong {}
@plain def c(): void {}
@test @test def d(): void {}
""";
val made := check_program("", program, shared);
t.expects(made.notes.length == 3);
t.expects(check_noted(7, 1, ec::invalid, program, shared));
t.expects(check_noted(8, 1, ec::invalid, program, shared));
t.expects(check_noted(9, 7, ec::duplicate, program, shared));
}
@test def compile_error_reports_where_checking_reaches_it(t: &mut tester): void {
val shared := "module std; module core;\npub trait attribute {}\npub struct intrinsic : attribute {}\npub struct span<T> { var data: *T; }\n@intrinsic pub def compile_error(text: span<u8>): void;";
t.expects(check_noted(2, 30, ec::invalid, "import std;\ndef f(): void { compile_error(\"no\"); }", shared));
t.expects(checks("import std;\ndef f<T>(): void { compile_error(\"later\"); }", shared));
}
@test def if_const_runs_its_condition_and_checks_the_branch_taken(t: &mut tester): void {
# A condition folding does not decide is run while checking, so only the
# branch it takes is checked, and reports its `compile_error`.
val shared := "module std; module core;\npub trait attribute {}\npub struct intrinsic : attribute {}\npub struct span<T> { var data: *T; }\n@intrinsic pub def compile_error(text: span<u8>): void;";
t.expects(checks("""
def three(): i64 { return 3; }
def f(): void {
if const three() > 2 { val ok: i64 = 1; } else { val wrong: i64 = true; }
}
"""));
val made := check_program("", "import std;\ndef three(): i64 { return 3; }\ndef f(): void { if const three() > 2 { compile_error(\"taken\"); } else { compile_error(\"not taken\"); } }", shared);
t.expects(made.notes.length == 1 && made.notes[0].code == ec::invalid && made.notes[0].detail.span == "taken");
}
@test def packs_give_each_argument_its_own_type(t: &mut tester): void {
val program := """
import std;
trait shown { def show(&this): i64; }
def i64::show(&this): i64 { return this; }
def bool::show(&this): i64 { return 1; }
def show_all<Ts...>(args: Ts...): i64 where Ts...: shown {
var out := 0;
for const x in args { out += x.show(); }
return out + args.length as i64;
}
def total<T, Ts...>(first: T, rest: Ts...): T where T: numeric where Ts...: numeric {
return first if const rest.length == 0;
return first + total(...rest) as T;
}
def head<Ts...>(args: Ts...): i64 where Ts...: shown { return args[0].show(); }
def f(): void {
val a := show_all(1, true);
val b := show_all::<i64, bool>(1, true);
val c := total(1, 2, 3);
val d := head(5);
}
""";
t.expects(checks(mini_std, program));
val made := check_program("", mini_std, """
import std;
trait shown { def show(&this): i64; }
def show_all<Ts...>(args: Ts...): i64 where Ts...: shown { return 0; }
def f(): void { val e := show_all(1.5); }
""");
t.expects(made.notes.length == 1 && made.notes.at(0).code == ec::bound);
}
#! What calling values needs of std, as the real one declares it.
const calling_std: strview = """
module std; module core;
pub trait signature { type result; type params...; }
pub trait invokable<def(Ts...): R> { def invoke(&mut this, args: Ts...): R; }
""";
@test def function_types_take_any_number_and_packs(t: &mut tester): void {
t.expects(checks(mini_std, """
import std;
def sum(xs: i64...): i64 { return 0; }
def apply<R, Ts...>(f: def(Ts...): R, args: Ts...): R { return f(...args); }
def add(a: i64, b: i64): i64 { return a + b; }
def f(): void {
val s: def(i64...): i64 = sum;
val t: def(span<i64>): i64 = sum;
val a := s(1, 2, 3) + s() + t(span::<i64>{ data = null, count = 0 }) + apply(add, 1, 2);
}
"""));
# A variadic parameter type is the last, and only a variadic one takes a view.
val wrong := """
import std;
def two(a: i64, b: i64): i64 { return a + b; }
def f(): void {
val g: def(i64..., bool): i64 = two;
val h := two;
val xs: i64[2] = [1, 2];
_ = h(...xs);
}
""";
t.expects(check_noted(4, 14, ec::invalid, mini_std, wrong));
t.expects(check_noted(7, 9, ec::arguments, mini_std, wrong));
}
@test def types_and_traits_take_packs(t: &mut tester): void {
t.expects(checks(mini_std, """
import std;
trait sink<Ts...> { def put(&mut this, args: Ts...): void; }
trait counted<Ts...> : sink<Ts...> { def count(&this): i64; }
struct log<A, Ts...> : counted<Ts...> { var n: i64; }
def log<A, Ts...>::put(&mut this, args: Ts...): void { this.n += args.length as i64; }
def log<A, Ts...>::count(&this): i64 { return this.n; }
def feed<S, Ts...>(into: &mut S, args: Ts...): void where S: sink<Ts...> { into.put(...args); }
def f(): void {
var l := log::<u8, i64, bool>{ n = 0 };
l.put(1, true);
feed(&mut l, 2, false);
val as_sink: &mut counted<i64, bool> = &mut l;
as_sink.put(3, true);
var none := log::<u8>{ n = 0 };
none.put();
}
"""));
# A pack is no field's type, an alias takes none, and one not known yet is
# passed on alone.
val wrong := """
import std;
struct box<A, Ts...> { var held: A; }
struct holds<Ts...> { var all: Ts; }
type all<Ts...> = box<i64, Ts...>;
def pass<Ts...>(args: Ts...): void {
val b: box<i64, i64, Ts...> = box::<i64, i64, Ts...>{ held = 1 };
}
""";
t.expects(check_noted(3, 32, ec::not_type, mini_std, wrong));
t.expects(check_noted(4, 10, ec::generic, mini_std, wrong));
t.expects(check_noted(6, 10, ec::generic, mini_std, wrong));
}
@test def pattern_parameters_name_the_parts_of_a_function_type(t: &mut tester): void {
t.expects(checks(mini_std, calling_std, """
import std;
trait first<def(A, Rest...): R> { def head(&this, a: A): R; def rest(&this, args: Rest...): u64; }
struct picker : first<def(i64, bool, u8): u8> {}
def picker::head(&this, a: i64): u8 { return a as u8; }
def picker::rest(&this, b: bool, c: u8): u64 { return 2; }
struct summer : invokable<def(i64...): i64> {}
def summer::invoke(&mut this, xs: i64...): i64 { return 0; }
struct wrapped<F> : invokable<F> where F: signature { var call: F; }
def wrapped<F>::invoke(&mut this, args: F::params...): F::result { return this.call(...args); }
def add(a: i64, b: i64): i64 { return a + b; }
def total(f: &mut invokable<def(i64...): i64>): i64 { return f.invoke(1, 2); }
def f(): void {
val p := picker{};
val n := p.head(1) as u64 + p.rest(true, 2);
var s := summer{};
val m := total(s);
var w := wrapped::<def(i64, i64): i64>{ call = add };
val k := w.invoke(1, 2);
}
"""));
# A pattern is given a function type with the parameters it names, and only a
# struct, union, or trait takes one.
val wrong := """
import std;
trait unary<def(A): R> { def one(&this, a: A): R; }
def generic<def(Ts...): R>(x: i64): void {}
def f(a: &unary<i64>, b: &unary<def(i64, i64): i64>, c: &unary<def(i64...): i64>): void {}
""";
t.expects(check_noted(3, 13, ec::generic, mini_std, wrong));
t.expects(check_noted(4, 11, ec::generic, mini_std, wrong));
t.expects(check_noted(4, 27, ec::generic, mini_std, wrong));
t.expects(check_noted(4, 58, ec::generic, mini_std, wrong));
}
@test def function_types_naming_a_pack_are_what_each_instance_gives(t: &mut tester): void {
# Written in a body, a function type naming a pack is the type its instance's
# pack makes of it, as the signature's is, a variadic one's included.
t.expects(checks(mini_std, calling_std, """
import std;
def plain<Ts...>(args: Ts...): i64 { return 0; }
def named<Ts...>(): void { val f: def(Ts...): i64 = plain::<Ts...>; }
def thunk<F, R, Ts...>(at: *mut u8, args: Ts...): R where F: invokable<def(Ts...): R> {
return (at as *mut F)[0](...args);
}
struct holder<def(Ts...): R> : invokable<def(Ts...): R> { var call: def(*mut u8, Ts...): R; var at: *mut u8; }
def holder<def(Ts...): R>::invoke(&mut this, args: Ts...): R { return this.call(this.at, ...args); }
def holder<def(Ts...): R>::of<F>(f: *mut F): holder<def(Ts...): R> where F: invokable<def(Ts...): R> {
return holder::<def(Ts...): R> { call = thunk::<F, R, Ts...>, at = f as *mut u8 };
}
def over_thunk<S, F>(at: *mut u8, args: S::params...): S::result where S: signature where F: invokable<S> {
return (at as *mut F)[0](...args);
}
struct over<S> : invokable<S> where S: signature { var call: def(*mut u8, S::params...): S::result; var at: *mut u8; }
def over<S>::invoke(&mut this, args: S::params...): S::result { return this.call(this.at, ...args); }
def over<S>::of<F>(f: *mut F): over<S> where F: invokable<S> {
return over::<S> { call = over_thunk::<S, F>, at = f as *mut u8 };
}
struct add : invokable<def(i64, i64): i64> {}
def add::invoke(&mut this, a: i64, b: i64): i64 { return a + b; }
struct summer : invokable<def(i64...): i64> {}
def summer::invoke(&mut this, xs: i64...): i64 { return 0; }
def f(): void {
var a := add{};
var s := summer{};
var h := holder::<def(i64, i64): i64>::of(&mut a as *mut add);
var all := holder::<def(i64...): i64>::of(&mut s as *mut summer);
var o: over<def(i64, i64): i64> = over::of(&mut a as *mut add);
val n := h(1, 2) + all(1, 2, 3) + o(3, 4);
named::<i64, bool>();
}
"""));
}
@test def signature_bounds_hold_wherever_their_parameter_is_declared(t: &mut tester): void {
# A pattern given a signature finds it bounded whether it is declared before
# or after the parameter bounded by the pattern.
t.expects(checks(mini_std, calling_std, """
import std;
def before<S, F>(f: &mut F, args: S::params...): S::result where S: signature where F: invokable<S> {
return f(...args);
}
def after<F, S>(f: &mut F, args: S::params...): S::result where F: invokable<S> where S: signature {
return f(...args);
}
struct add : invokable<def(i64, i64): i64> {}
def add::invoke(&mut this, a: i64, b: i64): i64 { return a + b; }
def f(): void {
var a := add{};
val n := before::<def(i64, i64): i64, add>(&mut a, 1, 2) + after::<add, def(i64, i64): i64>(&mut a, 3, 4);
}
"""));
}
@test def void_is_a_type_of_one_value(t: &mut tester): void {
# `void` is a value's type, of the one value `void {}`, which a function giving
# nothing gives: a type argument, a field, a local, an element, and a
# signature's result, and `return;` gives it where a union holds a `void`.
t.expects(checks(mini_std, calling_std, """
import std;
struct pair_of<A, B> { var first: A; var second: B; }
union outcome<T, E> { var value: T; var error: E; }
struct holder<S> : invokable<S> where S: signature { var call: def(S::params...): S::result; }
def holder<S>::invoke(&mut this, args: S::params...): S::result { return this.call(...args); }
def said(x: i64): void {}
def give<T>(x: T): T { return x; }
def checked(n: i64): outcome<void, i64> {
if n < 0 { return outcome::<void, i64> { error = n }; }
return;
}
def done(): void { return said(0); }
type unit = void;
const quiet: void = void {};
def f(): void {
val v := void {};
var w: void;
w = v;
val x := said(1);
give::<void>(said(2));
val p := pair_of::<void, i64> { first = void {}, second = 3 };
val u: unit = quiet;
var h := holder::<def(i64): void> { call = said };
h(4);
val o := checked(5);
val many: void[2] = [void {}, said(6)];
val r: &void = &v;
}
"""));
# A function giving a value still returns one, falling off its end returns
# nothing, and `void {}` is given no fields.
val wrong := """
import std;
union outcome<T, E> { var value: T; var error: E; }
def number(): i64 { return; }
def fell(): outcome<void, i64> { }
def f(): void {
val bad := void { x = 1 };
}
""";
t.expects(check_noted(3, 21, ec::mismatch, mini_std, wrong));
t.expects(check_noted(4, 5, ec::control, mini_std, wrong));
t.expects(check_noted(6, 21, ec::invalid, mini_std, wrong));
}
@test def array_patterns_name_the_element_and_length_of_a_fixed_array(t: &mut tester): void {
t.expects(checks(mini_std, """
import std;
struct lanes<L[N]> { var items: L[N]; }
def lanes<L[N]>::count(&this): u64 { return N; }
def lanes<L[N]>::first(&this): L { return this.items[0]; }
def first<L, const N: u64>(v: &lanes<L[N]>): L { return v.items[0]; }
type four = lanes<f32[4]>;
def f(): void {
val a := lanes::<i64[3]> { items = [1, 2, 3] };
val n: u64 = a.count();
val x: i64 = a.first() + first(&a);
val b := four {};
val m: u64 = b.count();
val c: lanes<lanes<u8[2]>[2]> = lanes::<lanes<u8[2]>[2]> {};
}
"""));
# A pattern is given a fixed array, and only a struct, union, or trait takes one.
val wrong := """
import std;
struct lanes<L[N]> { var items: L[N]; }
def generic<L[N]>(x: i64): void {}
def f(a: &lanes<i64>, b: &lanes<i64[2], u8>): void {}
""";
t.expects(check_noted(3, 13, ec::generic, mini_std, wrong));
t.expects(check_noted(4, 11, ec::generic, mini_std, wrong));
t.expects(check_noted(4, 27, ec::generic, mini_std, wrong));
}
# A struct or union has the bitwise operators and shifts by the methods that give
# them, as it has arithmetic, and integers give them as methods too.
@test def bitwise_operators_and_shifts_call_methods(t: &mut tester): void {
t.expects(checks(mini_std, """
import std;
struct bits { var word: u64; }
def bits::bit_and(&this, other: &bits): bits { return bits { word = this.word & other.word }; }
def bits::bit_or(&this, other: &bits): bits { return bits { word = this.word | other.word }; }
def bits::bit_xor(&this, other: &bits): bits { return bits { word = this.word ^ other.word }; }
def bits::bit_not(&this): bits { return bits { word = ~this.word }; }
def bits::shift_left(&this, by: &u32): bits { return bits { word = this.word << by }; }
def bits::shift_right(&this, by: &u32): bits { return bits { word = this.word >> by }; }
def f(a: bits, b: bits): u64 {
var c := (a & b) | (a ^ ~b);
c <<= 2;
c &= a >> 1;
val x: u32 = 12;
return c.word + (x.bit_and(&10) | x.shift_left(&2)) as u64;
}
"""));
val wrong := """
import std;
struct bits { var word: u64; }
def bits::bit_and(&this, other: &bits): bits { return this; }
def f(a: bits): void {
val b := a | a;
val c := a << 1;
}
""";
t.expects(check_noted(5, 14, ec::operands, mini_std, wrong));
t.expects(check_noted(6, 14, ec::operands, mini_std, wrong));
}
# An operator of numbers keeps an alias its operands are written with, so a
# mask made of masks has a mask's methods: the right operand's type where it is
# the number they operate as, else the left's.
@test def operators_keep_an_alias_of_a_number(t: &mut tester): void {
t.expects(checks(mini_std, """
import std;
type mask = u64;
type word = u64;
def mask::first(&this): u64 { return this & 7; }
def word::mask_of(&this): mask { return this; }
def f(a: mask, b: mask, c: u64, w: word): u64 {
val d := (a | b).first() + (a & 3).first() + (~a).first() + (a >> 1).first() + (a << c).first();
val e := (a + b).first() + (c ^ a).first() + (2 * a).first() + (w & a).first() + (a ^ w).mask_of();
return d + e;
}
"""));
val wrong := """
import std;
type mask = u64;
def mask::first(&this): u64 { return this & 7; }
def f(a: mask, c: u64): u64 {
return (a | c).first() + (c << a).first();
}
""";
t.expects(check_noted(5, 18, ec::member, mini_std, wrong));
t.expects(check_noted(5, 37, ec::member, mini_std, wrong));
}
# A vector of std holds 16 or 32 bytes of numbers, or 64 of at most 32.
@test def vectors_hold_sixteen_or_thirty_two_bytes_of_numbers(t: &mut tester): void {
val lanes_std := """
module std; module core;
pub struct simd<L[N]> { var _lanes: L[N]; }
pub struct mask<L[N]> { var _lanes: L[N]; }
""";
t.expects(checks(mini_std, lanes_std, """
import std;
def f(a: simd<f32[4]>, b: simd<u8[32]>, c: mask<f64[2]>, d: simd<i16[16]>, e: simd<f32[16]>, g: mask<u16[32]>): void {}
"""));
val wrong := """
import std;
def f(a: simd<f32[3]>, b: mask<u8[8]>, c: simd<bool[16]>, d: simd<u8[64]>): void {}
""";
t.expects(check_noted(2, 10, ec::generic, mini_std, lanes_std, wrong));
t.expects(check_noted(2, 27, ec::generic, mini_std, lanes_std, wrong));
t.expects(check_noted(2, 43, ec::generic, mini_std, lanes_std, wrong));
t.expects(check_noted(2, 62, ec::generic, mini_std, lanes_std, wrong));
}
@test def values_are_called_through_invoke(t: &mut tester): void {
t.expects(checks(mini_std, calling_std, """
import std;
struct adder : invokable<def(i64, i64): i64> { var calls: i64; }
def adder::invoke(&mut this, a: i64, b: i64): i64 { this.calls++; return a + b; }
struct doubler : invokable<def(i64): i64> {}
def doubler::invoke(&this, x: i64): i64 { return x * 2; }
struct holder { var f: adder; }
def twice<F>(f: &mut F, x: i64): i64 where F: invokable<def(i64): i64> { return f(f(x)); }
def run(f: &mut invokable<def(i64, i64): i64>): i64 { return f(1, 2); }
def square(x: i64): i64 { return x * x; }
def f(): void {
var a := adder{ calls = 0 };
val d := doubler{};
var h := holder{ f = adder{ calls = 0 } };
var sq: def(i64): i64 = square;
val n := a(1, 2) + d(3) + h.f(4, 5) + run(a) + twice(&mut sq, 2) + sq.invoke(3);
}
"""));
# A generic of another module calls a functor through its `invoke`, which needs
# no `pub` there, whether the functor is given by value or by reference.
t.expects(checks(mini_std, calling_std, """
import std;
module algo;
pub def count<F>(f: F, n: i64): i64 where F: invokable<def(i64): bool> {
var hits: i64 = 0;
for i in 0..n { if f(i) { hits++; } }
return hits;
}
""", """
import std;
import algo;
struct odd : invokable<def(i64): bool> { var seen: i64; }
def odd::invoke(&mut this, x: i64): bool { this.seen++; return x % 2 == 1; }
def f(): void {
var o := odd{ seen = 0 };
val n := count(o, 4) + count(&mut o, 4);
}
"""));
# A function's `invoke` gives what a bound's pattern binds, as one declared
# would: `half` gives the `K` of `invokable<def(i64): K>`.
t.expects(checks(mini_std, calling_std, """
import std;
def key_of<F, K>(f: F, x: i64): K where F: invokable<def(i64): K> { return f(x); }
def half(x: i64): u32 { return (x / 2) as u32; }
def f(): void { val k := key_of(half, 8); }
"""));
# A value of a type without `invoke` is not called, and one taking `&mut this`
# is called on what can be written.
val wrong := """
import std;
struct adder { var calls: i64; }
def adder::invoke(&mut this, a: i64, b: i64): i64 { return a + b; }
struct plain { var n: i64; }
def f(): void {
val a := adder{ calls = 0 };
_ = a(1, 2);
val p := plain{ n = 1 };
_ = p(1);
}
""";
t.expects(check_noted(7, 7, ec::assignable, mini_std, wrong));
t.expects(check_noted(9, 7, ec::operands, mini_std, wrong));
}
#! What reflection needs of std, as the real one declares it.
const reflection_std: strview = """
module std; module core;
pub trait attribute {}
pub trait zeroable {}
pub struct intrinsic : attribute {}
pub struct span<T> { var data: *T; var count: u64; }
pub val span<T>::length: u64 { get => this.count; }
pub def span<T>::at(&this, index: u64): &T { return &this.data[index]; }
pub enum target : u32 { functions = 1, methods = 2, fields = 16, structs = 32, properties = 0x1000, all = 0xFFFF }
pub struct decl { pub val kind: target; pub val generic: bool; }
@intrinsic pub def declarations(): span<decl>;
@intrinsic pub def decl_of<T>(): decl;
@intrinsic pub def members_of(d: decl): span<decl>;
@intrinsic pub def attached<A>(d: decl): span<A> where A: attribute;
@intrinsic pub def value_of(d: decl): void;
@intrinsic pub def compile_error(text: span<u8>): void;
""";
@test def reflection_reads_declarations_while_checking(t: &mut tester): void {
val program := """
import std;
struct check : attribute {}
@check def a(): i64 { return 1; }
@check def b(): i64 { return 2; }
@check def c(x: i64): i64 { return 3; }
struct p { var x: i64; var y: bool; }
def fields<T>(): i64 {
var count := 0;
for const f in members_of(decl_of::<T>()) {
if const f.kind == target::fields { count += 1; }
}
return count;
}
def run(): i64 {
var total := 0;
for const d in declarations() {
if const attached::<check>(d).length == 0 { continue; }
if const !(value_of(d) is def(): i64) {
compile_error("A check takes nothing and gives an i64.");
continue;
}
total += value_of(d)();
}
return total + fields::<p>();
}
""";
val made := check_program("", program, reflection_std);
t.expects(made.notes.length == 1);
t.expects(made.notes.length == 1 && made.notes.at(0).code == ec::invalid && made.notes.at(0).line == 19);
t.expects(made.skipped == 0);
}
@test def reflection_gives_an_instance_its_arguments(t: &mut tester): void {
# The fields of `page<u8>` hold `u8`, and `is (d)` names a field as its name
# would, within each instance.
val made := check_program("", """
import std;
struct page<T> { var items: T[2]; var count: T; }
union shape { var circle: f64; var square: u32; }
def fill<T>(): T where T: zeroable {
var made: T;
for const field in members_of(decl_of::<T>()) {
if const field.kind == target::fields {
var read: type_of(field);
made.(field) = read;
}
}
return made;
}
def which<T>(value: &T): i64 {
var at := 0;
for const field in members_of(decl_of::<T>()) {
if const field.kind == target::fields {
if value is (field) { return at; }
at += 1;
}
}
return at;
}
def run(): i64 {
val p := fill::<page<u8>>();
val s := shape{ square = 3 };
return which(&s) + p.count as i64;
}
""", reflection_std);
t.expects(made.notes.length == 0 && made.skipped == 0);
# What names no field of the value is reported, as what is no description is.
val wrong := check_program("", """
import std;
struct point { var x: i64; }
union shape { var circle: f64; }
def f(s: &shape, p: &point): void {
val n := 4;
for const field in members_of(decl_of::<point>()) {
if const field.kind == target::fields {
if s is (field) { return; }
val x := s.(field);
}
}
if s is (n) { return; }
val y := p.(n);
}
""", reflection_std);
t.expects(wrong.notes.length == 4 && wrong.skipped == 0);
t.expects(wrong.notes.length == 4 && wrong.notes.at(0).code == ec::invalid && wrong.notes.at(1).code == ec::member);
t.expects(wrong.notes.length == 4 && wrong.notes.at(2).code == ec::constant && wrong.notes.at(3).code == ec::constant);
}
@test def reflection_names_methods_properties_and_instances(t: &mut tester): void {
# `x.(d)` reads a property and calls a method as their names would, and
# `value_of(d)::<i64>` is the instance of the generic function `d` describes.
val made := check_program("", """
import std;
struct mark : attribute {}
struct point { var x: i64; }
def point::sum(&this): i64 { return this.x; }
val point::twice: i64 { get => this.x * 2; }
@mark def same<T>(x: T): T { return x; }
def run(p: &point): i64 {
var total := 0;
for const m in members_of(decl_of::<point>()) {
if const m.kind == target::methods { total += p.(m)(); }
if const m.kind == target::properties { total += p.(m); }
}
for const d in declarations() {
if const attached::<mark>(d).length == 0 { continue; }
total += value_of(d)::<i64>(21);
}
return total;
}
""", reflection_std);
t.expects(made.notes.length == 0 && made.skipped == 0);
# What is no description is reported where it is given, as `x.(d)` reports.
t.expects(check_noted(2, 40, ec::constant, """
import std;
def run(): void { val n := 4; value_of(n); }
""", reflection_std));
}
@test def an_edit_checks_again_only_what_it_affects(t: &mut tester): void {
val before := """
def helper(n: i64): i64 { return n + 1; }
def caller(): i64 { return helper(2); }
def other(): i64 { return 7; }
""";
# An edit within a body that keeps its signature checks only that body.
val body := check_edit(before, """
def helper(n: i64): i64 { return n * 2 + 1; }
def caller(): i64 { return helper(2); }
def other(): i64 { return 7; }
""");
t.expects(body.same);
t.expects(body.checked == 1);
# A changed signature checks again what calls it.
val signature := check_edit(before, """
def helper(n: bool): i64 { return 1; }
def caller(): i64 { return helper(2); }
def other(): i64 { return 7; }
""");
t.expects(signature.same && signature.notes == 1);
t.expects(signature.checked == 2);
# What is gone leaves what called it naming nothing.
val gone := check_edit(before, """
def caller(): i64 { return helper(2); }
def other(): i64 { return 7; }
""");
t.expects(gone.same && gone.notes == 1);
# What is new is found where it was named before.
val added := check_edit("def caller(): i64 { return helper(2); }", """
def caller(): i64 { return helper(2); }
def helper(n: i64): i64 { return n; }
""");
t.expects(added.same && added.notes == 0);
}
@test def an_edit_keeps_what_other_files_worked_out(t: &mut tester): void {
val lib := """
module geometry;
pub struct rect { pub var w: f64; }
pub def area(r: &rect): f64 { return r.w; }
""";
val made := check_edit(
"import geometry;\ndef f(): f64 { return area(rect{ w = 1.0 }); }",
"import geometry;\ndef f(): f64 { return area(rect{ w = 2.0 }) + 1.0; }",
lib);
t.expects(made.same && made.notes == 0 && made.checked == 1);
}
@test def hovering_shows_types_and_what_names_name(t: &mut tester): void {
val text := "def twice(n: i64): i64 { return n * 2; }\ndef f(): u8 { val small: u8 = 3; return small; }\ndef g(): i64 { return twice(4); }";
t.expects(hover_of(text, "small;").span == "val small: u8");
t.expects(hover_of(text, "twice(4)").span == "def twice(n: int): int");
}
@test def constants_run_functions_while_checking(t: &mut tester): void {
val program := """
def square(n: i64): i64 { return n * n; }
def table(): u32[4] {
var made: u32[4];
for i in 0..4 { made[i] = (i * 10) as u32; }
return made;
}
enum gap { a = 1, b = 4, c, d = 9 }
def through(n: i64): i64 { var t := 0; for i in 1..=n { t += i; } return t; }
def members(): i64 { var t := 0; for g in gap::b..=gap::d { t = t * 10 + (g as i64); } return t; }
const four := square(2);
const tens: u32[4] = table();
const thirty: u32 = tens[3];
def f(): void {
var a: i64[four];
var b: i64[square(3)];
var c: u8[thirty - 25];
var d: u8[through(3)];
var e: u8[members()];
}
""";
t.expects(checks(program));
t.expects(local_type("c", program).span == "u8[5]");
t.expects(local_type("b", program).span == "int[9]");
# `a..=b` counts `b` too, and a range of an enum's members each member.
t.expects(local_type("d", program).span == "u8[6]");
t.expects(local_type("e", program).span == "u8[459]");
val faults := check_program("", """
def forever(): i64 { var x := 0; while true { x++; } return x; }
def deep(n: i64): i64 { return deep(n + 1); }
const loops: i64 = forever();
const nested: i64 = deep(0);
""");
t.expects(faults.notes.length == 2);
t.expects(faults.notes.length == 2 && faults.notes.at(0).code == ec::constant && faults.notes.at(1).code == ec::constant);
}
@test def constant_parameters_are_values_worked_out_while_checking(t: &mut tester): void {
t.expects(checks(mini_std, """
import std;
struct buffer<T, const N: u64> where T: zeroable { var items: T[N]; }
def buffer<T, N>::capacity(&this): u64 { return N; }
def total<const N: u64>(xs: &i64[N]): i64 { return xs[0]; }
def grown<const N: u64>(xs: &i64[N]): i64[N + 1] { var out: i64[N + 1]; return out; }
enum color { red, green }
struct point { var x: i64; var y: i64 = 7; }
struct at<const P: point> {}
struct painted<const C: color> {}
const origin: point = point { x = 1 };
const four: u64 = 4;
def f(): void {
var b: buffer<i64, 4>;
val c: buffer<i64, four> = b;
val d: at<point { x = 1, y = 7 }> = at::<origin> {};
val e: painted<color::green> = painted::<color::green> {};
val xs: i64[3] = [1, 2, 3];
val n := total(&xs) + c.capacity() as i64;
val g: i64[4] = grown(&xs);
}
"""));
t.expects(local_type("b", """
struct buffer<T, const N: u64> { var n: T; }
def f(): void { val b := buffer::<u8, 2 * 8> { n = 1 }; }
""").span == "buffer<u8, 16>");
# A constant parameter takes a value of its type, and no bounds, and closes nothing.
t.expects(check_noted(2, 29, ec::overflow, """
struct keep<const V: u8> {}
def f(): void { val k: keep<300> = keep::<1> {}; }
"""));
t.expects(check_noted(2, 39, ec::generic, mini_std, """
import std;
struct bounded<const N: u64> where N: ordered {}
"""));
t.expects(check_noted(2, 31, ec::generic, """
struct buffer<T, const N: u64> { var n: T; }
def f(): void { val b: buffer<4, 4> = buffer::<u8, 4> { n = 1 }; }
"""));
t.expects(check_noted(2, 37, ec::constant, """
struct keep<const V: u64> {}
def f(n: u64): void { val k: keep<n + 1> = keep::<4> {}; }
"""));
# A constant a function gives is one, which the evaluator works out where a
# type argument reads it, or calls it.
t.expects(checks("""
struct keep<const V: u64> {}
def get(): u64 { return 4; }
const got := get();
def f(): void { val k: keep<got> = keep::<4> {}; val m: keep<get()> = keep::<4> {}; }
"""));
}
@test def calls_infer_from_bounds_results_and_waiting_arguments(t: &mut tester): void {
t.expects(checks(mini_std, """
import std;
def first<C, T>(c: &C): maybe<T> where C: iterable<T> { return nothing {}; }
struct box<T> { var value: T; }
def box<T>::new(value: T): box<T> { return box::<T> { value = value }; }
def same<T>(x: T): T { return x; }
def apply<T, U>(f: def(T): U, x: T): U { return f(x); }
def made<T>(): box<T> where T: zeroable { var b: box<T>; return b; }
def pick<T>(a: T, b: T): T { return b; }
def head<T>(xs: span<T>): T { return xs.at(0); }
def second<T, U>(a: T, b: U): U { return b; }
def f(s: span<i64>, b: box<u8>): void {
val e := first(&s);
val c: box<u8> = box::new(5);
val g: def(i64): i64 = same;
val h := apply(same, 3) + apply(def(x) => x * 2, 4);
val i := pick(made(), b);
val j := head([1, 2, 3]);
val k := second::<i64>(1, true);
val l := pick(1, 2.5);
}
"""));
val inferred := check_program("e", """
import std;
def first<C, T>(c: &C): maybe<T> where C: iterable<T> { return nothing {}; }
def f(s: span<i64>): void { val e := first(&s); }
""", mini_std);
t.expects(inferred.asked.span == "maybe<int>");
# What is wrong within an argument that waits is reported once, where it is
# first checked, though the argument is checked again for its parameter's
# type.
val waited := check_program("", """
import std;
def made<T>(x: i64): maybe<T> { return nothing {}; }
def take<T>(x: T): void {}
def f(): void { take::<maybe<i32>>(made(nope)); }
""", mini_std);
t.expects(waited.notes.length == 1 && waited.notes[0].code == ec::undefined);
}
@test def generic_traits_are_inferred_through_what_implements_them(t: &mut tester): void {
# A type implementing a generic trait gives its parameters through its
# methods, whether or not it names the trait.
val inferred := check_program("got", """
trait into<T> { def into(&this): T; }
struct count { var n: i64; }
def count::into(&this): i64 { return this.n; }
def pick<T>(x: &into<T>): T { return x.into(); }
def f(c: &count): void { val got := pick(c); }
""");
t.expects(inferred.notes.length == 0 && inferred.asked.span == "int");
# A generic trait's default gives the types of the trait's arguments.
val both := check_program("got", """
trait into<T> { def into(&this): T; }
trait pair<T>: into<T> { def twice(&this): T[2] { return [this.into(), this.into()]; } }
struct one: pair<i64> { var v: i64; }
def one::into(&this): i64 { return this.v; }
def f(o: &one): void { val got := o.twice(); }
""");
t.expects(both.notes.length == 0 && both.asked.span == "int[2]");
}
@test def traits_take_defaults_types_and_nested_instances(t: &mut tester): void {
# A type the bound asks for is the one given, or the trait's default, in each
# instance; a requirement a composed trait's default meets is met; and a member
# of a generic type's enum is of the enum given the type's arguments.
t.expects(checks("""
trait walk { type cursor; def start(&this): cursor; }
struct ctr { var at: i64; }
struct one { var to: i64; }
type one::cursor = ctr;
def one::start(&this): ctr { return ctr { at = this.to }; }
def first<C>(c: &C): C::cursor where C: walk { return c.start(); }
def f(o: &one): i64 { val x: ctr = first(o); return x.at; }
"""));
t.expects(checks("""
trait stored { type storage = self; }
struct bit { var on: bool; }
type bool::storage = bit;
def keep<T>(x: T::storage): T::storage where T: stored { return x; }
def f(): void {
val a: i64 = keep::<i64>(5);
val b: bit = keep::<bool>(bit { on = true });
}
"""));
t.expects(checks("""
trait base { def id(&this): i64; def f(&this): i64 { return 1; } }
trait keeps: base { def f(&this): i64; }
trait all: base { def l(&this): i64; }
struct z: all { var n: i64; }
def z::id(&this): i64 { return this.n; }
def z::l(&this): i64 { return 10; }
def f(x: &z): i64 { val k: &keeps = x; return k.f(); }
"""));
t.expects(checks("""
struct bag<T> { var count: u64; }
enum bag<T>::state { empty, full }
def bag<T>::status(&this): bag<T>::state {
return this.count == 4 ? bag::<T>::state::full : bag::<T>::state::empty;
}
def f(b: &bag<i64>): bool { return b.status() == bag::<i64>::state::empty; }
"""));
}
# The containers the tables are made of.
@test def locals_are_declared_once_in_each_block(t: &mut tester): void {
t.expects(check_noted(1, 32, ec::duplicate, "def f(): i64 { val x := 1; val x := x + 1; return x; }"));
# A parameter, a block within, a loop's body, and a function within may each
# declare a name again, hiding the one around them.
t.expects(checks("""
def f(x: i64): i64 {
val x := x + 1;
if x > 2 { val x := 3; return x; }
var n := 0;
while n < 2 { val n := 7; break; }
val g := def(x: i64): i64 { val x := x * 2; return x; };
return g(x);
}
"""));
# What an `is not` test names for the rest of the block is declared there.
t.expects(check_noted(4, 7, ec::duplicate, mini_std, """
import std;
def f(m: maybe<i64>): i64 {
if m is not some(v) { return 0; }
val v := 5;
return v;
}
"""));
}
@test def union_fields_are_lent_where_the_union_is_known_to_hold_them(t: &mut tester): void {
# `&mut` of a field could write it whichever field the union holds, and `&` of
# what closes could outlive it once the union holds another: a test names it
# where it is held.
val held := """
struct held : owning { var n: i64; }
def held::close(&mut this): void {}
struct pair { var a: i64; var h: held; }
""";
val made := check_program("", """
import std;
def f(a: &mut maybe<i64>, b: &mut maybe<held>, c: &mut maybe<pair>): void {
val p := &a.some;
val q := &mut a.some;
val s := &b.some;
val x := &c.some.a;
val y := &mut c.some.a;
val z := &c.some.h;
if b is some(&mut w) { w.n = 1; }
}
""", held, mini_std);
t.expects(made.notes.length == 4);
t.expects(made.notes.length == 4 && made.notes.at(0).line == 4 && made.notes.at(0).column == 12 && made.notes.at(0).code == ec::invalid);
t.expects(made.notes.length == 4 && made.notes.at(1).line == 5 && made.notes.at(1).column == 12 && made.notes.at(1).code == ec::owned);
t.expects(made.notes.length == 4 && made.notes.at(2).line == 7 && made.notes.at(2).column == 12 && made.notes.at(2).code == ec::invalid);
t.expects(made.notes.length == 4 && made.notes.at(3).line == 8 && made.notes.at(3).column == 12 && made.notes.at(3).code == ec::owned);
}
@test def properties_hold_values_and_generic_types_hold_variables_of_each_instance(t: &mut tester): void {
t.expects(check_noted(2, 22, ec::invalid, "struct p { var n: i64; }\nval p::nothing_here: void { get { return; } }"));
# Each instance has a variable of its own, read through it, and an alias of one
# has its own too.
t.expects(checks("struct box<T> { var v: T; }\nval box<T>::count: i64 = 3;\ndef f(): i64 { return box::<u8>::count; }"));
t.expects(check_noted(3, 23, ec::generic, "struct box<T> { var v: T; }\nval box<T>::count: i64 = 3;\ndef f(): i64 { return box::count; }"));
t.expects(checks("struct box<T> { var v: T; }\ntype ibox = box<i64>;\nval ibox::count: i64 = 3;\ndef f(): i64 { return ibox::count; }"));
}
@test def tests_and_conversions_that_cannot_mean_what_they_say_are_refused(t: &mut tester): void {
# `is` with a type asks the value's own type, which a union's field never is.
t.expects(check_noted(2, 42, ec::invalid, mini_std, "import std;\ndef f(m: maybe<i64>): bool { return m is nothing; }"));
t.expects(checks(mini_std, "import std;\ndef f(m: maybe<i64>): bool { return m is none; }"));
t.expects(checks(mini_std, "import std;\ndef f<T>(x: T): bool { return x is nothing; }"));
# A reference written `&x` converts as the value it refers to, so a number from
# it is `x`, where its address was surely meant.
t.expects(check_noted(1, 48, ec::conversion, "def f(): u64 { var x: u64 = 5; return (&mut x) as u64; }"));
t.expects(check_noted(1, 44, ec::conversion, "def f(): bool { val x := true; return (&x) as bool; }"));
t.expects(checks("def f(): u64 { var x: u64 = 5; return (&mut x) as *mut u64 as u64; }"));
t.expects(checks("def f(r: &u64): u32 { return r as u32; }"));
# A string compared with a view, either way round, which `view()` settles.
val text := """
module std; module core;
pub struct span<T> { var data: *T; var count: u64; }
pub def span<T>::equals(&this, other: &span<T>): bool { return this.count == other.count; }
pub type strview = span<u8>;
pub struct string { var count: u64; }
pub def string::equals(&this, other: &string): bool { return this.count == other.count; }
pub def string::view(&this): strview { return strview { count = this.count }; }
""";
t.expects(check_noted(2, 48, ec::operands, "import std;\ndef f(s: &string, v: strview): bool { return s == v; }", text));
t.expects(check_noted(2, 48, ec::operands, "import std;\ndef f(s: &string, v: strview): bool { return v != s; }", text));
t.expects(checks("import std;\ndef f(s: &string, v: strview): bool { return s.view() == v && s == \"abc\"; }", text));
}
@test def members_declared_on_cores_types_are_chosen_over_cores(t: &mut tester): void {
# A program's member of a name `core` gives the type too is no duplicate of it:
# where both are seen the program's is chosen, and `core`'s where the program's
# takes nothing given, so `core` adding a member later changes no program.
val made_core := """
module std; module core;
pub struct text { var n: i64; }
pub val text::size: i64 { get => this.n; }
pub def text::grow(&mut this, by: i64): void { this.n += by; }
pub def text::trim(&this): i64 { return 1; }
pub def text::measured(&this): i64 { return this.size + this.trim(); }
pub struct text::piece { pub var n: i64; }
pub def text::cut(&this): text::piece { return text::piece { n = this.n }; }
""";
t.expects(checks("""
import std;
def text::size(&this): bool { return true; }
def text::grow(&mut this, by: bool): void {}
def text::trim(&this): bool { return false; }
def text::piece(&this): bool { return true; }
def f(t: text): bool {
var c := t;
c.grow(1);
c.grow(true);
val a: bool = c.size();
val b: bool = c.trim();
val n: i64 = c.measured();
val d: bool = c.piece();
return a && b && d;
}
""", made_core));
# So is a type, as one a trait asks for, and `core`'s own code keeps its own.
t.expects(checks("""
import std;
type text::piece = bool;
def f(t: &text): bool { val p: text::piece = true; val k := t.cut(); val n: i64 = k.n; return p; }
""", made_core));
# The program's own overloads still take parameters apart.
val made := check_program("", """
import std;
def text::trim(&this): bool { return false; }
def text::trim(&this): bool { return true; }
""", made_core);
t.expects(made.notes.length == 1 && made.notes.at(0).line == 3 && made.notes.at(0).code == ec::duplicate);
}
@test def compound_assignments_change_in_place_where_a_type_can(t: &mut tester): void {
# `+=` calls `add_assign` where it takes the value, and `add` elsewhere.
t.expects(checks("""
struct acc { var n: i64; }
def acc::add(&this, other: &acc): acc { return acc { n = this.n + other.n }; }
def acc::add(&this, k: &i64): acc { return acc { n = this.n + k }; }
def acc::add_assign(&mut this, other: &acc): void { this.n += other.n; }
def f(): i64 { var x := acc { n = 1 }; x += acc { n = 2 }; x += 3; return x.n; }
"""));
t.expects(check_noted(3, 43, ec::operands, """
struct acc { var n: i64; }
def acc::add_assign(&mut this, other: &acc): acc { return this; }
def f(): void { var x := acc { n = 1 }; x += acc { n = 2 }; }
"""));
}
@test def a_module_sees_the_names_of_those_holding_it(t: &mut tester): void {
t.expects(checks("module app::util;\npub def go(): i64 { return helper(); }", "module app;\ndef helper(): i64 { return 1; }"));
# With no file of the module holding it, the note says so.
val made := check_program("", "module app::util;\npub def go(): i64 { return helper(); }");
t.expects(made.notes.length == 1 && made.notes.at(0).detail.span
== "`helper` is not declared where it is used. No file of `app`, which holds this file's module and whose names it sees, was read: is one missing?");
}
@test def naming_system_where_none_was_read_says_where_it_is(
t: &mut tester
): void {
val hint := "`system` is a package of its own, which std requires: require it, or give it by `--lib system=DIR`.";
# An import of it, or a path beginning with it, where no file of it was read.
val imported := check_program("", "import system;\ndef main(): void {}");
t.expects(imported.notes.length == 1);
t.expects(
imported.notes.at(0).detail.span == $"No file declares this module. {hint}".span
);
val pathed := check_program("", "def main(): void { system::exit(0); }");
t.expects(pathed.notes.length == 1);
t.expects(
pathed.notes.at(0).detail.span
== $"`system` is not declared where it is used. {hint}".span
);
# Another module, or one within a `system` that was read, is only missing.
val other := check_program("", "import elsewhere;\ndef main(): void {}");
t.expects(other.notes.length == 1);
t.expects(other.notes.at(0).detail.span == "No file declares this module.");
val within := check_program(
"",
"import system::absent;\ndef main(): void {}",
"module system;\npub def go(): void {}"
);
t.expects(within.notes.length == 1);
t.expects(within.notes.at(0).detail.span == "No file declares this module.");
}
@test def accessors_have_this_and_a_set_has_its_value(t: &mut tester): void {
t.expects(checks("""
struct r { var w: i64; var writes: i64; }
var r::width: i64 {
get => this.w;
set { this.w = value; this.writes += 1; }
}
var r::height: i64 { get { return this.w * 2; } set => this.w = value / 2; }
val r::area: i64 { get => this.width * this.height; }
trait sized { val area: i64 { get; } }
def f(): i64 { var v := r{}; v.width = 3; v.height += 2; return v.area; }
def g<T>(x: &T): i64 where T: sized { return x.area; }
def h(): i64 { return g(&r{ w = 2 }); }
"""));
# `get` reads `this`, and writes nothing through it; `value` is a `set`'s, of
# the property's type.
val reads := check_program("", "struct r { var w: i64; }\nval r::bad: i64 { get { this.w = 1; return 1; } }");
t.expects(reads.notes.length == 1 && reads.notes.at(0).line == 2 && reads.notes.at(0).code == ec::assignable);
t.expects(check_noted(2, 26, ec::undefined, "struct r { var w: i64; }\nval r::bad: i64 { get => value; }"));
val typed := check_program("", "struct r { var w: i64; }\nvar r::w2: i64 { get => this.w; set { val s: bool = value; } }");
t.expects(typed.notes.length == 1 && typed.notes.at(0).code == ec::mismatch);
# `value` is a plain name, which stays a union's field name too.
t.expects(checks(mini_std, """
import std;
struct r { var w: i64; }
var r::w2: result<i64, bool> {
get => this.w;
set { if value is value(n) { this.w = n; } }
}
"""));
}
@test def a_get_mut_reads_what_can_be_written(t: &mut tester): void {
# A `get mut` takes `&mut this`, and reads the property where the value can be
# written, giving the reference the property's type is as one that writes,
# which an assignment writes through. The `get` reads it elsewhere.
val bag := """
struct bag { var items: i64[3]; }
val bag::first: &i64 {
get => &this.items[0];
get mut => &mut this.items[0];
}
def f(): void {
val read := bag { items = [1, 2, 3] };
var written := bag { items = [1, 2, 3] };
val r := &read.first;
val w := &mut written.first;
written.first = 4;
written.first += 1;
written.first++;
}
""";
t.expects(checks(bag));
t.expects(local_type("r", bag).span == "&int");
t.expects(local_type("w", bag).span == "&mut int");
# What reads through a `&` reads by the `get`, so writes nothing, and a
# property only a `get mut` reads is no value of it.
val refused := """
struct bag { var items: i64[3]; }
val bag::first: &i64 {
get => &this.items[0];
get mut => &mut this.items[0];
}
val bag::whole: *mut i64 { get mut => (&mut this.items) as *mut i64; }
def f(b: &bag): void {
b.first = 4;
val p := b.whole;
}
""";
t.expects(check_noted(8, 5, ec::assignable, refused));
t.expects(check_noted(9, 14, ec::assignable, refused));
}
@test def a_property_shares_its_name_with_a_type(t: &mut tester): void {
# A property is reached through a value, and a type declared through the
# type through the type, so they may share a name, as a field and a type do.
t.expects(checks("""
struct bag { var n: i64; }
struct bag::iterator { var at: i64; }
val bag::iterator: bag::iterator { get => bag::iterator { at = this.n }; }
def f(b: &bag): i64 { val i: bag::iterator = b.iterator; return i.at; }
"""));
# A type and a method may not.
t.expects(check_noted(3, 10, ec::duplicate, "struct bag { var n: i64; }\ntype bag::k = i64;\ndef bag::k(&this): i64 { return 1; }"));
}
@test def a_trait_is_named_through_an_alias_of_it(t: &mut tester): void {
# An alias of a trait names it among a type's traits, in a bound, and in a
# test, as a trait renamed keeps its old name as one.
t.expects(checks("""
trait walks { def step(&mut this): i64; }
type moves = walks;
struct legs : moves { var n: i64; }
def legs::step(&mut this): i64 { return this.n; }
def go<W>(w: &mut W): i64 where W: moves { return w.step(); }
def f(): i64 {
var l := legs { n = 1 };
if const legs is moves {
return go(&mut l);
}
return 0;
}
"""));
}
@test def a_slice_is_taken_through_span_of_and_slice_of(t: &mut tester): void {
# `x[a:n]` of what is no view calls its type's `slice_of` where `x` can be
# written, and its `span_of` otherwise, as through a bound, given the bounds
# written. A trait's defaults are checked where it declares them, and are
# what a call through a bound leaving them out is given.
val shelf := """
struct shelf { var items: i64[4]; }
def shelf::span_of(other: &shelf, start: u64 = 0, count: u64 = ~0u): span<i64> {
return count == ~0u ? other.items[start:] : other.items[start:count];
}
def shelf::slice_of(other: &mut shelf, start: u64 = 0, count: u64 = ~0u): slice<i64> {
return count == ~0u ? other.items[start:] : other.items[start:count];
}
trait views { def span_of(other: &self, start: u64 = 0, count: u64 = ~0u): span<i64>; }
def tail<V>(v: &V): span<i64> where V: views { return v[1:]; }
def whole<V>(v: &V): span<i64> where V: views { return V::span_of(v); }
def f(): void {
val read := shelf { items = [1, 2, 3, 4] };
var written := shelf { items = [1, 2, 3, 4] };
val r := read[1:2];
val w := written[:2];
val held := &written;
val h := held[2:];
val b := tail(&read);
val a := whole(&read);
}
""";
# The views the syntax makes, which `mini_std` holds but for `slice`.
val views := """
module std; module core;
pub struct slice<T> { pub var data: *mut T; pub var count: u64; }
""";
t.expects(checks(shelf, mini_std, views));
t.expects(local_type("r", shelf, mini_std, views).span == "span<int>");
t.expects(local_type("w", shelf, mini_std, views).span == "slice<int>");
t.expects(local_type("h", shelf, mini_std, views).span == "span<int>");
# What is no view, and gives neither, is not sliced; what only `slice_of`
# slices is sliced where it can be written; a `span_of` is given the bounds;
# and the bounds are integers.
val refused := """
struct board { var cells: u8[4]; }
def board::slice_of(other: &mut board, start: u64 = 0, count: u64 = ~0u): slice<u8> { return other.cells[start:2]; }
struct old { var cells: u8[4]; }
def old::span_of(other: &old): span<u8> { return other.cells[:]; }
def f(b: &board, o: &old, d: &mut board): void {
val n := 5;
val x := n[0:1];
val y := b[0:2];
val z := o[0:2];
val w := d["a":1];
}
""";
t.expects(check_noted(7, 13, ec::operands, refused, mini_std, views));
t.expects(check_noted(8, 13, ec::assignable, refused, mini_std, views));
t.expects(check_noted(9, 13, ec::arguments, refused, mini_std, views));
t.expects(check_noted(10, 14, ec::mismatch, refused, mini_std, views));
# A type's function meets one a trait asks for where it gives a default for
# each parameter the trait does, and a trait's default is of its parameter's
# type.
t.expects(check_noted(2, 8, ec::bound, """
trait views { def span_of(other: &self, start: u64 = 0, count: u64 = ~0u): span<i64>; }
struct bare : views { var items: i64[2]; }
def bare::span_of(other: &bare, start: u64, count: u64): span<i64> { return other.items[start:count]; }
""", mini_std, views));
t.expects(check_noted(1, 54, ec::mismatch, """
trait views { def span_of(other: &self, start: u64 = "a", count: u64 = ~0u): span<i64>; }
""", mini_std, views));
}
@test def a_column_keeps_where_its_rows_are(t: &mut tester): void {
var made := column::<u32>::new();
for i in 0..100 {
made.append(i as u32);
}
t.expects(made.length == 100 && made.p[0] == 0 && made.p[99] == 99);
made.resize(300);
t.expects(made.length == 300 && made.p[99] == 99 && made.p[100] == 0 && made.p[299] == 0);
}
@test def an_index_lists_each_declaration_once_in_order(t: &mut tester): void {
var made := decl_index::new();
made.add(3, 10);
made.add(3, 4);
made.add(3, 12);
made.add(3, 4);
made.add(5, 1);
val listed := made.of(3);
t.expects(listed.length == 3 && listed[0] == 4 && listed[1] == 10 && listed[2] == 12);
t.expects(made.of(5).length == 1 && made.of(6).length == 0);
}
@test def a_ternary_names_what_its_tests_give(t: &mut tester): void {
t.expects(checks(mini_std, """
import std;
def f(a: maybe<i64>): i64 { return a is some(x) ? x + 1 : 0; }
def g(a: maybe<i64>): i64 { return a is not some(x) ? 0 : x; }
"""));
# Only in the branch where the test holds.
t.expects(check_noted(2, 55, ec::undefined, mini_std, """
import std;
def f(a: maybe<i64>): i64 { return a is some(x) ? 0 : x; }
"""));
}
@test def a_literal_of_text_is_walked_as_a_view(t: &mut tester): void {
t.expects(checks(mini_std, ranges_std, """
import std;
def f(): u64 {
var n: u64 = 0;
for c in "abc" { n += c as u64; }
return n;
}
"""));
}
@test def a_lambda_whose_types_are_written_gives_its_own(t: &mut tester): void {
# To a type parameter where overloads are chosen between, and to the type
# parameters a function type leaves unknown.
t.expects(checks(mini_std, ranges_std, """
import std;
struct w { var n: i64; }
def w::each<A, F>(&mut this, f: F): void where F: invokable<def(A): void> {}
def w::each<A, B, F>(&mut this, f: F): void where F: invokable<def(A, B): void> {}
def tested<T>(test: def(&T): bool): bool { return true; }
def f(): void {
var x := w{ n = 0 };
x.each(def(a: &i64) {});
x.each(def(a: &mut i64, b: &bool) {});
_ = tested(def(v: &u8) => true);
}
"""));
}
@test def a_pack_of_types_has_a_length(t: &mut tester): void {
t.expects(checks(mini_std, """
import std;
def count<Ts...>(xs: Ts...): u64 { return Ts.length; }
def f(): u64 { return count(1, true); }
"""));
}
@test def a_property_is_read_rather_than_called(t: &mut tester): void {
t.expects(check_noted(3, 30, ec::operands, """
struct s { var n: i64; }
val s::big: bool { get => this.n > 3; }
def f(v: s): bool { return v.big(); }
"""));
}
@test def a_copy_a_test_names_is_only_read(t: &mut tester): void {
t.expects(check_noted(2, 53, ec::assignable, mini_std, """
import std;
def f(a: maybe<&mut i64>): void { if a is some(x) { x = 3; } }
"""));
t.expects(checks(mini_std, """
import std;
def f(a: maybe<&mut i64>): void { if a is some(&mut x) { x = 3; } }
"""));
}
@test def a_property_called_through_its_type_is_reported_as_one(t: &mut tester): void {
# `span::<T>::empty()`, a function before `empty` was a property, is told to
# read it of a value, and, where the type has one, that `blank()` makes an
# empty value.
val made := check_program("", """
struct bag { var n: i64; }
val bag::empty: bool { get => this.n == 0; }
def bag::blank(): bag { return bag { n = 0 }; }
struct box { var n: i64; }
val box::empty: bool { get => this.n == 0; }
def f(): void {
val a := bag::empty();
val b := box::empty();
}
""");
t.expects(made.notes.length == 2, $"{made.notes.length}".span);
if made.notes.length == 2 {
t.expects(made.notes[0].line == 7 && made.notes[0].code == ec::operands, made.notes[0].detail.span);
t.expects(made.notes[0].detail.span.find("`empty` is a property") is some && made.notes[0].detail.span.find("`blank()`") is some, made.notes[0].detail.span);
t.expects(made.notes[1].line == 8 && made.notes[1].detail.span.find("blank") is none, made.notes[1].detail.span);
}
}
@test def a_call_whose_argument_failed_says_nothing_more(t: &mut tester): void {
# An argument in error fits any overload, or none, which says nothing of the
# call, so only what went wrong within it is reported.
t.expects(note_count("""
def root_of(n: i64): i64 { return n; }
def root_of(n: f64): f64 { return n; }
def f(): void {
val a := root_of(missing);
val b := root_of(missing + 1);
}
""") == 2);
}