Skip to content

v2.22.0 - Analyzer reaches class members; opt-in null-safety checks at load

Choose a tag to compare

@gmpassos gmpassos released this 25 Jul 06:17
· 106 commits to master since this release
8fd95c1

Reported: this should fail before it runs, not partway through.

class Foo {
  static void main(int a, int? b) {
     print(a);
     print(b);

     var c = a + b;   // should be a null-safety error at resolution
     print(c);
  }
}

Investigating it turned up something worse than the request.

1. The analyzer had never seen a class method

NullSafetyAnalyzer.analyze walked root.descendantChildren for ASTInvocableDeclaration. But ASTBlock.children is only [functions, statements], and ASTRoot keeps its classes in a separate map — so the traversal never entered a class body. ASTClass compounds it by keeping constructors and getters outside children as well.

Every method, constructor and getter of every class went unanalyzed, in the LSP/Problems panel too. Only top-level functions were ever checked — which is exactly why the existing analyzer tests, all written against top-level functions, never caught it.

analyze now walks each class and extension explicitly, plus their constructors and getters. No rule changed: the rules were correct, nothing was reaching them.

2. Opt-in enforcement at load

ApolloVM(nullSafetyChecks: true) makes loadCodeUnit throw the new NullSafetyError when the AST has null-safety errors, before the unit is registered:

[checks OFF] ApolloVMNullPointerException: Can't perform '+' with types: int + Null
    printed before throw: "5 | null"

[checks ON ] NullSafetyError: Can't load `t` (dart): 1 null-safety error(s).
      - The operand 'b' can be 'null', so it can't be used in an operation
        unconditionally. Use '??', '!' or a null check.
    printed before throw: ""
  • Off by default — loading is unchanged unless you opt in.
  • Only NullSafetySeverity.error blocks. Warnings (null!) and info stay diagnostic-only, exactly as in the LSP.
  • A BinaryCodeUnit (Wasm) carries no AST and is skipped.
  • Best-effort: an internal analyzer failure never becomes a load failure.
  • The thrown error carries the offending findings.

loadCodeUnit already throws SyntaxError on a bad parse, so throwing here matches its existing contract. No language gate is needed — only the Dart grammar parses ?, and a test asserts that rather than assuming it.

Tests

2510 passing (from 2499), plus Chrome wasm-gc and wasm-chrome.

Class-member coverage coming first mattered: the getter and constructor cases failed on the first attempt, which is how the second-level ASTClass gap surfaced. Load-enforcement coverage includes the default being off, off still failing mid-run with [5, null] already printed, on throwing before anything executes, a warning-only source still loading, and a syntax error still surfacing as SyntaxError.