Skip to content

Commit

Permalink
Macro. Support for more kinds of TypeAnnotation(s) as diagnostics tar…
Browse files Browse the repository at this point in the history
…get.

Change-Id: I67ce3ca386175c0055ceecfdfbd7dcc52c031f4a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/349781
Reviewed-by: Phil Quitslund <pquitslund@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
  • Loading branch information
scheglov authored and Commit Queue committed Feb 1, 2024
1 parent baf1e6e commit 423e769
Show file tree
Hide file tree
Showing 9 changed files with 522 additions and 56 deletions.
2 changes: 1 addition & 1 deletion pkg/analyzer/lib/src/dart/analysis/driver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ import 'package:meta/meta.dart';
// TODO(scheglov): Clean up the list of implicitly analyzed files.
class AnalysisDriver {
/// The version of data format, should be incremented on every format change.
static const int DATA_VERSION = 335;
static const int DATA_VERSION = 336;

/// The number of exception contexts allowed to write. Once this field is
/// zero, we stop writing any new exception contexts in this process.
Expand Down
50 changes: 38 additions & 12 deletions pkg/analyzer/lib/src/generated/error_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1420,6 +1420,13 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
_checkForMainFunction1(variable.name, variable.declaredElement!);
}

for (final variable in node.variables.variables) {
var element = variable.declaredElement;
if (element is TopLevelVariableElementImpl) {
_reportMacroDiagnostics(element, node.metadata);
}
}

super.visitTopLevelVariableDeclaration(node);
}

