Skip to content

Commit 9edecbc

Browse files
lrhncommit-bot@chromium.org
authored andcommitted
Add test checking that const contexts are not applied where they shouldn't.
Change-Id: I39782577b7555dc63311531ca17f61f8deec5b4a Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/98854 Reviewed-by: Lasse R.H. Nielsen <[email protected]> Reviewed-by: Aske Simon Christensen <[email protected]> Commit-Queue: Lasse R.H. Nielsen <[email protected]>
1 parent ec3c878 commit 9edecbc

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import "package:expect/expect.dart";
6+
7+
// Check places that are *not* supposed to be constant contexts,
8+
// but which do require constant values, do not introduce an implicit const.
9+
// Nested expressions still do.
10+
// (Also acts as regression test for http:/dartbug.com/36533)
11+
12+
class C {
13+
final v;
14+
15+
// Initializer of final field in class with const constructor.
16+
// Can't use `const C()`, it's a cyclic constant dependency.
17+
final i1 = []; //# 1: compile-time error
18+
final i2 = const [];
19+
final i3 = const [[]];
20+
21+
const C([this.v]);
22+
23+
// Initializer expression in generative const constructor.
24+
const C.c1() : v = C(); //# 2: compile-time error
25+
const C.c2() : v = const C();
26+
const C.c3() : v = const C(C());
27+
28+
// Expression in redirecting generative const constuctor.
29+
const C.r1() : this(C()); //# 3: compile-time error
30+
const C.r2() : this(const C());
31+
const C.r3() : this(const C(C()));
32+
33+
// Default value of positional optional parameter.
34+
static List<C> foo([
35+
p1 = C(), //# 4: compile-time error
36+
p2 = const C(),
37+
p3 = const C(C()),
38+
]) =>
39+
[p2, p3];
40+
41+
// Default value of named optional parameter.
42+
static List<C> bar({
43+
p1 = C(), //# 5: compile-time error
44+
p2 = const C(),
45+
p3 = const C(C()),
46+
}) =>
47+
[p2, p3];
48+
}
49+
50+
void main() {
51+
var c = const C();
52+
var cc = const C(C());
53+
54+
// Check that const constructors can be invoked without `const`,
55+
// creating new instances every time.
56+
var nc1 = C();
57+
var nc2 = C.c2();
58+
var nc3 = C.c3();
59+
var nc4 = C.r2();
60+
var nc5 = C.r3();
61+
Expect.allDistinct([nc1, nc2, nc3, nc4, nc5, c, cc]);
62+
63+
// Check that const invocations create identical objects.
64+
Expect.identical(c, C.c2().v);
65+
Expect.identical(cc, C.c3().v);
66+
67+
Expect.identical(c, C.r2().v);
68+
Expect.identical(cc, C.r3().v);
69+
70+
Expect.identical(const [], C().i2);
71+
Expect.identical(const [[]], C().i3);
72+
73+
Expect.identical(c, C.foo()[0]);
74+
Expect.identical(cc, C.foo()[1]);
75+
76+
Expect.identical(c, C.bar()[0]);
77+
Expect.identical(cc, C.bar()[1]);
78+
}

0 commit comments

Comments
 (0)