Files
sakurac/analyzer/checker/operators.sakura
T

1724 lines
74 KiB
Plaintext
Raw Normal View History

2026-10-26 10:14:11 -05:00
#! @file operators.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the checking of operators: prefix and postfix operators, taking a
#! reference, the binary operators, assignment, ternaries, casts with `as`,
#! and tests with `is`. Operators on constants give constants, so the length
#! of an array or the value of an enum member may be worked out of them.
module sakura::analyzer;
#! A prefix operator: `-x`, `!x`, `~x`, `++x`, `--x`, or `*p`.
#! @param this The checker.
#! @param node The operator's node.
#! @param expected The type expected of its value, or the error type where
#! none is.
#! @returns The type of its value, or the error type where it is not valid.
def checker::unary(&mut this, node: u32, expected: u32): u32 {
val operand := this.left_of(node);
match this.token_kind(node) {
tk::minus => {
val kind := this.infer_expr(operand);
return bt::error if kind == bt::error;
# A value of a type not chosen yet is used as each way it can be chosen.
return this.use_ways(node, kind, expected) if this.uses_ways(kind);
if this.types.kind_of(kind) == ty::untyped_uint {
this.report(node, ec::overflow, "An integer written with `u` is unsigned, so it cannot be negated.");
return bt::error;
}
if this.operates_by_method(kind) {
return this.operate(node, word::negate, operand, 0, expected);
}
if !this.numeric_like(this.read_type(kind)) {
this.report_operand(node, kind, "`-` negates a number.");
return bt::error;
}
if this.is_known(operand) {
val value := this.value_at(operand);
if value.floating {
this.set_value(node, constant::real_of(-value.real));
} else {
this.set_value(node, constant::integer(value.magnitude, !value.negative));
}
}
return this.operated(kind, kind, this.read_type(kind));
}
tk::negate => {
val kind := this.infer_expr(operand);
return bt::error if kind == bt::error;
return this.use_ways(node, kind, expected) if this.uses_ways(kind);
if !this.types.is_boolean(this.read_type(kind)) {
this.report_operand(node, kind, "`!` denies a `bool`.");
return bt::error;
}
if this.is_known(operand) {
this.set_value(node, constant::truth(this.value_at(operand).magnitude == 0));
}
return this.read_type(kind);
}
tk::bitnot => {
val kind := this.infer_expr(operand);
return bt::error if kind == bt::error;
return this.use_ways(node, kind, expected) if this.uses_ways(kind);
if this.operates_by_method(kind) {
return this.operate(node, word::bit_not, operand, 0, expected);
}
val read := this.read_type(kind);
if !this.integer_like(read) {
this.report_operand(node, kind, "`~` flips the bits of an integer.");
return bt::error;
}
if this.is_known(operand) {
val value := this.value_at(operand);
val plain := this.unalias(read);
val signed := this.types.kind_of(plain) != ty::untyped_uint && this.types.kind_of(plain) != ty::unsigned;
# An unsigned type keeps only its own bits, so `~0` of a `u8` is 255.
var flipped := ~value.bits();
if this.types.kind_of(plain) == ty::unsigned && this.types.first_of(plain) < 64 {
flipped &= (1u << this.types.first_of(plain) as u64) - 1;
}
this.set_value(node, constant::from_bits(flipped, signed));
}
return this.operated(kind, kind, read);
}
tk::increment, tk::decrement => return this.step(node, operand);
tk::star => {
# References are read through to the pointer they refer to, and a type
# parameter bounded by `pointer` is one, which writes where `if const
# T::mutable` says so.
val kind := this.infer_expr(operand);
return bt::error if kind == bt::error;
return this.use_ways(node, kind, expected) if this.uses_ways(kind);
val inner := this.unalias(this.shaped(this.reached(kind)));
if this.types.kind_of(inner) == ty::pointer {
this.mark_place(node, this.types.second_of(inner) != 0);
return this.types.first_of(inner);
}
this.report_operand(node, kind, "`*` reaches what a pointer points at.");
return bt::error;
}
tk::spread => {
this.report(node, ec::operands, "`...` spreads a view or a pack into the arguments of a call, and is only written there.");
this.infer_expr(operand);
return bt::error;
}
else => {}
}
return bt::error;
}
#! The type a value of the type is read as: what plain references refer to,
#! however many there are, and an alias's own type where it stands for a number.
#! @param this The checker.
#! @param kind The type.
#! @returns The type it is read as.
pub def checker::read_type(&mut this, kind: u32): u32 {
var at := kind;
var steps := 0;
while this.types.kind_of(at) == ty::reference && steps < 8 {
at = this.types.first_of(at);
steps++;
}
val plain := this.unalias(at);
return this.types.is_numeric(plain) || this.types.is_boolean(plain) ? plain : at;
}
#! `x++`, `x--`, `++x`, or `--x`, which change a number in place.
#! @param this The checker.
#! @param node The operator's node.
#! @param operand The node of the number it changes.
#! @returns The number's type, or the error type where it is not valid.
def checker::step(&mut this, node: u32, operand: u32): u32 {
# A place reached from a value of a type not chosen yet, as `(*p)++`, is
# changed as each way the type can be chosen, which may write it or not.
val root := this.unchosen_root(operand);
return this.use_ways(node, root, bt::error) if root != 0;
val kind := this.infer_expr(operand);
return bt::error if kind == bt::error;
return this.use_ways(node, kind, bt::error) if this.uses_ways(kind);
if !this.numeric_like(this.read_type(kind)) && this.types.kind_of(this.unalias(kind)) != ty::pointer {
this.report_operand(node, kind, "`++` and `--` change a number.");
return bt::error;
}
this.require_writable(operand);
return this.read_type(kind);
}
#! Whether a place holding a reference is pointed elsewhere by assigning it a
#! reference: a variable or field that can be written, but not a parameter,
#! through which a reference writes what it refers to, as the C compiler does.
#! @param this The checker.
#! @param target The place's node.
#! @returns Whether it is.
pub def checker::rebinds(&this, target: u32): bool {
return false if !this.is_place(target) || !this.is_writable(target);
if this.kind_of(target) == nk::name {
val ref := this.ref_at(target);
if (ref & ref_local) != 0 && this.kind_of(ref & ref_mask) == nk::param {
return false;
}
}
return true;
}
#! The name an `is` test or an arm gave a copy of what a union's field refers to,
#! where a place is reached from one, or 0.
#! @param this The checker.
#! @param node The place.
#! @returns The node declaring the name, or 0.
def checker::copied_binding(&this, node: u32): u32 {
var at := node;
var steps := 0;
while (this.kind_of(at) == nk::member || this.kind_of(at) == nk::index) && steps < 64 {
at = this.left_of(at);
steps++;
}
return 0 if this.kind_of(at) != nk::name;
val ref := this.ref_at(at);
return 0 if (ref & ref_local) == 0;
val local := ref & ref_mask;
return local if this.has_flag(local, rf::narrowed) && this.has_flag(local, rf::valued);
return 0;
}
#! Reports what is written to where it cannot be.
#! @param this The checker.
#! @param node The node of what is written.
#! @returns Whether it can be written.
pub def checker::require_writable(&mut this, node: u32): bool {
this.check_walked(node);
return true if this.is_writable(node) || this.type_at(node) == bt::error;
# What a `&mut` reference refers to is written through it, whether a call
# gives it or a `val` holds it.
val kind := this.type_at(node);
return true if this.types.kind_of(kind) == ty::reference && this.types.second_of(kind) != 0;
if this.is_place(node) {
# What an `is` test named by a copy of what a field refers to, as `p` of
# `x is some(p)` of a `maybe<&mut T>`, is only read; `&mut p` names the
# reference, which writes through.
val copied := this.copied_binding(node);
if copied != 0 {
val name := this.names.text(this.name_of(copied));
var text := string::from("`", nothing {});
text.append(name);
text.append("` is a copy of what the field refers to, which is only read: `&mut ");
text.append(name);
text.append("` names the reference itself, which writes through.");
this.report_text(node, ec::assignable, &text);
return false;
}
# What a type parameter bounded by `pointer` points at is written only where
# the pointer is known to write it.
if this.kind_of(node) == nk::unary && this.token_kind(node) == tk::star {
val pointer := this.std_decl(word::pointer);
val held := this.unalias(this.reached(this.type_at(this.left_of(node))));
if pointer != 0 && this.types.kind_of(held) == ty::param && this.param_bounded(held, pointer) {
this.report(node, ec::assignable,
"A pointer of a type parameter writes what it points at only where `if const T::mutable` says it does.");
return false;
}
}
this.report(node, ec::assignable,
"This cannot be written: it is a `val` or a constant, or reached through `&` rather than `&mut`.");
} else if this.types.kind_of(kind) == ty::reference {
this.report(node, ec::assignable, "A `&` reference reads what it refers to; write through a `&mut` one.");
} else {
this.report(node, ec::assignable, "Only a variable, a field, an element, or what a reference reaches is written.");
}
return false;
}
#! `&x` or `&mut x`. A value that is no place is copied to a new place first, so
#! `&200` refers to a copy of the constant.
#! @param this The checker.
#! @param node The `reference` node.
#! @param expected The type expected of it, or the error type where none is.
#! @returns The reference's type, or the error type where it is not valid.
def checker::reference(&mut this, node: u32, expected: u32): u32 {
val operand := this.left_of(node);
val mutable := this.node_has(node, nf::mutable);
# What a reference or a pointer is expected to reach types a constant it is to.
val reaching := this.unalias(expected);
val wanted: u32 = this.types.kind_of(reaching) == ty::reference || this.types.kind_of(reaching) == ty::pointer
? this.types.first_of(reaching)
: bt::error;
# A reference to a constant keeps the constant's type until the type it
# refers to is known, as `&"text"` given for a `&strview`.
var kind := this.infer_expr(operand);
return bt::error if kind == bt::error;
if this.types.is_untyped(kind) && wanted != bt::error {
kind = this.check_expr(operand, wanted);
}
if mutable {
this.check_walked(operand);
}
# A property's value is copied as any other value is, so only `&mut` is refused.
val held := this.ref_at(operand);
if mutable && this.kind_of(operand) == nk::member && held != 0 && (held & (ref_local | ref_type)) == 0
&& this.decls.kinds.p[held] == dk::property {
this.report(node, ec::invalid, "A property is read and written through its accessors, so `&mut` has no place to refer to; assign it instead.");
}
# A reference, as a variable or a call gives one, is lent as it is, since
# references never stack: `&` of one is that reference, and `&mut` of one
# writes where it does, whatever holds it. A `&` one only reads.
val given := this.unalias(kind);
if this.decide(node, rf::relent, this.types.kind_of(given) == ty::reference) {
if mutable && this.types.second_of(given) == 0 {
this.report(node, ec::assignable, "`&mut` refers to what can be written, and this is reached through `&`.");
}
return this.types.reference(this.types.first_of(given), mutable);
}
if mutable && this.is_place(operand) && !this.is_writable(operand) {
this.report(node, ec::assignable,
"`&mut` refers to what can be written, and this is a `val` or a constant, or reached through `&`.");
}
this.check_lends_field(node, operand, mutable);
return this.types.reference(kind, mutable);
}
#! Whether a value is a reference written as one, `&x` or `&mut x`, or a choice
#! between such, as `c ? &a : &b`. Only these lend: anything else, though its
#! type is a reference, as a call giving one, is taken as the value it reaches
#! where a binding, `for`, a pattern, or inference takes it.
#! @param this The checker.
#! @param node The value's node.
#! @returns Whether it is.
pub def checker::lent(&this, node: u32): bool {
match this.kind_of(node) {
nk::reference => return true;
nk::ternary => {
val chosen := this.chosen_arm(node);
return this.lent(chosen) if chosen != 0;
return this.lent(this.record(node, if_slot::then)) && this.lent(this.record(node, if_slot::other));
}
nk::match_value => {
if this.is_match_const(node) {
val chosen := this.chosen_arm(node);
return chosen != 0 && this.lent(this.right_of(chosen));
}
val arms := this.right_of(node);
return false if this.run_length(arms) == 0;
for &arm in this.run_view(arms) {
return false if !this.lent(this.right_of(arm));
}
return true;
}
else => return false;
}
return false;
}
#! Reports `&` or `&mut` lending a union's field, or what is within one, where
#! it could outlive the union holding that field: written through `&mut` once
#! the union holds another, or read through `&`, where what it refers to
#! closes, once that is closed. A test asks which field the union holds first,
#! as `is some(&mut q)`.
#! @param this The checker.
#! @param node The `reference` node.
#! @param operand What it refers to.
#! @param mutable Whether it is `&mut`.
def checker::check_lends_field(&mut this, node: u32, operand: u32, mutable: bool): void {
var at := operand;
var steps := 0;
while steps < 64 && (this.kind_of(at) == nk::member || this.kind_of(at) == nk::index) {
steps++;
val left := this.left_of(at);
# What a reference or pointer reaches is not held within the value it is in.
val through := this.types.kind_of(this.unalias(this.type_at(left)));
if this.kind_of(at) == nk::member {
val held := this.ref_at(at);
if held != 0 && (held & (ref_local | ref_type)) == 0 && this.decls.kinds.p[held] == dk::field {
val owner: u32 = this.decls.owners.p[held];
if this.decls.kinds.p[owner] == dk::union_type && !this.decls.has(owner, df::foreign) {
if mutable {
this.report(node, ec::invalid, "`&mut` of a union's field would write it whichever field the union holds: name it where it is held, as `if m is some(&mut q)`, or `match` by `&mut`.");
return;
}
val kind := this.type_at(operand);
if this.closes(kind) {
var text := string::from("`", nothing {});
val named := this.describe(kind);
text.append(named.span);
text.append("` is closed once the union holds another field, so `&` of it could outlive it: name it where it is held, as `if r is value(&p)`, or `match` by `&`.");
this.report_text(node, ec::owned, &text);
return;
}
}
}
}
break if through == ty::reference || through == ty::pointer;
at = left;
}
}
#! Reports an operator applied to what it does not take.
#! @param this The checker.
#! @param node The operator's node.
#! @param kind The operand's type, which the note names.
#! @param detail What the note says after naming it.
def checker::report_operand(&mut this, node: u32, kind: u32, detail: strview): void {
var text := string::from(detail, nothing {});
text.append(" This is `");
val named := this.describe(kind);
text.append(named.span);
text.append("`.");
this.report_text(node, ec::operands, &text);
}
#! A binary operator.
#! @param this The checker.
#! @param node The operator's node.
#! @param expected The type expected of its value, or the error type where
#! none is.
#! @returns The type of its value, or the error type where it is not valid.
def checker::binary(&mut this, node: u32, expected: u32): u32 {
val operator := this.token_kind(node);
val left := this.left_of(node);
val right := this.right_of(node);
if operator == tk::logand || operator == tk::logor {
# A test naming what a value of a type not chosen yet holds names it as each
# way the type can be chosen, so what joins it is checked in each.
val unchosen := this.unchosen_binding(node);
return this.use_ways(node, unchosen, expected) if unchosen != 0;
# What the left of `&&` names where it holds is named on its right, and what the
# left of `||` names where it does not.
this.check_condition(left);
val mark := this.scope_mark();
this.bind_tests(left, operator == tk::logand);
this.check_condition(right);
this.unbind_to(mark);
this.fold(node, operator, left, right, false);
return bt::boolean;
}
val a := this.infer_expr(left);
val b := this.infer_expr(right);
return bt::error if a == bt::error || b == bt::error;
# A value of a type not chosen yet is used as each way it can be chosen.
return this.use_ways(node, a, expected) if this.uses_ways(a);
return this.use_ways(node, b, expected) if this.uses_ways(b);
if operator == tk::lshift || operator == tk::rshift {
# A shift of what is no number is a call of `shift_left` or `shift_right`.
if this.operates_by_method(a) {
return this.operate(node, this.arithmetic_word(operator), left, right, expected);
}
return this.shift(node, operator, left, right, a, b);
}
val comparing := operator.compares();
# Pointers move by elements, and two count the elements between them, as do values
# of a type parameter `pointer` bounds.
val pa := this.unalias(a);
if (operator == tk::plus || operator == tk::minus) && !comparing && this.pointer_like(pa) {
val pb := this.unalias(this.read_type(b));
if operator == tk::minus && (this.types.kind_of(pb) == ty::pointer || pb == pa) {
return bt::int64;
}
if this.types.is_integer(pb) {
if this.types.is_untyped(b) {
this.check_expr(right, bt::int64);
}
return a;
}
}
# Arithmetic on what is no number is a call of the method that gives the
# operator, as `+` is of `add`, which the left operand's type is asked for: a
# struct or union, a type parameter through its bounds, or a number where the
# right operand is none.
val word := this.arithmetic_word(operator);
if word != 0 {
val numbered := this.types.is_numeric(this.unalias(this.read_type(a)));
if numbered ? !this.types.is_numeric(this.unalias(this.read_type(b))) && this.operates_by_method(b) : this.operates_by_method(a) {
return this.operate(node, word, left, right, expected);
}
}
# Bitwise operators take enums as their integers, as flags are combined.
val bitwise_op := operator == tk::bitand || operator == tk::bitor || operator == tk::bitxor;
var la := a;
var lb := b;
if bitwise_op && this.named_kind(this.unalias(this.read_type(a))) == dk::enum_type {
la = this.enum_integer(this.unalias(this.read_type(a)));
this.set_convert(left, la);
}
if bitwise_op && this.named_kind(this.unalias(this.read_type(b))) == dk::enum_type {
lb = this.enum_integer(this.unalias(this.read_type(b)));
this.set_convert(right, lb);
}
if la != a || lb != b {
val joined := this.common(la, lb);
if joined != bt::error {
this.fold(node, operator, left, right, false);
return joined;
}
}
# A string owns its text and a view only reads one, so which of the two a
# mixed comparison reads is not obvious, and `view()` says it. Text written in
# place is whichever the other is.
if comparing {
val ra := this.read_type(a);
val rb := this.read_type(b);
if (this.is_std(this.unalias(ra), word::string) && this.is_byte_view(rb))
|| (this.is_byte_view(ra) && this.is_std(this.unalias(rb), word::string)) {
this.report(node, ec::operands, "A string owns its text and a view only reads one, so compare the text of both, as `s.span == v`.");
return bt::error;
}
}
val shared := this.shared_type(node, operator, a, b);
return bt::error if shared == bt::error;
if comparing {
# Integers of different signs compare exactly, as they are.
val ra := this.unalias(this.read_type(a));
val rb := this.unalias(this.read_type(b));
val mixed := this.types.is_integer(ra) && this.types.is_integer(rb) && !this.types.is_untyped(ra)
&& !this.types.is_untyped(rb) && this.types.is_signed(ra) != this.types.is_signed(rb);
if !mixed {
this.convert_operand(left, shared);
this.convert_operand(right, shared);
}
this.check_compared(node, operator, shared);
this.fold(node, operator, left, right, this.types.is_signed(shared));
return this.types.is_untyped(a) && this.types.is_untyped(b) && this.is_known(node)
? bt::untyped_bool
: bt::boolean;
}
this.convert_operand(left, shared);
this.convert_operand(right, shared);
val numeric := this.read_type(shared);
val bitwise := operator == tk::bitand || operator == tk::bitor || operator == tk::bitxor;
if bitwise && !this.integer_like(numeric) {
this.report_operands(node, a, b, "are not both integers, which the operator takes");
return bt::error;
}
if !bitwise && !this.numeric_like(numeric) {
this.report_operands(node, a, b, "are not both numbers, which the operator takes");
return bt::error;
}
if !this.fold(node, operator, left, right, this.types.is_signed(numeric)) && this.is_known(left) && this.is_known(right) {
this.report(node, ec::overflow, "The constant overflows 64 bits, or divides by zero.");
return bt::error;
}
if this.is_known(node) && !this.types.is_untyped(numeric) {
val value := this.value_at(node);
if !this.in_range(numeric, &value) {
var text := string::from("The constant is outside the range of `", nothing {});
val named := this.describe(numeric);
text.append(named.span);
text.append("`.");
this.report_text(node, ec::overflow, &text);
}
}
return this.operated(a, b, numeric);
}
#! The type an arithmetic or bitwise operator gives, which keeps an alias its
#! operands are written with, as C's checker does: the right operand's type where
#! it is the number they operate as, else the left's, else that number. So `a | b`
#! of two masks is a mask, with a mask's methods.
#! @param this The checker.
#! @param a The left operand's type.
#! @param b The right operand's type.
#! @param numeric The number they operate as.
#! @returns The type.
def checker::operated(&mut this, a: u32, b: u32, numeric: u32): u32 {
val written_b := this.types.kind_of(b) == ty::reference ? this.types.first_of(b) : b;
val written_a := this.types.kind_of(a) == ty::reference ? this.types.first_of(a) : a;
return written_b if this.unalias(written_b) == numeric;
return written_a if this.unalias(written_a) == numeric;
return numeric;
}
#! The type both operands of a binary operator take: the type they share, an
#! integer and a float operating as the float, and integers of different signs
#! comparing exactly.
#! @param this The checker.
#! @param node The operator's node.
#! @param operator The operator.
#! @param a The left operand's type.
#! @param b The right operand's type.
#! @returns The type they share, or the error type where they share none.
def checker::shared_type(&mut this, node: u32, operator: tk, a: u32, b: u32): u32 {
val ra := this.read_type(a);
val rb := this.read_type(b);
val shared := this.common(ra, rb);
return shared if shared != bt::error;
val comparing := operator.compares();
if this.types.is_integer(ra) && this.types.is_integer(rb) && comparing {
return this.types.is_untyped(ra) ? rb : ra;
}
# An enum is its integer type with an integer.
val ua := this.unalias(ra);
val ub := this.unalias(rb);
if this.named_kind(ua) == dk::enum_type && this.types.is_integer(ub) {
return this.shared_type(node, operator, this.enum_integer(ua), b);
}
if this.named_kind(ub) == dk::enum_type && this.types.is_integer(ua) {
return this.shared_type(node, operator, a, this.enum_integer(ub));
}
if this.types.is_float(ua) && this.types.is_integer(ub) && !this.types.is_untyped(ub) {
return ua;
}
if this.types.is_float(ub) && this.types.is_integer(ua) && !this.types.is_untyped(ua) {
return ub;
}
# A pointer compares with `null`.
if comparing && (this.types.kind_of(ua) == ty::null_type || this.types.kind_of(ub) == ty::null_type) {
return this.types.kind_of(ua) == ty::null_type ? rb : ra;
}
this.report_operands(node, a, b, "share no type, so convert one with `as`");
return bt::error;
}
#! Converts an operand to the type both operands share, where an integer
#! operating with a float operates as the float.
#! @param this The checker.
#! @param node The operand's node.
#! @param shared The type both share.
def checker::convert_operand(&mut this, node: u32, shared: u32): void {
val kind := this.read_type(this.type_at(node));
if this.types.is_float(this.unalias(shared)) && this.types.is_integer(this.unalias(kind)) && !this.types.is_untyped(kind) {
this.set_convert(node, shared);
return;
}
this.convert(node, shared);
}
#! Reports operands an operator does not take.
#! @param this The checker.
#! @param node The operator's node.
#! @param a The left operand's type.
#! @param b The right operand's type.
#! @param why What the note says after naming them.
def checker::report_operands(&mut this, node: u32, a: u32, b: u32, why: strview): void {
var text := string::from("`", nothing {});
val first := this.describe(a);
val other := this.describe(b);
text.append(first.span);
text.append("` and `");
text.append(other.span);
text.append("` ");
text.append(why);
text.append(".");
this.report_text(node, ec::operands, &text);
}
#! Checks that values of the type compare as the operator asks: `==` and `!=`
#! on what is equatable, and `<` and the others on what is ordered.
#! @param this The checker.
#! @param node The comparison's node.
#! @param operator The comparison.
#! @param kind The type compared.
def checker::check_compared(&mut this, node: u32, operator: tk, kind: u32): void {
val plain := this.unalias(this.read_type(kind));
val equal := operator == tk::equals || operator == tk::noteq;
val ordered := this.types.is_numeric(plain) || this.named_kind(plain) == dk::enum_type
|| this.pointer_like(plain) || this.types.kind_of(plain) == ty::untyped_char;
return if ordered;
return if equal && (this.types.is_boolean(plain) || this.types.kind_of(plain) == ty::null_type);
return if equal && this.native_equality(plain, 0);
val word := equal ? word::equals : word::compare;
if this.has_method(plain, word) {
return;
}
# What an integer or an enum stands for is ordered as one is.
if this.types.kind_of(plain) == ty::param && this.param_ordinal(this.types.first_of(plain)) {
return;
}
var text := string::from("`", nothing {});
val named := this.describe(kind);
text.append(named.span);
text.append(equal
? "` has no `==`: a struct or union compares by implementing `equatable`."
: "` has no order: a struct or union is ordered by implementing `ordered`.");
this.report_text(node, ec::operands, &text);
}
#! Whether `==` compares values of the type without a method: numbers, `bool`,
#! enums, pointers, and fixed arrays of them.
#! @param this The checker.
#! @param kind The type.
#! @param depth How many fixed arrays deep it is, which stops a type nesting
#! without end.
#! @returns Whether it compares without one.
def checker::native_equality(&mut this, kind: u32, depth: u32): bool {
return false if depth > 8;
val plain := this.unalias(kind);
return true if this.types.is_numeric(plain) || this.types.is_boolean(plain)
|| this.named_kind(plain) == dk::enum_type || this.types.kind_of(plain) == ty::pointer
|| this.is_std(plain, word::cfn);
if this.types.kind_of(plain) == ty::fixed {
val element := this.types.first_of(plain);
return this.native_equality(element, depth + 1) || this.has_method(this.unalias(element), word::equals);
}
return false;
}
#! A shift, which takes integers of any types and gives its left's type.
#! @param this The checker.
#! @param node The shift's node.
#! @param operator `<<` or `>>`.
#! @param left The node of what is shifted.
#! @param right The node of the count.
#! @param a The type of what is shifted.
#! @param b The count's type.
#! @returns The left's type, or the error type where either is no integer.
def checker::shift(&mut this, node: u32, operator: tk, left: u32, right: u32, a: u32, b: u32): u32 {
val ra := this.read_type(a);
val rb := this.read_type(b);
if !this.integer_like(ra) || !this.integer_like(rb) {
this.report_operands(node, a, b, "are not both integers, which a shift takes");
return bt::error;
}
if this.types.is_untyped(rb) && !this.types.is_untyped(ra) {
this.check_expr(right, bt::uint64);
}
this.fold(node, operator, left, right, this.types.is_signed(ra));
return this.operated(a, a, ra);
}
#! Folds a binary operator of two constants into the constant it gives.
#! @param this The checker.
#! @param node The operator's node, which takes the constant.
#! @param operator The operator.
#! @param left The left operand's node.
#! @param right The right operand's node.
#! @param signed Whether the operands are signed, which division and shifts
#! care about.
#! @returns Whether it gives one.
def checker::fold(&mut this, node: u32, operator: tk, left: u32, right: u32, signed: bool): bool {
return false if !this.is_known(left) || !this.is_known(right);
val a := this.value_at(left);
val b := this.value_at(right);
var out := constant {};
return false if !this.fold_binary(operator, &a, &b, signed, &mut out);
this.set_value(node, out);
return true;
}
#! Whether arithmetic applies to values of the type: a number, or a type
#! parameter bounded by `numeric` or `integer`.
#! @param this The checker.
#! @param kind The type.
#! @returns Whether it applies.
pub def checker::numeric_like(&mut this, kind: u32): bool {
val plain := this.unalias(kind);
return this.types.is_numeric(plain) || (this.types.kind_of(plain) == ty::param && this.param_has_numeric(this.types.first_of(plain)));
}
#! Whether bitwise operators apply to values of the type: an integer, or a type
#! parameter bounded by `integer`.
#! @param this The checker.
#! @param kind The type.
#! @returns Whether they apply.
pub def checker::integer_like(&mut this, kind: u32): bool {
val plain := this.unalias(kind);
if this.types.kind_of(plain) == ty::param {
val integer := this.std_decl(word::integer);
return integer != 0 && this.param_bounded(plain, integer);
}
return this.types.is_integer(plain);
}
#! Whether a value of the type is an address: a pointer, or a value of a type
#! parameter bounded by `pointer`.
#! @param this The checker.
#! @param kind The type.
#! @returns Whether it is one.
pub def checker::pointer_like(&mut this, kind: u32): bool {
val plain := this.unalias(kind);
if this.types.kind_of(plain) == ty::param {
val pointer := this.std_decl(word::pointer);
return pointer != 0 && this.param_bounded(plain, pointer);
}
return this.types.kind_of(plain) == ty::pointer;
}
#! The method an arithmetic or bitwise operator, or a shift, calls on what is no
#! number.
#! @param this The checker.
#! @param operator The operator, as `+` or `+=`.
#! @returns The ID of the method's name, as `add`.
def checker::arithmetic_word(&this, operator: tk): u32 {
return match operator {
tk::plus, tk::pluseq => word::add,
tk::minus, tk::minuseq => word::sub,
tk::star, tk::stareq => word::mul,
tk::slash, tk::slasheq => word::div,
tk::modulo, tk::moduloeq => word::rem,
tk::bitand, tk::bitandeq => word::bit_and,
tk::bitor, tk::bitoreq => word::bit_or,
tk::bitxor, tk::bitxoreq => word::bit_xor,
tk::lshift, tk::lshifteq => word::shift_left,
tk::rshift, tk::rshifteq => word::shift_right,
else => 0
};
}
#! The method a compound assignment calls on its target in place, where the
#! target's type has one taking the value, as `+=` calls `add_assign`, or 0.
#! @param this The checker.
#! @param operator The compound assignment's operator.
#! @returns The ID of the method's name, or 0.
def checker::in_place_word(&this, operator: tk): u32 {
return match operator {
tk::pluseq => word::add_assign,
tk::minuseq => word::sub_assign,
tk::stareq => word::mul_assign,
tk::slasheq => word::div_assign,
tk::moduloeq => word::rem_assign,
tk::bitandeq => word::bit_and_assign,
tk::bitoreq => word::bit_or_assign,
tk::bitxoreq => word::bit_xor_assign,
tk::lshifteq => word::shift_left_assign,
tk::rshifteq => word::shift_right_assign,
else => 0
};
}
#! Whether an operator on a value of the type is a call of the method that
#! gives it: a struct or union, or a type parameter no bound makes a number or
#! a pointer of, which has the methods its bounds ask for.
#! @param this The checker.
#! @param kind The type.
#! @returns Whether it is.
pub def checker::operates_by_method(&mut this, kind: u32): bool {
val plain := this.unalias(this.reached(kind));
val named := this.named_kind(plain);
return true if named == dk::struct_type || named == dk::union_type;
return this.types.kind_of(plain) == ty::param && !this.numeric_like(plain) && !this.pointer_like(plain);
}
#! An operator calling the method that gives it, as `+` calls `add`. The type of
#! the operand it is called on is asked for the method, and the overload the
#! other operand fits is chosen as a call's is, so a constant takes the type of
#! the parameter, and the operator gives what the method gives. The node keeps
#! the method, which the compiler calls.
#! @param this The checker.
#! @param node The operator's node.
#! @param word The ID of the method's name.
#! @param left The node of the operand the method is called on.
#! @param right The node of the other operand, or 0 for a prefix operator.
#! @param expected The type expected of what it gives, or the error type
#! where none is.
#! @returns The type the method gives, or the error type where the type has no
#! such method taking the other operand.
def checker::operate(&mut this, node: u32, word: u32, left: u32, right: u32, expected: u32): u32 {
# A constant is asked as the number it is alone.
var kind := this.type_at(left);
if this.types.is_untyped(kind) {
kind = this.settle_expr(left);
}
val through := this.reached(kind);
val found := this.find_member(through, word);
var args := vector::<u32>::new();
var kinds := vector::<u32>::new();
if right != 0 {
args.append(right);
kinds.append(this.infer_expr(right));
}
var best := checker::candidate { worst = fit::none };
var tied := false;
if found.decl != 0 && this.decls.kinds.p[found.decl] == dk::function {
val alone := this.decls.overloads.p[found.decl] == 0 && this.decls.has(found.decl, df::generic);
val writes := this.receiver_writable(left);
val plain := this.unalias(through);
# An `@optional` method may be missing from what a bound stands for.
val bounded := this.types.kind_of(plain) == ty::param || this.named_kind(plain) == dk::trait_type;
# A method with type parameters of its own, beyond its type's, is none an
# operator calls, since nothing is written to give them.
val owned: u32 = this.types.kind_of(found.owner) == ty::named ? this.types.part_count(found.owner) : 0;
# A program's method on `core`'s type is chosen where it fits, and `core`'s
# of its name where none does.
var chain := found.decl;
while chain != 0 {
var walk := this.decls.walk_overloads(chain);
while true {
val at := walk.next();
break if at == 0;
val own := this.enclosing_trait(at) == 0 && this.decls.list_length(this.decls.params.p[at]) > owned;
if this.decls.has(at, df::method) && !own && !(bounded && this.decls.has(at, df::optional)) && this.member_seen(at) {
val made := this.try_candidate(at, left, found.owner, through, span::<u32>::blank(), args.span, kinds.span, expected, alone);
# An operator's method takes its operands and nothing more: `this`, and the
# other operand by reference.
val shaped := this.types.part_count(made.kind) as u64 == args.length + 1 && this.types.second_of(made.kind) == 0
&& (right == 0 || this.types.kind_of(this.unalias(this.types.part_at(made.kind, 1))) == ty::reference);
if made.worst != fit::none && made.worst != fit::overflow && !made.later && shaped {
this.keep_better(&made, &mut best, &mut tied, true, writes);
}
}
}
break if best.decl != 0;
chain = this.core_behind(chain);
}
}
if best.decl == 0 {
var text := string::from("`", nothing {});
val named := this.describe(through);
text.append(named.span);
text.append("` has no `");
text.append(this.names.text(word));
if right != 0 && kinds[0] != bt::error {
text.append("` taking a `&");
val other := this.describe(this.settle(this.reached(kinds[0])));
text.append(other.span);
}
text.append("`, which `");
text.append(this.text_of(node));
text.append("` calls.");
this.report_text(node, ec::operands, &text);
return bt::error;
}
if tied {
var text := string::from("The operands fit more than one `", nothing {});
text.append(this.names.text(word));
text.append("` as well, so convert the right one to the type of the one meant.");
this.report_text(node, ec::ambiguous, &text);
}
return this.apply_candidate(node, node, &best, left, args.span, kinds.span);
}
#! An assignment, `=` or a compound one as `+=`, which gives the value assigned.
#! @param this The checker.
#! @param node The `assign` node.
#! @returns The type of the value assigned, or the error type where it is not
#! valid.
def checker::assign(&mut this, node: u32): u32 {
val operator := this.token_kind(node);
val target := this.left_of(node);
val value := this.right_of(node);
# `_ = x` evaluates `x` and lets go of it.
if operator == tk::assign && this.kind_of(target) == nk::name && this.name_of(target) == names::underscore_id {
return this.settle_expr(value);
}
# A place of a type not chosen yet, or reached from a value of one, as `*p`
# is, is written as each way the type can be chosen, which may write it or
# not.
val root := this.unchosen_root(target);
return this.use_ways(node, root, bt::error) if root != 0;
val kind := this.infer_expr(target);
if kind == bt::error {
this.infer_expr(value);
return bt::error;
}
return this.use_ways(node, kind, bt::error) if this.uses_ways(kind);
# A property is written through its `set`, wanted as its `get` is where read.
val written := this.ref_at(target);
if this.kind_of(target) == nk::member && written != 0 && (written & (ref_local | ref_type)) == 0
&& this.decls.kinds.p[written] == dk::property {
this.want_accessor(this.property_setter(written), this.reached(this.type_at(this.left_of(target))), target);
}
# A variable holding a reference is pointed elsewhere by a reference written
# as one, `r = &x`, where it can be written, and anything else writes what it
# refers to, a reference too, as `r = s` writes what `s` reaches.
if operator == tk::assign && this.types.kind_of(kind) == ty::reference {
val given := this.infer_expr(value);
val none := constant {};
# Only a variable that can be written is pointed elsewhere; through one that
# cannot, or a parameter, a reference writes what it refers to. Within an
# instance, a place the template held a value of a type parameter in is
# given the value, as the template's was, though it is a reference here.
val points := this.rebinds(target) && this.lent(value) && this.types.kind_of(given) == ty::reference
&& this.fits(given, kind, &none, false, 0) != fit::none;
if !this.decide(node, rf::valued, !points) {
this.check_walked(target);
this.convert(value, kind);
return kind;
}
if this.types.second_of(kind) == 0 {
this.report(target, ec::assignable, "A `&` reference reads what it refers to; write through a `&mut` one.");
return kind;
}
this.convert(value, this.types.first_of(kind));
this.check_owned(value, this.types.first_of(kind));
return kind;
}
this.require_writable(target);
if operator == tk::assign {
this.check_expr(value, kind);
this.check_owned(value, kind);
return kind;
}
# A compound assignment applies its operator, as `x = x + y` would, so where
# that is a call of a method, what the method gives is written over the target.
# Where the target's type has the method changing it in place, as `+=` has
# `add_assign`, and it takes the value, that is called instead, giving nothing.
val read := this.read_type(kind);
val word := this.arithmetic_word(operator);
if word != 0 {
val given := this.hinted_expr(value, read);
val numbered := this.types.is_numeric(this.unalias(read));
if given != bt::error && !numbered && this.operates_by_method(read) && this.changes_in_place(operator, target, value, read) {
val made := this.operate(node, this.in_place_word(operator), target, value, read);
if made != bt::error && this.unalias(made) != bt::void_type {
var text := string::from("`", nothing {});
text.append(this.names.text(this.in_place_word(operator)));
text.append("` changes what it is called on in place, so it gives nothing, and it gives `");
val gives := this.describe(made);
text.append(gives.span);
text.append("` here.");
this.report_text(node, ec::operands, &text);
}
return kind;
}
if given != bt::error && (numbered
? !this.types.is_numeric(this.unalias(this.read_type(given))) && this.operates_by_method(given)
: this.operates_by_method(read)) {
val made := this.operate(node, word, target, value, read);
if made != bt::error && this.unalias(this.read_type(made)) != this.unalias(read) {
var text := string::from("`", nothing {});
text.append(this.text_of(node));
text.append("` writes what `");
text.append(this.names.text(word));
text.append("` gives over the `");
val held := this.describe(read);
text.append(held.span);
text.append("` it is called on, and `");
text.append(this.names.text(word));
text.append("` gives `");
val gives := this.describe(made);
text.append(gives.span);
text.append("` here.");
this.report_text(node, ec::operands, &text);
}
return kind;
}
}
if operator == tk::lshifteq || operator == tk::rshifteq {
val shift := this.infer_expr(value);
if !this.integer_like(read) || !this.integer_like(this.read_type(shift)) {
this.report_operands(node, kind, shift, "are not both integers, which a shift takes");
}
return kind;
}
if this.pointer_like(read) && (operator == tk::pluseq || operator == tk::minuseq) {
this.check_expr(value, bt::int64);
return kind;
}
this.check_expr(value, read);
val bitwise := operator == tk::bitandeq || operator == tk::bitoreq || operator == tk::bitxoreq;
val plain := this.unalias(read);
if bitwise && !this.integer_like(plain) {
this.report_operand(node, kind, "The operator takes integers.");
} else if !bitwise && !this.numeric_like(plain) {
this.report_operand(node, kind, "The operator takes numbers.");
}
return kind;
}
#! Whether a compound assignment's target has the method changing it in place
#! that its operator calls, as `add_assign` for `+=`, taking the value.
#! @param this The checker.
#! @param operator The compound assignment's operator.
#! @param target The target's node.
#! @param value The value's node.
#! @param read The target's type, as read.
#! @returns Whether it has.
def checker::changes_in_place(&mut this, operator: tk, target: u32, value: u32, read: u32): bool {
val word := this.in_place_word(operator);
return false if word == 0;
val through := this.reached(read);
val found := this.find_member(through, word);
return false if found.decl == 0 || this.decls.kinds.p[found.decl] != dk::function;
var args := vector::<u32>::new();
args.append(value);
return this.fits_overload(found.decl, target, found.owner, through, args.span);
}
#! `cond ? a : b`, whose branches share a type, or both take the type expected.
#! @param this The checker.
#! @param node The `ternary` node.
#! @param expected The type expected of its value, or the error type where
#! none is.
#! @returns The type its branches share, or the error type where they share
#! none.
def checker::ternary(&mut this, node: u32, expected: u32): u32 {
val condition := this.left_of(node);
# A test naming what a value of a type not chosen yet holds names it as each
# way the type can be chosen, so the ternary is checked in each.
val unchosen := this.unchosen_binding(condition);
return this.use_ways(node, unchosen, expected) if unchosen != 0;
this.check_condition(condition);
# A condition known while compiling that names a type parameter, as
# `T::mutable`, chooses the branch while compiling, as `if const` does.
if this.names_params(condition) {
val chosen := this.choose_ternary(node, expected);
return chosen if chosen != checker::unchosen_ternary;
}
val then := this.record(node, if_slot::then);
val other := this.record(node, if_slot::other);
# The names the condition's `is` tests give are named where they hold, as an
# `if`'s are: those of `x is f(v)` in the first branch, and of `x is not f(v)`
# in the second.
val mark := this.scope_mark();
if expected != bt::error {
this.bind_tests(condition, true);
this.check_expr(then, expected);
this.check_owned(then, expected);
this.unbind_to(mark);
this.bind_tests(condition, false);
this.check_expr(other, expected);
this.check_owned(other, expected);
this.unbind_to(mark);
return expected;
}
this.bind_tests(condition, true);
val a := this.infer_expr(then);
this.unbind_to(mark);
this.bind_tests(condition, false);
val b := this.infer_expr(other);
this.unbind_to(mark);
return bt::error if a == bt::error || b == bt::error;
var shared := this.common(a, b);
if shared == bt::error {
shared = this.common(this.reached_value(a), this.reached_value(b));
}
if shared == bt::error {
this.report_operands(node, a, b, "are the branches, which share no type");
return bt::error;
}
this.bind_tests(condition, true);
this.convert(then, shared);
this.check_owned(then, shared);
this.unbind_to(mark);
this.bind_tests(condition, false);
this.convert(other, shared);
this.check_owned(other, shared);
this.unbind_to(mark);
if this.is_known(condition) && this.is_known(then) && this.is_known(other) {
this.set_value(node, this.value_at(condition).magnitude != 0 ? this.value_at(then) : this.value_at(other));
}
return shared;
}
#! `x as T`, which converts between numbers, between integers and enums,
#! between pointers and integers, between pointers of the same mutability, and
#! a pointer to a reference at what it points at, as well as whatever converts
#! without `as`.
#! @param this The checker.
#! @param node The `cast` node.
#! @returns The type converted to, or the error type where `as` does not convert
#! to it.
def checker::cast(&mut this, node: u32): u32 {
val value := this.left_of(node);
val target := this.resolve_type(this.right_of(node));
# Where a generic's own body has not chosen the type yet, the value is checked
# in each way it can be chosen.
if this.choices != 0 && this.holds_unchosen(target, 0) {
return this.cast_ways(node, value, target);
}
val kind := this.infer_expr(value);
return bt::error if kind == bt::error || target == bt::error;
return this.cast_as(node, value, kind, target, true);
}
#! `x as T` where a generic's own body has not chosen `T` yet, as `M ? *mut U :
#! *U` before `M` is known: checked in each way it can be chosen, as
#! `check_ways` checks a value given as one, the value converting as each says.
#! @param this The checker.
#! @param node The `cast` node.
#! @param value The value converted.
#! @param target The type not chosen yet.
#! @returns The type, or the error type where `as` does not convert to it in
#! some way.
def checker::cast_ways(&mut this, node: u32, value: u32, target: u32): u32 {
val ways := this.choice_ways(target);
if ways.cases.over {
this.report_ways(node);
return bt::error;
}
val low := this.lowest_node(value);
var fine := true;
this.repeating++;
for c in 0..ways.chosen.length {
var pass := this.begin_way(&ways, c, low, node, false);
val kind := this.infer_expr(value);
val made := kind == bt::error ? bt::error : this.cast_as(node, value, kind, this.exactly(ways.chosen[c]), false);
fine = made != bt::error && fine;
this.end_case(&mut pass);
}
this.end_repeat();
return fine ? target : bt::error;
}
#! `x as T` of a value of a type checked, as `cast` says.
#! @param this The checker.
#! @param node The `cast` node.
#! @param value The value converted.
#! @param kind Its type.
#! @param target The type converted to.
#! @param folds Whether a constant converted is known as the type
#! converted to, which it is not in one of the ways a type not
#! chosen yet can be.
#! @returns The type converted to, or the error type where `as` does not
#! convert to it.
def checker::cast_as(&mut this, node: u32, value: u32, kind: u32, target: u32, folds: bool): u32 {
return bt::error if target == bt::error;
# A type parameter known to have a shape, as `T: pointer` says, converts as
# that shape.
val from := this.shaped_plain(this.read_type(kind));
val to := this.shaped_plain(target);
val known := folds && this.is_known(value);
# A reference converts as the value it refers to, so `&x` written to convert
# to a number would give `x`, where its address was surely meant.
if this.kind_of(value) == nk::reference
&& (this.types.is_numeric(to) || this.types.is_boolean(to) || this.named_kind(to) == dk::enum_type) {
this.report(node, ec::conversion, "`&x as` a number converts the value `x` holds, not its address: convert to a pointer first for the address, as `&x as *u8 as u64`, or drop the `&` for the value.");
return bt::error;
}
# An enum member's value is known as an integer.
if known && this.named_kind(from) == dk::enum_type && this.types.is_integer(to) {
this.set_value(node, this.value_at(value));
return target;
}
if known && this.types.is_numeric(from) && this.types.is_numeric(to) {
val held := this.value_at(value);
var made := held;
if this.types.is_float(to) && !held.floating {
made = constant::real_of(held.as_real());
} else if this.types.is_integer(to) && held.floating {
made = constant::integer((held.real < 0.0 ? -held.real : held.real) as u64, held.real < 0.0);
}
# A typed value converts as the program running converts it, wrapping, and
# only a constant of no type is outside the range of what it converts to.
if this.types.is_integer(to) && !held.floating && !this.in_range(to, &made) && !this.types.is_untyped(kind) {
this.set_value(node, this.wrap_constant(made, to));
return target;
}
if this.types.is_integer(to) && !held.floating && !this.in_range(to, &made) {
var text := string::from("The constant is outside the range of `", nothing {});
val named := this.describe(target);
text.append(named.span);
text.append("`.");
this.report_text(node, ec::overflow, &text);
return target;
}
this.set_value(node, made);
return target;
}
return target if this.casts(kind, from, to);
this.report_cast(node, kind, target);
return bt::error;
}
#! Reports `x as T` where `as` does not convert to `T`.
#! @param this The checker.
#! @param node The `cast` node.
#! @param kind The value's type.
#! @param target The type converted to.
def checker::report_cast(&mut this, node: u32, kind: u32, target: u32): void {
var text := string::from("`", nothing {});
val given := this.describe(kind);
# An alias choosing a type says the type it chose, as `M ? &mut T : &T` does
# where `M` is said to be `true`.
val chooses := this.types.kind_of(target) == ty::named && this.decls.has(this.types.first_of(target), df::chooses);
val want := this.describe(chooses ? this.unalias(target) : target);
text.append(given.span);
text.append("` does not convert to `");
text.append(want.span);
text.append("` with `as`.");
this.report_text(node, ec::conversion, &text);
}
#! Whether `as` converts a value of the type `kind`, read as `from`, to `to`.
#! @param this The checker.
#! @param kind The value's type, as written.
#! @param from The type it is read as.
#! @param to The type converted to.
#! @returns Whether it converts.
def checker::casts(&mut this, kind: u32, from: u32, to: u32): bool {
val none := constant {};
val how := this.fits(kind, to, &none, false, 0);
return true if how != fit::none;
# A reference is the address of what it refers to, and a reference to an
# array that of its first element.
val raw := this.unalias(kind);
if this.types.kind_of(raw) == ty::reference
&& (this.types.kind_of(to) == ty::pointer || this.is_std(to, word::cfn)) {
return this.is_std(to, word::cfn) || this.types.second_of(to) == 0 || this.types.second_of(raw) != 0;
}
# What a type parameter's bounds let it be converted to, and from.
if this.types.kind_of(from) == ty::param && this.param_converts(from)
&& (this.types.is_numeric(to) || this.named_kind(to) == dk::enum_type || this.types.kind_of(to) == ty::param) {
return true;
}
if this.types.kind_of(to) == ty::param && this.param_converts(to)
&& (this.types.is_numeric(from) || this.named_kind(from) == dk::enum_type) {
return true;
}
val fk := this.types.kind_of(from);
val tok := this.types.kind_of(to);
val from_number := this.types.is_numeric(from);
val to_number := this.types.is_numeric(to);
return true if from_number && to_number;
return true if (this.types.is_boolean(from) && to_number) || (from_number && this.types.is_boolean(to));
return true if this.named_kind(from) == dk::enum_type && (to_number || this.named_kind(to) == dk::enum_type);
return true if from_number && this.named_kind(to) == dk::enum_type && this.types.is_integer(from);
# Pointers convert to and from integers, and between types of the same
# mutability, or dropping it. A reference is the address of what it refers to.
val cfn_from := this.is_std(from, word::cfn);
val cfn_to := this.is_std(to, word::cfn);
val ptr_from := fk == ty::pointer || cfn_from;
val ptr_to := tok == ty::pointer || cfn_to;
return true if ptr_from && this.types.is_integer(to);
return true if this.types.is_integer(from) && ptr_to;
if (ptr_from || fk == ty::reference) && ptr_to {
return true if cfn_from || cfn_to;
val writes := this.types.second_of(from) != 0;
val wants := this.types.second_of(to) != 0;
return !wants || writes;
}
# A pointer becomes a reference at what it points at, as `through` gives one,
# one that writes only from a pointer that does, and never one to a trait,
# which holds a table beside the address.
if fk == ty::pointer && tok == ty::reference {
return false if this.named_kind(this.unalias(this.types.first_of(to))) == dk::trait_type;
return this.types.second_of(to) == 0 || this.types.second_of(from) != 0;
}
return fk == ty::null_type && ptr_to;
}
#! Whether a type parameter's bounds let `as` convert it to and from numbers:
#! `numeric`, `integer`, `ordinal`, or `real`.
#! @param this The checker.
#! @param kind The type parameter's type.
#! @returns Whether its bounds let it.
def checker::param_converts(&mut this, kind: u32): bool {
val numeric := this.std_decl(word::numeric);
val integer := this.std_decl(word::integer);
val ordinal := this.std_decl(word::ordinal);
val real := this.std_decl(word::real);
return (numeric != 0 && this.param_bounded(kind, numeric)) || (integer != 0 && this.param_bounded(kind, integer))
|| (ordinal != 0 && this.param_bounded(kind, ordinal)) || (real != 0 && this.param_bounded(kind, real));
}
#! What `is` and `match` say of a C union, which holds no tag.
pub const c_union_untagged: strview = "A C union holds nothing saying which field it holds, so `is` and `match` can't ask: read the field it was written through.";
#! `x is T`, `x is field`, `x is field(value)`, or `T is bound`: whether a type
#! satisfies a trait or is a type, or which field a union holds.
#! @param this The checker.
#! @param node The `is_test` node.
#! @returns `bool`, the test's type.
def checker::is_test(&mut this, node: u32): u32 {
val value := this.left_of(node);
val target := this.record(node, is_slot::target);
val binding := this.record(node, is_slot::binding);
val negated := this.node_has(node, nf::negated);
# `x is (d)` tests for the field of a union a description names, as though its
# name were written. Over a generic's own type parameters it waits for each
# instance, as reflection of them does.
if this.node_has(node, nf::computed) {
val kind := this.infer_expr(value);
this.infer_expr(target);
return bt::boolean if this.waits_for_instance() || kind == bt::error || this.type_at(target) == bt::error;
val one := this.reflected_one(target);
if one == checker::no_reflection {
this.report(target, ec::constant, "`x is (d)` takes a declaration known while compiling, as the variable of `for const d in declarations()` is.");
return bt::boolean;
}
val held: desc = this.descs[one];
val union_value := this.unalias(this.read_type(kind));
val holder := this.named_kind(union_value) == dk::union_type ? this.types.first_of(union_value) : 0;
if holder != 0 && this.decls.has(holder, df::foreign) {
this.report(target, ec::invalid, c_union_untagged);
return bt::boolean;
}
if held.param != 0 || holder == 0 || this.decls.member(holder, this.decls.names.p[held.decl]) != held.decl
|| this.decls.kinds.p[held.decl] != dk::field {
this.report(target, ec::invalid, "`x is (d)` asks which field a union holds, and `d` describes no field of this one.");
return bt::boolean;
}
this.set_ref(node, held.decl);
return bt::boolean;
}
# Where the left names a type, the test asks of the type itself. `type_of(d)`
# always names one, which is not known where reflection waits for an instance.
val tested := this.tested_type(value);
if tested == bt::error && this.kind_of(value) == nk::type_of {
return bt::boolean;
}
# A generic type named without type arguments, as `T is vector`, asks whether
# the type is one of its instances.
if tested != 0 {
val generic := this.bare_generic(target);
if generic != 0 {
this.decide_instance(node, tested, generic, negated);
return bt::boolean;
}
val bound := this.resolve_type(target);
return bt::boolean if this.refuse_sealed(target, this.trait_of_alias(bound), "no type is one");
this.decide_bound(node, tested, bound, negated);
return bt::boolean;
}
val kind := this.infer_expr(value);
return bt::boolean if kind == bt::error;
# A value of a type not chosen yet is tested as each way it can be chosen; a
# test naming what it holds is, with what names it after, where that is an
# `if`, a `while`, a ternary, or `&&` or `||`.
return this.use_ways(node, kind, bt::error) if this.uses_ways(kind);
# A union's field, as `r is data`, with the value it holds where it is named.
val held := this.unalias(this.read_type(kind));
if this.named_kind(held) == dk::union_type && this.kind_of(target) == nk::type_name && this.segment_count(target) == 1 {
val field := this.decls.member(this.types.first_of(held), this.name_of(target));
if field != 0 && this.decls.kinds.p[field] == dk::field && this.decls.has(this.types.first_of(held), df::foreign) {
this.report(target, ec::invalid, c_union_untagged);
return bt::boolean;
}
if field != 0 && this.decls.kinds.p[field] == dk::field {
this.check_reaches(target, held, field);
this.set_ref(node, field);
if binding != 0 {
this.declare_binding(binding, this.instantiated(this.decls.types.p[field], this.types.first_of(held), held), value);
}
return bt::boolean;
}
}
val generic := this.bare_generic(target);
if generic != 0 {
this.decide_instance(node, this.read_type(kind), generic, negated);
if binding != 0 {
this.report(binding, ec::invalid, "Only a union's field names the value it holds, as `r is data(text)`.");
}
return bt::boolean;
}
val bound := this.resolve_type(target);
return bt::boolean if this.refuse_sealed(target, this.trait_of_alias(bound), "no type is one");
if this.at.instance == 0 && this.is_fields_type(target, held, bound) {
return bt::boolean;
}
this.decide_bound(node, this.read_type(kind), bound, negated);
if binding != 0 {
this.report(binding, ec::invalid, "Only a union's field names the value it holds, as `r is data(text)`.");
}
return bt::boolean;
}
#! Reports `x is T` of a union where `T` is the type of one of its fields, as
#! `m is nothing` of a `maybe`: `is` with a type asks the value's own type,
#! which is the union, so it is never true, and the field was meant.
#! @param this The checker.
#! @param target The node of the type.
#! @param held The type of the value tested.
#! @param bound The type it is tested against.
#! @returns Whether it was reported.
def checker::is_fields_type(&mut this, target: u32, held: u32, bound: u32): bool {
return false if bound == bt::error || bound == held || this.named_kind(held) != dk::union_type;
val holder := this.types.first_of(held);
for &field in this.decls.list_view(this.decls.children.p[holder]) {
continue if this.decls.kinds.p[field] != dk::field;
continue if this.instantiated(this.decls.types.p[field], holder, held) != bound;
var text := string::from("`is` with a type asks the value's own type, `", nothing {});
text.append(this.describe(held).span);
text.append("`, so this is never true; name the field holding a `");
text.append(this.describe(bound).span);
text.append("`, as `is ");
text.append(this.decl_text(field));
text.append("`.");
this.report_text(target, ec::invalid, &text);
return true;
}
return false;
}
#! The type an `is` test's left names, where it names one rather than a value,
#! or 0.
#! @param this The checker.
#! @param node The test's left.
#! @returns The type it names, the error type where it names one wrongly, or 0
#! where it is a value.
def checker::tested_type(&mut this, node: u32): u32 {
match this.kind_of(node) {
nk::type_of => return this.resolve_type(node);
nk::name => {
# A constant parameter names a value, which is tested as one.
val bound := this.bound(this.name_of(node));
return 0 if (bound & ref_type) != 0 && this.is_constant_arg(bound & ref_mask);
return bound & ref_mask if (bound & ref_type) != 0;
return 0 if bound != 0;
val decl := this.lookup_global(this.name_of(node), 0);
# Within a sealed trait, its types are the ones the trait given the
# arguments checking is with gives.
if decl != 0 && this.decls.kinds.p[decl] == dk::assoc_type && this.is_sealed(this.decls.owners.p[decl]) {
val scoped := this.in_scope(this.name_of(node), true);
return scoped.decl == decl ? this.sealed_type(decl, scoped.owner, 0) : bt::error;
}
return this.is_type_decl(decl) ? this.decls.types.p[decl] : 0;
}
nk::path => {
val first := this.segment_name(node, 0);
return 0 if this.bound(first) != 0 && (this.bound(first) & ref_type) == 0;
val decl := this.lookup_type(first, 0);
if (this.bound(first) & ref_type) != 0 || this.is_type_decl(decl) {
val target := this.path_target(node);
if target.kind == bt::error && target.decl != 0 && this.decls.kinds.p[target.decl] == dk::assoc_type
&& this.is_sealed(this.decls.owners.p[target.decl]) {
return this.sealed_type(target.decl, target.owner, 0);
}
return target.kind != bt::error ? target.kind : this.is_type_decl(target.decl) ? this.decls.types.p[target.decl] : 0;
}
return 0;
}
else => return 0;
}
return 0;
}
#! Decides a test of whether a type satisfies a trait, or is a type, where it
#! can be decided while checking.
#! @param this The checker.
#! @param node The test's node, which takes the constant decided.
#! @param given The type tested.
#! @param bound The trait or type it is tested against.
#! @param negated Whether the test is `is not`.
def checker::decide_bound(&mut this, node: u32, given: u32, bound: u32, negated: bool): void {
return if given == bt::error || bound == bt::error;
# A constant is tested as the type it settles as, as `1 is integer` is.
val kind := this.types.is_untyped(given) ? this.settle(given) : given;
val asked := this.trait_of_alias(bound);
var holds := false;
if this.named_kind(asked) == dk::trait_type {
holds = this.satisfies(kind, asked);
} else {
holds = kind == bound;
}
# What a type parameter satisfies is known by its bounds, and what it is not
# is only known of each instance.
if this.types.is_open(kind) && !holds {
return;
}
this.set_value(node, constant::truth(holds != negated));
}
#! The generic struct or union an `is` test names without type arguments, as
#! `vector` of `T is vector`, which asks whether a type is one of its instances,
#! looking through aliases, as `strview is span` holds. A type a type around it
#! gives, as `node` within `list<T>`, takes that type's arguments, and so is no
#! such test.
#! @param this The checker.
#! @param target The test's target.
#! @returns The declaration, or 0 where the target is anything else.
pub def checker::bare_generic(&mut this, target: u32): u32 {
return 0 if target == 0 || this.kind_of(target) != nk::type_name;
val count := this.segment_count(target);
for i in 0..count {
return 0 if this.segment_args(target, i as u32) != 0;
}
return 0 if this.bound(this.segment_name(target, 0)) != 0;
var decl: u32 = 0;
if count == 1 {
val found := this.lookup_named(this.segment_name(target, 0), 0, true);
return 0 if found.owner != 0;
decl = found.decl;
} else {
this.quiet++;
val found := this.path_target(target);
this.quiet--;
return 0 if found.kind != bt::error || (found.owner != bt::error && this.types.kind_of(found.owner) == ty::named);
decl = found.decl;
}
return 0 if decl == 0 || this.decls.params.p[decl] == 0;
val kind: dk = this.decls.kinds.p[decl];
return 0 if kind != dk::struct_type && kind != dk::union_type;
this.set_ref(this.segment_node(target, count - 1), decl);
return decl;
}
#! Decides a test of whether a type is an instance of a generic struct or union,
#! where it can be decided while checking: a type parameter is one only in each
#! instance.
#! @param this The checker.
#! @param node The test's node, which takes the constant decided.
#! @param given The type tested.
#! @param decl The generic declaration.
#! @param negated Whether the test is `is not`.
def checker::decide_instance(&mut this, node: u32, given: u32, decl: u32, negated: bool): void {
return if given == bt::error;
val kind := this.types.is_untyped(given) ? this.settle(given) : given;
val plain := this.unalias(kind);
val holds := this.generic_instance(plain) && this.types.first_of(plain) == decl;
if this.types.is_open(kind) && !holds {
return;
}
this.set_value(node, constant::truth(holds != negated));
}
#! The bound an `if const` test proves of the type it tests: the trait it names,
#! or `generic` where it names a generic type without type arguments, as
#! `T is vector`, or 0 where it names a type, which is no bound.
#! @param this The checker.
#! @param target The test's target.
#! @returns The bound, or 0.
pub def checker::tested_bound(&mut this, target: u32): u32 {
return this.pattern_bound(this.test_pattern(target));
}
#! The pattern an `is` test's target is, as an arm of `match const` writes one:
#! a trait, a generic type without type arguments, or a type. The test itself
#! reported what is wrong of it, so it is read again quietly.
#! @param this The checker.
#! @param target The test's target.
#! @returns The pattern's type, or the error type.
pub def checker::test_pattern(&mut this, target: u32): u32 {
this.quiet++;
val made := this.const_pattern(target);
this.quiet--;
return made;
}
#! Declares the name an `is` test or an arm gives the value of a union's field,
#! a copy of it, or with `&` or `&mut` a reference to it. A field that closes is
#! taken out of a value a call made, which hands it over, as `else` and `for`
#! take theirs, where a variable keeps it. A value of its own can be changed and
#! moved from, as a `var` can.
#! @param this The checker.
#! @param binding The node of the name, or of `&name` or `&mut name`.
#! @param kind The field's type.
#! @param union_value The node of the union tested or matched.
pub def checker::declare_binding(&mut this, binding: u32, kind: u32, union_value: u32): void {
# A field holding a reference, as `some` of a `maybe<&T>`, gives that
# reference: `&` and `&mut` name it, and a name alone takes what it reaches.
val holds_reference := this.types.kind_of(this.unalias(kind)) == ty::reference;
if this.kind_of(binding) == nk::reference {
val name := this.left_of(binding);
val mutable := this.node_has(binding, nf::mutable);
if this.decide(binding, rf::relent, holds_reference) {
if mutable && this.types.second_of(this.unalias(kind)) == 0 {
this.report(binding, ec::assignable, "`&mut` refers to what can be written, and the field holds a `&` reference.");
}
this.set_type(name, this.types.reference(this.types.first_of(this.unalias(kind)), mutable));
this.mark_local(name);
this.set_flag(name, rf::narrowed);
return;
}
if mutable && !this.is_writable(union_value) {
this.report(binding, ec::assignable, "`&mut` refers to the field in place, so the union must be writable.");
}
this.set_type(name, this.types.reference(kind, mutable));
this.mark_local(name);
this.set_flag(name, rf::narrowed);
return;
}
val unnamed := this.kind_of(binding) == nk::name && this.name_of(binding) == names::underscore_id;
# Within an instance, a field the template held as a value, as `T`, stays as
# the template took it.
val copies := !unnamed && this.decide(binding, rf::valued, this.has_value(kind));
val held := copies ? this.types.first_of(this.unalias(kind)) : kind;
val kept := copies || this.is_place(union_value)
|| this.types.kind_of(this.unalias(this.type_at(union_value))) == ty::reference;
if !unnamed && kept && this.closes(held) {
var text := string::from("`", nothing {});
val named := this.describe(held);
text.append(named.span);
text.append(copies
? "` is closed where its scope ends, and the field refers to it where its owner holds it; name it by `&` or `&mut` rather than taking a copy of it."
: "` is closed where its scope ends, so the variable holding the union owns the field; name it by `&` or `&mut` rather than taking a copy of it.");
this.report_text(binding, ec::owned, &text);
}
this.set_type(binding, held);
this.mark_local(binding);
this.set_flag(binding, rf::narrowed);
# A copy of what a reference reaches is only read, as a `for` pass's is.
if !copies {
this.set_flag(binding, rf::mutable);
}
}
#! The node an `is` test or an arm names the value of a union's field by, as
#! `v`, `&v`, or `&mut v`, or 0 where it names none, or names it `_`, which takes
#! nothing out of the union: `x is some(_)` asks only what `x is some` does.
#! @param this The checker.
#! @param binding The node written for it, or 0.
#! @returns The node, or 0.
pub def checker::named_binding(&this, binding: u32): u32 {
return 0 if binding != 0 && this.kind_of(binding) == nk::name && this.name_of(binding) == names::underscore_id;
return binding;
}
#! Binds the names the tests of a condition give, where the condition holding
#! means what `positive` says: the names of `x is f(v)` where it holds, and of
#! `x is not f(v)` where it does not.
#! @param this The checker.
#! @param condition The condition's node.
#! @param positive Whether the condition holds where the names are bound.
pub def checker::bind_tests(&mut this, condition: u32, positive: bool): void {
return if condition == 0;
match this.kind_of(condition) {
nk::is_test => {
val negated := this.node_has(condition, nf::negated);
return if negated == positive;
val binding := this.record(condition, is_slot::binding);
return if binding == 0;
val name := this.kind_of(binding) == nk::reference ? this.left_of(binding) : binding;
return if !this.has_flag(name, rf::narrowed);
this.bind(this.name_of(name), ref_local | name);
}
nk::binary => {
val operator := this.token_kind(condition);
if (operator == tk::logand && positive) || (operator == tk::logor && !positive) {
this.bind_tests(this.left_of(condition), positive);
this.bind_tests(this.right_of(condition), positive);
}
}
nk::unary => {
if this.token_kind(condition) == tk::negate {
this.bind_tests(this.left_of(condition), !positive);
}
}
else => {}
}
}