Expand Down Expand Up @@ -5962,11 +5969,19 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
var node = locationNode(location.parent);
switch (node) {
case FunctionDeclaration():
return node.returnType;
if (node.returnType case var returnType?) {
return returnType;
}
// The type is omitted.
return SimpleIdentifierImpl(node.name);
case GenericFunctionType():
return node.returnType;
return node.returnType ?? node;
case MethodDeclaration():
return node.returnType;
if (node.returnType case var returnType?) {
return returnType;
}
// The type is omitted.
return SimpleIdentifierImpl(node.name);
default:
throw UnimplementedError('${node.runtimeType}');
}
Expand All @@ -5978,10 +5993,19 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
var parent = node?.parent;
switch (node) {
case SimpleFormalParameter():
return node.type;
if (node.type case var type?) {
return type;
} else if (node.name case var nameToken?) {
return SimpleIdentifierImpl(nameToken);
}
return null;
case VariableDeclaration():
if (parent is VariableDeclarationList) {
return parent.type;
if (parent.type case var type?) {
return type;
}
// The type is omitted.
return SimpleIdentifierImpl(node.name);
}
}
throw UnimplementedError(
Expand Down Expand Up @@ -6085,13 +6109,15 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
diagnostic.contextMessages.map(convertMessage).toList(),
);
case TypeAnnotationMacroDiagnosticTarget():
var errorNode = locationNode(target.location)!;
errorReporter.reportErrorForNode(
errorCode,
errorNode,
[diagnostic.message.message],
diagnostic.contextMessages.map(convertMessage).toList(),
);
var errorNode = locationNode(target.location);
if (errorNode != null) {
errorReporter.reportErrorForNode(
errorCode,
errorNode,
[diagnostic.message.message],
diagnostic.contextMessages.map(convertMessage).toList(),
);
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions pkg/analyzer/lib/src/summary2/bundle_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2550,6 +2550,7 @@ class TopLevelVariableElementLinkedData
element.metadata = reader._readAnnotationList(
unitElement: unitElement,
);
element.macroDiagnostics = reader.readMacroDiagnostics();
element.type = reader.readRequiredType();
if (element is ConstTopLevelVariableElementImpl) {
var initializer = reader._readOptionalExpression();
Expand Down
1 change: 1 addition & 0 deletions pkg/analyzer/lib/src/summary2/bundle_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,7 @@ class BundleWriter {
TopLevelVariableElementFlags.write(_sink, element);
_sink._writeTopLevelInferenceError(element.typeInferenceError);
_resolutionSink._writeAnnotationList(element.metadata);
_resolutionSink.writeMacroDiagnostics(element.macroDiagnostics);
_resolutionSink.writeType(element.type);
_resolutionSink._writeOptionalNode(element.constantInitializer);
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/analyzer/lib/src/summary2/macro_application.dart
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ class LibraryMacroApplier {
macro.TypeAnnotationDiagnosticTarget macroTarget,
) {
switch (macroTarget.typeAnnotation) {
case NamedTypeAnnotation typeAnnotation:
case TypeAnnotationWithLocation typeAnnotation:
return TypeAnnotationMacroDiagnosticTarget(
location: typeAnnotation.location,
);
Expand Down
132 changes: 91 additions & 41 deletions pkg/analyzer/lib/src/summary2/macro_declarations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1653,7 +1653,7 @@ class DeclarationBuilderFromNode {
ast.NamedType node,
TypeAnnotationLocation location,
) {
return NamedTypeAnnotation(
return _NamedTypeAnnotation(
id: macro.RemoteInstance.uniqueId,
identifier: _namedTypeIdentifier(node),
isNullable: node.question != null,
Expand Down Expand Up @@ -1715,7 +1715,7 @@ class DeclarationBuilderFromNode {
}
}

return macro.FunctionTypeAnnotationImpl(
return _FunctionTypeAnnotation(
id: macro.RemoteInstance.uniqueId,
isNullable: node.question != null,
namedParameters: namedParameters,
Expand All @@ -1725,58 +1725,61 @@ class DeclarationBuilderFromNode {
ReturnTypeLocation(location),
),
typeParameters: _typeParameters(node.typeParameters),
location: location,
);
case ast.NamedTypeImpl():
return _namedType(node, location);
case ast.RecordTypeAnnotationImpl():
return _typeAnnotationRecord(node);
return _typeAnnotationRecord(node, location);
}
}

macro.TypeAnnotationImpl _typeAnnotationFunctionReturnType(
ast.FunctionDeclaration node,
) {
final element = node.declaredElement!;
var location = ReturnTypeLocation(
ElementTypeLocation(element),
);

final returnType = node.returnType;
if (returnType == null) {
return _OmittedTypeAnnotationFunctionReturnType(element);
return _OmittedTypeAnnotationFunctionReturnType(element, location);
}
return _typeAnnotation(
returnType,
ReturnTypeLocation(
ElementTypeLocation(element),
),
);

return _typeAnnotation(returnType, location);
}

macro.TypeAnnotationImpl _typeAnnotationMethodReturnType(
ast.MethodDeclaration node,
) {
final element = node.declaredElement!;

var location = ReturnTypeLocation(
ElementTypeLocation(element),
);

final returnType = node.returnType;
if (returnType == null) {
return _OmittedTypeAnnotationFunctionReturnType(element);
return _OmittedTypeAnnotationFunctionReturnType(element, location);
}
return _typeAnnotation(
returnType,
ReturnTypeLocation(
ElementTypeLocation(element),
),
);

return _typeAnnotation(returnType, location);
}

macro.TypeAnnotationImpl _typeAnnotationOrDynamic(
ast.TypeAnnotation? node,
TypeAnnotationLocation location,
) {
if (node == null) {
return _OmittedTypeAnnotationDynamic();
return _OmittedTypeAnnotationDynamic(location);
}
return _typeAnnotation(node, location);
}

macro.RecordTypeAnnotationImpl _typeAnnotationRecord(
ast.RecordTypeAnnotation node,
TypeAnnotationLocation location,
) {
final unitNode = node.thisOrAncestorOfType<ast.CompilationUnit>()!;
final unitElement = unitNode.declaredElement!;
Expand All @@ -1801,7 +1804,7 @@ class DeclarationBuilderFromNode {
);
}

return macro.RecordTypeAnnotationImpl(
return _RecordTypeAnnotation(
id: macro.RemoteInstance.uniqueId,
positionalFields: node.positionalFields.indexed.map((pair) {
return buildField(
Expand All @@ -1817,6 +1820,7 @@ class DeclarationBuilderFromNode {
}).toList() ??
[],
isNullable: node.question != null,
location: location,
);
}

Expand All @@ -1839,15 +1843,13 @@ class DeclarationBuilderFromNode {
macro.TypeAnnotationImpl _typeAnnotationVariable(
ast.TypeAnnotation? type,
VariableElement element,
TypeAnnotationLocation location,
TypeAnnotationLocation parentLocation,
) {
var location = VariableTypeLocation(parentLocation);
if (type == null) {
return _OmittedTypeAnnotationVariable(element);
return _OmittedTypeAnnotationVariable(element, location);
}
return _typeAnnotation(
type,
VariableTypeLocation(location),
);
return _typeAnnotation(type, location);
}

macro.TypeParameterDeclarationImpl _typeParameter(
Expand Down Expand Up @@ -2094,16 +2096,8 @@ class MixinDeclarationImpl extends macro.MixinDeclarationImpl
});
}

class NamedTypeAnnotation extends macro.NamedTypeAnnotationImpl {
final TypeAnnotationLocation location;

NamedTypeAnnotation({
required super.id,
required super.isNullable,
required super.identifier,
required super.typeArguments,
required this.location,
});
abstract class TypeAnnotationWithLocation implements macro.TypeAnnotation {
TypeAnnotationLocation get location;
}

class VariableDeclarationImpl extends macro.VariableDeclarationImpl
Expand Down Expand Up @@ -2135,6 +2129,36 @@ class _DeclaredIdentifierImpl extends IdentifierImpl {
});
}

class _FunctionTypeAnnotation extends macro.FunctionTypeAnnotationImpl
implements TypeAnnotationWithLocation {
@override
final TypeAnnotationLocation location;

_FunctionTypeAnnotation({
required super.id,
required super.isNullable,
required super.namedParameters,
required super.positionalParameters,
required super.returnType,
required super.typeParameters,
required this.location,
});
}

class _NamedTypeAnnotation extends macro.NamedTypeAnnotationImpl
implements TypeAnnotationWithLocation {
@override
final TypeAnnotationLocation location;

_NamedTypeAnnotation({
required super.id,
required super.isNullable,
required super.identifier,
required super.typeArguments,
required this.location,
});
}

class _NamedTypeIdentifierImpl extends IdentifierImpl {
final ast.NamedType node;

Expand All @@ -2155,20 +2179,46 @@ sealed class _OmittedTypeAnnotation extends macro.OmittedTypeAnnotationImpl {
);
}

class _OmittedTypeAnnotationDynamic extends _OmittedTypeAnnotation {
_OmittedTypeAnnotationDynamic();
class _OmittedTypeAnnotationDynamic extends _OmittedTypeAnnotation
implements TypeAnnotationWithLocation {
@override
final TypeAnnotationLocation location;

_OmittedTypeAnnotationDynamic(this.location);
}

class _OmittedTypeAnnotationFunctionReturnType extends _OmittedTypeAnnotation {
class _OmittedTypeAnnotationFunctionReturnType extends _OmittedTypeAnnotation
implements TypeAnnotationWithLocation {
final ExecutableElement element;

_OmittedTypeAnnotationFunctionReturnType(this.element);
@override
final TypeAnnotationLocation location;

_OmittedTypeAnnotationFunctionReturnType(this.element, this.location);
}

class _OmittedTypeAnnotationVariable extends _OmittedTypeAnnotation {
class _OmittedTypeAnnotationVariable extends _OmittedTypeAnnotation
implements TypeAnnotationWithLocation {
final VariableElement element;

_OmittedTypeAnnotationVariable(this.element);
@override
final TypeAnnotationLocation location;

_OmittedTypeAnnotationVariable(this.element, this.location);
}

class _RecordTypeAnnotation extends macro.RecordTypeAnnotationImpl
implements TypeAnnotationWithLocation {
@override
final TypeAnnotationLocation location;

_RecordTypeAnnotation({
required super.id,
required super.isNullable,
required super.namedFields,
required super.positionalFields,
required this.location,
});
}

class _VoidIdentifierImpl extends IdentifierImpl {
Expand Down
Loading

0 comments on commit 423e769

Please sign in to comment.