Skip to content

Commit 706d105

Browse files
scheglovCommit Queue
authored andcommitted
Macro. Support for resolving OmittedTypeAnnotationCode.
Change-Id: Ida96aae6e2c2e64c4deb00ad029de64b69a2dccc Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/341386 Reviewed-by: Phil Quitslund <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent f3be94a commit 706d105

File tree

3 files changed

+77
-28
lines changed

3 files changed

+77
-28
lines changed

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

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -108,19 +108,8 @@ class DeclarationBuilder {
108108
macro.TypeAnnotation inferOmittedType(
109109
macro.OmittedTypeAnnotation omittedType,
110110
) {
111-
switch (omittedType) {
112-
case _OmittedTypeAnnotationDynamic():
113-
final type = DynamicTypeImpl.instance;
114-
return fromElement._dartType(type);
115-
case _OmittedTypeAnnotationMethodReturnType():
116-
final type = omittedType.element.returnType;
117-
return fromElement._dartType(type);
118-
case _OmittedTypeAnnotationVariable():
119-
final type = omittedType.element.type;
120-
return fromElement._dartType(type);
121-
default:
122-
throw UnimplementedError('${omittedType.runtimeType}');
123-
}
111+
final type = resolveType(omittedType.code);
112+
return fromElement._dartType(type);
124113
}
125114

126115
macro.ResolvedIdentifier resolveIdentifier(macro.Identifier identifier) {
@@ -173,6 +162,13 @@ class DeclarationBuilder {
173162
uri: element.source.uri,
174163
staticScope: null,
175164
);
165+
case TypeParameterElement():
166+
return macro.ResolvedIdentifier(
167+
kind: macro.IdentifierKind.local,
168+
name: element.name,
169+
uri: null,
170+
staticScope: null,
171+
);
176172
default:
177173
// TODO(scheglov): other elements
178174
throw UnimplementedError('${element.runtimeType}');
@@ -190,8 +186,7 @@ class DeclarationBuilder {
190186
case macro.NamedTypeAnnotationCode():
191187
return _resolveTypeCodeNamed(typeCode);
192188
case macro.OmittedTypeAnnotationCode():
193-
// TODO(scheglov): implement
194-
throw UnimplementedError('(${typeCode.runtimeType}) $typeCode');
189+
return _resolveTypeCodeOmitted(typeCode);
195190
case macro.RawTypeAnnotationCode():
196191
// TODO(scheglov): implement
197192
throw UnimplementedError('(${typeCode.runtimeType}) $typeCode');
@@ -369,6 +364,20 @@ class DeclarationBuilder {
369364
}
370365
}
371366

367+
DartType _resolveTypeCodeOmitted(macro.OmittedTypeAnnotationCode typeCode) {
368+
final omittedType = typeCode.typeAnnotation;
369+
switch (omittedType) {
370+
case _OmittedTypeAnnotationDynamic():
371+
return DynamicTypeImpl.instance;
372+
case _OmittedTypeAnnotationMethodReturnType():
373+
return omittedType.element.returnType;
374+
case _OmittedTypeAnnotationVariable():
375+
return omittedType.element.type;
376+
default:
377+
throw UnimplementedError('${omittedType.runtimeType}');
378+
}
379+
}
380+
372381
static macro.ExpressionCode _expressionCode(ast.Expression node) {
373382
return macro.ExpressionCode.fromString('$node');
374383
}

pkg/analyzer/test/src/summary/macro/static_type.dart

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
import 'package:_fe_analyzer_shared/src/macros/api.dart';
66

7-
/*macro*/ class IsExactly implements MethodDeclarationsMacro {
7+
/*macro*/ class IsExactly implements FunctionDefinitionMacro {
88
const IsExactly();
99

1010
@override
11-
buildDeclarationsForMethod(method, builder) async {
12-
final positional = method.positionalParameters.toList();
11+
buildDefinitionForFunction(declaration, builder) async {
12+
final positional = declaration.positionalParameters.toList();
1313
final first = positional[0];
1414
final second = positional[1];
1515

@@ -20,9 +20,8 @@ import 'package:_fe_analyzer_shared/src/macros/api.dart';
2020
final secondStaticType = await builder.resolve(secondTypeCode);
2121

2222
final result = await firstStaticType.isExactly(secondStaticType);
23-
final code = ' void isExactly_$result() {}';
24-
builder.declareInType(
25-
DeclarationCode.fromString(code),
23+
builder.augment(
24+
FunctionBodyCode.fromString('=> $result; // isExactly'),
2625
);
2726
}
2827
}

pkg/analyzer/test/src/summary/macro_test.dart

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6291,32 +6291,64 @@ mixin A {}
62916291
);
62926292
}
62936293

6294-
test_isExactly_typeParameter_notSame() async {
6294+
test_isExactly_omittedType_notSame() async {
62956295
final library = await buildLibrary('''
62966296
import 'static_type.dart';
62976297
62986298
class A {
6299+
void foo(int a, double b) {}
6300+
}
6301+
6302+
class B extends A {
62996303
@IsExactly()
6300-
void foo<T, U>(T a, U b) {}
6304+
void foo(a, b) {}
63016305
}
63026306
''');
63036307

63046308
final generated = _getMacroGeneratedCode(library);
6305-
expect(generated, contains('void isExactly_false() {}'));
6309+
_assertIsExactlyValue(generated, false);
63066310
}
63076311

6308-
test_isExactly_typeParameter_same() async {
6312+
test_isExactly_omittedType_same() async {
63096313
final library = await buildLibrary('''
63106314
import 'static_type.dart';
63116315
63126316
class A {
6317+
void foo(int a, int b) {}
6318+
}
6319+
6320+
class B extends A {
63136321
@IsExactly()
6314-
void foo<T>(T a, T b) {}
6322+
void foo(a, b) {}
63156323
}
63166324
''');
63176325

63186326
final generated = _getMacroGeneratedCode(library);
6319-
expect(generated, contains('void isExactly_true() {}'));
6327+
_assertIsExactlyValue(generated, true);
6328+
}
6329+
6330+
test_isExactly_typeParameter_notSame() async {
6331+
final library = await buildLibrary('''
6332+
import 'static_type.dart';
6333+
6334+
@IsExactly()
6335+
void foo<T, U>(T a, U b) {}
6336+
''');
6337+
6338+
final generated = _getMacroGeneratedCode(library);
6339+
_assertIsExactlyValue(generated, false);
6340+
}
6341+
6342+
test_isExactly_typeParameter_same() async {
6343+
final library = await buildLibrary('''
6344+
import 'static_type.dart';
6345+
6346+
@IsExactly()
6347+
void foo<T>(T a, T b) {}
6348+
''');
6349+
6350+
final generated = _getMacroGeneratedCode(library);
6351+
_assertIsExactlyValue(generated, true);
63206352
}
63216353

63226354
Future<void> _assertIsExactly({
@@ -6337,14 +6369,23 @@ class A {
63376369
''');
63386370

63396371
final generated = _getMacroGeneratedCode(library);
6340-
final expected = 'void isExactly_$isExactly() {}';
6372+
final expected = _isExactlyExpected(isExactly);
63416373
if (!generated.contains(expected)) {
63426374
fail(
63436375
'`$firstTypeCode` isExactly `$secondTypeCode`'
63446376
' expected to be `$isExactly`, but is not.\n',
63456377
);
63466378
}
63476379
}
6380+
6381+
void _assertIsExactlyValue(String generated, bool isExactly) {
6382+
final expected = _isExactlyExpected(isExactly);
6383+
expect(generated, contains(expected));
6384+
}
6385+
6386+
String _isExactlyExpected(bool isExactly) {
6387+
return '=> $isExactly; // isExactly';
6388+
}
63486389
}
63496390

63506391
abstract class MacroTypesTest extends MacroElementsBaseTest {

0 commit comments

Comments
 (0)