Skip to content

v2.20.0 - Go: a nullable `T?` is generated as a pointer `*T`

Choose a tag to compare

@gmpassos gmpassos released this 25 Jul 01:15
· 113 commits to master since this release
ccf03e9

Closes the last open item from the null-safety series. Go had no representation for nullability: a nullable local was emitted as var s string = nil — source that does not compile — and ?? was reported as unsupported because a Go value type cannot be compared to nil. 2.19.0 turned the broken output into an explicit error; this implements the representation.

T? → *T

A pointer is the one Go form that can hold nil.

Dart Go
int? a (param) a *int
String? name (field) name *string
int? x = null var x *int = nil
int? x = 5 var x *int = goPtr(5)
read x (*x)
x == null x == nil (no deref)
a ?? b func() int { if a != nil { return *a }; return b }()
List<int>? []int (already nilable — left alone)
  • Reads deref, assignment targets do not — an assignment rebinds the pointer itself. return x needed its own handling, since it takes a separate generation path that bypasses the read hook.
  • Parameters are registered before the body is generated. The generator emits a member's body before its parameter list, so registering a nullable parameter while writing the signature would be too late for the body's reads to deref.
  • goPtr is a generated helper, because Go cannot write &5. It is emitted only in modules that use it.
  • Already-nilable Go types are untouched — List<int>? stays []int.

Verified by compiling, not by string matching

var s string = nil shipped because the generator's output was only ever asserted against text. Every shape above is now checked by writing the generated source into a module and running go build with a real toolchain — which is how the two bugs above were found, both producing plausible-looking Go that did not type-check.

Still unsupported in Go — but reported, not mis-emitted

  • ?. — the shared fallback degrades it to ., which was merely lossy when a nullable was a plain T. Against a *T it would skip the nil check and yield a value where a pointer is expected. Lowering it needs the accessed member's type at generation time.
  • ??= — lowering to t = t ?? v needs the target's element type to build the inline function's return type. Plain ?? is unaffected.

Tests

2463 passing (from 2460), plus Chrome wasm-gc and wasm-chrome.