diff --git a/content/changelog.mdx b/content/changelog.mdx index 48cae95..9924808 100644 --- a/content/changelog.mdx +++ b/content/changelog.mdx @@ -9,6 +9,10 @@ order: 3 Every release of Burn, newest first. Versions are explained in [Burn standards](./standards.mdx#yearly-versions); releases can be downloaded from [GitHub](https://github.com/burnlang/burn/releases) or installed with burnup. +## Unreleased + +- The compiler rejects memory hazards that used to compile, each with a suggested fix ([memory](./language/memory.mdx#what-the-compiler-rejects)): adding to or removing from an array or map inside a `for` loop over it, using an array, map or object while an `async fun` it was given to may still be changing it, and async functions that change globals or read global arrays, maps and objects. + ## 26.1.0-experimental-9 Your editor explains the language: hover over a keyword or a built-in type to see what it does, and go to its diff --git a/content/language/async.mdx b/content/language/async.mdx index f05ec4b..467aa67 100644 --- a/content/language/async.mdx +++ b/content/language/async.mdx @@ -83,10 +83,36 @@ prints `main ends` first, then `a` and `b` once the tasks wake up, and only then ## Sharing data -Tasks share global variables and the objects passed to them. Memory management is safe across threads: reference -counting switches to atomic operations while tasks run, so sharing objects never frees them too early. Changing the -same array, map or object from several tasks at the same time is not synchronized, though; give each task its own data -and combine the results after `await`: +Memory management is safe across threads: reference counting switches to atomic operations while tasks run, so +sharing objects never frees them too early. Changing the same array, map or object from two threads at once would +race, so the compiler does not allow it: + +- An array, map or object passed to an `async fun` cannot be used again until the call is awaited, either directly + (`await fill(xs)`) or through the variable that holds its future. Pass `copy(xs)` to keep using `xs` meanwhile. +- An `async fun` cannot assign to global variables, or read globals that hold an array, map or object (constants are + fine). Pass what it needs as arguments and return what it makes. + +```burn +async fun fill(xs: [int]): int { + xs.push(len(xs)) + return len(xs) +} + +var xs = [1] +var task = fill(xs) +print(await task, len(xs)) +var other = fill(copy(xs)) +print(len(xs), await other) +``` + +Using `xs` between `fill(xs)` and `await task` is a compile error: + +``` +error: `xs` was given to the async call on line 7, which may still be changing it + = help: `await` the call before using `xs` again, or pass a copy: `copy(xs)` +``` + +Give each task its own data and combine the results after `await`: ```burn async fun countWords(text: string): int { diff --git a/content/language/control-flow.mdx b/content/language/control-flow.mdx index 0277250..106876b 100644 --- a/content/language/control-flow.mdx +++ b/content/language/control-flow.mdx @@ -100,16 +100,27 @@ for name, age in ages { - A range's end is computed once, before the first round. A range whose end is not above its start runs zero times; there is no counting down with ranges, use a `while` or a three-part `for`. -- An array loop reads the array's length before each round: elements pushed during the loop are visited, and the - loop ends early when elements are removed. -- A map loop visits the keys the map had when the loop started, in the order they were first added. Keys added - during the loop are not visited, and removing a key that the loop has not reached yet is a runtime error when it - gets there. +- The body cannot add to or remove from the array or map the loop goes over (`push`, `append`, `pop`, `insert`, + `remove`, `clear`); the compiler reports it. Loop over a copy instead, or collect the changes and make them after + the loop. Changing elements in place (`xs[i] = ...`, `m[k] = ...`) is fine. +- A map loop visits the keys in the order they were first added. - String loops go by character (Unicode code point), not by byte. - The loop variables only exist inside the loop, and each round has its own, so [closures](./functions.mdx#closures) created in the loop keep their own value. - Parentheses are allowed here too: `for (i in 0..3) { ... }`. +```burn +var xs = [1, 2, 3] +for x in copy(xs) { + xs.push(x * 10) +} + +var seen = {"a": 1, "b": 2} +for k in keys(seen) { + remove(seen, k) +} +``` + ## `break` and `continue` `break` leaves the innermost loop, and `continue` skips to its next round. There are no labels; to leave several diff --git a/content/language/memory.mdx b/content/language/memory.mdx index e6af84c..c1448fa 100644 --- a/content/language/memory.mdx +++ b/content/language/memory.mdx @@ -40,6 +40,20 @@ The compiler keeps the bookkeeping cheap: - `s += piece` grows the string in place when `s` is its only owner, so building text in a loop takes linear time. - Async tasks get their own share of the values passed to them, so tasks can safely use the same objects. +## What the compiler rejects + +Freeing is automatic, so the memory errors left are the ones where two pieces of code change the same value at the +wrong time. The compiler reports each of them, with the fix: + +| Mistake | Fix | +| --- | --- | +| Using a variable after `destroy x()` | assign it a new object first | +| Adding to or removing from an array or map inside a `for` loop over it | loop over `copy(xs)` or `keys(m)` | +| Using an array, map or object while an `async fun` it was passed to may still run | `await` first, or pass `copy(xs)` | +| Changing a global, or reading a global array, map or object, inside an `async fun` | pass it as an argument and return the result | + +See [loops](./control-flow.mdx#for--in) and [sharing data between tasks](./async.mdx#sharing-data). + ## Cycles Objects that point at each other, like a parent node and its children or two structs that refer to each other, are