Files
sdk/tests/language/local_null_initialization.dart
T
Ömer Ağacan c552af0a9e [dart2wasm] Minor refactoring and comments in variable declaration compiler
Refactoring:

- Update the checks when updating a local for a captured variable to
  check whether `local` is null, instead of whether it's not updated.

  If the `local` is available then we know that it's not updated. It's
  more direct to check whether we've created a local for the variable or
  not.

- Add an assertion checking the the capture field and local for a
  variable can only differ in nullability.

Documentation:

- Document that context field for a captured local will always be
  nullable, to be able to allocate the context without dummy values.

- Document in a few places that `!capture.written` means the variable is
  captured but not updated, so they can be held in a local (instead of
  getting them from the context on every read).

Change-Id: I66048cb36f75e35ee3c479c41f2bbfca247a990f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/470981
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
2026-01-07 03:38:06 -08:00

35 lines
952 B
Dart

// Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// This test checks that variable declarations in loops are reset in each
// iteration. (the `replacement` variable below)
//
// Before this test, getting this wrong in dart2wasm only caused one test
// failure in a large `dart:convert` test. This test is smaller and checks the
// same thing.
import 'package:expect/expect.dart';
const _TEST_INPUT = "<A>";
List<int?> _convert(String text) {
List<int?> result = [];
for (var i = 0; i < text.length; i++) {
var ch = text[i];
int? replacement;
switch (ch) {
case '<':
replacement = 1;
case '>':
replacement = 2;
}
result.add(replacement);
}
return result;
}
void main() {
Expect.listEquals(_convert("<A>"), [1, null, 2]);
}