Repository navigation
Description
Follow-up of #1819 (branch fix-globals, commit 362aa520). §9.1c: "Globals are places; §21.1's access rules apply." §21.1 rule 1: "The compiler rejects code that uses a borrow after the borrowed place has been mutated."
#1819 makes a call a write of every global its callee writes, for every call whose body Sema can name: free, method, static, qualified extension, generic specialization, trait static dispatch, pipeline stage, operator method, for over an Iter[T] (next) or a Gen (each), a closure binding, a callable parameter (bound to what its callers pass), a closure or named function handed to a callee. Four routes still run a body that writes a global with no check. Each repro below compiles and, under run --debug-alloc with WITH_ALLOC_NO_REUSE=1 WITH_DEBUG_ALLOC_SCRIBBLE=1, prints an empty line where first! belongs: r reads the freed, scribbled element.
1. Implicit Drop (rule 7: an implicit drop is a use; the drop runs a body)
var G: Vec[str] = Vec.new()
type Tok:
n: i32
impl Drop for Tok:
move fn drop():
for i in 0..64: G.push(f"item{i}")
fn main:
G.push("first" ++ "!")
let r = G[0]
{
let t = Tok { n: 1 }
print(f"{t.n}")
}
print(r)
Sema has the Drop body's writes; the drop point is MIR's (D65: MIR decides where and when). Needs a check at the drop point against the views live there — the fact Sema would need (which drops run at a scope exit, including field and element drop glue) does not exist in Sema today.
2. dyn trait dispatch
var G: Vec[str] = Vec.new()
trait Grow:
fn grow(self: &Self)
type W { x: i32 }
impl Grow for W:
fn grow(self: &Self):
for i in 0..64: G.push(f"item{i}")
fn run(g: dyn Grow):
let r = G[0]
g.grow()
print(r)
fn main:
G.push("first" ++ "!")
run(W { x: 0 })
Within one compilation the impls of Grow.grow are known: the call runs one of them. Proposed: a dyn call's bodies are every impl of the method (check_dyn_trait_method_call).
3. A callable stored in a field (or a collection, or returned)
var G: Vec[str] = Vec.new()
type H:
f: fn() -> Unit
fn grow():
for i in 0..64: G.push(f"item{i}")
fn main:
G.push("first" ++ "!")
let h = H { f: grow }
let r = G[0]
h.f()
print(r)
The call's body is whatever callable reached the field. Proposed: a call through a callable Sema cannot bind runs every body that is ever used as a callable value (a closure, or a function named as a value) — the address-taken set.
4. A bundle interface function (open spec question)
A .wi may export storage (pub var COUNTER: i32, test/bundle_interface/wi_demo.wi), and a program can hold a view of it across a call of a bundle function that writes it. D39: "No body-inferred ownership/effect information is part of the bundle interface. Callable semantics are determined by the declared signature. If some information is necessary for a caller to safely typecheck and cannot be derived unambiguously from the signature, With's source declaration language must gain a way to state it explicitly." A declaration says nothing about the globals its body writes. Needs Eric's ruling: recommended answer in the #1819 report — an interface function writes every global its bundle's interface exports (conservative; can only refuse a valid program).
Activity
Sema: an implicit drop writes every global its type's Drop impls writ…
test: dyn parameters that only observe are spelled `&dyn T`, not a by…
Spec v7.13 (D79): a bundle function's global writes are a declared, c…
Spec v7.13 (D79): a bundle function's global writes are a declared, c…
Spec v7.11–v7.13 (D78, D79): SIMD vectors; the unsafe callable type; …
#3026: the #1819/#1827 global-view pins keep a List field — a str fie…
#3026 (D111): an unannotated let of a str place takes the str; a str …