Files
sdk/tests/language_2/nnbd/syntax/null_assertion_test.dart
T
Konstantin Shcheglov 3c596e802b Report an error when a potentially non-nullable local variable is not initialized.
R=brianwilkerson@google.com, paulberry@google.com

Change-Id: Ie1d148ff584c202edc334659eeb4f792bb8773d4
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/106620
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
2019-06-19 18:07:56 +00:00

76 lines
1.5 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.
// SharedOptions=--enable-experiment=non-nullable
// Test that the trailing "!" is accepted after a sampling of expression
// syntaxes. Verify that the compiler understands the resulting type to be
// non-nullable by constructing a list containing the expression, and assigning
// it to List<Object>. Where possible, verify that the runtime implements the
// proper null-check semantics by verifying that the presence of `null` causes
// an exception to be thrown.
import 'package:expect/expect.dart';
class C {
const C();
Object? get x => null;
void f() {}
}
Object? f() => null;
main() {
List<Object> listOfObject = [];
var x1 = [0!];
listOfObject = x1;
var x2 = [true!];
listOfObject = x2;
var x3 = ["foo"!];
listOfObject = x3;
var x4 = [#foo!];
listOfObject = x4;
var x5 = [[1]!];
listOfObject = x5;
var x6 = [{1:2}!];
listOfObject = x6;
var x7 = [{1}!];
listOfObject = x7;
var x8 = [new C()!];
listOfObject = x8;
var x9 = [const C()!];
listOfObject = x9;
Expect.throws(() {
var x10 = [f()!];
listOfObject = x10;
});
C c = new C();
Expect.throws(() {
var x11 = [c.x!];
listOfObject = x11;
});
var g = f;
Expect.throws(() {
var x12 = [g()!];
listOfObject = x12;
});
int i = 0;
var x13 = [i++!];
listOfObject = x13;
}