Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add assert-in-initializer-list to formal specification.
Add tests for new syntax. Not dart-formatted yet since the formatter doesn't accept the new syntax. R=eernst@google.com, floitsch@google.com Review-Url: https://codereview.chromium.org/2962153002 .
- Loading branch information
Showing
11 changed files
with
440 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
tests/language/assertion_initializer_const_error2_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright (c) 201, 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. | ||
// VMOptions=--assert_initializer | ||
// | ||
// Dart test program testing assert statements. | ||
|
||
import "package:expect/expect.dart"; | ||
|
||
class C { | ||
final int x; | ||
// Const constructors. | ||
const C.cc01(this.x, y) | ||
: assert(x < y) //# cc01: compile-time error | ||
; | ||
const C.cc02(x, y) : x = x, | ||
assert(x < y) //# cc02: compile-time error | ||
; | ||
const C.cc03(x, y) : | ||
assert(x < y), //# cc03: compile-time error | ||
x = x; | ||
const C.cc04(this.x, y) : super() | ||
, assert(x < y) //# cc04: compile-time error | ||
; | ||
const C.cc05(this.x, y) : | ||
assert(x < y), //# cc05: compile-time error | ||
super(); | ||
const C.cc06(x, y) : x = x, super() | ||
, assert(x < y) //# cc06: compile-time error | ||
; | ||
const C.cc07(x, y) : | ||
assert(x < y), //# cc07: compile-time error | ||
super(), x = x; | ||
const C.cc08(x, y) : | ||
assert(x < y), //# cc08: compile-time error | ||
super(), x = x | ||
, assert(y > x) //# cc08: continued | ||
; | ||
const C.cc09(this.x, y) | ||
: assert(x < y, "$x < $y") //# cc09: compile-time error | ||
; | ||
const C.cc10(this.x, y) | ||
: assert(x < y,) //# cc10: compile-time error | ||
; | ||
const C.cc11(this.x, y) | ||
: assert(x < y, "$x < $y",) //# cc11: compile-time error | ||
; | ||
} | ||
|
||
|
||
main() { | ||
// Failing assertions in const invociations are compile-time errors. | ||
const C.cc01(2, 1); //# cc01: compile-time error | ||
const C.cc02(2, 1); //# cc02: compile-time error | ||
const C.cc03(2, 1); //# cc03: compile-time error | ||
const C.cc04(2, 1); //# cc04: compile-time error | ||
const C.cc05(2, 1); //# cc05: compile-time error | ||
const C.cc06(2, 1); //# cc06: compile-time error | ||
const C.cc07(2, 1); //# cc07: compile-time error | ||
const C.cc08(2, 1); //# cc08: compile-time error | ||
const C.cc09(2, 1); //# cc09: compile-time error | ||
const C.cc10(2, 1); //# cc10: compile-time error | ||
const C.cc11(2, 1); //# cc11: compile-time error | ||
} |
20 changes: 20 additions & 0 deletions
20
tests/language/assertion_initializer_const_error_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright (c) 201, 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. | ||
// VMOptions=--assert_initializer | ||
|
||
class C { | ||
static bool check(x, y) => x < y; | ||
final int x; | ||
const C(this.x); | ||
// The expression is not a potentially constant expression. | ||
// This is a compile-time error even in production mode. | ||
const C.bc03(this.x, y) | ||
: assert(check(x, y)) //# 01: compile-time error | ||
; | ||
} | ||
|
||
main() { | ||
var c = new C.bc03(1, 2); | ||
if (c.x != 1) throw "non-trivial use of c"; | ||
} |
29 changes: 29 additions & 0 deletions
29
tests/language/assertion_initializer_const_function_error_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (c) 2017, 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. | ||
// VMOptions=--assert_initializer | ||
// | ||
// Dart test program testing assert statements. | ||
|
||
import "package:expect/expect.dart"; | ||
|
||
class C { | ||
static bool staticTrue() => true; | ||
final int x; | ||
const C(this.x); | ||
// The expression *is* a compile-time constant, but not a bool value. | ||
// Static warning, assertion throws which makes it a compile-time error. | ||
const C.bc02(this.x, y) | ||
: assert(staticTrue) //# 01: static type warning | ||
; | ||
} | ||
|
||
|
||
main() { | ||
// Assertion fails, so in checked mode it's a compile-time error. | ||
// Production mode will succeed because the assertion isn't evaluated. | ||
var c = const C(1); | ||
c = const C.bc02(1, 2); //# 01: compile-time error | ||
if (c.x != 1) throw "non-trivial use of c"; | ||
Expect.identical(const C(1), c); | ||
} |
29 changes: 29 additions & 0 deletions
29
tests/language/assertion_initializer_const_function_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (c) 201, 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. | ||
// VMOptions=--assert_initializer | ||
// | ||
// Dart test program testing assert statements. | ||
|
||
import "package:expect/expect.dart"; | ||
|
||
class C { | ||
static bool staticTrue() => true; | ||
final int x; | ||
const C(this.x); | ||
// The expression *is* compile-time constant, but not a bool value. | ||
// Static warning, runtime always fails assertion. | ||
const C.bc01(this.x, y) | ||
: assert(staticTrue) //# 01: static type warning | ||
; | ||
} | ||
|
||
main() { | ||
bool checkedMode = false; | ||
assert(checkedMode = true); | ||
if (checkedMode) { | ||
Expect.throws(() => new C.bc01(1, 2), (e) => e is AssertionError); | ||
} else { | ||
Expect.equals(1, new C.bc01(1, 2).x); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
// Copyright (c) 201, 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. | ||
// VMOptions=--assert_initializer | ||
// | ||
// Dart test program testing assert statements. | ||
|
||
import "package:expect/expect.dart"; | ||
|
||
class C { | ||
static bool check(x, y) => x < y; | ||
static bool staticTrue() => true; | ||
final int x; | ||
const C(this.x); | ||
|
||
C.c01(this.x, y) : assert(x < y); | ||
C.c02(x, y) : x = x, assert(x < y); | ||
C.c03(x, y) : assert(x < y), x = x; | ||
C.c04(this.x, y) : super(), assert(x < y); | ||
C.c05(this.x, y) : assert(x < y), super(); | ||
C.c06(x, y) : x = x, super(), assert(x < y); | ||
C.c07(x, y) : assert(x < y), super(), x = x; | ||
C.c08(x, y) : assert(x < y), super(), x = x, assert(y > x); | ||
C.c09(this.x, y) : assert(x < y, "$x < $y"); | ||
C.c10(this.x, y) : assert(x < y,); | ||
C.c11(this.x, y) : assert(x < y, "$x < $y",); | ||
|
||
const C.cc01(this.x, y) : assert(x < y); | ||
const C.cc02(x, y) : x = x, assert(x < y); | ||
const C.cc03(x, y) : assert(x < y), x = x; | ||
const C.cc04(this.x, y) : super(), assert(x < y); | ||
const C.cc05(this.x, y) : assert(x < y), super(); | ||
const C.cc06(x, y) : x = x, super(), assert(x < y); | ||
const C.cc07(x, y) : assert(x < y), super(), x = x; | ||
const C.cc08(x, y) : assert(x < y), super(), x = x, assert(y > x); | ||
const C.cc09(this.x, y) : assert(x < y, "$x < $y"); | ||
const C.cc10(this.x, y) : assert(x < y,); | ||
const C.cc11(this.x, y) : assert(x < y, "$x < $y",); | ||
|
||
C.nc01(this.x, y) : assert(check(x, y)); | ||
C.nc02(x, y) : x = x, assert(check(x, y)); | ||
C.nc03(x, y) : assert(check(x, y)), x = x; | ||
C.nc04(this.x, y) : super(), assert(check(x, y)); | ||
C.nc05(this.x, y) : assert(check(x, y)), super(); | ||
C.nc06(x, y) : x = x, super(), assert(check(x, y)); | ||
C.nc07(x, y) : assert(check(x, y)), super(), x = x; | ||
C.nc08(x, y) : assert(check(x, y)), super(), x = x, assert(y > x); | ||
C.nc09(this.x, y) : assert(check(x, y), "$x < $y"); | ||
C.nc10(this.x, y) : assert(check(x, y),); | ||
C.nc11(this.x, y) : assert(check(x, y), "$x < $y",); | ||
|
||
C.fc01(this.x, y) : assert(() => x < y); | ||
C.fc02(x, y) : x = x, assert(() => x < y); | ||
C.fc03(x, y) : assert(() => x < y), x = x; | ||
C.fc04(this.x, y) : super(), assert(() => x < y); | ||
C.fc05(this.x, y) : assert(() => x < y), super(); | ||
C.fc06(x, y) : x = x, super(), assert(() => x < y); | ||
C.fc07(x, y) : assert(() => x < y), super(), x = x; | ||
C.fc08(x, y) : assert(() => x < y), super(), x = x, assert(y > x); | ||
C.fc09(this.x, y) : assert(() => x < y, "$x < $y"); | ||
C.fc10(this.x, y) : assert(() => x < y,); | ||
C.fc11(this.x, y) : assert(() => x < y, "$x < $y",); | ||
} | ||
|
||
|
||
main() { | ||
// Test all constructors with both succeeding and failing asserts. | ||
test(1, 2); | ||
test(2, 1); | ||
|
||
const c1 = const C(1); | ||
|
||
// Asserts do not affect canonization. | ||
Expect.identical(c1, const C.cc01(1, 2)); | ||
Expect.identical(c1, const C.cc02(1, 2)); | ||
Expect.identical(c1, const C.cc03(1, 2)); | ||
Expect.identical(c1, const C.cc04(1, 2)); | ||
Expect.identical(c1, const C.cc05(1, 2)); | ||
Expect.identical(c1, const C.cc06(1, 2)); | ||
Expect.identical(c1, const C.cc07(1, 2)); | ||
Expect.identical(c1, const C.cc08(1, 2)); | ||
Expect.identical(c1, const C.cc09(1, 2)); | ||
Expect.identical(c1, const C.cc10(1, 2)); | ||
Expect.identical(c1, const C.cc11(1, 2)); | ||
} | ||
|
||
void test(int x, int y) { | ||
bool checkedMode = false; | ||
assert(checkedMode = true); | ||
|
||
bool Function(C Function()) doTest = (checkedMode && x >= y) | ||
? (f) { Expect.throws(f, (e) => e is AssertionError); } | ||
: (f) { Expect.equals(x, f().x); }; | ||
|
||
doTest(() => new C.c01(x, y)); | ||
doTest(() => new C.c02(x, y)); | ||
doTest(() => new C.c03(x, y)); | ||
doTest(() => new C.c04(x, y)); | ||
doTest(() => new C.c05(x, y)); | ||
doTest(() => new C.c06(x, y)); | ||
doTest(() => new C.c07(x, y)); | ||
doTest(() => new C.c08(x, y)); | ||
doTest(() => new C.c09(x, y)); | ||
doTest(() => new C.c10(x, y)); | ||
doTest(() => new C.c11(x, y)); | ||
doTest(() => new C.cc01(x, y)); | ||
doTest(() => new C.cc02(x, y)); | ||
doTest(() => new C.cc03(x, y)); | ||
doTest(() => new C.cc04(x, y)); | ||
doTest(() => new C.cc05(x, y)); | ||
doTest(() => new C.cc06(x, y)); | ||
doTest(() => new C.cc07(x, y)); | ||
doTest(() => new C.cc08(x, y)); | ||
doTest(() => new C.cc09(x, y)); | ||
doTest(() => new C.cc10(x, y)); | ||
doTest(() => new C.cc11(x, y)); | ||
doTest(() => new C.nc01(x, y)); | ||
doTest(() => new C.nc02(x, y)); | ||
doTest(() => new C.nc03(x, y)); | ||
doTest(() => new C.nc04(x, y)); | ||
doTest(() => new C.nc05(x, y)); | ||
doTest(() => new C.nc06(x, y)); | ||
doTest(() => new C.nc07(x, y)); | ||
doTest(() => new C.nc08(x, y)); | ||
doTest(() => new C.nc09(x, y)); | ||
doTest(() => new C.nc10(x, y)); | ||
doTest(() => new C.nc11(x, y)); | ||
doTest(() => new C.fc01(x, y)); | ||
doTest(() => new C.fc02(x, y)); | ||
doTest(() => new C.fc03(x, y)); | ||
doTest(() => new C.fc04(x, y)); | ||
doTest(() => new C.fc05(x, y)); | ||
doTest(() => new C.fc06(x, y)); | ||
doTest(() => new C.fc07(x, y)); | ||
doTest(() => new C.fc08(x, y)); | ||
doTest(() => new C.fc09(x, y)); | ||
doTest(() => new C.fc10(x, y)); | ||
doTest(() => new C.fc11(x, y)); | ||
} | ||
|
Oops, something went wrong.