Skip to content

Commit 6b5580d

Browse files
davidmorganCommit Queue
authored andcommitted
[macros] Add language test for introspection cycle.
Small changes so it passes on the analyzer, the CFE does not yet detect cycles. [email protected], [email protected] Change-Id: Iaeb586da7066e50b5b90c9793bc34b51e0eabbbc Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/345062 Reviewed-by: Konstantin Shcheglov <[email protected]> Reviewed-by: Jake Macdonald <[email protected]> Auto-Submit: Morgan :) <[email protected]> Commit-Queue: Morgan :) <[email protected]>
1 parent 0eac43b commit 6b5580d

File tree

4 files changed

+53
-15
lines changed

4 files changed

+53
-15
lines changed

pkg/analyzer/lib/src/summary2/macro_application.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,8 @@ class LibraryMacroApplier {
280280
_elementToIntrospectionCycleId[element] = id;
281281
}
282282

283-
throw StateError('[$id] Declarations phase introspection cycle.');
283+
throw macro.MacroIntrospectionCycleExceptionImpl(
284+
'[$id] Declarations phase introspection cycle.');
284285
}
285286
}
286287
}
@@ -1050,7 +1051,8 @@ class _DeclarationPhaseIntrospector extends _TypePhaseIntrospector
10501051
// We might have detected a cycle for this target element.
10511052
// Either just now, or before.
10521053
if (applier._elementToIntrospectionCycleId[element] case var id?) {
1053-
throw StateError('[$id] Declarations phase introspection cycle.');
1054+
throw macro.MacroIntrospectionCycleExceptionImpl(
1055+
'[$id] Declarations phase introspection cycle.');
10541056
}
10551057
}
10561058
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) 2024, 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+
// SharedOptions=--enable-experiment=macros
5+
6+
import 'impl/assert_in_declarations_phase_macro.dart';
7+
8+
@AssertInDeclarationsPhase(
9+
targetName: 'B',
10+
constructorsOf: ['b()'],
11+
expectThrowsA: 'MacroIntrospectionCycleExceptionImpl',
12+
)
13+
class A {
14+
A.a();
15+
}
16+
17+
@AssertInDeclarationsPhase(
18+
targetName: 'A',
19+
constructorsOf: ['a()'],
20+
expectThrowsA: 'MacroIntrospectionCycleExceptionImpl',
21+
)
22+
class B {
23+
B.b();
24+
}
25+
26+
void main() {}

tests/language/macros/introspect/failure_test.dart

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,25 @@ import 'impl/assert_in_types_phase_macro.dart';
1111
// pass with a false positive. This test should start failing at the same time.
1212

1313
@AssertInTypesPhase(
14-
// [error line 13, column 1, length 218]
15-
// [analyzer] COMPILE_TIME_ERROR.MACRO_ERROR
14+
// [error line 13, column 1]
15+
// [analyzer] unspecified
16+
// [cfe] unspecified
1617
targetLibrary: 'dart:core',
1718
targetName: 'int',
1819
resolveIdentifier: '<intentional mismatch and failure>',
1920
)
2021
@AssertInDefinitionsPhase(
21-
// [error line 20, column 1, length 191]
22-
// [analyzer] COMPILE_TIME_ERROR.MACRO_ERROR
22+
// [error line 21, column 1]
23+
// [analyzer] unspecified
24+
// [cfe] unspecified
2325
targetName: 'A',
2426
constructorsOf: ['<intentional mismatch and failure>'],
2527
)
2628
@AssertInDeclarationsPhase(
27-
// [error line 26, column 1, length 191]
28-
// [analyzer] COMPILE_TIME_ERROR.MACRO_ERROR
29+
// [error line 28, column 1]
30+
// [analyzer] unspecified
31+
// [cfe] unspecified
2932
targetName: 'A',
3033
constructorsOf: ['<intentional mismatch and failure'],
3134
)
3235
class A {}
33-
34-
// TODO(davidmorgan): add CFE error coverage.

tests/language/macros/introspect/impl/assert_in_declarations_phase_macro.dart

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,34 @@ import 'package:expect/expect.dart';
99

1010
import 'impl.dart';
1111

12-
macro class AssertInDeclarationsPhase
13-
implements ClassDeclarationsMacro {
12+
macro class AssertInDeclarationsPhase implements ClassDeclarationsMacro {
1413
final String? targetLibrary;
1514
final String targetName;
1615
final List? constructorsOf;
1716
final List? fieldsOf;
1817
final List? methodsOf;
18+
final String? expectThrowsA;
1919

2020
const AssertInDeclarationsPhase(
2121
{this.targetLibrary,
2222
required this.targetName,
2323
this.constructorsOf,
2424
this.fieldsOf,
25-
this.methodsOf});
25+
this.methodsOf,
26+
this.expectThrowsA});
2627

2728
@override
2829
Future<void> buildDeclarationsForClass(
29-
ClassDeclaration clazz, MemberDeclarationBuilder builder) =>
30-
_assert(clazz, builder);
30+
ClassDeclaration clazz, MemberDeclarationBuilder builder) async {
31+
Object? thrown;
32+
try {
33+
await _assert(clazz, builder);
34+
thrown = null;
35+
} catch (e) {
36+
thrown = e;
37+
}
38+
Expect.equals(expectThrowsA, thrown?.runtimeType.toString());
39+
}
3140

3241
// TODO(davidmorgan): support asserting in more places.
3342

0 commit comments

Comments
 (0)