Skip to content

Commit c65b444

Browse files
committed
Analyzer: Move TOP_LEVEL_CYCLE to be a compile-time error
Also remove duplication with RECURSIVE_COMPILE_TIME_CONSTANT. Also write some tests for TOP_LEVEL_CYCLE. Change-Id: I33cb765236a83bd925f9f0c6e65ac9398761c699 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/155701 Reviewed-by: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent afee49e commit c65b444

File tree

8 files changed

+69
-23
lines changed

8 files changed

+69
-23
lines changed

pkg/analyzer/lib/error/error.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ const List<ErrorCode> errorCodeValues = [
329329
CompileTimeErrorCode.SUPER_INITIALIZER_IN_OBJECT,
330330
CompileTimeErrorCode.SWITCH_CASE_COMPLETES_NORMALLY,
331331
CompileTimeErrorCode.THROW_OF_INVALID_TYPE,
332+
CompileTimeErrorCode.TOP_LEVEL_CYCLE,
332333
CompileTimeErrorCode.TYPE_ALIAS_CANNOT_REFERENCE_ITSELF,
333334
CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS,
334335
// ignore: deprecated_member_use_from_same_package
@@ -838,7 +839,6 @@ const List<ErrorCode> errorCodeValues = [
838839
StrongModeCode.IMPLICIT_DYNAMIC_RETURN,
839840
StrongModeCode.IMPLICIT_DYNAMIC_TYPE,
840841
StrongModeCode.IMPLICIT_DYNAMIC_VARIABLE,
841-
StrongModeCode.TOP_LEVEL_CYCLE,
842842
StrongModeCode.TOP_LEVEL_FUNCTION_LITERAL_BLOCK,
843843
StrongModeCode.TOP_LEVEL_IDENTIFIER_NO_TYPE,
844844
StrongModeCode.TOP_LEVEL_INSTANCE_GETTER,

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6254,6 +6254,19 @@ class CompileTimeErrorCode extends AnalyzerErrorCode {
62546254
"The type '{0}' of the thrown expression must be assignable to 'Object'.",
62556255
hasPublishedDocs: true);
62566256

6257+
/**
6258+
* Parameters:
6259+
* 0: the element whose type could not be inferred.
6260+
* 1: The [TopLevelInferenceError]'s arguments that led to the cycle.
6261+
*/
6262+
static const CompileTimeErrorCode TOP_LEVEL_CYCLE = CompileTimeErrorCode(
6263+
'TOP_LEVEL_CYCLE',
6264+
"The type of '{0}' can't be inferred because it depends on itself "
6265+
"through the cycle: {1}.",
6266+
correction:
6267+
"Try adding an explicit type to one or more of the variables in the "
6268+
"cycle in order to break the cycle.");
6269+
62576270
/**
62586271
* Parameters:
62596272
* 0: the name of the type used in the instance creation that should be
@@ -10853,14 +10866,6 @@ class StrongModeCode extends ErrorCode {
1085310866
*/
1085410867
/* TODO(leafp) Delete most of these.
1085510868
*/
10856-
static const StrongModeCode TOP_LEVEL_CYCLE = StrongModeCode(
10857-
ErrorType.COMPILE_TIME_ERROR,
10858-
'TOP_LEVEL_CYCLE',
10859-
"The type of '{0}' can't be inferred because it depends on itself "
10860-
"through the cycle: {1}.",
10861-
correction:
10862-
"Try adding an explicit type to one or more of the variables in the "
10863-
"cycle in order to break the cycle.");
1086410869

1086510870
static const StrongModeCode TOP_LEVEL_FUNCTION_LITERAL_BLOCK = StrongModeCode(
1086610871
ErrorType.HINT,

pkg/analyzer/lib/src/task/strong/checker.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,8 +1059,12 @@ class _TopLevelInitializerValidator extends RecursiveAstVisitor<void> {
10591059
TopLevelInferenceError error = variable.typeInferenceError;
10601060
if (error != null) {
10611061
if (error.kind == TopLevelInferenceErrorKind.dependencyCycle) {
1062-
_codeChecker._recordMessage(
1063-
n, StrongModeCode.TOP_LEVEL_CYCLE, [_name, error.arguments]);
1062+
// Errors on const should have been reported with
1063+
// [CompileTimeErrorCode.RECURSIVE_COMPILE_TIME_CONSTANT].
1064+
if (!variable.isConst) {
1065+
_codeChecker._recordMessage(n, CompileTimeErrorCode.TOP_LEVEL_CYCLE,
1066+
[_name, error.arguments]);
1067+
}
10641068
} else {
10651069
_codeChecker._recordMessage(
10661070
n, StrongModeCode.TOP_LEVEL_IDENTIFIER_NO_TYPE, [_name, e.name]);

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ const x = y + 1;
2121
const y = x + 1;
2222
''', [
2323
error(CompileTimeErrorCode.RECURSIVE_COMPILE_TIME_CONSTANT, 6, 1),
24-
error(StrongModeCode.TOP_LEVEL_CYCLE, 10, 1),
2524
error(CompileTimeErrorCode.RECURSIVE_COMPILE_TIME_CONSTANT, 23, 1),
26-
error(StrongModeCode.TOP_LEVEL_CYCLE, 27, 1),
2725
]);
2826
}
2927

@@ -72,7 +70,6 @@ class C {
7270
const x = x;
7371
''', [
7472
error(CompileTimeErrorCode.RECURSIVE_COMPILE_TIME_CONSTANT, 6, 1),
75-
error(StrongModeCode.TOP_LEVEL_CYCLE, 10, 1),
7673
]);
7774
}
7875

@@ -85,7 +82,6 @@ const elems = const [
8582
];
8683
''', [
8784
error(CompileTimeErrorCode.RECURSIVE_COMPILE_TIME_CONSTANT, 6, 5),
88-
error(StrongModeCode.TOP_LEVEL_CYCLE, 39, 5),
8985
]);
9086
}
9187
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,7 @@ import 'switch_expression_not_assignable_test.dart'
525525
as switch_expression_not_assignable;
526526
import 'throw_of_invalid_type_test.dart' as throw_of_invalid_type;
527527
import 'todo_test.dart' as todo_test;
528+
import 'top_level_cycle_test.dart' as top_level_cycle;
528529
import 'top_level_instance_getter_test.dart' as top_level_instance_getter;
529530
import 'top_level_instance_method_test.dart' as top_level_instance_method;
530531
import 'type_alias_cannot_reference_itself_test.dart'
@@ -949,6 +950,7 @@ main() {
949950
switch_expression_not_assignable.main();
950951
throw_of_invalid_type.main();
951952
todo_test.main();
953+
top_level_cycle.main();
952954
top_level_instance_getter.main();
953955
top_level_instance_method.main();
954956
type_alias_cannot_reference_itself.main();
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) 2020, 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(TopLevelCycleTest);
13+
});
14+
}
15+
16+
@reflectiveTest
17+
class TopLevelCycleTest extends DriverResolutionTest {
18+
test_cycle() async {
19+
await assertErrorsInCode(r'''
20+
var x = y + 1;
21+
var y = x + 1;
22+
''', [
23+
error(CompileTimeErrorCode.TOP_LEVEL_CYCLE, 8, 1),
24+
error(CompileTimeErrorCode.TOP_LEVEL_CYCLE, 23, 1),
25+
]);
26+
}
27+
28+
test_singleVariable() async {
29+
await assertErrorsInCode(r'''
30+
var x = x;
31+
''', [
32+
error(CompileTimeErrorCode.TOP_LEVEL_CYCLE, 8, 1),
33+
]);
34+
}
35+
36+
test_singleVariable_fromList() async {
37+
await assertErrorsInCode(r'''
38+
var elems = [
39+
[
40+
1, elems, 3,
41+
],
42+
];
43+
''', [
44+
error(CompileTimeErrorCode.TOP_LEVEL_CYCLE, 25, 5),
45+
]);
46+
}
47+
}

tests/language/const/syntax_test.dart

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,11 @@ const A2 = A3 + 1;
9595
// ^^
9696
// [analyzer] COMPILE_TIME_ERROR.RECURSIVE_COMPILE_TIME_CONSTANT
9797
// [cfe] Can't infer the type of 'A2': circularity found during type inference.
98-
// ^^
99-
// [analyzer] COMPILE_TIME_ERROR.TOP_LEVEL_CYCLE
10098
// ^
10199
// [cfe] Constant evaluation error:
102100
const A3 = A2 + 1;
103101
// ^^
104102
// [analyzer] COMPILE_TIME_ERROR.RECURSIVE_COMPILE_TIME_CONSTANT
105-
// ^^
106-
// [analyzer] COMPILE_TIME_ERROR.TOP_LEVEL_CYCLE
107103

108104
class C0 {
109105
static const X = const C1();

tests/language_2/const/syntax_test.dart

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,11 @@ const A2 = A3 + 1;
9292
// ^^
9393
// [analyzer] COMPILE_TIME_ERROR.RECURSIVE_COMPILE_TIME_CONSTANT
9494
// [cfe] Can't infer the type of 'A2': circularity found during type inference.
95-
// ^^
96-
// [analyzer] COMPILE_TIME_ERROR.TOP_LEVEL_CYCLE
9795
// ^
9896
// [cfe] Constant evaluation error:
9997
const A3 = A2 + 1;
10098
// ^^
10199
// [analyzer] COMPILE_TIME_ERROR.RECURSIVE_COMPILE_TIME_CONSTANT
102-
// ^^
103-
// [analyzer] COMPILE_TIME_ERROR.TOP_LEVEL_CYCLE
104100

105101
class C0 {
106102
static const X = const C1();

0 commit comments

Comments
 (0)