blob: 6acea532a99c47952191d203231206e4794f9b76 [file] [log] [blame]
// 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;
}