dymokomi/luce-base
Source of release 0.46.0
0.46.0 / src / sema / check / statements.lucb
#==============================================================================================
#
# check/statements - Statements
#
# DESCRIPTION:
# Bindings, assignments, control flow, `defer`, and `match` (§8).
#
#==============================================================================================
extend Checker:
# mark: Statements =========================================================================
func check_block(block: ast.Node*) -> !:
self.push_scope()
var cursor = block.body
while let s = cursor:
let conditional = s.kind == ast.Kind.if_ or s.kind == ast.Kind.while_
self.folded_quietly = false
try self.check_stmt(s)
if let rest = s.next:
if prune.certainly_leaves(s):
# code after a folded platform test is another target's and may name what
# this one lacks; anything else unreachable is still checked, then dropped
if not (conditional and self.folded_quietly):
var r = self.reporter()
try r.warn(rest, "unreachable code")
try self.check_unreachable(rest)
s.next = none
cursor = s.next
self.pop_scope()
var r = self.reporter()
try prune.prune_block(block, &r)
## The statements from `first` on, which never run: checked as any others, in a scope of
## their own, so a mistake there is not hidden by the `return` above it.
func check_unreachable(first: ast.Node*) -> !:
self.push_scope()
var cursor: ast.Node*? = first
while let s = cursor:
self.folded_quietly = false
try self.check_stmt(s)
cursor = s.next
self.pop_scope()
func check_stmt(s: ast.Node*) -> !:
match s.kind:
.let_, .var_:
try self.check_binding(s)
.assign:
try self.check_assign(s)
.if_:
try self.check_if(s)
if not s.has(ast.flag_if_let):
let quiet = try self.fold_condition(s)
self.folded_quietly = quiet
var r = self.reporter()
try prune.fold_constant_branch(s, &r, quiet)
.while_:
self.push_scope()
try self.check_condition(s)
try self.enter_loop(s)
let body = s.body else return try self.fail(s, "a loop needs a body")
try self.check_block(body)
self.leave_loop(s)
self.pop_scope()
if not s.has(ast.flag_if_let):
let quiet = try self.fold_condition(s)
var r = self.reporter()
try prune.fold_constant_branch(s, &r, quiet)
.for_:
try self.check_for(s)
.match_:
_ = try self.match_type(s, types.none_id, false)
.return_:
if self.in_defer:
try self.fail(s, "a deferred block cannot `return` (§8.8)")
try self.check_return(s)
.break_, .continue_:
if self.loop_depth == 0:
try self.fail(s, "not inside a loop")
elif s.text.length > 0 and not self.has_label(s.text):
try self.fail(s, "no enclosing loop has this label")
.defer_, .errdefer_:
if s.kind == ast.Kind.errdefer_ and not self.fallible:
try self.fail(s, "`errdefer` needs a fallible function")
try self.check_deferred(s)
.recover_:
if not self.in_catch:
try self.fail(s, "`recover` belongs in a `catch` handler")
let v = s.left else return try self.fail(s, "`recover` needs a value")
_ = try self.expect(v, self.catch_result)
try self.handled_recovered(v)
.free_:
let v = s.left else return try self.fail(s, "`free` needs a value")
let got = try self.check_expr(v, types.none_id)
let t = self.table.get(got)
if t.kind != types.Kind.pointer and t.kind != types.Kind.span:
try self.fail(v, "`free` takes a pointer or a span")
if let alloc_in = s.right:
try self.check_allocator(alloc_in)
.with_:
let a = s.left else return try self.fail(s, "`with` names an allocator")
try self.check_allocator(a)
let body = s.body else return try self.fail(s, "`with` needs a body")
try self.check_block(body)
.expr_stmt:
let v = s.left else return try self.fail(s, "empty statement")
let got = try self.check_expr(v, types.none_id)
if self.table.kind(got) == types.Kind.fallible:
try self.fail(v, "a fallible result must be handled: `try`, `catch`, or `else`")
if not is_call_statement(v):
try self.fail(v, "a statement is a call; an expression whose value is unused is not one (§7.9)")
.asm_:
var operand = s.right
while let o = operand:
if let v = o.left:
if v.kind != ast.Kind.name or v.text != "_":
_ = try self.check_expr(v, types.none_id)
operand = o.next
try self.check_asm_references(s)
.block:
try self.check_block(s)
_:
try self.fail(s, "unsupported statement")
## A deferred block (§8.8) is checked where it is written and runs as its scope is left,
## reading its names then. Nothing leaves it: no `return`, no failure escaping it (`try`,
## `error`), no `break` or `continue` to a loop outside it.
func check_deferred(s: ast.Node*) -> !:
let block = s.body else return try self.fail(s, "`defer` needs a statement or a block")
let saved_fallible = self.fallible
let saved_effect = self.expression_effect
let saved_loops = self.loop_depth
let saved_labels = self.label_count
let saved_defer = self.in_defer
self.fallible = false
self.expression_effect = false
self.loop_depth = 0
self.label_count = 0
self.in_defer = true
try self.check_block(block)
self.fallible = saved_fallible
self.expression_effect = saved_effect
self.loop_depth = saved_loops
self.label_count = saved_labels
self.in_defer = saved_defer
## `{name}` in an asm block's text stands for the register the compiler chose for the
## `reg` operand whose expression is the name `name` (§8.9); anything else is an error.
func check_asm_references(s: ast.Node*) -> !:
var lines = s.body
while let l = lines:
let b = l.text.bytes
var i: usize = 0
while i < b.length:
if b[i] == '{':
var j = i + 1
while j < b.length and b[j] != '}':
j += 1
if j >= b.length:
return try self.fail(l, "an unclosed `{` in assembly text")
let name = (str)b[i + 1..<j]
if not asm_names_operand(s, name):
var clipped: u8[64]
let message = try strings.format(self.scratch[self.scratch.length // 2..<self.scratch.length], f"the assembly text names `{try clip(clipped, name)}`, which is not a `reg` operand")
return try self.fail(l, message)
i = j
i += 1
lines = l.next
## `var x: T` where `T` has no zero value: what keeps it from one (§6.1).
func no_zero_value(s: ast.Node*, declared: u32) -> !:
var room: u8[512]
if let f = try self.undefaulted_field(declared):
let owner = self.table.get(declared).decl else return try self.fail(s, "this type has no zero value; give the `var` a value")
return try self.fail(s, said(room, f"this type has no zero value: field `{f.text}` of `{owner.text}` has no default and its type no zero value; give the field a default or the `var` a value"))
return try self.fail(s, "this type has no zero value; give the `var` a value")
## `let`/`var`, with a name or a tuple of names (§6.1).
## Whether `e` is a member of a `packed` struct whose natural alignment exceeds one:
## the address C would make undefined to dereference (§5.11).
func is_packed_field(e: ast.Node*) -> bool:
if e.kind != ast.Kind.member:
return false
let owner = e.left else return false
let field = e.resolved else return false
if field.kind != ast.Kind.field:
return false
let decl = self.table.get(self.deref(owner.type_id)).decl else return false
return decl.has(ast.flag_packed) and self.table.align_of(field.type_id) > 1
## A loop opens: its label, when it has one, may be neither a core name (§3.5) nor one
## of the labels already open around it (§8.5).
func enter_loop(s: ast.Node*) -> !:
self.loop_depth += 1
if s.text.length == 0:
return
if token.is_core_name(s.text):
try self.fail(s, "this name belongs to the language; a label cannot take it (§3.5)")
elif self.has_label(s.text):
try self.fail(s, "this label is already open")
if self.label_count == self.labels.length:
var text: u8[96]
return try self.fail(s, try strings.format(text, f"at most {self.labels.length} labeled loops nest"))
self.labels[self.label_count] = s.text
self.label_count += 1
func leave_loop(s: ast.Node*):
self.loop_depth -= 1
if s.text.length > 0:
self.label_count -= 1
func has_label(name: str) -> bool:
var i: usize = 0
while i < self.label_count and i < self.labels.length:
if self.labels[i] == name:
return true
i += 1
return false
func check_binding(s: ast.Node*) -> !:
let mutable = s.kind == ast.Kind.var_
var declared = types.none_id
if let t = s.ty:
declared = try self.resolve_type(t)
try self.storable(declared, t)
if s.body != none:
let init = s.left else return try self.fail(s, "a tuple binding needs a value")
let got = try self.expect(init, declared)
let t = self.table.get(got)
if t.kind != types.Kind.tuple:
return try self.fail(init, "a tuple binding needs a tuple value")
if ast.count(s.body) != t.args.length:
return try self.fail(s, "the number of names does not match the tuple")
var i: usize = 0
var cursor = s.body
while let n = cursor:
n.type_id = t.args[i]
if n.text != "_":
try self.bind(n, n.text, t.args[i], n, mutable)
i += 1
cursor = n.next
s.type_id = got
return
var id = declared
if declared == types.fmt_id:
return try self.fail(s, "a formatted string is not stored; `fmt` is a parameter type only (§9.1)")
if let init = s.left:
if declared != types.none_id:
_ = try self.expect(init, declared)
else:
let got = try self.check_expr(init, declared)
id = try self.settle(init, got)
if id == types.none_id:
return try self.fail(init, "`none` needs a written type")
if got == types.untyped_int_id or got == types.untyped_float_id:
_ = try self.coerce(init, got, id)
if id == types.fmt_id:
return try self.fail(init, "a formatted string is not a value: print it, write it, or format it (§4.4)")
if id == types.unit_id:
return try self.fail(init, "this expression has no value")
if id == types.never_id:
return try self.fail(init, "this expression never yields a value")
if self.table.kind(id) == types.Kind.fallible:
return try self.fail(init, "a fallible result must be handled: `try`, `catch`, or `else`")
if init.has(ast.flag_type_ref):
return try self.fail(init, "a type is not a value")
elif s.has(ast.flag_uninit):
if not mutable or declared == types.none_id:
return try self.fail(s, "`= ---` needs `var` with a written type")
else:
if not mutable:
return try self.fail(s, "a `let` needs a value")
if declared == types.none_id:
return try self.fail(s, "a `var` without a value needs a type")
if not self.table.zeroable(declared):
if not try self.has_zero_value(declared):
return try self.no_zero_value(s, declared)
# a struct whose fields have defaults or zero values: `var s: S` is `S()`
s.left = try self.zero_construction(declared, s)
s.type_id = id
try self.bind(s, s.text, id, s, mutable)
# `let s = &local` or `let s = local_array[..]`: the name inherits the escape taint
if let init = s.left:
if is_local_value(init) and s.text != "_":
self.set_from_local(s.text, true)
# `let text = failure.message`: the name views the handled message too (§11.3)
let held = self.handled_view(init)
if held > 0:
self.set_handled(s.text, held)
func check_assign(s: ast.Node*) -> !:
let place = s.left else return try self.fail(s, "missing place")
let value = s.right else return try self.fail(s, "missing value")
if place.kind == ast.Kind.name and place.text == "_":
return try self.check_drop(s, value)
let pt = try self.check_expr(place, types.none_id)
if not self.is_mutable_place(place) and not self.changes_receiver(place):
if place.kind == ast.Kind.name:
return try self.fail(place, "cannot assign to a `let`")
return try self.fail(place, "this place is not mutable")
if s.text == "=":
_ = try self.expect(value, pt)
if is_local_value(value) and self.escapes(pt) and self.place_outlives(place):
# §6.6: a local's address may not be stored in a global or through a pointer
try self.fail(value, "this pointer or view names a local and must not be stored where it outlives the function")
try self.handled_stored(place, value, pt)
return
let target = self.table.get(pt).elem if self.table.kind(pt) == types.Kind.atomic else pt
# `p += n` and `p -= n` move a pointer by elements, as `p = p + n` does (§7.7)
if self.table.kind(target) == types.Kind.pointer and (s.text == "+=" or s.text == "-="):
let nt = try self.check_expr(value, types.isize_id)
if nt == types.untyped_int_id:
_ = try self.coerce(value, nt, types.isize_id)
elif not (self.table.widens(nt, types.usize_id) or self.table.widens(nt, types.isize_id) or nt == types.usize_id or nt == types.isize_id):
try self.fail(value, "a pointer offset is `usize` or `isize`")
return
let vt = try self.expect(value, target)
let op = (str)s.text.bytes[..<s.text.length - 1]
if self.table.kind(pt) == types.Kind.atomic and not (op == "+" or op == "-" or op == "|" or op == "&" or op == "^"):
return try self.fail(s, "an atomic compound assignment is `+=` `-=` `|=` `&=` or `^=` (§15.1)")
if op == "<<" or op == ">>":
if not self.table.is_integer(target):
try self.fail(s, "a shift needs an integer")
return
if op == "&" or op == "|" or op == "^":
if not (self.table.is_integer(target) or target == types.bool_id or self.is_backed_enum(target)):
try self.fail(s, "bit operators need integers")
return
if op == "/":
if not self.table.is_float(target):
try self.fail(s, "integer division is `//`")
return
if op == "//" or op == "%" or op == "+%" or op == "-%" or op == "*%" or op == "+|" or op == "-|" or op == "*|":
if not self.table.is_integer(target):
try self.fail(s, "this operator needs integers")
return
if not self.table.is_numeric(target):
try self.fail(s, "arithmetic needs numbers")
_ = vt
## The condition of `if` and `while`: a `bool`, or `let name = optional`.
func check_condition(s: ast.Node*) -> !:
let condition = s.left else return try self.fail(s, "missing condition")
if s.has(ast.flag_if_let):
let init = condition.left else return try self.fail(condition, "`let` needs a value")
let got = self.plain_read(try self.check_expr(init, types.none_id))
let t = self.table.get(got)
if t.kind != types.Kind.optional:
return try self.fail(init, "`if let` needs an optional value")
condition.type_id = t.elem
try self.bind(condition, condition.text, t.elem, condition, false)
return
_ = try self.expect(condition, types.bool_id)
func check_if(s: ast.Node*) -> !:
self.push_scope()
try self.check_condition(s)
let body = s.body else return try self.fail(s, "`if` needs a body")
try self.check_block(body)
self.pop_scope()
if let rest = s.right:
if rest.kind == ast.Kind.if_:
try self.check_if(rest)
if not rest.has(ast.flag_if_let):
# an `elif` on a constant folds like the `if` it follows (§19.6)
let quiet = try self.fold_condition(rest)
var r = self.reporter()
try prune.fold_constant_branch(rest, &r, quiet)
else:
try self.check_block(rest)
## Whether member `e` is the `.indexed` of the `for` iterable being checked.
func is_indexed_iterable(e: ast.Node*) -> bool:
let member = self.indexed_iterable else return false
return member == e
## `_ = value` (§7.9): the value is evaluated and dropped, its failure handled first as
## a binding's would be; a `catch` handler of it may end without `recover`. The
## statement becomes the expression alone, which the backends evaluate and drop.
func check_drop(s: ast.Node*, value: ast.Node*) -> !:
if s.text != "=":
return try self.fail(s, "`_` takes a value with `=`")
let saved = self.dropped_catch
self.dropped_catch = value if value.kind == ast.Kind.catch_ else none
let got = self.check_expr(value, types.none_id) catch failure:
self.dropped_catch = saved
error(failure.code, failure.message)
self.dropped_catch = saved
if self.table.kind(got) == types.Kind.fallible:
return try self.fail(value, "a fallible result must be handled: `try`, `catch`, or `else`")
if value.has(ast.flag_type_ref):
return try self.fail(value, "a type is not a value")
_ = try self.settle(value, got)
s.kind = ast.Kind.expr_stmt
s.left = value
s.right = none
## `for x in items`, `for (i, x) in items.indexed()`, `for i in a..<b`,
## `for c in text`, `for p in &items` (§8.3).
func check_for(s: ast.Node*) -> !:
let binding = s.left else return try self.fail(s, "`for` needs a name")
let iterable = s.right else return try self.fail(s, "`for` needs something to iterate")
var declared = types.none_id
if let t = binding.ty:
declared = try self.resolve_type(t)
self.indexed_iterable = none
if iterable.kind == ast.Kind.call:
if let callee = iterable.left:
if callee.kind == ast.Kind.member and callee.text == "indexed":
self.indexed_iterable = callee
let got = try self.check_expr(iterable, declared)
self.indexed_iterable = none
let t = self.table.get(got)
var elem = types.none_id
if t.kind == types.Kind.span or t.kind == types.Kind.array:
elem = t.elem
if s.has(ast.flag_by_pointer):
let const_elem = t.is_const if t.kind == types.Kind.span else not self.is_mutable_place(iterable)
elem = try self.table.pointer(t.elem, const_elem, false)
elif s.has(ast.flag_by_pointer):
return try self.fail(iterable, "`&` iterates a span or an array by pointer (§8.3)")
elif got == types.str_id:
elem = types.char_id
elif self.iterator_method(got) != none:
return try self.desugar_iterable_for(s, binding, iterable)
else:
return try self.fail(iterable, "`for` iterates a span, an array, a range, text, or an Iterable")
self.push_scope()
if binding.body != none:
let et = self.table.get(elem)
if et.kind != types.Kind.tuple:
return try self.fail(binding, "a tuple of names needs tuple elements; use `.indexed()`")
if ast.count(binding.body) != et.args.length:
return try self.fail(binding, "the number of names does not match the element")
var i: usize = 0
var cursor = binding.body
while let n = cursor:
n.type_id = et.args[i]
if n.text != "_":
try self.bind(n, n.text, et.args[i], n, false)
i += 1
cursor = n.next
else:
if declared != types.none_id and not self.assignable(none, elem, declared):
return try self.mismatch(binding, declared, elem)
binding.type_id = elem if declared == types.none_id else declared
try self.bind(binding, binding.text, binding.type_id, binding, false)
try self.enter_loop(s)
let body = s.body else return try self.fail(s, "a loop needs a body")
try self.check_block(body)
self.leave_loop(s)
self.pop_scope()
## The `iterator()` method of a struct or enum type, the mark of an Iterable (§14.4).
func iterator_method(id: u32) -> ast.Node*?:
let t = self.table.get(id)
if t.kind != types.Kind.struct_ and t.kind != types.Kind.enum_:
return none
let d = t.decl else return none
return find_member(d, "iterator", ast.Kind.func_)
## `for x in source: body` over an Iterable (§8.3) becomes, in its own block,
## `var __iterN = source.iterator()` and `while let x = __iterN.next(): body`: the
## iterator lives in a hidden local of its concrete type, and the backends see only a
## loop they already know.
func desugar_iterable_for(s: ast.Node*, binding: ast.Node*, sequence: ast.Node*) -> !:
if binding.body != none:
return try self.fail(binding, "an Iterable yields one value per step; bind one name")
let hidden = try self.hidden_name("__iter")
let start = try ast.Node.make(ast.Kind.var_, hidden, s.start, s.line)
start.left = try method_call(sequence, "iterator", s)
let advance = try ast.Node.make(ast.Kind.name, hidden, s.start, s.line)
var step = try ast.Node.make(ast.Kind.let_, binding.text, binding.start, binding.line)
step.ty = binding.ty
step.left = try method_call(advance, "next", s)
let loop = try ast.Node.make(ast.Kind.while_, s.text, s.start, s.line)
loop.flags |= ast.flag_if_let
loop.left = step
loop.body = s.body
start.next = loop
s.kind = ast.Kind.block
s.left = none
s.right = none
s.body = start
try self.check_block(s)
func check_return(s: ast.Node*) -> !:
let want = self.result_type
if s.left == none and want != types.unit_id and want != types.none_id:
try self.fail(s, "this function returns a value")
let v = s.left else return
if want == types.unit_id:
let got = try self.check_expr(v, types.unit_id)
if got != types.unit_id and got != types.never_id:
try self.fail(v, "this function returns nothing")
return
_ = try self.expect(v, want)
if is_local_value(v) and self.escapes(want):
try self.fail(v, "this pointer or view names a local and must not escape the function")
try self.handled_leaves(v, want, "returned")
## `match` as a statement (`is_expr` false) or an expression yielding a type.
func match_type(m: ast.Node*, hint: u32, is_expr: bool) -> u32!:
let scrutinee = m.left else return try self.fail(m, "`match` needs a value")
var st = try self.check_expr(scrutinee, types.none_id)
if st == types.untyped_int_id or st == types.untyped_float_id:
st = try self.coerce(scrutinee, st, concrete(st))
var result = hint
var coverage = Coverage(wildcard = false, some = false, none_ = false, truth = false, falsity = false, cases = 0)
var arm = m.body
while let a = arm:
self.push_scope()
var pattern = a.left
var first = true
while let p = pattern:
try self.check_pattern(p, st, first)
if literal_repeats(m, a, p):
return try self.fail(p, "duplicate pattern")
try self.note_pattern(p, st, a.right == none, &coverage)
first = false
pattern = p.next
if let guard = a.right:
_ = try self.expect(guard, types.bool_id)
let body = a.body else return try self.fail(a, "an arm needs a body")
if is_expr:
let bt = try self.check_expr(body, result)
if result == types.none_id:
if bt != types.never_id:
result = try self.settle(body, bt)
else:
_ = try self.coerce(body, bt, result)
else:
try self.check_block(body)
self.pop_scope()
arm = a.next
if is_expr and result == types.none_id:
return try self.fail(m, "the arms yield no value")
try self.check_exhaustive(m, st, coverage)
return result if is_expr else types.unit_id
## What the unguarded arms of a `match` cover so far (§8.4): a guarded arm counts for
## nothing, since its guard may fail.
func note_pattern(p: ast.Node*, st: u32, unguarded: bool, coverage: Coverage*) -> !:
if coverage.wildcard:
return try self.fail(p, "this pattern is unreachable: an earlier `_` arm takes everything")
if p.has(ast.flag_wildcard):
if unguarded:
coverage.wildcard = true
return
let v = p.left else return
let t = self.table.get(st)
if v.kind == ast.Kind.case_value and t.kind == types.Kind.optional and v.resolved == none:
let already = coverage.some if v.text == "some" else coverage.none_
if already:
return try self.fail(p, "duplicate pattern")
if unguarded:
if v.text == "some":
coverage.some = true
else:
coverage.none_ = true
return
if v.kind == ast.Kind.case_value:
let case_decl = v.resolved else return
let d = self.table.get(v.type_id).decl else return
let index = case_index(d, case_decl)
let bit: u64 = (u64(1) << index) if index < 64 else 0
if (coverage.cases & bit) != 0 and bit != 0:
return try self.fail(p, "duplicate pattern")
if unguarded:
coverage.cases |= bit
return
if v.kind == ast.Kind.literal and p.right == none:
if v.text == "true" or v.text == "false":
let already = coverage.truth if v.text == "true" else coverage.falsity
if already:
return try self.fail(p, "duplicate pattern")
if unguarded:
if v.text == "true":
coverage.truth = true
else:
coverage.falsity = true
return
## Every `match` is exhaustive (§8.4): the compiler names what is missing.
func check_exhaustive(m: ast.Node*, st: u32, coverage: Coverage) -> !:
if coverage.wildcard:
return
let t = self.table.get(st)
if t.kind == types.Kind.optional:
if not (coverage.some and coverage.none_):
try self.fail(m, "`match` on an optional needs `.some` and `.none`, or a `_` arm")
return
if t.kind == types.Kind.enum_ and not self.is_backed_enum(st):
let d = t.decl else return
let count = case_count(d)
var index: usize = 0
while index < count and index < 64:
if (coverage.cases & (1 << index)) == 0:
return try self.fail(m, "`match` is missing a case; name it or add a `_` arm")
index += 1
if count > 64:
try self.fail(m, "`match` on an enum with more than 64 cases needs a `_` arm")
return
if st == types.bool_id:
if not (coverage.truth and coverage.falsity):
try self.fail(m, "`match` on a `bool` needs `true` and `false`, or a `_` arm")
return
if self.is_backed_enum(st):
return try self.fail(m, "`match` on an integer-backed enum needs a `_` arm: a combination need not be a declared case")
try self.fail(m, "`match` on this type needs a `_` arm")
## A pattern against the scrutinee type: `_`, `.case(names)`, a literal, a
## constant, or a range of literals.
## A pattern of an arm; after the first pattern of an arm the names are bound
## already and every further pattern must bind the same names to the same types.
func check_pattern(p: ast.Node*, st: u32, first: bool) -> !:
if p.has(ast.flag_wildcard):
p.type_id = st
return
let t = self.table.get(st)
let v = p.left else return try self.fail(p, "empty pattern")
if v.kind == ast.Kind.tuple:
# a tuple is not a pattern (§8.4): no backend compares one, so none pretends to
return try self.fail(v, "a tuple is not a pattern; match one member at a time, or compare with `==`")
if v.kind == ast.Kind.case_value and t.kind == types.Kind.optional and (v.text == "some" or v.text == "none"):
# `.some(name)` and `.none` take an optional apart (§8.4)
v.type_id = st
p.type_id = st
if v.text == "none":
if v.body != none:
return try self.fail(v, "`.none` carries no value")
return
let name = v.body else return try self.fail(v, "`.some` binds its value: write `.some(name)`")
if name.next != none:
return try self.fail(name, "`.some` carries one value")
name.type_id = t.elem
if name.text != "_":
if first:
try self.bind(name, name.text, t.elem, name, false)
else:
let b = self.lookup(name.text) else return try self.fail(name, "every pattern of an arm binds the same names")
if b.type_id != t.elem:
return try self.fail(name, "a name bound by every pattern of an arm has one type")
name.resolved = b.decl
return
if v.kind == ast.Kind.case_value:
var enum_id = st
if t.kind == types.Kind.optional:
enum_id = t.elem
if self.table.kind(enum_id) != types.Kind.enum_:
return try self.fail(p, "`.case` needs an enum scrutinee")
let d = self.table.get(enum_id).decl else return try self.fail(p, "no enum")
let case_decl = find_member(d, v.text, ast.Kind.enum_case) else return try self.fail(v, "no such case")
v.resolved = case_decl
v.type_id = enum_id
var name = v.body
var param = case_decl.body
while let n = name:
let q = param else return try self.fail(n, "this case carries fewer values")
n.type_id = try self.substitute(q.type_id, d, self.table.get(enum_id).args)
if n.text != "_":
if first:
try self.bind(n, n.text, n.type_id, n, false)
else:
let b = self.lookup(n.text) else return try self.fail(n, "every pattern of an arm binds the same names")
if b.type_id != n.type_id:
return try self.fail(n, "a name bound by every pattern of an arm has one type")
n.resolved = b.decl
name = n.next
param = q.next
if param != none and v.body != none:
try self.fail(v, "this case carries more values")
p.type_id = enum_id
return
if v.kind == ast.Kind.name and not v.has(ast.flag_wildcard):
# patterns are closed (§8.4): a name would read as a binding to one reader
# and a constant to another, and the C backend cannot label a case with it
return try self.fail(v, "a name is not a pattern; patterns are literals, ranges and cases (§8.4)")
let vt = try self.check_expr(v, st)
_ = try self.coerce(v, vt, st)
if let hi = p.right:
let ht = try self.check_expr(hi, st)
_ = try self.coerce(hi, ht, st)
if not self.table.is_integer(st) and st != types.char_id:
try self.fail(p, "a range pattern needs integers")
p.type_id = st
## The call `receiver.name()` as a fresh tree at `at`'s position.
func method_call(receiver: ast.Node*, name: str, at: ast.Node*) -> ast.Node*!:
let member = try ast.Node.make(ast.Kind.member, name, at.start, at.line)
member.left = receiver
let call = try ast.Node.make(ast.Kind.call, "", at.start, at.line)
call.left = member
return call