#! @file lifetimes.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the lifetime warnings: where a function keeps a reference past what #! it refers to, as the checker is sure of it. A reference, a view, or an #! anonymous function refers to the locals it names, which end where their #! scopes do, and to a value a statement makes, which ends with the statement #! unless a name is bound to it. Nothing keeps what they refer to alive, so one #! that outlives it dangles, which is the program's mistake, as in C++. #! #! The checker is sure of what it sees written: a value refers to a local where #! it is `&` of the local's own storage, a `val` bound to such a value, an #! anonymous function naming the local, or a struct, union, or array literal #! holding one. It is sure the value outlives the local where it is returned #! from the function declaring the local, or written into a global, into what #! a parameter refers or points to, or into a local of a scope around the #! local's. A value a statement makes outlives it where it is returned, written #! anywhere, or held in a literal a name is bound to. What a call does with #! what it is given, the checker does not follow, and says nothing of. #! #! Only functions that are no generic's are walked, so each warning is said #! once, and `@allow` quiets those of a function. import std::containers; module sakura::analyzer; #! What a value refers to, as far as the checker is sure. enum lifetime_kind : u8 { none, #! Nothing it is sure of. local, #! A local, as `local` says. temporary, #! A value its statement makes. } #! What a value refers to, the local where it is one, and the node of the #! value saying so: the `&`, the slice, the anonymous function, or the `val` #! named, within any literal holding it. struct lifetime_ref { var kind: lifetime_kind; #! What it is. var local: u32; #! The node declaring the local, where it is one. var at: u32; #! The node saying so, which a warning is made at. } #! What a walk of a function's body holds. One is kept by the checker and #! walks each function in turn, so what it holds is made once. struct lifetime_walk { var scopes: vector; #! The scopes it is within, innermost last: the function, then each block, loop, test, arm, and anonymous function. var functions: vector; #! The function and the anonymous functions within it it is within, innermost last. var nodes: vector; #! The nodes each node being walked holds, which it walks in turn, those of the nodes around it before them. var notes: column; #! What was noted of each node of the file that may declare a local, by the node. var walk: u32; #! Which walk this is, counted from 1, so what earlier walks noted is told apart without being cleared. var depth: u32; #! How deeply the walk is nested, which a body nested past `lifetime_depth` stops. } #! Where a node that may declare a local is: its scope, and the function or #! anonymous function it is in, as the walk that noted them found them. struct lifetime_note { var scope: u32; #! The scope it is in. var owner: u32; #! The function or anonymous function it is in. var walk: u32; #! The walk that noted them. } #! Makes a walk, which walks nothing until `check_lifetimes` starts it. #! @returns The walk. def lifetime_walk::new(): lifetime_walk { return lifetime_walk { scopes = vector::::new(), functions = vector::::new(), nodes = vector::::new(), notes = column::::new() }; } #! What the walk noted of a node, or nothing noted where it noted nothing. #! @param this The walk. #! @param node The node. #! @returns The note. def lifetime_walk::note_of(&this, node: u32): lifetime_note { if (node as u64) < this.notes.length { val held: lifetime_note = this.notes.p[node]; return held if held.walk == this.walk; } return lifetime_note {}; } #! How deeply a walk goes before it stops, saying nothing of what is deeper. const lifetime_depth: u32 = 400; #! Warns of where a function's body keeps a reference past what it refers to, #! unless it is a generic's, which each instance would warn of again, or is #! `@allow`. #! @param this The checker. #! @param id The function's declaration. pub def checker::check_lifetimes(&mut this, id: u32): void { return if this.decls.has(id, df::generic) || this.decls.has(id, df::allows) || this.decls.has(id, df::accessor); val node: u32 = this.decls.nodes.p[id]; return if node == 0 || this.kind_of(node) != nk::function; val walk := &mut this.lifetimes; walk.walk++; walk.scopes.clear(); walk.functions.clear(); walk.nodes.clear(); walk.depth = 0; this.lifetime_visit(walk, node); } #! Walks a node, noting where what it declares is, then warns of what it keeps #! past what that refers to. #! @param this The checker. #! @param walk The walk. #! @param node The node. def checker::lifetime_visit(&mut this, walk: &mut lifetime_walk, node: u32): void { return if node == 0 || walk.depth >= lifetime_depth; val kind := this.kind_of(node); if kind == nk::variable || kind == nk::param || kind == nk::name || kind == nk::reference { if walk.scopes.length != 0 { walk.notes.put(node, lifetime_note { scope = walk.scopes.last(), owner = walk.functions.last(), walk = walk.walk }); } } val function := kind == nk::function || kind == nk::lambda; val scope := function || kind == nk::block || kind == nk::for_stmt || kind == nk::if_stmt || kind == nk::while_stmt || kind == nk::match_arm; if function { walk.functions.append(node); } if scope { walk.scopes.append(node); } # Only what was checked is walked: of a `when` or an `if const`, the branch # taken, and of a block, the statements up to one decided to leave it. The # nodes a node holds are put on the walk's, past those of the nodes around # it, walked from there, and taken off again. val mark := walk.nodes.length; match kind { nk::when_block => walk.nodes.append(this.when_branch(node)); nk::if_const => { val condition := this.left_of(node); if this.is_known(condition) { walk.nodes.append(this.value_at(condition).magnitude != 0 ? this.record(node, if_slot::then) : this.record(node, if_slot::other)); } } nk::block => { val run := this.left_of(node); for i in 0..this.run_length(run) { val each := this.run_at(run, i as u32); walk.nodes.append(each); break if this.lifetime_skips(each); } } else => this.syn().children(node, &mut walk.nodes); } val end := walk.nodes.length; walk.depth++; for i in mark..end { this.lifetime_visit(walk, walk.nodes[i]); } walk.depth--; if end != mark { walk.nodes.resize(mark); } match kind { nk::return_stmt => this.lifetime_returned(walk, this.left_of(node)); nk::lambda => { # A lambda whose body is a value returns it. val body := this.record(node, lambda_slot::body); if body != 0 && this.kind_of(body) != nk::block { this.lifetime_returned(walk, body); } } nk::assign => { if this.token_kind(node) == tk::assign { this.lifetime_written(walk, this.left_of(node), this.right_of(node)); } } nk::variable => { # A name bound to a value its statement makes refers to it as long as # the name lives, but a literal holding a reference to one does not. val value := this.record(node, variable_slot::value); if value != 0 && this.kind_of(value) != nk::reference { val held := this.lifetime_of(walk, value, 0); if held.kind == lifetime_kind::temporary { this.warn(held.at, ec::lifetime, "The value referred to is made in the statement, and ends with it, so the value holding a reference to it outlives it. Bound to a name, as `val p := &point { x = 1 };`, a value lives as long as the name."); } } } else => {} } if scope { walk.scopes.truncate(); } if function { walk.functions.truncate(); } } #! Warns where a value returned refers to a local of the function returning it, #! or to a value the `return` makes. #! @param this The checker. #! @param walk The walk. #! @param value The value returned, or 0. def checker::lifetime_returned(&mut this, walk: &mut lifetime_walk, value: u32): void { return if value == 0 || walk.functions.length == 0; val held := this.lifetime_of(walk, value, 0); if held.kind == lifetime_kind::temporary { this.warn(held.at, ec::lifetime, "The value returned refers to a value made in the statement, which ends with it."); return; } return if held.kind != lifetime_kind::local || walk.note_of(held.local).owner != walk.functions.last(); var text := string::from("The value returned refers to `", nothing {}); text.append(this.names.text(this.local_name(held.local))); text.append("`, which ends as the function returns."); this.warn_text(held.at, ec::lifetime, &text, held.local); } #! Warns where a value written to a place refers to a local the place outlives, #! or to a value the statement makes. #! @param this The checker. #! @param walk The walk. #! @param target The place written. #! @param value The value written. def checker::lifetime_written(&mut this, walk: &mut lifetime_walk, target: u32, value: u32): void { return if this.kind_of(target) == nk::name && this.names.text(this.name_of(target)) == "_"; val held := this.lifetime_of(walk, value, 0); if held.kind == lifetime_kind::temporary { this.warn(held.at, ec::lifetime, "The value referred to is made in the statement, and ends with it, so the reference written here outlives it. Bound to a name, as `val p := &point { x = 1 };`, a value lives as long as the name."); return; } return if held.kind != lifetime_kind::local; # The local or global whose storage the place is within, and whether it is # reached through a reference or pointer. var at := target; var through := false; var steps := 0; while steps < 64 { steps++; val kind := this.kind_of(at); if kind == nk::member || kind == nk::index || kind == nk::slice { val object := this.left_of(at); val held_type := this.types.kind_of(this.unalias(this.type_at(object))); if kind == nk::member { through = through || held_type == ty::reference || held_type == ty::pointer; } else { through = through || held_type != ty::fixed; } at = object; continue; } if kind == nk::unary && this.token_kind(at) == tk::star { through = true; at = this.left_of(at); continue; } break; } return if this.kind_of(at) != nk::name; val ref := this.ref_at(at); return if ref == 0 || (ref & ref_type) != 0; var outlives := false; var into := string::new(nothing {}); if (ref & ref_local) == 0 { # A global outlives every local. outlives = this.decls.kinds.p[ref & ref_mask] == dk::variable; into.append("a global"); } else { val root := ref & ref_mask; if through { # What a parameter refers or points to is the caller's, which outlives # every local of the function. val root_type := this.types.kind_of(this.unalias(this.type_at(root))); outlives = this.kind_of(root) == nk::param && (root_type == ty::reference || root_type == ty::pointer); into.append("what `"); into.append(this.names.text(this.local_name(root))); into.append("` refers to"); } else { # A local of a scope around the local's outlives it. val outer := this.lifetime_scope(walk, root); val inner := this.lifetime_scope(walk, held.local); outlives = outer >= 0 && inner >= 0 && outer < inner; into.append("`"); into.append(this.names.text(this.local_name(root))); into.append("`"); } } return if !outlives; var text := string::from("`", nothing {}); text.append(this.names.text(this.local_name(held.local))); text.append("` ends where its scope does, so the reference written to "); text.append(into.view()); text.append(" outlives it."); this.warn_text(held.at, ec::lifetime, &text, held.local); } #! Where among the scopes the walk is within a local is declared. #! @param this The checker. #! @param walk The walk. #! @param local The node declaring the local. #! @returns The index of its scope, or -1 where it is in none of them. def checker::lifetime_scope(&this, walk: &lifetime_walk, local: u32): i64 { val scope := walk.note_of(local).scope; return -1 if scope == 0; for i in 0..walk.scopes.length { return i as i64 if walk.scopes[i] == scope; } return -1; } #! What a value refers to, as far as the checker is sure. #! @param this The checker. #! @param walk The walk. #! @param node The value. #! @param depth How many `val`s it was followed through. #! @returns What it refers to. def checker::lifetime_of(&mut this, walk: &lifetime_walk, node: u32, depth: u32): lifetime_ref { val none := lifetime_ref { kind = lifetime_kind::none }; return none if node == 0 || depth > 8; match this.kind_of(node) { nk::reference => { val inner := this.left_of(node); if this.is_place(inner) { val root := this.lifetime_root(inner); return root != 0 ? lifetime_ref { kind = lifetime_kind::local, local = root, at = node } : none; } # `&` of a reference refers to what it does, which is not known. val given := this.types.kind_of(this.unalias(this.type_at(inner))); return given == ty::reference ? none : lifetime_ref { kind = lifetime_kind::temporary, at = node }; } nk::slice => { val object := this.left_of(node); val held := this.types.kind_of(this.unalias(this.type_at(object))); return none if held != ty::fixed || !this.is_place(object); val root := this.lifetime_root(object); return root != 0 ? lifetime_ref { kind = lifetime_kind::local, local = root, at = node } : none; } nk::name => { # A `val` refers to what its value does, and one bound to a value its # statement makes refers to it as long as the `val` lives. val ref := this.ref_at(node); return none if (ref & ref_local) == 0; val local := ref & ref_mask; return none if this.kind_of(local) != nk::variable || this.node_has(local, nf::mutable); val value := this.record(local, variable_slot::value); return none if value == 0; val held := this.lifetime_of(walk, value, depth + 1); if held.kind == lifetime_kind::temporary { return this.kind_of(value) == nk::reference ? lifetime_ref { kind = lifetime_kind::local, local = local, at = node } : none; } return held.kind == lifetime_kind::none ? none : lifetime_ref { kind = held.kind, local = held.local, at = node }; } nk::lambda => { # An anonymous function refers to the locals around it that it names. var held := this.lifetime_captured(walk, node); held.at = node; return held; } nk::construct => { for &each in this.run_view(this.right_of(node)) { val held := this.lifetime_of(walk, this.left_of(each), depth); return held if held.kind != lifetime_kind::none; } return none; } nk::array => { for &each in this.run_view(this.left_of(node)) { val held := this.lifetime_of(walk, each, depth); return held if held.kind != lifetime_kind::none; } return none; } else => return none; } } #! The local an anonymous function names that a function around it declares, #! the one declared first where it names several. #! @param this The checker. #! @param walk The walk. #! @param node The `lambda` node. #! @returns The local, or nothing it is sure of. def checker::lifetime_captured(&mut this, walk: &lifetime_walk, node: u32): lifetime_ref { var todo := vector::::new(); this.syn().children(node, &mut todo); var seen := 0; var first: u32 = 0; while todo.length != 0 && seen < 4096 { seen++; val at: u32 = todo.last(); todo.truncate(); if this.kind_of(at) == nk::name { val ref := this.ref_at(at); if (ref & ref_local) != 0 { val local := ref & ref_mask; val owner := walk.note_of(local).owner; if owner != 0 && walk.functions.view().contains(&owner) && (first == 0 || this.node_offset_of(local) < this.node_offset_of(first)) { first = local; } } } this.syn().children(at, &mut todo); } return first != 0 ? lifetime_ref { kind = lifetime_kind::local, local = first } : lifetime_ref { kind = lifetime_kind::none }; } #! The local whose own storage a place is within, past fields and the elements #! of fixed arrays it holds as values. A local that is a reference names what #! it refers to, so a place within it is within no local the checker knows. #! @param this The checker. #! @param node The place. #! @returns The node declaring the local, or 0 where the place is not within one. def checker::lifetime_root(&mut this, node: u32): u32 { var at := node; var steps := 0; while steps < 64 { steps++; val kind := this.kind_of(at); if kind == nk::member || kind == nk::index { val object := this.left_of(at); val held := this.types.kind_of(this.unalias(this.type_at(object))); return 0 if kind == nk::member && (held == ty::reference || held == ty::pointer); return 0 if kind == nk::index && held != ty::fixed; if kind == nk::member { val ref := this.ref_at(at); return 0 if ref == 0 || (ref & (ref_local | ref_type)) != 0 || this.decls.kinds.p[ref] != dk::field; } at = object; continue; } break; } return 0 if this.kind_of(at) != nk::name; val ref := this.ref_at(at); return 0 if (ref & ref_local) == 0; val held := this.types.kind_of(this.unalias(this.type_at(ref & ref_mask))); return held == ty::reference ? 0 : ref & ref_mask; } #! Whether a statement leaves where it was decided while checking, so the #! checker checked nothing after it, as the emitter's `skips` says. #! @param this The checker. #! @param node The statement. #! @returns Whether it does. def checker::lifetime_skips(&mut this, node: u32): bool { match this.kind_of(node) { nk::block => { val run := this.left_of(node); for i in 0..this.run_length(run) { return true if this.lifetime_skips(this.run_at(run, i as u32)); } } nk::when_block => return this.lifetime_skips(this.when_branch(node)); nk::if_const => { val condition := this.left_of(node); return false if !this.is_known(condition); val then := this.record(node, if_slot::then); val other := this.record(node, if_slot::other); if this.value_at(condition).magnitude != 0 { return this.lifetime_skips(then) || (other == 0 && this.has_flag(then, rf::leaves)); } return other != 0 && this.lifetime_skips(other); } else => {} } return false; } #! Where in the file a node's token begins. #! @param this The checker. #! @param node The node. #! @returns The token's offset. def checker::node_offset_of(&this, node: u32): u32 { val syntax := &this.syn(); return syntax.token_offsets[syntax.node_tokens[node]]; } #! The name a local is declared with. #! @param this The checker. #! @param local The node declaring it. #! @returns The name's ID. def checker::local_name(&this, local: u32): u32 { return this.name_of(local); }