Skip to content

Commit ebc0a88

Browse files
srawlinscommit-bot@chromium.org
authored andcommitted
Error for assert initializer in redirecting constructor; fixes #37867
Bug: #37867 Change-Id: I74a809f2202e7ea1beeb45a8ce5d62613f102045 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/120100 Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 3e3406d commit ebc0a88

File tree

5 files changed

+72
-1
lines changed

5 files changed

+72
-1
lines changed

pkg/analyzer/lib/error/error.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ const List<ErrorCode> errorCodeValues = const [
7171
CompileTimeErrorCode.AMBIGUOUS_SET_OR_MAP_LITERAL_EITHER,
7272
CompileTimeErrorCode.ANNOTATION_WITH_NON_CLASS,
7373
CompileTimeErrorCode.ANNOTATION_WITH_TYPE_ARGUMENTS,
74+
CompileTimeErrorCode.ASSERT_IN_REDIRECTING_CONSTRUCTOR,
7475
CompileTimeErrorCode.ASYNC_FOR_IN_WRONG_CONTEXT,
7576
CompileTimeErrorCode.AWAIT_IN_WRONG_CONTEXT,
7677
CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_EXTENSION_NAME,

pkg/analyzer/lib/src/error/codes.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,10 @@ class CompileTimeErrorCode extends AnalyzerErrorCode {
410410
static const ParserErrorCode ANNOTATION_WITH_TYPE_ARGUMENTS =
411411
ParserErrorCode.ANNOTATION_WITH_TYPE_ARGUMENTS;
412412

413+
static const CompileTimeErrorCode ASSERT_IN_REDIRECTING_CONSTRUCTOR =
414+
const CompileTimeErrorCode('ASSERT_IN_REDIRECTING_CONSTRUCTOR',
415+
"A redirecting constructor can't have an 'assert' initializer.");
416+
413417
/**
414418
* 17.6.3 Asynchronous For-in: It is a compile-time error if an asynchronous
415419
* for-in statement appears inside a synchronous function.

pkg/analyzer/lib/src/generated/error_verifier.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4538,7 +4538,8 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
45384538
* redirected constructor invocation(s), super constructor invocations and
45394539
* field initializers.
45404540
*
4541-
* See [CompileTimeErrorCode.DEFAULT_VALUE_IN_REDIRECTING_FACTORY_CONSTRUCTOR],
4541+
* See [CompileTimeErrorCode.ASSERT_IN_REDIRECTING_CONSTRUCTOR],
4542+
* [CompileTimeErrorCode.DEFAULT_VALUE_IN_REDIRECTING_FACTORY_CONSTRUCTOR],
45424543
* [CompileTimeErrorCode.FIELD_INITIALIZER_REDIRECTING_CONSTRUCTOR],
45434544
* [CompileTimeErrorCode.MULTIPLE_REDIRECTING_CONSTRUCTOR_INVOCATIONS],
45444545
* [CompileTimeErrorCode.SUPER_IN_REDIRECTING_CONSTRUCTOR], and
@@ -4606,6 +4607,11 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
46064607
CompileTimeErrorCode.FIELD_INITIALIZER_REDIRECTING_CONSTRUCTOR,
46074608
initializer);
46084609
}
4610+
if (initializer is AssertInitializer) {
4611+
_errorReporter.reportErrorForNode(
4612+
CompileTimeErrorCode.ASSERT_IN_REDIRECTING_CONSTRUCTOR,
4613+
initializer);
4614+
}
46094615
}
46104616
}
46114617
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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:analyzer/src/error/codes.dart';
6+
import 'package:test_reflective_loader/test_reflective_loader.dart';
7+
8+
import '../dart/resolution/driver_resolution.dart';
9+
10+
main() {
11+
defineReflectiveSuite(() {
12+
defineReflectiveTests(AssertInRedirectingConstructorTest);
13+
});
14+
}
15+
16+
@reflectiveTest
17+
class AssertInRedirectingConstructorTest extends DriverResolutionTest {
18+
test_assertBeforeRedirection() async {
19+
await assertErrorsInCode(r'''
20+
class A {}
21+
class B {
22+
B(int x) : assert(x > 0), this.name();
23+
B.name() {}
24+
}
25+
''', [error(CompileTimeErrorCode.ASSERT_IN_REDIRECTING_CONSTRUCTOR, 34, 13)]);
26+
}
27+
28+
test_redirectionBeforeAssert() async {
29+
await assertErrorsInCode(r'''
30+
class A {}
31+
class B {
32+
B(int x) : this.name(), assert(x > 0);
33+
B.name() {}
34+
}
35+
''', [error(CompileTimeErrorCode.ASSERT_IN_REDIRECTING_CONSTRUCTOR, 47, 13)]);
36+
}
37+
38+
test_justRedirection() async {
39+
await assertNoErrorsInCode(r'''
40+
class A {}
41+
class B {
42+
B(int x) : this.name();
43+
B.name() {}
44+
}
45+
''');
46+
}
47+
48+
test_justAssert() async {
49+
await assertNoErrorsInCode(r'''
50+
class A {}
51+
class B {
52+
B(int x) : assert(x > 0);
53+
B.name() {}
54+
}
55+
''');
56+
}
57+
}

pkg/analyzer/test/src/diagnostics/test_all.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import 'ambiguous_extension_member_access_test.dart'
1010
import 'ambiguous_import_test.dart' as ambiguous_import;
1111
import 'ambiguous_set_or_map_literal_test.dart' as ambiguous_set_or_map_literal;
1212
import 'argument_type_not_assignable_test.dart' as argument_type_not_assignable;
13+
import 'assert_in_redirecting_constructor_test.dart'
14+
as assert_in_redirecting_constructor;
1315
import 'assignment_to_const_test.dart' as assignment_to_const;
1416
import 'assignment_to_final_local_test.dart' as assignment_to_final_local;
1517
import 'assignment_to_final_no_setter_test.dart'
@@ -338,6 +340,7 @@ main() {
338340
ambiguous_import.main();
339341
ambiguous_set_or_map_literal.main();
340342
argument_type_not_assignable.main();
343+
assert_in_redirecting_constructor.main();
341344
assignment_to_const.main();
342345
assignment_to_final_local.main();
343346
assignment_to_final_no_setter.main();

0 commit comments

Comments
 (0)