#! @file choosing.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the checking of `match const`, which chooses by a type while #! compiling, and what it and `if const` say of a type parameter within a #! generic's own body. As a statement or a value, `match const` keeps only the #! arm whose pattern the type fits, as a chain of `if const` does, and as a #! type, it is the type that arm gives, chosen once the type it chooses by #! holds no type parameter, or within a generic's own body, where what is #! proved of the parameter decides it, and otherwise a type of its own. An alias #! of one stands for the type it chooses. A pattern is a trait, which a type #! fits by implementing it, a generic type without type arguments, as `vector`, #! which a type fits by being one of its instances, looking through aliases, or #! a type, which a type fits by being it exactly, as `is` says. The first arm #! the type fits is chosen, and `else` fits every type. #! #! Within a generic's own body, an arm is checked with its pattern proved of #! the type parameter, and the patterns of the arms before it disproved, as an #! `if const` branch is with what its condition says, what implies a pattern #! disproved disproved too. Where that is one of several cases, as for an arm of #! several patterns or `if const A || B`, the arm is checked once for each, each #! note said once, with its case, and a case that can't hold is left out. A #! type parameter said to be a type is that type there, a value of it one of #! that type, and one said to be an instance of a generic, or bounded by `array` #! or `pointer`, has that shape, as `vector` or #! `T::element[T::length]`, which it is indexed, walked, and has members as. #! #! `C ? A : B` as a type chooses as `match const` does, part by part of `C`: a #! test of a type by what it tests, as `T is reference`, and any other part by #! the constant `bool` it is, each part resolved with what those before it #! proved, and `if const` says what a constant parameter is, as `if const #! MUTABLE` or `if const N == 4`, which such a type is chosen by. Within an #! instance, a `match const` type and `C ? A : B` resolve only what they #! choose, so `T::target` is never asked of a type with none. Within a #! generic's own body, a value given where such a type is not chosen yet, or a #! type holding one, as `&castref`, is checked once in each way it can be #! chosen, with what that way says, a value of one converts as each way does, #! and what uses one, as `*p` or `for x in xs`, is checked in each way too, #! giving the type each gives, or one chosen as the value's is. module sakura::analyzer; #! What `choose_arm` gives where the type chosen by is not known yet, as of a #! type parameter in a generic's own body. pub const checker::undecided_arm: u32 = 0xFFFFFFFF; #! Added to a type parameter's declaration among `proved_params`, says the #! pattern beside it among `proved_bounds` is one the parameter fits. pub const checker::fit_known: u32 = 0x40000000; #! Added to a type parameter's declaration among `proved_params`, says the #! pattern beside it among `proved_bounds` is one the parameter does not fit. pub const checker::unfit_known: u32 = 0x80000000; #! The most cases a body is checked in, past which the condition it holds in is #! reported, as a range `for const` unrolls past 1024 numbers is. pub const checker::max_cases: u64 = 1024; #! What a case says of a type parameter: that it fits a pattern, or does not. pub struct checker::fact { var param: u32; #! The type parameter's declaration. var pattern: u32; #! The pattern: a trait, a generic type without type arguments, or a type. var holds: bool; #! Whether the parameter fits it. } #! The cases a body is checked in, each what it says of type parameters, as an #! `if const A || B` branch holds in two: where `A` does and where `B` does. pub struct checker::cases { var facts: vector; #! Each case's facts, one case after another. var ends: vector; #! Where each case's facts end among them. var over: bool; #! Whether there were more than `max_cases`, so they are one saying nothing. } #! What a pass checking a body in one case changed, which ending it undoes. pub struct checker::case_pass { var proved: u64; #! How many entries `proved_params` held before. var mark: u32; #! The scope's mark before. var outer: u32; #! The table checked in before, where the pass checks in one of its own, or `no_table`. var text: string; #! What `case_text` said before. } #! No cases, to which each is added. #! @returns The cases. pub def checker::cases::none(): checker::cases { return checker::cases { facts = vector::::new(), ends = vector::::new() }; } #! One case saying nothing. #! @returns The cases. pub def checker::cases::one(): checker::cases { var made := checker::cases::none(); made.ends.append(0); return made; } #! Where a case's facts begin among them. #! @param this The cases. #! @param index The case. #! @returns Where. pub def checker::cases::start(&this, index: u64): u64 { return index == 0 ? 0 : this.ends[index - 1]; } #! A copy of the cases. #! @param this The cases. #! @returns The copy. pub def checker::cases::copy(&this): checker::cases { var made := checker::cases::none(); made.facts.extend(this.facts.span); made.ends.extend(this.ends.span); made.over = this.over; return made; } #! One case saying nothing, as there being too many is. #! @returns The cases. pub def checker::cases::too_many(): checker::cases { var made := checker::cases::one(); made.over = true; return made; } #! Takes the last case back. #! @param this The cases. pub def checker::cases::drop_last(&mut this): void { this.ends.truncate(); this.facts.resize(this.ends.length == 0 ? 0u : this.ends.last); } #! Adds a case of the facts of two, one after the other. #! @param this The cases. #! @param a The cases the first's facts are of. #! @param i The first. #! @param b The cases the second's facts are of. #! @param j The second, or none where `b` is `a` and `j` is `i`. pub def checker::cases::add_joined(&mut this, a: &checker::cases, i: u64, b: &checker::cases, j: u64): void { for k in a.start(i)..a.ends[i] { this.facts.append(a.facts[k]); } for k in b.start(j)..b.ends[j] { this.facts.append(b.facts[k]); } this.ends.append(this.facts.length); } #! The type a pattern of `match const` is written as: a trait, a type, or for a #! generic type named without type arguments, the type of its declaration given #! none, which stands for any of its instances. #! @param this The checker. #! @param node The pattern's node. #! @returns The pattern's type, or the error type where it names none. pub def checker::const_pattern(&mut this, node: u32, of: u32 = 0): u32 { # A value, as `4` or `true`, is the constant type argument it is, which a # constant chosen by is matched against. if this.value_pattern(node) { if of == 0 { this.report(node, ec::constant, "A value is a pattern of `match const` choosing by a constant, as `match const N { 4 => ... }`; this chooses by a type."); return bt::error; } this.check_expr(node, of); val id := this.known_of(node, of, 0); return id == 0 ? bt::error : this.known_type(id); } val generic := this.bare_generic(node); return this.types.named(generic, span::::blank()) if generic != 0; val made := this.trait_of_alias(this.resolve_type(node)); return bt::error if this.refuse_sealed(node, made, "no type is one"); return made; } #! The generic declaration a pattern stands for any instance of, as `vector`. #! @param this The checker. #! @param pattern The pattern's type. #! @returns The declaration, or 0 where the pattern is a trait or a type. pub def checker::instance_pattern(&this, pattern: u32): u32 { return 0 if this.types.kind_of(pattern) != ty::named || this.types.part_count(pattern) != 0; val decl := this.types.first_of(pattern); return 0 if this.decls.params.p[decl] == 0; val kind: dk = this.decls.kinds.p[decl]; return kind == dk::struct_type || kind == dk::union_type ? decl : 0; } #! Whether a type fits a pattern of `match const`, as `is` decides it. #! @param this The checker. #! @param given The type. #! @param pattern The pattern's type. #! @returns 1 where it fits, 0 where it does not, and -1 where only each #! instance knows, as where it holds a type parameter. pub def checker::pattern_fits(&mut this, given: u32, pattern: u32): i32 { return 0 if given == bt::error || pattern == bt::error; val kind := this.types.is_untyped(given) ? this.settle(given) : given; var holds := false; val generic := this.instance_pattern(pattern); if generic != 0 { val plain := this.unalias(kind); holds = this.generic_instance(plain) && this.types.first_of(plain) == generic; } else if this.named_kind(pattern) == dk::trait_type { holds = this.satisfies(kind, pattern); } else { holds = kind == pattern; } # Which kind of type the compiler decides a type is, its own shape says, # whatever type parameters it holds, so `&mut T` is a reference and never a # pointer. return 0 if !holds && this.decided_kind(pattern) != 0 && this.shape_known(kind); return -1 if !holds && (this.types.is_open(kind) || this.types.is_open(pattern)); return holds ? 1 : 0; } #! Whether a type's own shape is known, whatever type parameters it holds: it #! is no type parameter, pack, or type not chosen yet, which only each instance #! knows the shape of. #! @param this The checker. #! @param kind The type. #! @returns Whether it is. def checker::shape_known(&mut this, kind: u32): bool { val of := this.types.kind_of(this.unalias(kind)); return of != ty::param && of != ty::pack && of != ty::matched && of != ty::error; } #! Whether a pattern of `match const` is a value written in place, as `4`, #! `-1`, `'a'`, or `true`, so what it chooses by is a constant. #! @param this The checker. #! @param node The pattern's node. #! @returns Whether it is. pub def checker::value_pattern(&this, node: u32): bool { val kind := this.kind_of(node); return kind == nk::number || kind == nk::boolean || kind == nk::character || kind == nk::string || (kind == nk::unary && this.token_kind(node) == tk::minus); } #! Whether a `match const` chooses by a constant, as `match const N { 4 => ... #! }`: where a pattern of it is a value. #! @param this The checker. #! @param node The `match_stmt`, `match_value`, or `type_match` node. #! @returns Whether it does. pub def checker::matches_values(&this, node: u32): bool { for &arm in this.run_view(this.right_of(node)) { for &pattern in this.run_view(this.left_of(arm)) { return true if this.value_pattern(pattern); } } return false; } #! The type a `match const` statement or value chooses by: the type its subject #! names, as `T`, or else the type of the value it is; or where its patterns #! are values, the constant it is, as the constant type argument it is, or the #! constant parameter it names, as `N` or `T::mutable`. #! @param this The checker. #! @param subject The subject's node. #! @param values Whether its patterns are values. #! @param of Where the constant's type is written, where they are. #! @returns The type, or the error type. def checker::const_subject(&mut this, subject: u32, values: bool, of: &mut u32): u32 { if !values { val named := this.tested_type(subject); return named if named != 0; return this.read_type(this.infer_expr(subject)); } # A type chosen by takes no values, which each pattern reports. this.quiet++; val named := this.tested_type(subject); this.quiet--; return named if named != 0 && named != bt::error; val kind := this.infer_expr(subject); return bt::error if kind == bt::error; of = this.settle(this.read_type(kind)); val param := this.valued_param(subject); if param != 0 { of = this.decls.targets.p[param]; return this.decls.types.p[param]; } val id := this.known_or_run(subject, of); if id == 0 { this.report(subject, ec::constant, "A `match const` of values chooses by a constant known while compiling: a constant, a constant parameter, or what a function gives of them."); return bt::error; } return this.known_type(id); } #! The constant a `match const` type choosing by values chooses by, named as a #! type is, as `N` or `T::mutable`, as the constant type argument it is, or the #! constant parameter it names. #! @param this The checker. #! @param node The `type_name` node. #! @returns The type, or the error type. def checker::constant_subject(&mut this, node: u32): u32 { val kind := this.constant_name(node, bt::error); return bt::error if kind == bt::error; val of := this.settle(this.read_type(kind)); val errors := this.errors; val id := this.known_or_run(node, of); if id == 0 { if this.errors == errors { this.report(node, ec::constant, "A `match const` of values chooses by a constant known while compiling: a constant, a constant parameter, or what a function gives of them."); } return bt::error; } return this.known_type(id); } #! The type of the constants a type a `match const` type chooses by stands for, #! where it is a constant parameter or a constant type argument, or 0. #! @param this The checker. #! @param kind The type chosen by. #! @returns The type, or 0. def checker::constant_of(&this, kind: u32): u32 { if this.types.kind_of(kind) == ty::param && this.decls.has(this.types.first_of(kind), df::valued) { return this.decls.targets.p[this.types.first_of(kind)]; } return this.types.kind_of(kind) == ty::value ? this.types.first_of(kind) : 0u; } #! The type of each pattern of a `match const`, in the order of its arms and #! their patterns, each resolved once, so a mistake in one is reported once. #! @param this The checker. #! @param node The `match_stmt` or `match_value` node. #! @param of The type of the constant chosen by, or 0 where it is a type. #! @returns The types, the error type where a pattern names none. def checker::arm_patterns(&mut this, node: u32, of: u32): vector { var kinds := vector::::new(); for &arm in this.run_view(this.right_of(node)) { for &pattern in this.run_view(this.left_of(arm)) { kinds.append(this.const_pattern(pattern, of)); } } return kinds; } #! The first arm of a `match const` whose pattern the type fits, deciding them #! in order, by what is proved within a generic's own body too, so an arm whose #! fitting only each instance knows leaves every arm after it undecided too. #! @param this The checker. #! @param node The `match_stmt` or `match_value` node. #! @param tested The type chosen by. #! @param kinds The type of each pattern, as `arm_patterns` gives them. #! @param fitted Where the pattern it fits is written, or 0 for `else`. #! @returns The arm, 0 where none fits, or `undecided_arm`. def checker::choose_arm(&mut this, node: u32, tested: u32, kinds: span, fitted: &mut u32): u32 { var at: u64 = 0; for &arm in this.run_view(this.right_of(node)) { val patterns := this.left_of(arm); if patterns == 0 { fitted = 0; return arm; } for &pattern in this.run_view(patterns) { val kind := kinds[at]; at++; continue if kind == bt::error; val fits := this.proved_fit(tested, kind); return checker::undecided_arm if fits < 0; if fits > 0 { fitted = kind; return arm; } } } return 0; } #! The arm a `match const` decided chose, in the result table being read: the #! one each instance keeps, which later passes walk alone. #! @param this The checker. #! @param node The `match_stmt` or `match_value` node. #! @returns The `match_arm` node, or 0 where none was chosen. pub def checker::chosen_arm(&this, node: u32): u32 { return this.chosen_arms.get_or(&table_node { table = this.at.table, node = node }, 0); } #! Whether a match is a `match const`, which chooses by a type. #! @param this The checker. #! @param node The `match_stmt` or `match_value` node. #! @returns Whether it is. pub def checker::is_match_const(&this, node: u32): bool { return this.token_kind(node) == tk::kw_const; } #! Reports a `match const` no arm of which fits the type it chooses by. #! @param this The checker. #! @param node The match's node. #! @param tested The type. def checker::report_no_arm(&mut this, node: u32, tested: u32): void { var text := string::from("No arm of the `match const` fits `", nothing {}); text.append(this.describe(tested).span); text.append("`, and it has no `else`."); this.report_text(node, ec::arms, &text); } #! `match const T { ... }` as a statement: the arm whose pattern `T` fits first #! is checked, with the bound its pattern tests given to `T`, and is the only #! one kept. Within a generic's own body, where only each instance knows which, #! each arm is checked with what its pattern says of `T`. An instance no arm #! fits, without `else`, is an error. #! @param this The checker. #! @param node The `match_stmt` node. #! @returns How it ends. pub def checker::check_match_const(&mut this, node: u32): flow { val subject := this.left_of(node); var of: u32 = 0; val tested := this.const_subject(subject, this.matches_values(node), &mut of); return flow::goes_on if tested == bt::error; # Within a generic's own body, each arm is checked with what its pattern says # of the type parameter chosen by, and what those before it do not, as an `if # const` branch is: a trait or a generic type proves its bound, and a type # names the parameter as it, so the arm is checked in each instance taking # it. An arm of several patterns is checked with each. var kinds := this.arm_patterns(node, of); var fitted: u32 = 0; val chosen := this.choose_arm(node, tested, kinds.span, &mut fitted); if chosen == checker::undecided_arm { var all_leave := true; var has_else := false; var at: u64 = 0; val param := this.param_of(tested); this.undecided++; for &arm in this.run_view(this.right_of(node)) { val patterns := this.left_of(arm); has_else = has_else || patterns == 0; val count := this.run_length(patterns) as u64; val cases := this.arm_cases(param, kinds.span, at, count); at += count; if this.check_cases(this.right_of(arm), &cases) == flow::goes_on { all_leave = false; } } this.undecided--; return has_else && all_leave ? flow::leaves : flow::goes_on; } if chosen == 0 { this.report_no_arm(node, tested); return flow::goes_on; } # The arm chosen gives the type parameter chosen by the bound its pattern # tests, as an `if const` branch taken does. this.chosen_arms.set(table_node { table = this.at.table, node = node }, chosen); val proved := this.proved_params.length; this.give_kind(subject, fitted); val taken := this.check_block(this.right_of(chosen)); this.unprove_to(proved); return taken; } #! `match const T { ... }` as a value: the value of the arm whose pattern `T` #! fits first, the only one kept, of the type expected or its own. Within a #! generic's own body each arm is checked, and their values share a type. #! @param this The checker. #! @param node The `match_value` node. #! @param expected The type expected, or the error type where none is. #! @returns Its type, or the error type. pub def checker::match_const_value(&mut this, node: u32, expected: u32): u32 { val subject := this.left_of(node); var of: u32 = 0; val tested := this.const_subject(subject, this.matches_values(node), &mut of); return bt::error if tested == bt::error; var kinds := this.arm_patterns(node, of); var fitted: u32 = 0; val chosen := this.choose_arm(node, tested, kinds.span, &mut fitted); if chosen == checker::undecided_arm { var shared: u32 = 0; var at: u64 = 0; val param := this.param_of(tested); this.undecided++; for &arm in this.run_view(this.right_of(node)) { val count := this.run_length(this.left_of(arm)) as u64; val cases := this.arm_cases(param, kinds.span, at, count); at += count; val body := this.right_of(arm); val repeats := cases.ends.length > 1; if repeats { this.repeating++; } for c in 0..cases.ends.length { var pass := this.begin_case(&cases, c, body); if expected != bt::error { this.check_expr(body, expected); } else { val given := this.infer_expr(body); if shared == 0 { shared = given; } else if shared != bt::error && given != bt::error { val joined := this.common(shared, given); if joined == bt::error { this.report_operands(body, shared, given, "are the values of arms of one match, which share no type"); } shared = joined; } } this.end_case(&mut pass); } if repeats { this.end_repeat(); } } this.undecided--; return expected if expected != bt::error; return shared == 0 || shared == bt::error ? bt::error : this.settle(shared); } if chosen == 0 { this.report_no_arm(node, tested); return bt::error; } this.chosen_arms.set(table_node { table = this.at.table, node = node }, chosen); val proved := this.proved_params.length; this.give_kind(subject, fitted); val body := this.right_of(chosen); # Where none is expected, an instance's is of the type its generic's own body # gave the arms, which they share, given the instance's types, so `integer => # 1u, else => 0` gives a `u64` in each. var made := expected; if made == bt::error && this.at.instance != 0 { val shared := this.in_instance(this.template_type(node)); if shared != 0 && shared != bt::error && !this.types.is_open(shared) && !this.types.is_untyped(shared) { made = shared; } } if made != bt::error { this.check_expr(body, made); } else { val given := this.infer_expr(body); made = given == bt::error ? bt::error : this.settle(given); if made != bt::error && made != given { this.convert(body, made); } } if made != bt::error { this.check_owned(body, made); } this.unprove_to(proved); # The value of the arm kept is the match's, so one known while compiling is # known of it. if this.is_known(body) { this.set_value(node, this.value_at(body)); } return made; } #! `match const T { ... }` as a type: the type the arm whose pattern `T` fits #! first gives. Each arm's type is resolved with what its pattern says of `T` #! where `T` is a type parameter, so `pointer => T::target` names what a #! pointer points at. Where `T` holds a type parameter, the choice waits for #! each instance, so the type is one of its own, which nothing else is, until #! `unalias` chooses. #! @param this The checker. #! @param node The `type_match` node. #! @returns The type, or the error type. pub def checker::resolve_type_match(&mut this, node: u32): u32 { # One whose patterns are values chooses by the constant its subject names, as # `match const N { 4 => u32, else => u64 }`. val written := this.left_of(node); val subject := this.matches_values(node) && this.kind_of(written) == nk::type_name && !this.names_type(written) ? this.constant_subject(written) : this.resolve_type(written); return bt::error if subject == bt::error; val arms := this.right_of(node); if this.run_length(arms) == 0 { this.report(node, ec::arms, "A `match const` type has arms, each a trait or type `=>` the type it gives, and may end with `else`."); return bt::error; } # Each arm's type is resolved once for each of its patterns, with that pattern # proved of the type parameter chosen by and those before it disproved, so # `pointer, reference => T::target` names what each points at or refers to. var kinds := vector::::new(); var failed := false; val of := this.constant_of(subject); for &arm in this.run_view(arms) { for &pattern in this.run_view(this.left_of(arm)) { val kind := this.const_pattern(pattern, of); failed = failed || kind == bt::error; kinds.append(kind); } } # A pattern the type is known not to fit gives nothing, as in an instance, # and nothing after one it is known to fit is resolved, so `reference => # T::target` is not where `T` is `u8`. var parts := vector::::new(); parts.append(subject); val param := this.param_of(subject); var at: u64 = 0; var chosen := false; for &arm in this.run_view(arms) { break if chosen; val count := this.run_length(this.left_of(arm)) as u64; val cases := this.arm_cases(param, kinds.span, at, count); val repeats := cases.ends.length > 1; if repeats { this.repeating++; } val saved := string::copy_of(&this.case_text); for c in 0..(count == 0 ? 1u : count) { val pattern := count == 0 ? 0 : kinds[at + c]; if pattern != 0 && pattern != bt::error { val fits := this.proved_fit(subject, pattern); continue if fits == 0; chosen = fits > 0; } val proved := this.proved_params.length; val mark := this.scope_mark(); val index := repeats ? c : 0; # An arm an instance chooses gives the type parameter the subject names the # bound it fits, as a decided `if const` does. if chosen && param == 0 { this.give_named(this.left_of(node), pattern); } chosen = chosen || pattern == 0; this.case_serial++; this.apply_case(&cases, index, true); if repeats { this.case_words(&cases, index); } val given := this.resolve_type(this.right_of(arm)); this.case_text.clear(); this.case_text.append(saved.span); this.unbind_to(mark); this.unprove_to(proved); failed = failed || given == bt::error; parts.append(pattern); parts.append(given); break if chosen; } if repeats { this.end_repeat(); } at += count; } return bt::error if failed; # A type chosen by a type it knows is chosen now, and reported here where no # arm fits it. val made := this.types.intern(ty::matched, this.at.file, node, parts.span); this.choices++; return this.choosable(made) ? this.chosen_type(this.reduce_match(made), 0) : made; } #! The type parameter a type is, which what is proved of it is said of. #! @param this The checker. #! @param kind The type. #! @returns The type parameter's declaration, or 0 where the type is none. def checker::param_of(&mut this, kind: u32): u32 { return 0 if kind == bt::error; val plain := this.unalias(kind); return this.types.kind_of(plain) == ty::param ? this.types.first_of(plain) : 0; } #! The type parameter what an `is` test tests is, where it is one: the type its #! left names, as `T`, or the type of the value it is. #! @param this The checker. #! @param value The test's left. #! @returns The type parameter's declaration, or 0. def checker::tested_param(&mut this, value: u32): u32 { this.quiet++; var tested := this.tested_type(value); this.quiet--; if tested == 0 { tested = this.read_type(this.type_at(value)); } return this.param_of(tested); } #! The cases an `if const` condition holds in, as `positive` says, each with #! what it says of type parameters: `T is shown` that `T` fits `shown`, `T is #! not shown` that it does not, both sides of `&&` at once, and each side of #! `||` apart. What says nothing of a type parameter is a case saying nothing. #! @param this The checker. #! @param condition The condition's node. #! @param positive Whether the condition holds in them. #! @returns The cases. def checker::cases_of(&mut this, condition: u32, positive: bool): checker::cases { match this.kind_of(condition) { nk::is_test => { var made := checker::cases::one(); return made if this.node_has(condition, nf::computed); val param := this.tested_param(this.left_of(condition)); return made if param == 0; val pattern := this.test_pattern(this.record(condition, is_slot::target)); return made if pattern == bt::error; made.facts.append(checker::fact { param = param, pattern = pattern, holds = this.node_has(condition, nf::negated) != positive }); made.ends[0] = 1; return made; } nk::binary => { val operator := this.token_kind(condition); if operator == tk::logand || operator == tk::logor { val left := this.cases_of(this.left_of(condition), positive); val right := this.cases_of(this.right_of(condition), positive); return this.joined_cases(&left, &right, (operator == tk::logand) == positive); } # `N == 4` or `N != 4` says what a constant parameter is, or is not. if operator == tk::equals || operator == tk::noteq { var param := this.valued_param(this.left_of(condition)); var other := this.right_of(condition); if param == 0 { param = this.valued_param(this.right_of(condition)); other = this.left_of(condition); } if param != 0 { val id := this.known_of(other, this.decls.targets.p[param], 0); if id != 0 && !this.known_depends(id) { return this.one_fact(param, this.known_type(id), (operator == tk::equals) == positive); } } } } nk::unary => { if this.token_kind(condition) == tk::negate { return this.cases_of(this.left_of(condition), !positive); } } # A constant parameter of `bool`, as `if const MUTABLE`, holds or does not. nk::name, nk::path => { val param := this.valued_param(condition); if param != 0 && this.unalias(this.decls.targets.p[param]) == bt::boolean { return this.one_fact(param, this.truth_type(), positive); } return this.sealed_cases(condition, positive); } else => {} } return checker::cases::one(); } #! The cases a sealed trait's constant read in a condition holds in, as #! `positive` says, where its value tests the trait's type parameters: what it #! says of each, said of the type parameter the trait is given for it, so `if #! const owns::::value`, of `const value := T is owning;`, says `T` is #! owning, as `if const T is owning` does. #! @param this The checker. #! @param node The `name` or `path` node reading the constant. #! @param positive Whether the condition holds in them. #! @returns The cases, or one saying nothing. def checker::sealed_cases(&mut this, node: u32, positive: bool): checker::cases { val ref := this.ref_at(node); if ref == 0 || (ref & (ref_type | ref_local)) != 0 || this.decls.kinds.p[ref] != dk::variable || !this.trait_asks(ref) || !this.is_sealed(this.decls.owners.p[ref]) || this.sealed_alone(ref) || this.sealed_depth > 8 { return checker::cases::one(); } val holder: u32 = this.decls.owners.p[ref]; val owner := this.kind_of(node) == nk::path ? this.path_target(node).owner : this.at.scope; val value := this.record_in(ref, variable_slot::value); if value == 0 || owner == bt::error || this.types.kind_of(owner) != ty::named || this.types.first_of(owner) != holder { return checker::cases::one(); } # What its value says of the trait's parameters, worked out within it. val saved := this.at; this.enter_decl(ref, 0); this.sealed_depth++; val inner := this.cases_of(value, positive); this.sealed_depth--; this.leave(saved); return checker::cases::one() if inner.over; # Said of what the trait is given for each, where that is a type parameter. val params := this.decls.list_of(this.decls.params.p[holder]); val args := this.types.parts_of(owner); var made := checker::cases::none(); for c in 0..inner.ends.length { for f in inner.start(c)..inner.ends[c] { val each: checker::fact = inner.facts[f]; for i in 0..params.length { continue if params[i] != each.param || i >= args.length || this.types.kind_of(args[i]) != ty::param; made.facts.append(checker::fact { param = this.types.first_of(args[i]), pattern = this.substitute(each.pattern, params.span, args.span), holds = each.holds }); } } made.ends.append(made.facts.length); } return made; } #! One case of one fact. #! @param this The checker. #! @param param The type parameter's declaration. #! @param pattern The pattern. #! @param holds Whether the parameter fits it. #! @returns The cases. def checker::one_fact(&mut this, param: u32, pattern: u32, holds: bool): checker::cases { var made := checker::cases::one(); made.facts.append(checker::fact { param = param, pattern = pattern, holds = holds }); made.ends[0] = 1; return made; } #! The constant parameter a name or path names, as `MUTABLE`, or that stands #! for a constant a bound asks of a type parameter, as `T::mutable`. #! @param this The checker. #! @param node The node. #! @returns Its declaration, or 0 where it names none. def checker::valued_param(&mut this, node: u32): u32 { return 0 if this.kind_of(node) != nk::name && this.kind_of(node) != nk::path; val ref := this.ref_at(node); if ref != 0 && (ref & (ref_type | ref_local)) == 0 && this.decls.kinds.p[ref] == dk::variable && this.trait_asks(ref) { val asked := this.path_constant(node, ref); return asked != bt::error && this.types.kind_of(asked) == ty::param ? this.types.first_of(asked) : 0; } return 0 if (ref & ref_type) == 0; val kind := ref & ref_mask; return 0 if this.types.kind_of(kind) != ty::param; return this.decls.has(this.types.first_of(kind), df::valued) ? this.types.first_of(kind) : 0; } #! The constant type argument `true` is. #! @param this The checker. #! @returns The type. pub def checker::truth_type(&mut this): u32 { return this.known_type(this.known_number(constant::truth(true), bt::boolean)); } #! Whether a type is the constant type argument `true`. #! @param this The checker. #! @param kind The type. #! @returns Whether it is. pub def checker::is_truth(&this, kind: u32): bool { return false if this.types.kind_of(kind) != ty::value || this.types.first_of(kind) != bt::boolean; val held: known = this.knowns.p[this.types.second_of(kind)]; return held.kind == kk::number && held.value.magnitude == 1; } #! A step of choosing a type by a condition, as `C ? A : B` does: a part of the #! condition still to decide, and the step each way it goes leads to. The first #! two steps are the types chosen between, `A` and `B`. struct checker::choice_step { var condition: u32; #! The part's node, or 0 for one of the types. var yes: u32; #! The step where it holds. var no: u32; #! The step where it fails. } #! What choosing a type by a condition keeps as it goes. struct checker::choosing { var node: u32; #! The `type_cond` node, or the `ternary` node choosing between values. var steps: vector; #! The steps, the two types first. var path: checker::cases; #! What the parts decided on the way say of type parameters, as one case. var made: u64; #! How many times one of the types was resolved. var failed: bool; #! Whether a part failed, or there were too many cases. var values: bool; #! Whether it chooses between values, whose types are what each gives. var expected: u32; #! The type expected of the values, or the error type. var seen: u32; #! Which values were checked: 1 the first, 2 the second. } #! `C ? A : B` as a type: `A` where the constant `C` holds, and `B` where it #! does not. Each part of `C` chooses as a `match const` type does: an `is` #! test of a type by what it tests, as `T is reference`, and any other part by #! the `bool` it is, as `MUTABLE`, `N > 4`, or what a function gives, as #! `small(N)`. Where a part is known, the type is chosen now, and otherwise #! where what is proved within a generic's own body decides it, and otherwise #! in each instance, until which it is a type of its own. What follows a part #! on each way is resolved with what the part says of a type parameter there, #! so `T is reference ? T::target : T` names what `T` refers to, and `T is #! pointer && T::target is integer` tests what it points at. #! @param this The checker. #! @param node The `type_cond` node. #! @returns The type, or the error type. pub def checker::resolve_type_cond(&mut this, node: u32): u32 { var choice := checker::choosing { node = node, steps = vector::::new(), path = checker::cases::one() }; choice.steps.append(checker::choice_step {}); choice.steps.append(checker::choice_step {}); this.repeating++; val made := this.choose_by(&mut choice, this.left_of(node), 0, 1); this.end_repeat(); return choice.failed ? bt::error : made; } #! The type a part of a condition chooses, as `resolve_type_cond` says: `!` #! turns which way it leads, `&&` decides its right only where its left holds, #! and `||` only where it fails. #! @param this The checker. #! @param choice What choosing keeps. #! @param condition The part's node. #! @param yes The step where it holds. #! @param no The step where it fails. #! @returns The type, or the error type. def checker::choose_by(&mut this, choice: &mut checker::choosing, condition: u32, yes: u32, no: u32): u32 { match this.kind_of(condition) { nk::unary => { if this.token_kind(condition) == tk::negate { this.set_type(condition, bt::boolean); return this.choose_by(choice, this.left_of(condition), no, yes); } } nk::binary => { val operator := this.token_kind(condition); if operator == tk::logand || operator == tk::logor { this.set_type(condition, bt::boolean); val rest := choice.steps.length as u32; choice.steps.append(checker::choice_step { condition = this.right_of(condition), yes = yes, no = no }); return this.choose_by(choice, this.left_of(condition), operator == tk::logand ? rest : yes, operator == tk::logand ? no : rest); } } else => {} } var subject: u32 = 0; var pattern: u32 = 0; var negated := false; if !this.choice_test(condition, &mut subject, &mut pattern, &mut negated) { choice.failed = true; this.choose_step(choice, yes); return this.choose_step(choice, no); } val holds := negated ? no : yes; val fails := negated ? yes : no; val param := this.param_of(subject); # Where the part is decided, as in an instance, the type parameter it names # gets the bound it was decided to fit, as `if const` gives it. val named := this.kind_of(condition) == nk::is_test ? this.left_of(condition) : 0; val fits := this.proved_fit(subject, pattern); return this.choose_with(choice, param, pattern, true, holds, named) if fits > 0; return this.choose_with(choice, param, pattern, false, fails, 0) if fits == 0; val given_holds := this.choose_with(choice, param, pattern, true, holds, 0); val given_fails := this.choose_with(choice, param, pattern, false, fails, 0); return bt::error if given_holds == bt::error || given_fails == bt::error; var parts := vector::::new(); parts.append(subject); parts.append(pattern); parts.append(given_holds); parts.append(0); parts.append(given_fails); val made := this.types.intern(ty::matched, this.at.file, choice.node, parts.span); this.choices++; return this.choosable(made) ? this.chosen_type(this.reduce_match(made), 0) : made; } #! What a part of a condition a type is chosen by tests, checked where it is #! reached, with what the parts before it said: an `is` test of a type, the #! type and the pattern it tests, as `T` and `reference` of `T is reference`; #! `N == k` of a constant parameter, the parameter and `k`; and any other part, #! the `bool` it is, which is `true` or not. #! @param this The checker. #! @param condition The part's node. #! @param subject Where the type tested is written. #! @param pattern Where what it is tested for is written. #! @param negated Where it is written whether the test holds where the #! subject does not fit the pattern, as `is not` does. #! @returns Whether the part is one a type is chosen by, which is reported #! where it is not. def checker::choice_test(&mut this, condition: u32, subject: &mut u32, pattern: &mut u32, negated: &mut bool): bool { val kind := this.check_expr(condition, bt::boolean); return false if kind == bt::error; match this.kind_of(condition) { nk::is_test => { if !this.node_has(condition, nf::computed) { this.quiet++; val tested := this.tested_type(this.left_of(condition)); this.quiet--; if tested != 0 && tested != bt::error { val made := this.test_pattern(this.record(condition, is_slot::target)); return false if made == bt::error; subject = tested; pattern = made; negated = this.node_has(condition, nf::negated); return true; } } } # `N == 4` says what a constant parameter is, or is not. nk::binary => { val operator := this.token_kind(condition); if operator == tk::equals || operator == tk::noteq { var param := this.valued_param(this.left_of(condition)); var other := this.right_of(condition); if param == 0 { param = this.valued_param(this.right_of(condition)); other = this.left_of(condition); } if param != 0 { val id := this.known_of(other, this.decls.targets.p[param], 0); if id != 0 && !this.known_depends(id) { subject = this.decls.types.p[param]; pattern = this.known_type(id); negated = operator == tk::noteq; return true; } } } } else => {} } # Any other part is the `bool` it is, which folding, or else the evaluator, # works out, and which within a generic's own body may be worked out from its # type parameters by each instance. val errors := this.errors; val id := this.known_or_run(condition, bt::boolean); if id == 0 { if this.errors == errors { this.report(condition, ec::constant, "A type chooses by a `bool` known while compiling: a constant, a constant parameter, a test of a type, or what a function gives of them."); } return false; } subject = this.known_type(id); pattern = this.truth_type(); negated = false; return true; } #! The type a step of choosing leads to, with what a part decided says of a #! type parameter. #! @param this The checker. #! @param choice What choosing keeps. #! @param param The type parameter the part tested, or 0. #! @param pattern What it was tested for. #! @param holds Whether it fits it there. #! @param step The step. #! @param named The node of what the part tested, where it was decided to #! fit the pattern, or 0. #! @returns The type, or the error type. def checker::choose_with(&mut this, choice: &mut checker::choosing, param: u32, pattern: u32, holds: bool, step: u32, named: u32): u32 { val proved := this.proved_params.length; val mark := this.scope_mark(); val facts := choice.path.facts.length; if named != 0 { this.give_named(named, pattern); } if param != 0 { val one := this.one_fact(param, pattern, holds); this.apply_case(&one, 0, true); choice.path.facts.append(checker::fact { param = param, pattern = pattern, holds = holds }); choice.path.ends[0] = choice.path.facts.length; } val made := this.choose_step(choice, step); choice.path.facts.resize(facts); choice.path.ends[0] = facts; this.unbind_to(mark); this.unprove_to(proved); return made; } #! The type a step of choosing leads to: one of the types chosen between, #! resolved with what the parts decided on the way say, or what the next part #! chooses. #! @param this The checker. #! @param choice What choosing keeps. #! @param step The step. #! @returns The type, or the error type. def checker::choose_step(&mut this, choice: &mut checker::choosing, step: u32): u32 { if step > 1 { val next: checker::choice_step = choice.steps[step]; return this.choose_by(choice, next.condition, next.yes, next.no); } choice.made++; if choice.made > checker::max_cases { if choice.made == checker::max_cases + 1 { var text := string::from("The condition holds, or fails, in more than ", nothing {}); val most := $"{checker::max_cases} cases, each of which a type is resolved in."; text.append(most.span); this.report_text(this.left_of(choice.node), ec::limit, &text); } choice.failed = true; return bt::error; } val saved := string::copy_of(&this.case_text); this.case_serial++; this.case_words(&choice.path, 0); val branch := this.record(choice.node, step == 0 ? if_slot::then : if_slot::other); val made := choice.values ? this.choose_value(choice, branch, step == 0 ? 1u : 2u) : this.resolve_type(branch); this.case_text.clear(); this.case_text.append(saved.span); return made; } #! A branch of a ternary choosing between values by a condition only each #! instance knows, checked with what the parts of its condition decided on the #! way say: where it was checked already, with a table of its own. #! @param this The checker. #! @param choice What choosing keeps. #! @param branch The branch. #! @param bit Which branch it is among `seen`. #! @returns Its type, or the error type. def checker::choose_value(&mut this, choice: &mut checker::choosing, branch: u32, bit: u32): u32 { var outer := checker::no_table; if (choice.seen & bit) != 0 { outer = this.at.table; val low := this.lowest_node(branch); this.use_table(this.add_child_table(low, branch - low + 1)); } choice.seen |= bit; var made := choice.expected; if made != bt::error { if this.check_expr(branch, made) != bt::error { this.check_owned(branch, made); } else { made = bt::error; } } else { made = this.infer_expr(branch); } if outer != checker::no_table { this.use_table(outer); } return made; } #! Whether a condition names a type parameter of the generic it is within, as #! `T::mutable`, `M`, or `T is pointer` do, so where it is known while #! compiling, each instance knows it. #! @param this The checker. #! @param node The condition's node. #! @returns Whether it does. pub def checker::names_params(&mut this, node: u32): bool { val decl := this.at.decl; return false if decl == 0; val owner: u32 = this.decls.owners.p[decl]; return false if this.at.instance == 0 && !this.decls.has(decl, df::generic) && (owner == 0 || !this.decls.has(owner, df::generic)); var todo := vector::::new(); todo.append(node); while todo.length != 0 { val at: u32 = todo.last; todo.truncate(); continue if at == 0; val kind := this.kind_of(at); if kind == nk::name { return true if this.param_named(this.name_of(at)) != 0; } else if kind == nk::path || kind == nk::type_name { return true if this.param_named(this.segment_name(at, 0)) != 0; } this.syn().children(at, &mut todo); } return false; } #! What `choose_ternary` gives where the condition is not one it chooses by. pub const checker::unchosen_ternary: u32 = 0xFFFFFFFF; #! A ternary whose condition is known while compiling and names a type #! parameter, as `T::mutable ? &mut p[0] : &p[0]`, which chooses its branch as #! `if const` does: where the condition is known, or what is proved decides #! it, only the branch it chooses is checked, and kept, which later passes #! walk alone; and within a generic's own body, where only each instance knows #! it, each branch is checked with what the condition says, as a `C ? A : B` #! type chooses, and the ternary is of the type expected, or the one its #! branches share, or else one chosen as the condition chooses. #! @param this The checker. #! @param node The `ternary` node. #! @param expected The type expected of it, or the error type. #! @returns Its type, the error type, or `unchosen_ternary` where the condition #! is not known while compiling. pub def checker::choose_ternary(&mut this, node: u32, expected: u32): u32 { val condition := this.left_of(node); var holds: i32 = -1; if this.is_known(condition) { holds = this.value_at(condition).magnitude != 0 ? 1 : 0; } else { holds = this.decided_condition(condition); } if holds < 0 { val id := this.known_or_run(condition, bt::boolean); return checker::unchosen_ternary if id == 0; if this.known_depends(id) || this.knowns.p[id].kind != kk::number { return this.ternary_ways(node, expected); } holds = condition_holds(this.knowns.p[id]) ? 1 : 0; } return this.ternary_branch(node, holds == 1, expected); } #! The branch a ternary choosing by a condition known while compiling takes, #! checked alone, with what the condition says, and kept as the ternary's. Where #! none is expected, an instance's is of the type its generic's own body gave #! the branches, which they share, given the instance's types. #! @param this The checker. #! @param node The `ternary` node. #! @param holds Whether the condition holds. #! @param expected The type expected of it, or the error type. #! @returns Its type, or the error type. def checker::ternary_branch(&mut this, node: u32, holds: bool, expected: u32): u32 { val condition := this.left_of(node); val branch := this.record(node, holds ? if_slot::then : if_slot::other); this.chosen_arms.set(table_node { table = this.at.table, node = node }, branch); val proved := this.proved_params.length; val mark := this.scope_mark(); this.give_bounds(condition, holds); this.bind_tests(condition, holds); var made := expected; if made == bt::error && this.at.instance != 0 { val shared := this.in_instance(this.template_type(node)); if shared != 0 && shared != bt::error && !this.types.is_open(shared) && !this.types.is_untyped(shared) { made = shared; } } if made != bt::error { if this.check_expr(branch, made) != bt::error { this.check_owned(branch, made); } else { made = bt::error; } } else { made = this.infer_expr(branch); } this.unbind_to(mark); this.unprove_to(proved); if made != bt::error && this.is_known(branch) { this.set_value(node, this.value_at(branch)); } return made; } #! A ternary choosing between values by a condition only each instance knows, #! within a generic's own body: each branch checked with what each part of the #! condition says, as `resolve_type_cond` resolves a type's, the ternary of the #! type expected, or that its branches share, or else one chosen as the #! condition chooses, as `T::mutable ? &mut T::target : u8`. #! @param this The checker. #! @param node The `ternary` node. #! @param expected The type expected of it, or the error type. #! @returns Its type, or the error type. def checker::ternary_ways(&mut this, node: u32, expected: u32): u32 { var choice := checker::choosing { node = node, steps = vector::::new(), path = checker::cases::one(), values = true, expected = expected }; choice.steps.append(checker::choice_step {}); choice.steps.append(checker::choice_step {}); this.repeating++; this.undecided++; val made := this.choose_by(&mut choice, this.left_of(node), 0, 1); this.undecided--; this.end_repeat(); return bt::error if choice.failed || made == bt::error; return expected if expected != bt::error; return made if this.types.kind_of(made) != ty::matched; var given := vector::::new(); this.choice_results(made, &mut given, 0); var shared: u32 = 0; for &each in given.span { return bt::error if each == bt::error; shared = shared == 0 ? each : this.common(shared, each); } return shared != 0 && shared != bt::error ? shared : made; } #! Adds the types a type `match const` or `C ? A : B` chooses between gives, #! following those it chooses in turn. #! @param this The checker. #! @param kind The type. #! @param into The types. #! @param depth How deeply the search has gone. def checker::choice_results(&mut this, kind: u32, into: &mut vector, depth: u32): void { if this.types.kind_of(kind) != ty::matched || depth > 32 { into.append(kind); return; } var at: u32 = 2; while at < this.types.part_count(kind) { this.choice_results(this.types.part_at(kind, at), into, depth + 1); at += 2; } } #! The cases two conditions hold in: where both do, each case of one with each #! of the other, or where either does, the cases of each. Cases saying nothing #! of any type parameter are one, and past `max_cases`, so are they all. #! @param this The checker. #! @param a The cases of one. #! @param b The cases of the other. #! @param both Whether both hold, rather than either. #! @returns The cases. def checker::joined_cases(&mut this, a: &checker::cases, b: &checker::cases, both: bool): checker::cases { return checker::cases::too_many() if a.over || b.over; return checker::cases::one() if a.facts.length == 0 && b.facts.length == 0; var made := checker::cases::none(); if both { return checker::cases::too_many() if a.ends.length * b.ends.length > checker::max_cases; for i in 0..a.ends.length { for j in 0..b.ends.length { made.add_joined(a, i, b, j); this.keep_last(&mut made); } } return made; } return checker::cases::too_many() if a.ends.length + b.ends.length > checker::max_cases; val none := checker::cases::one(); for i in 0..a.ends.length { made.add_joined(a, i, &none, 0); this.keep_last(&mut made); } for j in 0..b.ends.length { made.add_joined(b, j, &none, 0); this.keep_last(&mut made); } return made; } #! Keeps the case added last only where it can hold, and where no case before #! it says the same: `T is integer && T is not integer` never holds, nor `T is #! i32 && T is u8`, nor `T is i32 && T is not numeric`, as an `i32` is. #! @param this The checker. #! @param made The cases. def checker::keep_last(&mut this, made: &mut checker::cases): void { val last := made.ends.length - 1; val start := made.start(last); val end: u64 = made.ends[last]; for i in start..end { val a: checker::fact = made.facts[i]; for j in i + 1..end { val b: checker::fact = made.facts[j]; continue if a.param != b.param; if !this.facts_agree(&a, &b) { made.drop_last(); return; } } } for k in 0..last { if this.same_case(made, k, last) { made.drop_last(); return; } } } #! Whether two facts of one type parameter can hold at once. #! @param this The checker. #! @param a One. #! @param b The other. #! @returns Whether they can, as far as is told. def checker::facts_agree(&mut this, a: &checker::fact, b: &checker::fact): bool { if a.pattern == b.pattern { return a.holds == b.holds; } return true if !a.holds && !b.holds; # A pattern fitted is the only type, or generic type, of its kind fitted, and # what it rules out is ruled out of what is fitted. if a.holds && b.holds { val a_exact := this.pattern_bound(a.pattern) == 0; val b_exact := this.pattern_bound(b.pattern) == 0; return false if a_exact && b_exact; return this.pattern_fits(a.pattern, b.pattern) != 0 if a_exact; return this.pattern_fits(b.pattern, a.pattern) != 0 if b_exact; return !(this.instance_pattern(a.pattern) != 0 && this.instance_pattern(b.pattern) != 0); } val fitted := a.holds ? a.pattern : b.pattern; val ruled := a.holds ? b.pattern : a.pattern; return !this.rules_out(ruled, fitted); } #! Whether two cases say the same, in any order. #! @param this The checker. #! @param made The cases. #! @param a One case. #! @param b The other. #! @returns Whether they do. def checker::same_case(&mut this, made: &checker::cases, a: u64, b: u64): bool { return false if made.ends[a] - made.start(a) != made.ends[b] - made.start(b); for i in made.start(a)..made.ends[a] { val each: checker::fact = made.facts[i]; var found := false; for j in made.start(b)..made.ends[b] { val other: checker::fact = made.facts[j]; if other.param == each.param && other.pattern == each.pattern && other.holds == each.holds { found = true; break; } } return false if !found; } return true; } #! Whether a type parameter not fitting one pattern rules out its fitting #! another: a trait every type fitting the other satisfies, as `numeric` is of #! `integer`, or of a type fitting it, as of `i32`. #! @param this The checker. #! @param ruled The pattern not fitted. #! @param pattern The other. #! @returns Whether it does. def checker::rules_out(&mut this, ruled: u32, pattern: u32): bool { return true if ruled == pattern; # A type, as `i32`, fits what it fits. if this.pattern_bound(pattern) == 0 { return !this.types.is_open(pattern) && this.pattern_fits(pattern, ruled) > 0; } return false if this.named_kind(ruled) != dk::trait_type; # What every integer, or every float, satisfies, a type parameter that is one # does, as `numeric` is of `integer`. val holder: u32 = this.types.first_of(pattern); val integer := this.std_decl(word::integer); val real := this.std_decl(word::real); if this.types.kind_of(pattern) == ty::named && holder != 0 && (holder == integer || holder == real) { var any := false; for k in 0..bt::count as u32 { continue if this.types.is_untyped(k) || !(holder == integer ? this.types.is_integer(k) : this.types.is_float(k)); any = true; return false if !this.satisfies(k, ruled); } return any; } # What a type parameter bounded by the other alone satisfies, every type # fitting it does. val scratch := this.scratch_param(); val proved := this.proved_params.length; this.proved_params.append(scratch); this.proved_bounds.append(this.pattern_bound(pattern)); val holds := this.satisfies(this.types.param(scratch), ruled); this.unprove_to(proved); return holds; } #! A type parameter of no declaration's, bounded by nothing, which `rules_out` #! asks what a bound alone gives. #! @param this The checker. #! @returns Its declaration. def checker::scratch_param(&mut this): u32 { return this.scratch if this.scratch != 0; val generic := this.std_decl(word::generic); this.scratch = this.decls.add(dk::type_param, names::underscore_id, this.decls.files.p[generic], this.decls.nodes.p[generic], this.decls.modules.p[generic]); this.decls.states.p[this.scratch] = ds::resolved; this.decls.types.p[this.scratch] = this.types.param(this.scratch); return this.scratch; } #! The cases an arm of `match const` is checked in within a generic's own body: #! one for each of its patterns, the type parameter chosen by fitting it, or #! for `else` one, each with the parameter fitting none of the patterns before. #! @param this The checker. #! @param param The type parameter chosen by, or 0 where it is chosen by none. #! @param kinds The type of every pattern of the match, in order. #! @param from Where the arm's patterns begin among them. #! @param count How many it has, 0 for `else`. #! @returns The cases. def checker::arm_cases(&mut this, param: u32, kinds: span, from: u64, count: u64): checker::cases { return checker::cases::one() if param == 0; var made := checker::cases::none(); val each := count == 0 ? 1u : count; for c in 0..each { for e in 0..from { if kinds[e] != bt::error { made.facts.append(checker::fact { param = param, pattern = kinds[e], holds = false }); } } if count != 0 && kinds[from + c] != bt::error { made.facts.append(checker::fact { param = param, pattern = kinds[from + c], holds = true }); } made.ends.append(made.facts.length); } return checker::cases::one() if made.facts.length == 0; return made; } #! Says what a case says of type parameters, until `unprove_to` forgets it: a #! trait or a generic type fitted proves its bound, a type fitted names the #! parameter as it where `names` says, and is what the types of what is checked #! read it as, as `exactly` says, and each pattern is noted as fitted or not, #! which a `match const` type within the body is chosen by. #! @param this The checker. #! @param cases The cases. #! @param index The case. #! @param names Whether a type fitted names the parameter. def checker::apply_case(&mut this, cases: &checker::cases, index: u64, names: bool): void { for i in cases.start(index)..cases.ends[index] { val each: checker::fact = cases.facts[i]; if !each.holds { this.proved_params.append(each.param | checker::unfit_known); this.proved_bounds.append(each.pattern); continue; } val bound := this.pattern_bound(each.pattern); if bound != 0 { this.proved_params.append(each.param); this.proved_bounds.append(bound); } this.proved_params.append(each.param | checker::fit_known); this.proved_bounds.append(each.pattern); continue if bound != 0; this.exact_facts++; # A constant parameter is still read as one, though what it is is noted. if names && !this.decls.has(each.param, df::valued) { this.bind(this.decls.names.p[each.param], ref_type | each.pattern); } } } #! A type as what is checked reads it, where what is proved says a type #! parameter is a type, as `T is strview` does: with the parameter that type, so #! a value of `T` is a `strview` there, as it is in each instance taking it. #! @param this The checker. #! @param kind The type. #! @returns The type. pub def checker::exactly(&mut this, kind: u32): u32 { return kind if this.exact_facts == 0 || kind == bt::error || !this.types.is_open(kind); var from := vector::::new(); var to := vector::::new(); for i in 0..this.proved_params.length { val at: u32 = this.proved_params[i]; continue if (at & (checker::fit_known | checker::unfit_known)) != checker::fit_known; val known: u32 = this.proved_bounds[i]; continue if this.pattern_bound(known) != 0; from.append(at & ~(checker::fit_known | checker::unfit_known)); to.append(known); } return from.length == 0 ? kind : this.substitute(kind, from.span, to.span); } #! Whether an entry of what is proved says a type parameter is a type. #! @param this The checker. #! @param at The entry's index. #! @returns Whether it does. def checker::exact_fact(&mut this, at: u64): bool { val param: u32 = this.proved_params[at]; return (param & (checker::fit_known | checker::unfit_known)) == checker::fit_known && this.pattern_bound(this.proved_bounds[at]) == 0; } #! Begins checking a body in one of its cases. The first is checked where the #! body's results are kept, and each after it with a result table of its own, #! as a pass of `for const` is. #! @param this The checker. #! @param cases The cases. #! @param index The case. #! @param body The body. #! @returns What `end_case` undoes. def checker::begin_case(&mut this, cases: &checker::cases, index: u64, body: u32): checker::case_pass { return this.begin_case_at(cases, index, this.lowest_node(body), body, true); } #! Begins checking the nodes from `low` to `high` in one of their cases, as #! `begin_case` does a body's. #! @param this The checker. #! @param cases The cases. #! @param index The case. #! @param low The first node. #! @param high The last node. #! @param names Whether a type a case fits names the type parameter. #! @returns What `end_case` undoes. def checker::begin_case_at(&mut this, cases: &checker::cases, index: u64, low: u32, high: u32, names: bool): checker::case_pass { var pass := checker::case_pass { proved = this.proved_params.length, mark = this.scope_mark(), outer = checker::no_table, text = string::copy_of(&this.case_text) }; if index > 0 { pass.outer = this.at.table; this.use_table(this.add_child_table(low, high - low + 1)); } this.case_serial++; this.apply_case(cases, index, names); if cases.ends.length > 1 { this.case_words(cases, index); } return pass; } #! Ends checking a body in a case, undoing what `begin_case` did. #! @param this The checker. #! @param pass What it did. def checker::end_case(&mut this, pass: &mut checker::case_pass): void { this.case_text.clear(); this.case_text.append(pass.text.span); this.unbind_to(pass.mark); this.unprove_to(pass.proved); if pass.outer != checker::no_table { this.use_table(pass.outer); } } #! Ends checking a body once for each of its cases, saying the notes held: one #! said in only one case with what that case says, as "(Where `T` is #! `real`.)", and one said in more without. #! @param this The checker. def checker::end_repeat(&mut this): void { this.repeating--; return if this.repeating != 0; for &mut each in this.held_notes { if each.passes == 1 && each.text.length != 0 { each.made.detail.append(" "); each.made.detail.append(each.text.span); } this.sink.give(&mut each.made); } this.held_notes.clear(); } #! Checks a branch of an `if const` or an arm of `match const` within a #! generic's own body once for each case it holds in, with what that case says #! of type parameters. #! @param this The checker. #! @param branch The branch, or the arm's body. #! @param cases The cases. #! @returns How it ends: leaving only where it does in each case. def checker::check_cases(&mut this, branch: u32, cases: &checker::cases): flow { val repeats := cases.ends.length > 1; if repeats { this.repeating++; } var all_leave := true; for c in 0..cases.ends.length { var pass := this.begin_case(cases, c, branch); if this.check_block(branch) == flow::goes_on { all_leave = false; } this.end_case(&mut pass); } if repeats { this.end_repeat(); } return all_leave ? flow::leaves : flow::goes_on; } #! Writes what a case says of type parameters fitting, as notes said in it say #! it: "(Where `T` is `real`.)", and of a constant `bool`, what it is, as #! "(Where `M` is `false`.)". #! @param this The checker. #! @param cases The cases. #! @param index The case. def checker::case_words(&mut this, cases: &checker::cases, index: u64): void { this.case_text.clear(); for i in cases.start(index)..cases.ends[index] { val each: checker::fact = cases.facts[i]; val truth := this.is_truth(each.pattern); continue if !each.holds && !truth; this.case_text.append(this.case_text.length == 0 ? "(Where `" : ", and `"); val named := this.describe(this.decls.types.p[each.param]); this.case_text.append(named.span); this.case_text.append("` is `"); if each.holds { val shown := this.describe(each.pattern); this.case_text.append(shown.span); } else { this.case_text.append("false"); } this.case_text.append("`"); } if this.case_text.length != 0 { this.case_text.append(".)"); } } #! The ways a type not chosen yet can be chosen, as `M ? *mut T : *T` can two #! before `M` is known: what each says of type parameters, as a case, and the #! type chosen there. struct checker::ways { var cases: checker::cases; #! What each way says, a case of each. var chosen: vector; #! The type each way chooses. } #! What walking the ways a type holding types not chosen yet can be chosen #! keeps: the ways found, or where a type is built of what each gives, that. struct checker::way_walk { var whole: u32; #! The type walked. var parts: vector; #! The types not chosen yet it holds, each walked within the ways of those before it. var leaves: vector; #! What each part is chosen as on the way walked. var path: checker::cases; #! What the way walked says of type parameters, as one case. var followed: vector; #! The aliases choosing a type followed on the way, within the part walked. var ways: checker::ways; #! The ways found. var results: vector; #! Where a type is built, the type each way gives. var next: u64; #! The next of `results` to take. var builds: bool; #! Whether a type is built, rather than the ways found. } #! Whether a type is one a generic's own body has not chosen yet, as `M ? *mut #! T : *T` is before `M` is known: one that is a type of its own until each #! instance chooses. #! @param this The checker. #! @param kind The type. #! @returns Whether it is. pub def checker::unchosen(&mut this, kind: u32): bool { return this.types.is_open(kind) && this.types.kind_of(this.unalias(kind)) == ty::matched; } #! Whether a type is one a generic's own body has not chosen yet, as `unchosen` #! says, or holds one, as `&castref` and `vector` do. #! @param this The checker. #! @param kind The type. #! @param depth How deeply the search has gone. #! @returns Whether it is or does. pub def checker::holds_unchosen(&mut this, kind: u32, depth: u32): bool { return false if this.choices == 0 || depth > 16 || !this.types.is_open(kind); val tkd := this.types.kind_of(kind); return false if tkd == ty::param; return true if this.unchosen(kind); match tkd { ty::pointer, ty::reference, ty::fixed => return this.holds_unchosen(this.types.first_of(kind), depth + 1); ty::named, ty::function, ty::pack => { return true if tkd == ty::function && this.holds_unchosen(this.types.first_of(kind), depth + 1); for i in 0..this.types.part_count(kind) { return true if this.holds_unchosen(this.types.part_at(kind, i as u32), depth + 1); } } else => {} } return false; } #! A type with each type within it a generic's own body has not chosen, but #! that what is proved there chooses, replaced by the type chosen, as `M ? &mut #! T : &T` is `&mut T` within `if const M`, so a value of it is one of that type. #! @param this The checker. #! @param kind The type. #! @param depth How deeply the search has gone. #! @returns The type. pub def checker::decided(&mut this, kind: u32, depth: u32): u32 { return kind if depth > 16 || !this.types.is_open(kind); val tkd := this.types.kind_of(kind); return kind if tkd == ty::param || tkd == ty::value || tkd == ty::pack; if tkd == ty::matched || (tkd == ty::named && this.decls.has(this.types.first_of(kind), df::chooses)) { val plain := this.unalias(kind); return kind if plain == kind || plain == bt::error || this.types.kind_of(plain) == ty::matched; return this.decided(plain, depth + 1); } match tkd { ty::pointer, ty::reference, ty::fixed => { val inner := this.decided(this.types.first_of(kind), depth + 1); return kind if inner == this.types.first_of(kind); val parts := this.types.parts_of(kind); return this.types.intern(tkd, inner, this.types.second_of(kind), parts.span); } ty::named, ty::function => { var parts := vector::::new(); var changed := false; for i in 0..this.types.part_count(kind) { val part := this.types.part_at(kind, i as u32); val made := this.decided(part, depth + 1); changed = changed || made != part; parts.append(made); } var first := this.types.first_of(kind); if tkd == ty::function { val result := this.decided(first, depth + 1); changed = changed || result != first; first = result; } return changed ? this.types.intern(tkd, first, this.types.second_of(kind), parts.span) : kind; } else => return kind; } return kind; } #! Adds the types not chosen yet a type is or holds, each once, as `castref` #! of `&castref`. #! @param this The checker. #! @param kind The type. #! @param into The types. #! @param depth How deeply the search has gone. def checker::unchosen_parts(&mut this, kind: u32, into: &mut vector, depth: u32): void { return if depth > 16 || !this.types.is_open(kind) || this.types.kind_of(kind) == ty::param; if this.unchosen(kind) { for &each in into.span { return if each == kind; } into.append(kind); return; } val tkd := this.types.kind_of(kind); match tkd { ty::pointer, ty::reference, ty::fixed => this.unchosen_parts(this.types.first_of(kind), into, depth + 1); ty::named, ty::function, ty::pack => { if tkd == ty::function { this.unchosen_parts(this.types.first_of(kind), into, depth + 1); } for i in 0..this.types.part_count(kind) { this.unchosen_parts(this.types.part_at(kind, i as u32), into, depth + 1); } } else => {} } } #! A type with each of some types within it replaced by another, as #! `&castref` is `&&mut T::target` where `castref` is chosen so. #! @param this The checker. #! @param kind The type. #! @param from The types replaced. #! @param to What each is replaced by. #! @param depth How deeply the search has gone. #! @returns The type. def checker::replaced(&mut this, kind: u32, from: span, to: span, depth: u32): u32 { for i in 0..from.length { return to[i] if from[i] == kind; } return kind if depth > 16 || !this.types.is_open(kind); val tkd := this.types.kind_of(kind); match tkd { ty::pointer, ty::reference, ty::fixed => { val inner := this.replaced(this.types.first_of(kind), from, to, depth + 1); return kind if inner == this.types.first_of(kind); val parts := this.types.parts_of(kind); return this.types.intern(tkd, inner, this.types.second_of(kind), parts.span); } ty::named, ty::function, ty::pack => { var parts := vector::::new(); var changed := false; for i in 0..this.types.part_count(kind) { val part := this.types.part_at(kind, i as u32); val made := this.replaced(part, from, to, depth + 1); changed = changed || made != part; parts.append(made); } var first := this.types.first_of(kind); if tkd == ty::function { val result := this.replaced(first, from, to, depth + 1); changed = changed || result != first; first = result; } return changed ? this.types.intern(tkd, first, this.types.second_of(kind), parts.span) : kind; } else => return kind; } return kind; } #! Begins walking the ways a type holding types not chosen yet can be chosen. #! @param this The checker. #! @param kind The type. #! @returns What the walk keeps. def checker::way_walk_of(&mut this, kind: u32): checker::way_walk { var walk := checker::way_walk { whole = kind, parts = vector::::new(), leaves = vector::::new(), path = checker::cases::one(), followed = vector::::new(), ways = checker::ways { cases = checker::cases::none(), chosen = vector::::new() }, results = vector::::new() }; this.unchosen_parts(kind, &mut walk.parts, 0); walk.leaves.resize(walk.parts.length); return walk; } #! The ways a type not chosen yet, or holding one, can be chosen, each with what #! it says of type parameters, and the type chosen there, as each instance could #! choose it. Within a way, a type chosen in turn by what it chooses is followed #! to the ways that can go, but for an alias followed on the way to it already, #! which is a type of its own there, as `strip_pointer` is within #! `strip_pointer`, and a way that can't hold, as `T is pointer` where `T` is #! said not to be one, is left out. A type holding several is chosen in each way #! of the first, each of the second, and so on. #! @param this The checker. #! @param kind The type. #! @returns The ways, too many where they are more than `max_cases`. def checker::choice_ways(&mut this, kind: u32): checker::ways { var walk := this.way_walk_of(kind); if walk.parts.length == 0 { val none := checker::cases::one(); walk.ways.cases.add_joined(&none, 0, &none, 0); walk.ways.chosen.append(kind); return move(&mut walk.ways); } val first: u32 = walk.parts[0]; this.walk_ways(&mut walk, 0, first, 0); return move(&mut walk.ways); } #! The type a value of a type not chosen yet gives in each way, where what it is #! used in gives a type in each: the one type where each gives it, and otherwise #! one chosen as the value's is, as `total + x` of a `word` is `N == 4 ? u32 : #! u64`. #! @param this The checker. #! @param kind The value's type. #! @param results The type given in each of the ways `choice_ways` gives. #! @returns The type. def checker::ways_type(&mut this, kind: u32, results: span): u32 { var same := true; for &each in results { same = same && each == results[0]; } return results[0] if same; var walk := this.way_walk_of(kind); walk.builds = true; walk.results.extend(results); return bt::error if walk.parts.length == 0; val first: u32 = walk.parts[0]; return this.walk_ways(&mut walk, 0, first, 0); } #! Walks the ways a part of a type can be chosen, with what is said on the way #! to it, and of each, the ways of the parts after it: adding each way, or where #! a type is built, giving the type built of what each way gives. #! @param this The checker. #! @param walk What the walk keeps. #! @param index The part walked. #! @param kind The type reached within it. #! @param start Where the aliases followed within it begin among `followed`. #! @returns The type built, or 0 where none is. def checker::walk_ways(&mut this, walk: &mut checker::way_walk, index: u64, kind: u32, start: u64): u32 { # Past the last part is a way. if index == walk.parts.length { if walk.builds { val made: u32 = walk.next < walk.results.length ? walk.results[walk.next] : bt::error; walk.next++; return made; } if walk.ways.chosen.length == checker::max_cases { walk.ways.cases.over = true; return 0; } val none := checker::cases::one(); walk.ways.cases.add_joined(&walk.path, 0, &none, 0); walk.ways.chosen.append(this.replaced(walk.whole, walk.parts.span, walk.leaves.span, 0)); return 0; } return 0 if walk.ways.cases.over; val alias := this.types.kind_of(kind) == ty::named && this.decls.has(this.types.first_of(kind), df::chooses) ? this.types.first_of(kind) : 0u; var again := false; for i in start..walk.followed.length { again = again || (alias != 0 && walk.followed[i] == alias); } val plain := again ? kind : this.unalias(kind); if this.types.kind_of(plain) != ty::matched || again || walk.followed.length - start > checker::max_aliases { walk.leaves[index] = kind; val next := index + 1 < walk.parts.length ? walk.parts[index + 1] : 0u; return this.walk_ways(walk, index + 1, next, walk.followed.length); } walk.followed.append(alias); val subject := this.types.part_at(plain, 0); val param := this.param_of(subject); val proved := this.proved_params.length; val facts := walk.path.facts.length; val count := this.types.part_count(plain); var arms := vector::::new(); arms.append(subject); var made: u32 = 0; var at: u32 = 1; while at + 1 < count { val pattern := this.types.part_at(plain, at); val result := this.types.part_at(plain, at + 1); val fits := pattern == 0 ? 1 : this.proved_fit(subject, pattern); if fits > 0 { made = this.walk_ways(walk, index, result, start); if arms.length > 1 { arms.append(0); arms.append(made); } break; } if fits < 0 { # Where only each instance knows, it is one way, and the arms after it # are chosen where it does not fit. val before := this.proved_params.length; val known := walk.path.facts.length; this.add_fact(param, pattern, true, &mut walk.path); val given := this.walk_ways(walk, index, result, start); walk.path.facts.resize(known); walk.path.ends[0] = known; this.unprove_to(before); this.add_fact(param, pattern, false, &mut walk.path); arms.append(pattern); arms.append(given); } at += 2; } walk.followed.truncate(); walk.path.facts.resize(facts); walk.path.ends[0] = facts; this.unprove_to(proved); return 0 if !walk.builds; return made if arms.length == 1; return this.types.intern(ty::matched, this.types.first_of(plain), this.types.second_of(plain), arms.span); } #! Says, along a way of choosing a type, that a type parameter fits a pattern, #! or does not, as what is proved, where the type chosen by is one. #! @param this The checker. #! @param param The type parameter's declaration, or 0. #! @param pattern The pattern. #! @param holds Whether it fits it. #! @param path What the way says, as one case. def checker::add_fact(&mut this, param: u32, pattern: u32, holds: bool, path: &mut checker::cases): void { return if param == 0; val one := this.one_fact(param, pattern, holds); this.apply_case(&one, 0, false); path.facts.append(checker::fact { param = param, pattern = pattern, holds = holds }); path.ends[0] = path.facts.length; } #! Begins checking in one way a type not chosen yet can be chosen, with what it #! says of type parameters, as `begin_case_at` does a case: the nodes from #! `low` to `high` with a table of their own for each way after the first, or #! for each where `own` says so, or where `high` is 0, in no table of their own. #! @param this The checker. #! @param ways The ways. #! @param index The way. #! @param low The first node. #! @param high The last node, or 0. #! @param own Whether the first way has a table of its own too. #! @returns What `end_case` undoes. def checker::begin_way(&mut this, ways: &checker::ways, index: u64, low: u32, high: u32, own: bool): checker::case_pass { var pass := checker::case_pass { proved = this.proved_params.length, mark = this.scope_mark(), outer = checker::no_table, text = string::copy_of(&this.case_text) }; if high != 0 && (index > 0 || own) { pass.outer = this.at.table; this.use_table(this.add_child_table(low, high - low + 1)); } this.case_serial++; this.apply_case(&ways.cases, index, false); if ways.cases.ends.length > 1 { this.case_words(&ways.cases, index); } return pass; } #! How a value that `fits` says fits nowhere fits where either type holds a type #! not chosen yet within it, as `&castref` does: as it fits in each way that #! can be chosen. #! @param this The checker. #! @param from The value's type. #! @param to The type expected. #! @param value The value, where it is known while compiling. #! @param known Whether it is. #! @returns How it fits. pub def checker::fits_unchosen(&mut this, from: u32, to: u32, value: &constant, known: bool): fit { return fit::none if !this.types.is_open(from) && !this.types.is_open(to); return fit::none if !this.holds_unchosen(from, 0) && !this.holds_unchosen(to, 0); return this.fits_ways(from, to, value, known, 1); } #! How a value fits where one of two types a generic's own body has not chosen #! yet, or that holds one, is: as it fits in each way that type can be chosen, #! with what the way says of type parameters, as `M ? *mut T : *T` fits a `*T`, #! and a `*mut T` fits it, or not at all where it fits one way not. #! @param this The checker. #! @param from The value's type. #! @param to The type expected. #! @param value The value, where it is known while compiling. #! @param known Whether it is. #! @param depth How deeply the search has gone. #! @returns How it fits: as it does each way where that is the same, and #! otherwise as it converts. pub def checker::fits_ways(&mut this, from: u32, to: u32, value: &constant, known: bool, depth: u32): fit { return fit::none if depth > 8; val chosen_from := this.holds_unchosen(from, 0); val ways := this.choice_ways(chosen_from ? from : to); return fit::none if ways.cases.over; var made := fit::exact; for c in 0..ways.chosen.length { val proved := this.proved_params.length; this.apply_case(&ways.cases, c, false); val way: u32 = ways.chosen[c]; val a := chosen_from ? way : from; val b := chosen_from ? to : way; var how := this.fits(a, b, value, known, depth + 1); # The other may hold one too, chosen in each way of its own. if how == fit::none && (this.holds_unchosen(a, 0) || this.holds_unchosen(b, 0)) { how = this.fits_ways(a, b, value, known, depth + 1); } this.unprove_to(proved); return how if how == fit::none || how == fit::overflow; made = c == 0 || made == how ? how : fit::convert; } return made; } #! Reports a value checked in more ways than `max_cases`. #! @param this The checker. #! @param node The value's node. def checker::report_ways(&mut this, node: u32): void { var text := string::from("The type this is given as, or used as, can be chosen in more than ", nothing {}); val most := $"{checker::max_cases} ways, each of which it is checked in."; text.append(most.span); this.report_text(node, ec::limit, &text); } #! Checks a value given where a type a generic's own body has not chosen yet is #! expected, or one holding one, as `M ? *mut T : *T` before `M` is known, once #! in each way it can be chosen, with what that way says of type parameters, as #! an `if const` branch is checked in each case it holds in: the first where the #! value's results are kept, and each after it with a table of its own, its #! notes said once. A value already checked is converted in each way. #! @param this The checker. #! @param node The value. #! @param expected The type not chosen yet. #! @returns The type, or the error type where the value converts to it in some #! way not. pub def checker::check_ways(&mut this, node: u32, expected: u32): u32 { val ways := this.choice_ways(expected); if ways.cases.over { this.report_ways(node); return bt::error; } val checked := this.has_flag(node, rf::checked); val low := checked ? node : this.lowest_node(node); var fine := true; this.repeating++; for c in 0..ways.chosen.length { var pass := this.begin_way(&ways, c, low, checked ? 0u : node, false); val wanted := this.decided(this.exactly(ways.chosen[c]), 0); val own := checked ? this.type_at(node) : this.expr(node, wanted); fine = own != bt::error && this.convert(node, wanted) && fine; this.end_case(&mut pass); } this.end_repeat(); return fine ? expected : bt::error; } #! Whether a value of the type is used in each way the type can be chosen, as #! `use_ways` says: where it is one a generic's own body has not chosen yet, or #! holds one, but within as many uses checked so as `max_uses`. #! @param this The checker. #! @param kind The value's type. #! @returns Whether it is. pub def checker::uses_ways(&mut this, kind: u32): bool { return this.choices != 0 && this.types.is_open(kind) && this.using_ways < checker::max_uses && this.holds_unchosen(kind, 0); } #! The most uses of values of types not chosen yet checked within one another, #! past which a type chosen without end, as `strip_pointer` is, is used as a #! type of its own. pub const checker::max_uses: u32 = 8; #! Checks what uses a value of a type a generic's own body has not chosen yet, #! or one holding one, as `*p` of an `addptr`, an operator, a member, an #! index, or a call of a method does, once in each way the type can be chosen, #! with what that way says of type parameters, each with a table of its own, #! the first's results kept as the use's, as though it were checked there. It is #! a place where it is one in each way, and refers to what it refers to in each. #! @param this The checker. #! @param node The use. #! @param kind The value's type. #! @param expected The type expected of the use, or the error type. #! @returns What the use gives, of the type it gives in each way, or one chosen #! as the value's is where they differ, as `ways_type` says, or the #! error type where it is not valid in some way. pub def checker::use_ways(&mut this, node: u32, kind: u32, expected: u32): u32 { val ways := this.choice_ways(kind); if ways.cases.over { this.report_ways(node); return bt::error; } val low := this.lowest_node(node); var results := vector::::new(); var places := true; var writes := true; var ref: u32 = 0; var refers := true; var fine := true; this.repeating++; this.using_ways++; for c in 0..ways.chosen.length { var pass := this.begin_way(&ways, c, low, node, true); val made := this.expr(node, expected); results.append(made); fine = fine && made != bt::error; places = places && this.has_flag(node, rf::place); writes = writes && this.has_flag(node, rf::writable); val at := this.ref_at(node); refers = refers && (c == 0 || at == ref); ref = at; # The first way's results are the use's, as an `if const` branch keeps its # first case's, so each instance follows what it decided. if c == 0 && node > low { this.keep_results(this.at.table, pass.outer, low, node - 1); } this.end_case(&mut pass); } this.using_ways--; this.end_repeat(); return bt::error if !fine; if places { this.mark_place(node, writes); } if refers && ref != 0 { this.set_ref(node, ref); } return this.ways_type(kind, results.span); } #! The type of the value a place is reached from through members, elements, and #! `*`, as `p` of `*p` or `h` of `h.x[0]`, where it is of a type not chosen yet, #! or holds one, so what writes the place is checked in each way it can be #! chosen, which may write it or not. #! @param this The checker. #! @param node The place's node. #! @returns The value's type, or 0 where it is none, or the place is a name. def checker::unchosen_root(&mut this, node: u32): u32 { return 0 if this.choices == 0; var at := node; var steps := 0; while steps < 16 { val kind := this.kind_of(at); break if kind != nk::member && kind != nk::index && kind != nk::slice && !(kind == nk::unary && this.token_kind(at) == tk::star); at = this.left_of(at); steps++; } return 0 if steps == 0 || at == 0; val kind := this.infer_expr(at); return kind != bt::error && this.uses_ways(kind) ? kind : 0u; } #! The type of a value of a type not chosen yet, or holding one, a test within a #! condition names what it holds of, as `x is some(y)` does, joined by `&&`, #! `||`, and `!`. A test whose value uses what a test before it names is left, #! as its value is checked only once that is named. #! @param this The checker. #! @param condition The condition's node. #! @returns The value's type, or 0 where there is none. def checker::unchosen_binding(&mut this, condition: u32): u32 { return 0 if this.choices == 0; var bound := vector::::new(); return this.binding_scan(condition, &mut bound); } #! The type `unchosen_binding` gives of a part of a condition, with the names #! the tests before it name. #! @param this The checker. #! @param node The part's node. #! @param bound The names the tests before it name, to which its own are added. #! @returns The value's type, or 0 where there is none. def checker::binding_scan(&mut this, node: u32, bound: &mut vector): u32 { return 0 if node == 0; match this.kind_of(node) { nk::binary => { val operator := this.token_kind(node); return 0 if operator != tk::logand && operator != tk::logor; val left := this.binding_scan(this.left_of(node), bound); return left != 0 ? left : this.binding_scan(this.right_of(node), bound); } nk::unary => return this.token_kind(node) == tk::negate ? this.binding_scan(this.left_of(node), bound) : 0u; nk::is_test => { val binding := this.record(node, is_slot::binding); return 0 if binding == 0 || this.node_has(node, nf::computed); val value := this.left_of(node); var made: u32 = 0; if !this.names_any(value, bound.span) { this.quiet++; val tested := this.tested_type(value); this.quiet--; if tested == 0 { val kind := this.infer_expr(value); made = kind != bt::error && this.uses_ways(kind) ? kind : 0u; } } this.names_within(binding, bound); return made; } else => return 0; } return 0; } #! Whether an expression names any of some names. #! @param this The checker. #! @param node The expression. #! @param names The names. #! @returns Whether it does. def checker::names_any(&mut this, node: u32, names: span): bool { return false if names.length == 0; var held := vector::::new(); this.names_within(node, &mut held); for &each in held.span { for &name in names { return true if each == name; } } return false; } #! Adds the names an expression or a pattern names. #! @param this The checker. #! @param node The node. #! @param into The names. def checker::names_within(&mut this, node: u32, into: &mut vector): void { var todo := vector::::new(); todo.append(node); while todo.length != 0 { val at: u32 = todo.last; todo.truncate(); continue if at == 0; if this.kind_of(at) == nk::name { into.append(this.name_of(at)); } this.syn().children(at, &mut todo); } } #! Checks a statement using a value of a type not chosen yet, or holding one, #! where it names what the value gives, as `for x in xs` and `if r is some(x)` #! do, once in each way the type can be chosen, as `use_ways` checks a use: with #! what that way says of type parameters, each with a table of its own, the #! first's results kept as the statement's. #! @param this The checker. #! @param node The statement: a `for`, an `if`, or a `while`. #! @param kind The value's type. #! @returns How it ends: leaving only where it does in each way. def checker::stmt_ways(&mut this, node: u32, kind: u32): flow { val ways := this.choice_ways(kind); if ways.cases.over { this.report_ways(node); return flow::goes_on; } val low := this.lowest_node(node); var all_leave := true; this.repeating++; this.using_ways++; for c in 0..ways.chosen.length { var pass := this.begin_way(&ways, c, low, node, true); var made := flow::goes_on; match this.kind_of(node) { nk::for_stmt => this.check_for(node); nk::while_stmt => this.check_while(node); nk::if_stmt => made = this.check_if(node); else => made = this.check_stmt(node); } all_leave = all_leave && made == flow::leaves; if c == 0 { this.keep_results(this.at.table, pass.outer, low, node); } this.end_case(&mut pass); } this.using_ways--; this.end_repeat(); return all_leave ? flow::leaves : flow::goes_on; } #! Checks a match on a value of a type not chosen yet, or holding one, once in #! each way the type can be chosen, as `use_ways` checks a use: it handles every #! case and leaves where it does in each, and as a value, is of the type its #! arms share in each, or one chosen as the value's is where they differ. #! @param this The checker. #! @param node The match's node. #! @param value Whether it is used as a value. #! @param expected The type expected of its value, or the error type. #! @param made Where what checking finds is written. #! @param kind The type of the value matched. def checker::match_ways(&mut this, node: u32, value: bool, expected: u32, made: &mut checker::matched, kind: u32): void { val ways := this.choice_ways(kind); if ways.cases.over { this.report_ways(node); return; } val low := this.lowest_node(node); var results := vector::::new(); var every := true; var all_leave := true; this.repeating++; this.using_ways++; for c in 0..ways.chosen.length { var pass := this.begin_way(&ways, c, low, node, true); var each := checker::matched { shared = made.shared, every = true, all_leave = true }; this.check_match(node, value, expected, &mut each); results.append(each.shared == 0 ? bt::error : each.shared); every = every && each.every; all_leave = all_leave && each.all_leave; if c == 0 && node > low { this.keep_results(this.at.table, pass.outer, low, node - 1); } this.end_case(&mut pass); } this.using_ways--; this.end_repeat(); made.every = every; made.all_leave = all_leave; if value { var failed := false; for &each in results.span { failed = failed || each == bt::error; } made.shared = failed ? 0u : this.ways_type(kind, results.span); } } #! Copies the results of the nodes from `low` to `high` from one table to #! another holding them. #! @param this The checker. #! @param from The table copied from. #! @param to The table copied to. #! @param low The first node. #! @param high The last node. def checker::keep_results(&mut this, from: u32, to: u32, low: u32, high: u32): void { val a: rtable = this.tables.p[from]; val b: rtable = this.tables.p[to]; return if low < a.base || low < b.base || high - a.base >= a.count || high - b.base >= b.count; for n in low..=high { this.copy_results((a.offset + n - a.base) as u64, (b.offset + n - b.base) as u64); } } #! The first node of what a node holds, whose nodes are numbered from it to the #! node itself. #! @param this The checker. #! @param node The node. #! @returns The node. def checker::lowest_node(&mut this, node: u32): u32 { var todo := vector::::new(); var lowest := node; todo.append(node); while todo.length != 0 { val at: u32 = todo.last; todo.truncate(); continue if at == 0; lowest = at < lowest ? at : lowest; this.syn().children(at, &mut todo); } return lowest; } #! Whether a type fits a pattern, as `pattern_fits` says, or within a generic's #! own body, where what an `if const` branch or a `match const` arm says of a #! type parameter decides it: a pattern it is said to fit, or not to, or one #! that rules it out, as not fitting `numeric` does `integer`, a type it is named #! as, or a type its bounds rule out, as `strview` is of an `integer`. #! @param this The checker. #! @param given The type. #! @param pattern The pattern's type. #! @returns 1 where it fits, 0 where it does not, and -1 where only each #! instance knows. pub def checker::proved_fit(&mut this, given: u32, pattern: u32): i32 { val fits := this.pattern_fits(given, pattern); return fits if fits >= 0; val param := this.param_of(given); return -1 if param == 0; var i := this.proved_params.length; while i > 0 { i--; val at: u32 = this.proved_params[i]; val known: u32 = this.proved_bounds[i]; if at == (param | checker::unfit_known) { return 0 if this.rules_out(known, pattern); # A `bool` that is not one value is the other. return 1 if this.bool_value(known) >= 0 && this.bool_value(pattern) >= 0; } else if at == (param | checker::fit_known) { return 1 if known == pattern; # Named as a type, it fits what that type fits, and one generic type's # instance is no other's. return this.pattern_fits(known, pattern) if this.pattern_bound(known) == 0; return 0 if this.instance_pattern(known) != 0 && this.instance_pattern(pattern) != 0; } } # A kind of type the compiler decides is none of the others, so one a type # parameter is bounded by, or said to fit, rules out the rest, as `T: # pointer` does `T is reference`. val kind := this.decided_kind(pattern); if kind != 0 { this.resolve(param); val run: u32 = this.decls.children.p[param]; for b in 0..this.decls.list_length(run) { val other := this.decided_kind(this.decls.list_at(run, b as u32)); return 0 if other != 0 && other != kind; } for b in 0..this.proved_params.length { val at: u32 = this.proved_params[b]; continue if at != param && at != (param | checker::fit_known); val other := this.decided_kind(this.proved_bounds[b]); return 0 if other != 0 && other != kind; } } # A type its bounds rule out it is not. if this.pattern_bound(pattern) == 0 && !this.types.is_open(pattern) { this.resolve(param); val run: u32 = this.decls.children.p[param]; for b in 0..this.decls.list_length(run) { val bound := this.decls.list_at(run, b as u32); return 0 if this.named_kind(bound) == dk::trait_type && !this.satisfies(pattern, bound); } for b in 0..this.proved_params.length { continue if this.proved_params[b] != param; return 0 if !this.satisfies(pattern, this.proved_bounds[b]); } } return -1; } #! Which kind of type a trait the compiler decides says a type is, of those no #! type is two of: a pointer, a reference, an array, an integer, a real, or an #! instance of a generic type. #! @param this The checker. #! @param kind The trait's type. #! @returns 1 to 6 for each kind, or 0 where it is none of those traits. def checker::decided_kind(&mut this, kind: u32): u32 { return 0 if this.named_kind(kind) != dk::trait_type; val decl := this.types.first_of(kind); val name: u32 = this.decls.names.p[decl]; return 0 if this.std_decl(name) != decl; return 1 if name == word::pointer; return 2 if name == word::reference; return 3 if name == word::array; return 4 if name == word::integer; return 5 if name == word::real; return 6 if name == word::generic; return 0; } #! Which `bool` a constant type argument is, as `true` and `false` are. #! @param this The checker. #! @param kind The type. #! @returns 1 for `true`, 0 for `false`, and -1 where it is no `bool`. pub def checker::bool_value(&this, kind: u32): i32 { return -1 if this.types.kind_of(kind) != ty::value || this.types.first_of(kind) != bt::boolean; val held: known = this.knowns.p[this.types.second_of(kind)]; return -1 if held.kind != kk::number; return held.value.magnitude != 0 ? 1 : 0; } #! Whether what is proved within a generic's own body decides a condition #! known while compiling, as `if const !T::mutable` is where `T::mutable` is #! said not to hold: where it rules out every case the condition holds in, or #! every case it fails in. #! @param this The checker. #! @param condition The condition's node. #! @returns 1 where it holds, 0 where it fails, and -1 where only each instance #! knows. pub def checker::decided_condition(&mut this, condition: u32): i32 { return -1 if this.proved_params.length == 0; val holds := this.cases_of(condition, true); return 0 if this.ruled_out(&holds); val fails := this.cases_of(condition, false); return 1 if this.ruled_out(&fails); return -1; } #! Whether what is proved rules out every one of some cases: each says what a #! type parameter fits, or does not, that what is proved says otherwise. #! @param this The checker. #! @param cases The cases. #! @returns Whether it does. def checker::ruled_out(&mut this, cases: &checker::cases): bool { return false if cases.over; for c in 0..cases.ends.length { var out := false; for i in cases.start(c)..cases.ends[c] { val each: checker::fact = cases.facts[i]; val fits := this.proved_fit(this.decls.types.p[each.param], each.pattern); if (each.holds && fits == 0) || (!each.holds && fits > 0) { out = true; break; } } return false if !out; } return true; } #! The type a `match const` type chooses within a generic's own body, where #! what is proved of the type parameter it chooses by decides it. #! @param this The checker. #! @param kind The type. #! @returns The type chosen, or 0 where only each instance knows. pub def checker::proved_choice(&mut this, kind: u32): u32 { val subject := this.types.part_at(kind, 0); val count := this.types.part_count(kind); var at: u32 = 1; while at + 1 < count { val pattern := this.types.part_at(kind, at); return this.types.part_at(kind, at + 1) if pattern == 0; val fits := this.proved_fit(subject, pattern); return 0 if fits < 0; return this.types.part_at(kind, at + 1) if fits > 0; at += 2; } return 0; } #! A type with each alias of a `match const` it holds that knows the type it #! chooses by replaced by the type it chooses, as `strip_pointer<*i32>` is #! `i32`, so the two are one type, to `is` and as an instance's argument. One #! choosing by a type parameter is a type of its own until each instance gives #! the parameter a type. #! @param this The checker. #! @param kind The type. #! @param depth How deeply the search has gone. #! @returns The type, or the error type where the choosing never ends. pub def checker::chosen_type(&mut this, kind: u32, depth: u32): u32 { return kind if this.choosing_aliases == 0 || kind < bt::count as u32 || depth > checker::max_aliases as u32; val tkd := this.types.kind_of(kind); match tkd { ty::named, ty::function, ty::pack => { var parts := vector::::new(); var changed := false; for i in 0..this.types.part_count(kind) { val part := this.types.part_at(kind, i as u32); return kind if part == bt::error; val made := this.chosen_type(part, depth + 1); changed = changed || made != part; parts.append(made); } var first := this.types.first_of(kind); if tkd == ty::function { val result := this.chosen_type(first, depth + 1); changed = changed || result != first; first = result; } val whole := changed ? this.types.intern(tkd, first, this.types.second_of(kind), parts.span) : kind; return whole if !this.chooses_now(whole); # The alias is followed to the type it chooses, which may be another. var at := whole; var steps: u64 = 0; while this.chooses_now(at) { if steps == checker::max_aliases { this.report_endless(whole); return bt::error; } val target := this.alias_target(at); at = this.types.kind_of(target) == ty::matched && this.choosable(target) ? this.reduce_match(target) : target; steps++; } return this.chosen_type(at, depth + 1); } ty::pointer, ty::reference, ty::fixed => { val inner := this.chosen_type(this.types.first_of(kind), depth + 1); return kind if inner == this.types.first_of(kind); var parts := vector::::new(); parts.extend(this.types.parts_view(kind)); return this.types.intern(tkd, inner, this.types.second_of(kind), parts.span); } else => return kind; } return kind; } #! Whether a type is an alias of `match const` that chooses now: given types #! holding no type parameter, or whose choice those don't change, and not being #! resolved, as within its own declaration. #! @param this The checker. #! @param kind The type. #! @returns Whether it is. def checker::chooses_now(&mut this, kind: u32): bool { return false if this.types.kind_of(kind) != ty::named; val decl := this.types.first_of(kind); return false if !this.decls.has(decl, df::chooses) || this.decls.states.p[decl] == ds::resolving; return true if !this.types.is_open(kind); # One given types holding type parameters chooses now where what it chooses # by decides it whatever they are, as `castref<*mut T>` chooses `pointer`. val target := this.alias_target(kind); return this.types.kind_of(target) == ty::matched && this.choosable(target); } #! The shape a type parameter is known to have within a generic's own body, as #! `T: array` says `T` is a `T::element[T::length]`, and `T is vector` that it #! is a `vector`, so it is indexed, walked, and has members as #! that type, and is one with it. A type it is named as is that type. #! @param this The checker. #! @param kind The type. #! @returns The shape, or 0 where the type is no type parameter or has none. pub def checker::param_shape(&mut this, kind: u32): u32 { val plain := this.unalias(kind); return 0 if this.types.kind_of(plain) != ty::param; val param := this.types.first_of(plain); return 0 if this.decls.has(param, df::valued); var i := this.proved_params.length; while i > 0 { i--; continue if this.proved_params[i] != (param | checker::fit_known); val known: u32 = this.proved_bounds[i]; return known if this.pattern_bound(known) == 0; val generic := this.instance_pattern(known); if generic != 0 { val made := this.generic_shape(plain, generic); return made if made != 0; } } val array := this.std_decl(word::array); return this.array_shape(plain, array) if array != 0 && this.param_bounded(plain, array); val pointer := this.std_decl(word::pointer); return this.pointer_shape(plain, pointer) if pointer != 0 && this.param_bounded(plain, pointer); val reference := this.std_decl(word::reference); return this.pointer_shape(plain, reference) if reference != 0 && this.param_bounded(plain, reference); return 0; } #! A type with its aliases followed, as `unalias` gives it, or where that is a #! type parameter known to have a shape, that shape's, as `shaped` gives it. #! @param this The checker. #! @param kind The type. #! @returns The type. pub def checker::shaped_plain(&mut this, kind: u32): u32 { val plain := this.unalias(kind); return plain if this.types.kind_of(plain) != ty::param; val shape := this.param_shape(plain); return shape != 0 ? this.unalias(shape) : plain; } #! A type as the shape its type parameter is known to have, as `param_shape` #! says, or the type itself. #! @param this The checker. #! @param kind The type. #! @returns The type. pub def checker::shaped(&mut this, kind: u32): u32 { return kind if this.types.kind_of(kind) != ty::param && !this.is_alias(kind); val shape := this.param_shape(kind); return shape != 0 ? shape : kind; } #! The shape of a type parameter bounded by `array`: `T::element[T::length]`. #! @param this The checker. #! @param kind The type parameter's type. #! @param array The declaration of `array`. #! @returns The shape. def checker::array_shape(&mut this, kind: u32, array: u32): u32 { val bound := this.decls.types.p[array]; val element := this.trait_member(array, word::element); val length := this.trait_member(array, word::length); return 0 if element == 0 || length == 0; return this.types.fixed_of(this.projected(kind, element, bound), this.constant_projected(kind, length, bound)); } #! The shape of a type parameter bounded by `pointer`: `*T::target`, which `*x` #! reads, and `*mut T::target`, which it writes too, where `if const #! T::mutable` says so; or by `reference`, `&T::target`, and `&mut T::target` #! so. #! @param this The checker. #! @param kind The type parameter's type. #! @param bound The declaration of `pointer` or `reference`. #! @returns The shape. def checker::pointer_shape(&mut this, kind: u32, bound: u32): u32 { val holder := this.decls.types.p[bound]; val target := this.trait_member(bound, word::target); val mutable := this.trait_member(bound, word::mutable); return 0 if target == 0; val writes := mutable != 0 && this.proved_true(this.constant_projected(kind, mutable, holder)); val held := this.projected(kind, target, holder); return this.decls.names.p[bound] == word::reference ? this.types.reference(held, writes) : this.types.pointer(held, writes); } #! Whether what is proved says a constant parameter is `true`, as `if const #! T::mutable` says of `T::mutable`. #! @param this The checker. #! @param kind The constant parameter's type. #! @returns Whether it does. def checker::proved_true(&mut this, kind: u32): bool { return false if this.types.kind_of(kind) != ty::param; val param := this.types.first_of(kind); val truth := this.truth_type(); for i in 0..this.proved_params.length { return true if this.proved_params[i] == (param | checker::fit_known) && this.proved_bounds[i] == truth; } return false; } #! The shape of a type parameter known to be an instance of a generic struct or #! union: the generic given the types of `T::args`, as `vector`, #! a constant parameter the constant there, and a pack the types from there on, #! each bounded as the generic bounds its parameter. #! @param this The checker. #! @param kind The type parameter's type. #! @param generic The generic declaration. #! @returns The shape, or 0 where `generic` names none. def checker::generic_shape(&mut this, kind: u32, generic: u32): u32 { val bound := this.std_decl(word::generic); return 0 if bound == 0; val asked := this.trait_member(bound, word::args); return 0 if asked == 0; val pack := this.projected(kind, asked, this.decls.types.p[bound]); this.resolve(generic); val params: u32 = this.decls.params.p[generic]; val count := this.decls.list_length(params); var given := vector::::new(); for i in 0..count { given.append(this.shape_param(pack, generic, i as u32)); } # Each is bounded as the generic bounds its parameter, given the others. var from := vector::::new(); from.extend(this.decls.list_view(params)); for i in 0..count { val param := this.decls.list_at(params, i as u32); val stand := this.types.first_of(given[i]); continue if this.decls.children.p[stand] != 0 || this.decls.has(param, df::valued); this.resolve(param); var written := vector::::new(); written.extend(this.decls.list_view(this.decls.children.p[param])); var bounds := vector::::new(); for &each in written.span { bounds.append(this.substitute(each, from.span, given.span)); } if bounds.length != 0 { this.decls.children.p[stand] = this.decls.make_list(bounds.span); } } return this.types.named(generic, given.span); } #! Whether a type parameter standing for the elements of an array has a zero #! value: where the array does, as `T::element` does where `T: array` is also #! `zeroable`, since an array has one only where its elements do. #! @param this The checker. #! @param kind The type parameter's type. #! @returns Whether it has. def checker::element_zeroable(&mut this, kind: u32): bool { val param := this.types.first_of(kind); for &each in this.stand_ins { continue if each.param != param; return false if this.decls.names.p[each.asked] != word::element; val holder: u32 = this.decls.owners.p[each.asked]; return holder == this.std_decl(word::array) && this.param_zeroable(each.base); } return false; } #! The type a generic's own body gave a node, which each instance shares. #! @param this The checker. #! @param node The node. #! @returns The type, or 0 where the body gave it none. def checker::template_type(&this, node: u32): u32 { val row := this.file_row(this.at.file, node); return row < this.r_types.length ? this.r_types.p[row] : 0; } #! A type a generic's own body worked out, as an instance has it: each type #! parameter of the declaration, and those standing for what its bounds ask of #! one, given the type the instance gives it. #! @param this The checker. #! @param kind The type. #! @returns The type. pub def checker::in_instance(&mut this, kind: u32): u32 { return kind if this.at.instance == 0 || kind == 0 || !this.types.is_open(kind); var params := vector::::new(); this.params_in(kind, &mut params, 0); # What stands for what a bound asks of a type parameter, or for a pack's type, # is given its type through that parameter. var i: u64 = 0; while i < params.length { val param: u32 = params[i]; i++; for &each in this.stand_ins { if each.param == param { this.add_param(&mut params, this.types.first_of(each.base)); } } for &each in this.element_ins { if each.param == param { this.add_param(&mut params, this.types.first_of(each.pack)); this.params_in(each.at, &mut params, 0); } } } var from := vector::::new(); var to := vector::::new(); for ¶m in params { val name := this.decls.names.p[param]; continue if this.param_named(name) != param; val bound := this.bound(name); continue if (bound & ref_type) == 0; from.append(param); to.append(bound & ref_mask); } return this.substitute(kind, from.span, to.span); } #! Gathers the declarations of the type parameters a type holds. #! @param this The checker. #! @param kind The type. #! @param into Where each is added, once. #! @param depth How deeply the search has gone. def checker::params_in(&this, kind: u32, into: &mut vector, depth: u32): void { return if kind == 0 || kind < bt::count as u32 || depth > 32 || !this.types.is_open(kind); val tkd := this.types.kind_of(kind); if tkd == ty::param { this.add_param(into, this.types.first_of(kind)); return; } if tkd == ty::pack && this.types.is_open_pack(kind) && this.types.first_of(kind) != 0 { this.add_param(into, this.types.first_of(kind)); } if tkd == ty::function || tkd == ty::pointer || tkd == ty::reference || tkd == ty::fixed { this.params_in(this.types.first_of(kind), into, depth + 1); } for i in 0..this.types.part_count(kind) { this.params_in(this.types.part_at(kind, i as u32), into, depth + 1); } } #! Adds a declaration to a list, once. #! @param this The checker. #! @param into The list. #! @param param The declaration. def checker::add_param(&this, into: &mut vector, param: u32): void { for &each in into.span { return if each == param; } into.append(param); } #! Gives the type parameter a `match const` names the bound the pattern of the #! arm an instance chose tests, as `give_is` does for `if const`. #! @param this The checker. #! @param subject The node of what the match chooses by. #! @param pattern The pattern the type fit, 0 for `else`. def checker::give_kind(&mut this, subject: u32, pattern: u32): void { return if pattern == 0 || pattern == bt::error || this.kind_of(subject) != nk::name; val param := this.param_named(this.name_of(subject)); return if param == 0; val bound := this.pattern_bound(pattern); return if bound == 0; this.proved_params.append(param); this.proved_bounds.append(bound); } #! Gives the type parameter a node names alone, as `T`, the bound a pattern it #! was decided to fit tests, as an instance deciding `T is reference` does, so #! `T::target` names what it refers to, until `unprove_to` forgets it. #! @param this The checker. #! @param node The `name` or `type_name` node. #! @param pattern The pattern's type. def checker::give_named(&mut this, node: u32, pattern: u32): void { val kind := this.kind_of(node); return if pattern == 0 || pattern == bt::error; return if kind != nk::name && (kind != nk::type_name || this.segment_count(node) != 1 || this.run_length(this.segment_args(node, 0)) != 0); val param := this.param_named(kind == nk::name ? this.name_of(node) : this.segment_name(node, 0)); return if param == 0; val bound := this.pattern_bound(pattern); return if bound == 0; this.proved_params.append(param); this.proved_bounds.append(bound); } #! The bound a pattern tests: the trait it is, or `generic` for a generic type #! named without type arguments. #! @param this The checker. #! @param pattern The pattern's type. #! @returns The bound, or 0 where the pattern is a type. def checker::pattern_bound(&mut this, pattern: u32): u32 { if this.instance_pattern(pattern) != 0 { val generic := this.std_decl(word::generic); return generic != 0 ? this.decls.types.p[generic] : 0; } return this.named_kind(pattern) == dk::trait_type ? pattern : 0; } #! Whether a type is one `match const` chooses that can be chosen now: its #! patterns hold no type parameter, and the type it chooses by holds none, or #! has a shape of its own that decides each arm up to the one it fits, each a #! kind of type the compiler decides, as `*mut T` fits `pointer` and `&mut T` #! does not, whatever `T` is, and whatever is proved of it. #! @param this The checker. #! @param kind The type. #! @returns Whether it is. pub def checker::choosable(&mut this, kind: u32): bool { return false if this.types.kind_of(kind) != ty::matched; val count := this.types.part_count(kind); var at: u32 = 1; while at < count { return false if this.types.is_open(this.types.part_at(kind, at)); at += 2; } val subject := this.types.part_at(kind, 0); return true if !this.types.is_open(subject); return false if !this.shape_known(subject); at = 1; while at + 1 < count { val pattern := this.types.part_at(kind, at); return true if pattern == 0; return false if this.decided_kind(pattern) == 0; return true if this.pattern_fits(subject, pattern) > 0; at += 2; } return false; } #! The type a `match const` type chooses: what the first arm whose pattern the #! type it chooses by fits gives, chosen in turn where that is one too, as a #! part of `C ? A : B` leads to the next. One no arm fits is reported where it #! was written, and so is one choosing within itself without end. #! @param this The checker. #! @param kind The type, which `choosable` says can be chosen. #! @returns The type chosen, or the error type. pub def checker::reduce_match(&mut this, kind: u32): u32 { if this.reducing as u64 >= checker::max_aliases { this.report_endless(kind); return bt::error; } this.reducing++; val subject := this.types.part_at(kind, 0); val count := this.types.part_count(kind); var made: u32 = bt::error; var found := false; var at: u32 = 1; while at + 1 < count { val pattern := this.types.part_at(kind, at); if pattern == 0 || this.pattern_fits(subject, pattern) > 0 { made = this.types.part_at(kind, at + 1); found = true; break; } at += 2; } # What a part of `C ? A : B` chooses may be what the next part chooses. if found && this.choosable(made) { made = this.reduce_match(made); } this.reducing--; if !found && this.first_report(kind) { var text := string::from("No arm of the `match const` fits `", nothing {}); text.append(this.describe(subject).span); text.append("`, and it has no `else`."); this.report_in(this.types.first_of(kind), this.types.second_of(kind), ec::arms, text.span); } return made; } #! Reports a type whose aliases, or the types `match const` chooses, are not #! followed to their end within `max_aliases` steps, as one choosing by itself #! is not: where the `match const` was written, or the alias declared. #! @param this The checker. #! @param kind The type. pub def checker::report_endless(&mut this, kind: u32): void { return if !this.first_report(kind); var text := string::from("`", nothing {}); text.append(this.describe(kind).span); text.append("` is not worked out within 32 steps of its aliases and `match const` choices, so they never end."); if this.types.kind_of(kind) == ty::matched { this.report_in(this.types.first_of(kind), this.types.second_of(kind), ec::cycle, text.span); } else if this.types.kind_of(kind) == ty::named { this.report_decl(this.types.first_of(kind), ec::cycle, text.span); } } #! Whether what is wrong of a type is to be reported now: once, and not where #! checking is quiet, so it is reported where it is not. #! @param this The checker. #! @param kind The type. #! @returns Whether it is. def checker::first_report(&mut this, kind: u32): bool { return false if this.quiet != 0 || (this.types.flags.p[kind] & tf::reported as u8) != 0; this.types.flags.p[kind] |= tf::reported as u8; return true; }