Skip to content

v2.18.0 - Wasm `null` in the boxed-Object domain + two analyzer gaps

Choose a tag to compare

@gmpassos gmpassos released this 24 Jul 22:44
· 117 commits to master since this release
2410e17

Compiling a null literal to Wasm threw a bare UnimplementedError: generateASTExpressionNullValue — a leftover TODO — so an ordinary Dart idiom could not be compiled at all:

var a = args.length > 0 ? args[0] : null;

null as a boxed value

null is now a real value in the backend's boxed domain: the boxed-Object pointer 0. The heap never allocates at address 0, so it needs no cell, costs no allocation, and is distinguishable from every real box.

  • Ternary arms unify. Both arms of a conditional are now coerced to the block's result type, and an arm that is null forces that type to a boxed Object — otherwise c ? 1 : null mixed an i64 with an i32 and produced a module that failed to validate.
  • ?? tests a boxed operand. It previously always yielded its left operand ("a Wasm value is never null"), which remains correct in the numeric domain. A boxed operand is now compared against the null box, so a ?? 99 falls back when a is null.
  • String interpolation prints null, checking the null box before dereferencing a tag.
  • __alloc look-ahead. Boxing allocates, but the alloc export is decided before the Code section while the boxing is only discovered while writing it — so [1, null, 3] and a ?? 99 aborted at run time with No exported Wasm function __alloc. The generator now scans function bodies for a null literal up front.
  • An Object? return decodes to null instead of the raw pointer 0. Two causes: the return path had no Object case, and _typeTag's fallback gave tag 5 to both Object and a class instance, so the runner could not tell a box from a bare instance pointer. A class instance now carries tag 8, leaving 5 unambiguously "boxed value".

Where null genuinely has no representation — a slot whose Wasm type is concrete, such as int (i64) or String (a string pointer) — the compiler now reports an UnsupportedSyntaxError naming the type and suggesting var / Object? / dynamic, rather than emitting a module that fails to validate.

Null-safety analyzer: operands and nullable-to-non-nullable assignment

The analyzer checked unconditional member/method/index access on a nullable, and the null literal assigned to a non-nullable slot. Two adjacent mistakes went unreported:

void run(int? a, int b) {
  int? x = a;
  int? y = b;
  var base = x + (y ?? 0);   // x can be null — now reported
}

void run(int? a) {
  int x = a;                 // int? into int — now reported
}
  • unchecked-nullable-operand — a nullable operand in an operation. ??, == and != are exempt (a nullable operand is exactly what they exist to handle), and ! or a preceding null check still suppress it.
  • nullable-to-non-nullable — a nullable value assigned to a non-nullable slot: the existing literal-null error, one step removed.

Tests

+15 tests, 2450 passing (from 2435), plus Chrome wasm-gc and wasm-chrome. Coverage targets the shapes that were failing: null in a var/Object? slot, ternary arm boxing, Object? return decoding, == null, ?? fallback, both clean-error cases, and the two analyzer rules with their suppression and negative cases.