Macro. Invoke typeDeclarationOf() for all TypeDeclaration(s).

Also include 'stackTrace' for all MacroImplementationExceptionImpl.

I reverted the same instance for type alias, we don't do it in general.
These instances do change as we run macros, e.g. append annotations.

Change-Id: I5a9f03722ba19ef5a4aada88fb044779327305bc
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/354064
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Konstantin Shcheglov 2024-02-23 23:08:48 +00:00 committed by Commit Queue
parent e5520938b1
commit 93b4bfcfff
3 changed files with 59 additions and 39 deletions

View file

@ -1243,11 +1243,14 @@ class _TypePhaseIntrospector implements macro.TypePhaseIntrospector {
element = element.variable;
}
if (element == null) {
throw macro.MacroImplementationExceptionImpl([
'Unresolved identifier.',
'library: $library',
'name: $name',
].join('\n'));
throw macro.MacroImplementationExceptionImpl(
[
'Unresolved identifier.',
'library: $library',
'name: $name',
].join('\n'),
stackTrace: StackTrace.current.toString(),
);
}
return declarationBuilder.fromElement.identifier(element);
}

View file

@ -141,13 +141,18 @@ class DeclarationBuilder {
/// See [macro.DefinitionPhaseIntrospector.declarationOf].
macro.DeclarationImpl declarationOf(macro.Identifier identifier) {
if (identifier is! IdentifierImpl) {
throw macro.MacroImplementationExceptionImpl('Not analyzer identifier.');
throw macro.MacroImplementationExceptionImpl(
'Not analyzer identifier.',
stackTrace: StackTrace.current.toString(),
);
}
final element = identifier.element;
if (element == null) {
throw macro.MacroImplementationExceptionImpl(
'Identifier without element.');
'Identifier without element.',
stackTrace: StackTrace.current.toString(),
);
}
return declarationOfElement(element);
@ -319,7 +324,10 @@ class DeclarationBuilder {
case macro.OmittedTypeAnnotationCode():
return _resolveTypeCodeOmitted(typeCode);
case macro.RawTypeAnnotationCode():
throw macro.MacroImplementationExceptionImpl('Not supported');
throw macro.MacroImplementationExceptionImpl(
'Not supported',
stackTrace: StackTrace.current.toString(),
);
case macro.RecordTypeAnnotationCode():
return _resolveTypeCodeRecord(typeCode);
}
@ -328,13 +336,18 @@ class DeclarationBuilder {
/// See [macro.DeclarationPhaseIntrospector.typeDeclarationOf].
macro.TypeDeclarationImpl typeDeclarationOf(macro.Identifier identifier) {
if (identifier is! IdentifierImpl) {
throw macro.MacroImplementationExceptionImpl('Not analyzer identifier.');
throw macro.MacroImplementationExceptionImpl(
'Not analyzer identifier.',
stackTrace: StackTrace.current.toString(),
);
}
final element = identifier.element;
if (element == null) {
throw macro.MacroImplementationExceptionImpl(
'Identifier without element.');
'Identifier without element.',
stackTrace: StackTrace.current.toString(),
);
}
final node = nodeOfElement(element);
@ -736,7 +749,9 @@ class DeclarationBuilderFromElement {
default:
// TODO(scheglov): other elements
throw macro.MacroImplementationExceptionImpl(
'element: (${element.runtimeType}) $element');
'element: (${element.runtimeType}) $element',
stackTrace: StackTrace.current.toString(),
);
}
}
@ -1112,9 +1127,6 @@ class DeclarationBuilderFromNode {
final Map<Element, LibraryImpl> _libraryMap = Map.identity();
final Map<ast.GenericTypeAliasImpl, macro.TypeAliasDeclarationImpl>
_typeAliasDeclarationMap = Map.identity();
DeclarationBuilderFromNode(this.builder);
ClassDeclarationImpl classDeclaration(
@ -1472,7 +1484,17 @@ class DeclarationBuilderFromNode {
macro.TypeAliasDeclarationImpl typeAliasDeclaration(
ast.GenericTypeAliasImpl node,
) {
return _typeAliasDeclarationMap[node] ??= _typeAliasDeclaration(node);
final element = node.declaredElement as TypeAliasElementImpl;
return TypeAliasDeclarationImpl._(
id: macro.RemoteInstance.uniqueId,
element: element,
identifier: _declaredIdentifier(node.name, element),
library: library(element),
metadata: _buildMetadata(element),
aliasedType: _typeAnnotationAliasedType(node),
typeParameters: _typeParameterDeclarations(node.typeParameters),
);
}
/// See [macro.DeclarationPhaseIntrospector.typeDeclarationOf].
@ -1495,7 +1517,9 @@ class DeclarationBuilderFromNode {
default:
// TODO(scheglov): other nodes
throw macro.MacroImplementationExceptionImpl(
'node: (${node.runtimeType}) $node');
'node: (${node.runtimeType}) $node',
stackTrace: StackTrace.current.toString(),
);
}
}
@ -1814,22 +1838,6 @@ class DeclarationBuilderFromNode {
}
}
macro.TypeAliasDeclarationImpl _typeAliasDeclaration(
ast.GenericTypeAliasImpl node,
) {
final element = node.declaredElement as TypeAliasElementImpl;
return TypeAliasDeclarationImpl._(
id: macro.RemoteInstance.uniqueId,
element: element,
identifier: _declaredIdentifier(node.name, element),
library: library(element),
metadata: _buildMetadata(element),
aliasedType: _typeAnnotationAliasedType(node),
typeParameters: _typeParameterDeclarations(node.typeParameters),
);
}
macro.TypeAnnotationImpl _typeAnnotation(
ast.TypeAnnotation node,
TypeAnnotationLocation location,

View file

@ -36,6 +36,8 @@ import 'package:_fe_analyzer_shared/src/macros/api.dart';
ClassDeclaration declaration,
MemberDeclarationBuilder builder,
) async {
await _typeDeclarationOf(declaration, builder);
await _write(builder, declaration, (printer) async {
await printer.writeClassDeclaration(declaration);
});
@ -56,6 +58,8 @@ import 'package:_fe_analyzer_shared/src/macros/api.dart';
EnumDeclaration declaration,
EnumDeclarationBuilder builder,
) async {
await _typeDeclarationOf(declaration, builder);
await _write(builder, declaration, (printer) async {
await printer.writeEnumDeclaration(declaration);
});
@ -76,6 +80,8 @@ import 'package:_fe_analyzer_shared/src/macros/api.dart';
ExtensionDeclaration declaration,
MemberDeclarationBuilder builder,
) async {
await _typeDeclarationOf(declaration, builder);
await _write(builder, declaration, (printer) async {
await printer.writeExtensionDeclaration(declaration);
});
@ -86,6 +92,8 @@ import 'package:_fe_analyzer_shared/src/macros/api.dart';
ExtensionTypeDeclaration declaration,
MemberDeclarationBuilder builder,
) async {
await _typeDeclarationOf(declaration, builder);
await _write(builder, declaration, (printer) async {
await printer.writeExtensionTypeDeclaration(declaration);
});
@ -159,6 +167,8 @@ import 'package:_fe_analyzer_shared/src/macros/api.dart';
MixinDeclaration declaration,
MemberDeclarationBuilder builder,
) async {
await _typeDeclarationOf(declaration, builder);
await _write(builder, declaration, (printer) async {
await printer.writeMixinDeclaration(declaration);
});
@ -169,7 +179,8 @@ import 'package:_fe_analyzer_shared/src/macros/api.dart';
TypeAliasDeclaration declaration,
DeclarationBuilder builder,
) async {
await _typeDeclarationOfSelf(declaration, builder);
await _typeDeclarationOf(declaration, builder);
await _write(builder, declaration, (printer) async {
await printer.writeTypeAliasDeclaration(declaration);
});
@ -193,15 +204,13 @@ import 'package:_fe_analyzer_shared/src/macros/api.dart';
);
}
Future<void> _typeDeclarationOfSelf(
Declaration declaration,
Future<void> _typeDeclarationOf(
TypeDeclaration declaration,
DeclarationBuilder builder,
) async {
var identifier = declaration.identifier;
var self = await builder.typeDeclarationOf(identifier);
if (!identical(self, declaration)) {
throw StateError('Expected to be the same.');
}
await builder.typeDeclarationOf(identifier);
// No check, just don't crash.
}
Future<void> _write(