Esc

pointer_lifetime

Finds a pointer used after its storage moves or its element becomes invalid.

Category restriction, allow by default.

Off by default; the example is shown with the rule enabled.

Example
#import "Basic";

main :: () {
    xs: [..] int;

    defer array_free(xs);

    array_add(*xs, 1);
    xs.count = xs.allocated;
    p := *xs[0];

    array_add(*xs, 2);

    print("%\n", p.*);
}
Diagnostic
warning[pointer_lifetime]: `p` points into `xs`, which may be resized: array_add(*xs, 2)
  --> example.jai:14:18
   |
10 |     p := *xs[0];
   |          ------ pointer derived here
   ...
12 |     array_add(*xs, 2);
   |     ----------------- `xs` storage invalidated here
   ...
14 |     print("%\n", p.*);
   |                  ^ pointer used here after its storage was invalidated
   |
   = help: take the pointer again after growth, or store an index instead
Fixed
#import "Basic";

main :: () {
    xs: [..] int;

    defer array_free(xs);

    array_add(*xs, 1);
    first := xs[0];

    array_add(*xs, 2);

    print("%\n", first);
}

@experimental. Off by default (-W pointer_lifetime turns it on). The successor to pointer_into_resizable: it follows the checked control flow, so it sees every branch, loop, defer, labelled jump, #if choice and macro expansion the compiler took, per polymorphic instance.

It fires only when every path to a use of p := *xs[i] passes a call that grows xs after p was taken. What a procedure does to its arguments is inferred from its body and the bodies it calls (through allocator calls, wrappers and recursion), so push :: (s: *Stack, v: int) { array_add(*s.items, v); } counts as growing s.items with no note. A call grows only if it does so on every path that returns; one that grows only when out of capacity is a "may", which strict mode will report. @GrowsArray still works as a stated contract. When the growth is several calls deep, the finding lists the chain (up to seven calls, eliding the middle). A path that re-takes the pointer, replaces xs, or runs inline assembly makes the use unknown, and unknown is quiet. The finding points at the use, with labels at the derivation and at the last invalidating call on each path:

12 |     p := *xs[0];
   |          ------ pointer derived here
14 |     grow(*xs);
   |     --------- `xs` storage invalidated here
15 |     sink(p);
   |          ^ pointer used here after its storage was invalidated

The pointer is followed through copies: other locals, struct fields, pointer arithmetic, casts, and views or strings made from the array (v: []int = xs; then v.data). *outer[0].inner[0] points into the inner array, so growing outer leaves it alone; *outer[0].inner points into outer and is invalidated. A @GrowsArray call that returns a pointer into its argument (p := array_add(*xs);) counts as a fresh derivation. Reading v.count or replacing v.data after growth is not a use.

Anything reached through a pointer the lint can't resolve (a heap field a callee could rebind, an overlapping union field, a struct passed as a different struct type with cast(*B) *a whose member has no twin of the same type at the same byte offset) is unknown, so it stays quiet. A callee that keeps or writes through a pointer argument in a way the lint can't follow makes only that argument unknown. JAILINT_LIFETIME_STATS=1 prints per-file counts of bodies analysed and why others were skipped.