mirror of
https://github.com/dart-lang/sdk
synced 2024-09-15 23:49:47 +00:00
82b78916fd
Change-Id: If31c487e3ebe2c1ae847aff7c8994580b8b6f2f6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/309660 Reviewed-by: Alexander Thomas <athom@google.com> Commit-Queue: Alexander Thomas <athom@google.com>
57 lines
1.5 KiB
Dart
57 lines
1.5 KiB
Dart
// Copyright (c) 2018, 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";
|
|
|
|
import "implicit_const_context_prefix_constructor_generic_test.dart" as prefix;
|
|
|
|
// Test that constructor invocations are constant
|
|
// when evaluated in a const context.
|
|
|
|
class C<T> {
|
|
final T x;
|
|
const C(this.x);
|
|
|
|
// Static const.
|
|
static const staticConst = prefix.C<int>(42);
|
|
}
|
|
|
|
// Top-level const.
|
|
const topConst = prefix.C<int>(42);
|
|
|
|
main() {
|
|
const c0 = const prefix.C<int>(42); // Explicit const.
|
|
|
|
// RHS of const local variable.
|
|
const c1 = prefix.C<int>(42);
|
|
|
|
// Inside const expression.
|
|
var c2 = (const [prefix.C<int>(42)])[0]; // List element.
|
|
var c3 = (const {prefix.C<int>(42): 0}).keys.first; // Map key.
|
|
var c4 = (const {0: prefix.C<int>(42)}).values.first; // Map value.
|
|
var c5 = (const C(prefix.C<int>(42))).x; // Constructor argument.
|
|
|
|
Expect.identical(c0, c1);
|
|
Expect.identical(c0, c2);
|
|
Expect.identical(c0, c3);
|
|
Expect.identical(c0, c4);
|
|
Expect.identical(c0, c5);
|
|
Expect.identical(c0, C.staticConst);
|
|
Expect.identical(c0, topConst);
|
|
|
|
// Switch case parenthesized const expression.
|
|
switch (c0) {
|
|
case const (prefix.C<int>(42)):
|
|
break;
|
|
default:
|
|
Expect.fail("Didn't match constant");
|
|
}
|
|
|
|
// Annotation argument.
|
|
// (Cannot check that it's const, just that it's accepted).
|
|
@C(prefix.C<int>(42))
|
|
var foo = null;
|
|
foo; // avoid "unused" hints.
|
|
}
|