Skip to content

Commit

Permalink
Macro. Issue 55696. If no/wrong number of type arguments, get them fr…
Browse files Browse the repository at this point in the history
…om actual DartType.

Bug: #55696
Change-Id: I6798ab5b262b6961edc2640342b9c39568cc7af2
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/371561
Commit-Queue: Konstantin Shcheglov <[email protected]>
Reviewed-by: Brian Wilkerson <[email protected]>
  • Loading branch information
scheglov authored and Commit Queue committed Jun 14, 2024
1 parent 205ad1c commit ad54e9b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 35 deletions.
81 changes: 46 additions & 35 deletions pkg/analyzer/lib/src/summary2/macro_declarations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,13 @@ class DeclarationBuilderFromElement {
}
}

List<macro.TypeAnnotationImpl> _dartTypes(List<DartType> types) {
return List.generate(types.length, (index) {
var type = types[index];
return _dartType(type);
}, growable: false);
}

EnumValueDeclarationImpl _enumConstantElement(
FieldElementImpl element,
) {
Expand Down Expand Up @@ -1028,7 +1035,7 @@ class DeclarationBuilderFromElement {
id: macro.RemoteInstance.uniqueId,
isNullable: type.nullabilitySuffix == NullabilitySuffix.question,
identifier: identifier(type.element),
typeArguments: type.typeArguments.map(_dartType).toList(),
typeArguments: _dartTypes(type.typeArguments),
);
}

Expand Down Expand Up @@ -1457,7 +1464,30 @@ class DeclarationBuilderFromNode {
macro.MethodDeclarationImpl methodDeclaration(
ast.MethodDeclarationImpl node,
) {
return _methodDeclaration(node);
var definingType = _definingType(node);
var element = node.declaredElement!;

var (namedParameters, positionalParameters) =
_executableFormalParameters(element, node.parameters);

return MethodDeclarationImpl._(
id: macro.RemoteInstance.uniqueId,
definingType: definingType,
element: element,
identifier: _declaredIdentifier(node.name, element),
library: library(element),
metadata: _buildMetadata(element),
hasBody: node.body is! ast.EmptyFunctionBody,
hasExternal: node.externalKeyword != null,
hasStatic: node.isStatic,
isGetter: node.isGetter,
isOperator: node.isOperator,
isSetter: node.isSetter,
namedParameters: namedParameters,
positionalParameters: positionalParameters,
returnType: _typeAnnotationMethodReturnType(node),
typeParameters: _typeParameterDeclarations(node.typeParameters),
);
}

MixinDeclarationImpl mixinDeclaration(
Expand Down Expand Up @@ -1809,47 +1839,28 @@ class DeclarationBuilderFromNode {
);
}

MethodDeclarationImpl _methodDeclaration(
ast.MethodDeclarationImpl node,
) {
var definingType = _definingType(node);
var element = node.declaredElement!;

var (namedParameters, positionalParameters) =
_executableFormalParameters(element, node.parameters);

return MethodDeclarationImpl._(
id: macro.RemoteInstance.uniqueId,
definingType: definingType,
element: element,
identifier: _declaredIdentifier(node.name, element),
library: library(element),
metadata: _buildMetadata(element),
hasBody: node.body is! ast.EmptyFunctionBody,
hasExternal: node.externalKeyword != null,
hasStatic: node.isStatic,
isGetter: node.isGetter,
isOperator: node.isOperator,
isSetter: node.isSetter,
namedParameters: namedParameters,
positionalParameters: positionalParameters,
returnType: _typeAnnotationMethodReturnType(node),
typeParameters: _typeParameterDeclarations(node.typeParameters),
);
}

macro.NamedTypeAnnotationImpl _namedType(
ast.NamedType node,
TypeAnnotationLocation location,
) {
var typeArguments = _typeAnnotations(
node.typeArguments?.arguments,
location,
);

// If the named type does not have type arguments, or has the wrong
// number of type arguments, replace them from the actual type.
if (node.type case InterfaceType type) {
if (typeArguments.length != type.element.typeParameters.length) {
typeArguments = builder.fromElement._dartTypes(type.typeArguments);
}
}

return _NamedTypeAnnotation(
id: macro.RemoteInstance.uniqueId,
identifier: _namedTypeIdentifier(node),
isNullable: node.question != null,
typeArguments: _typeAnnotations(
node.typeArguments?.arguments,
location,
),
typeArguments: typeArguments,
location: location,
);
}
Expand Down
22 changes: 22 additions & 0 deletions pkg/analyzer/test/src/summary/macro_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13792,6 +13792,28 @@ class A
''');
}

test_namedTypeAnnotation_typeArguments_absent() async {
await _assertIntrospectText(r'''
@Introspect()
Map get foo => {};
''', r'''
foo
flags: hasBody isGetter
returnType: Map<dynamic, dynamic>
''');
}

test_namedTypeAnnotation_typeArguments_wrongCount() async {
await _assertIntrospectText(r'''
@Introspect()
Map<int> get foo => {};
''', r'''
foo
flags: hasBody isGetter
returnType: Map<dynamic, dynamic>
''');
}

test_typeAlias_namedType() async {
await _assertIntrospectText(r'''
@Introspect()
Expand Down

0 comments on commit ad54e9b

Please sign in to comment.