Files
sdk/tests/language/nnbd/syntax/late_modifier_edge_cases_test.dart
T
Lasse R.H. Nielsen d352bc28b8 Remove (most) uses of the "non-nullable" experiment flag.
Since the flag is now enabled by default, there should be no mention of it.
There are still some uses in front_end/testcases that are not just removable
(it also uses `no-non-nullable`). There migth be more uses that are not
as easily found as grepping for `--enable-experiment

Removes two VM tests where fixing them meant they were just duplicating
the corresponding non *_2/ tests.
Fixes #44941

TEST= Large number of tests chaged.=(no-)?non-nullable`.

Change-Id: Ief755981ccde9a5482fcdf408c2929c74433a710
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/183688
Commit-Queue: Lasse R.H. Nielsen <lrn@google.com>
Reviewed-by: Nate Bosch <nbosch@google.com>
2021-02-12 17:16:54 +00:00

146 lines
3.9 KiB
Dart

// Copyright (c) 2019, 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.
import 'package:expect/expect.dart';
class A {
late int lateField = initLateField();
int initLateField() {
lateField = 456;
Expect.equals(456, lateField);
return 123;
}
late final int lateFinalField = initLateFinalField();
int count = 0;
int initLateFinalField() {
if (count == 5) return count;
return ++count + lateFinalField;
}
static late int staticLateField = initStaticLateField();
static int initStaticLateField() {
staticLateField = 456;
Expect.equals(456, staticLateField);
return 123;
}
static late final int staticLateFinalField = initStaticLateFinalField();
static int staticCount = 0;
static int initStaticLateFinalField() {
if (staticCount == 5) return staticCount;
return ++staticCount + staticLateFinalField;
}
}
lateFieldWithInitThatWritesIntermediateValue() {
A a = A();
Expect.equals(123, a.lateField);
Expect.throws(() => a.lateFinalField);
Expect.equals(5, a.lateFinalField);
Expect.equals(123, A.staticLateField);
Expect.throws(() => A.staticLateFinalField);
Expect.equals(5, A.staticLateFinalField);
}
lateFieldWithComplicatedInitializers() {
late int count = 0;
int closure() {
++count;
return 5;
}
late int nestedLateVar = closure();
late var lateVar = [for (int i = 0; i < nestedLateVar; ++i) i * i];
Expect.equals(0, count);
Expect.listEquals([0, 1, 4, 9, 16], lateVar);
Expect.equals(1, count);
Expect.listEquals([0, 1, 4, 9, 16], lateVar);
Expect.equals(1, count);
count = 0;
late var lateVarInIf = [++count, for (int i = 0; i < 3; ++i) i * i];
Expect.equals(0, count);
if (true) Expect.listEquals([1, 0, 1, 4], lateVarInIf);
Expect.equals(1, count);
Expect.listEquals([1, 0, 1, 4], lateVarInIf);
Expect.equals(1, count);
count = 0;
late var lateVarInFor = [++count, for (int i = 0; i < 3; ++i) i * i];
Expect.equals(0, count);
for (int i = 0; i < 3; ++i) Expect.listEquals([1, 0, 1, 4], lateVarInFor);
Expect.equals(1, count);
count = 0;
late var lateVarInClosure = [++count, for (int i = 0; i < 3; ++i) i * i];
void anotherClosure() {
Expect.listEquals([1, 0, 1, 4], lateVarInClosure);
}
Expect.equals(0, count);
anotherClosure();
anotherClosure();
anotherClosure();
Expect.equals(1, count);
count = 0;
late int lateVarClosureOuter = () {
Expect.equals(0, count);
++count;
late int lateVarClosureInner = closure();
Expect.equals(1, count);
Expect.equals(5, lateVarClosureInner);
Expect.equals(2, count);
return lateVarClosureInner;
}();
Expect.equals(0, count);
Expect.equals(5, lateVarClosureOuter);
Expect.equals(2, count);
}
class B {
late int throwBeforeWrite = initThrowBeforeWrite();
int initThrowBeforeWrite() {
throw AssertionError();
return 123;
}
late int throwAfterWrite = initThrowAfterWrite();
int initThrowAfterWrite() {
throwAfterWrite = 456;
throw AssertionError();
return 123;
}
}
lateFieldWithThrowingInitializers() {
late int throwBeforeWrite = () {
throw AssertionError();
return 123;
}();
Expect.throwsAssertionError(() => throwBeforeWrite);
Expect.throwsAssertionError(() => throwBeforeWrite);
Expect.throwsAssertionError(() => throwBeforeWrite);
B b = B();
Expect.throwsAssertionError(() => b.throwBeforeWrite);
Expect.throwsAssertionError(() => b.throwBeforeWrite);
Expect.throwsAssertionError(() => b.throwBeforeWrite);
Expect.throwsAssertionError(() => b.throwAfterWrite);
Expect.equals(456, b.throwAfterWrite);
Expect.equals(456, b.throwAfterWrite);
}
main() {
lateFieldWithInitThatWritesIntermediateValue();
lateFieldWithComplicatedInitializers();
lateFieldWithThrowingInitializers();
}