mirror of
https://github.com/dart-lang/sdk
synced 2024-09-16 04:27:17 +00:00
51bc545135
But there is a new "const expression" pattern syntax that does introduce a const context, so test that here instead. Change-Id: Iaf615093c3d2fb32065fa8647bf3663118b0c274 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/285761 Commit-Queue: Bob Nystrom <rnystrom@google.com> Reviewed-by: Jake Macdonald <jakemac@google.com> Auto-Submit: Bob Nystrom <rnystrom@google.com>
66 lines
1.6 KiB
Dart
66 lines
1.6 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.
|
|
|
|
// SharedOptions=--enable-experiment=patterns,records
|
|
|
|
import "package:expect/expect.dart";
|
|
|
|
// Test that map literals are constant when evaluated in a const context.
|
|
|
|
class C {
|
|
final Object x;
|
|
const C(this.x);
|
|
|
|
// Static const.
|
|
static const staticConst = <int, int>{37: 87};
|
|
}
|
|
|
|
// Top-level const.
|
|
const topConst = <int, int>{37: 87};
|
|
|
|
main() {
|
|
const c0 = const <int, int>{37: 87}; // Explicit const.
|
|
|
|
// RHS of const local variable.
|
|
const c1 = <int, int>{37: 87};
|
|
|
|
// Inside const expression.
|
|
var c2 = (const [
|
|
<int, int>{37: 87}
|
|
])[0]; // List element.
|
|
var c3 = (const {
|
|
<int, int>{37: 87}: 0
|
|
})
|
|
.keys
|
|
.first; // Map key.
|
|
var c4 = (const {
|
|
0: <int, int>{37: 87}
|
|
})
|
|
.values
|
|
.first; // Map value.
|
|
var c5 = (const C(<int, int>{37: 87})).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 (<int, int>{37: 87}):
|
|
break;
|
|
default:
|
|
Expect.fail("Didn't match constant");
|
|
}
|
|
|
|
// Annotation argument.
|
|
// (Cannot check that it's const, just that it's accepted).
|
|
@C(<int, int>{37: 87})
|
|
var foo = null;
|
|
foo; // avoid "unused" hints.
|
|
}
|