[macros] Remove Declaration from RecordField
This removes the Declaration from RecordFieldDeclaration and renames it
to RecordField. The fields of a record type are not declarations but
are similar to FormalParameter of a FunctionTypeAnnotation; they contain
part of the information for the RecordTypeAnnotation but are not
identifiable on their own.
Change-Id: Iaa9d95d49f2152096b970c6e2d24c524327f933e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/355740
Commit-Queue: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Jake Macdonald <jakemac@google.com>
diff --git a/pkg/_fe_analyzer_shared/lib/src/macros/api/introspection.dart b/pkg/_fe_analyzer_shared/lib/src/macros/api/introspection.dart
index f9fea338..20a3d01 100644
--- a/pkg/_fe_analyzer_shared/lib/src/macros/api/introspection.dart
+++ b/pkg/_fe_analyzer_shared/lib/src/macros/api/introspection.dart
@@ -66,13 +66,13 @@
Iterable<TypeAnnotation> get typeArguments;
}
-/// The interface for record type declarations.
+/// The interface for record type annotations.
abstract interface class RecordTypeAnnotation implements TypeAnnotation {
/// The positional fields for this record.
- Iterable<RecordFieldDeclaration> get positionalFields;
+ Iterable<RecordField> get positionalFields;
/// The named fields for this record.
- Iterable<RecordFieldDeclaration> get namedFields;
+ Iterable<RecordField> get namedFields;
}
/// An omitted type annotation.
@@ -374,13 +374,12 @@
abstract interface class TypeParameterDeclaration
implements TypeDeclaration, TypeParameter {}
-/// Introspection information for a field declaration on a Record type.
+/// Introspection information for a field on a Record type.
///
/// Note that for positional fields the [identifier] will be the synthesized
/// one (`$1` etc), while for named fields it will be the declared name.
-abstract interface class RecordFieldDeclaration implements Declaration {
- /// A convenience method to get a `code` object equivalent to this field
- /// declaration.
+abstract interface class RecordField {
+ /// A convenience method to get a `code` object equivalent to this field.
RecordFieldCode get code;
/// Record fields don't always have names (if they are positional).
diff --git a/pkg/_fe_analyzer_shared/lib/src/macros/executor/introspection_impls.dart b/pkg/_fe_analyzer_shared/lib/src/macros/executor/introspection_impls.dart
index b241154..6af7dc8 100644
--- a/pkg/_fe_analyzer_shared/lib/src/macros/executor/introspection_impls.dart
+++ b/pkg/_fe_analyzer_shared/lib/src/macros/executor/introspection_impls.dart
@@ -90,21 +90,19 @@
@override
TypeAnnotationCode get code {
RecordTypeAnnotationCode underlyingType = new RecordTypeAnnotationCode(
- namedFields: [
- for (RecordFieldDeclarationImpl field in namedFields) field.code
- ],
+ namedFields: [for (RecordFieldImpl field in namedFields) field.code],
positionalFields: [
- for (RecordFieldDeclarationImpl field in positionalFields) field.code
+ for (RecordFieldImpl field in positionalFields) field.code
],
);
return isNullable ? underlyingType.asNullable : underlyingType;
}
@override
- final List<RecordFieldDeclarationImpl> namedFields;
+ final List<RecordFieldImpl> namedFields;
@override
- final List<RecordFieldDeclarationImpl> positionalFields;
+ final List<RecordFieldImpl> positionalFields;
@override
RemoteInstanceKind get kind => RemoteInstanceKind.recordTypeAnnotation;
@@ -121,25 +119,20 @@
super.serializeUncached(serializer);
serializer.startList();
- for (RecordFieldDeclarationImpl field in namedFields) {
+ for (RecordFieldImpl field in namedFields) {
field.serialize(serializer);
}
serializer.endList();
serializer.startList();
- for (RecordFieldDeclarationImpl field in positionalFields) {
+ for (RecordFieldImpl field in positionalFields) {
field.serialize(serializer);
}
serializer.endList();
}
}
-// TODO: Currently the `name` is duplicated (if present) in both the
-// `identifier` and the `name` fields, because for positional fields they will
-// not be the same. We could optimize it to read the name from the `identifier`
-// field for named record fields though.
-class RecordFieldDeclarationImpl extends DeclarationImpl
- implements RecordFieldDeclaration {
+class RecordFieldImpl extends RemoteInstance implements RecordField {
@override
RecordFieldCode get code {
return new RecordFieldCode(type: type.code, name: name);
@@ -152,16 +145,13 @@
final TypeAnnotationImpl type;
@override
- RemoteInstanceKind get kind => RemoteInstanceKind.recordFieldDeclaration;
+ RemoteInstanceKind get kind => RemoteInstanceKind.recordField;
- RecordFieldDeclarationImpl({
- required super.id,
- required super.identifier,
- required super.library,
- required super.metadata,
+ RecordFieldImpl({
+ required int id,
required this.name,
required this.type,
- });
+ }) : super(id);
@override
void serializeUncached(Serializer serializer) {
diff --git a/pkg/_fe_analyzer_shared/lib/src/macros/executor/remote_instance.dart b/pkg/_fe_analyzer_shared/lib/src/macros/executor/remote_instance.dart
index b2715cd..b951792 100644
--- a/pkg/_fe_analyzer_shared/lib/src/macros/executor/remote_instance.dart
+++ b/pkg/_fe_analyzer_shared/lib/src/macros/executor/remote_instance.dart
@@ -121,7 +121,7 @@
namedStaticType,
namedTypeAnnotation,
omittedTypeAnnotation,
- recordFieldDeclaration,
+ recordField,
recordTypeAnnotation,
staticType,
typeAliasDeclaration,
diff --git a/pkg/_fe_analyzer_shared/lib/src/macros/executor/serialization_extensions.dart b/pkg/_fe_analyzer_shared/lib/src/macros/executor/serialization_extensions.dart
index f32b74f..470574d 100644
--- a/pkg/_fe_analyzer_shared/lib/src/macros/executor/serialization_extensions.dart
+++ b/pkg/_fe_analyzer_shared/lib/src/macros/executor/serialization_extensions.dart
@@ -64,8 +64,8 @@
(this..moveNext())._expectOmittedTypeAnnotation(id),
RemoteInstanceKind.formalParameterDeclaration =>
(this..moveNext())._expectFormalParameterDeclaration(id),
- RemoteInstanceKind.recordFieldDeclaration =>
- (this..moveNext())._expectRecordFieldDeclaration(id),
+ RemoteInstanceKind.recordField =>
+ (this..moveNext())._expectRecordField(id),
RemoteInstanceKind.recordTypeAnnotation =>
(this..moveNext())._expectRecordTypeAnnotation(id),
RemoteInstanceKind.typeAliasDeclaration =>
@@ -165,14 +165,10 @@
type: RemoteInstance.deserialize(this),
);
- RecordFieldDeclarationImpl _expectRecordFieldDeclaration(int id) =>
- new RecordFieldDeclarationImpl(
- id: id,
- identifier: expectRemoteInstance(),
- library: RemoteInstance.deserialize(this),
- metadata: (this..moveNext())._expectRemoteInstanceList(),
- name: (this..moveNext()).expectNullableString(),
- type: (this..moveNext()).expectRemoteInstance());
+ RecordFieldImpl _expectRecordField(int id) => new RecordFieldImpl(
+ id: id,
+ name: expectNullableString(),
+ type: (this..moveNext()).expectRemoteInstance());
RecordTypeAnnotationImpl _expectRecordTypeAnnotation(int id) =>
new RecordTypeAnnotationImpl(
diff --git a/pkg/_fe_analyzer_shared/test/macros/executor/serialization_test.dart b/pkg/_fe_analyzer_shared/test/macros/executor/serialization_test.dart
index b896d6c..61ddecb 100644
--- a/pkg/_fe_analyzer_shared/test/macros/executor/serialization_test.dart
+++ b/pkg/_fe_analyzer_shared/test/macros/executor/serialization_test.dart
@@ -472,23 +472,15 @@
id: RemoteInstance.uniqueId,
isNullable: rand.nextBool(),
namedFields: [
- RecordFieldDeclarationImpl(
+ RecordFieldImpl(
id: RemoteInstance.uniqueId,
- identifier:
- IdentifierImpl(id: RemoteInstance.uniqueId, name: 'hello'),
- library: Fixtures.library,
- metadata: [],
name: 'hello',
type: barType,
),
],
positionalFields: [
- RecordFieldDeclarationImpl(
+ RecordFieldImpl(
id: RemoteInstance.uniqueId,
- identifier:
- IdentifierImpl(id: RemoteInstance.uniqueId, name: r'$1'),
- library: Fixtures.library,
- metadata: [],
name: rand.nextBool() ? null : 'zoiks',
type: fooType,
),
diff --git a/pkg/_fe_analyzer_shared/test/macros/util.dart b/pkg/_fe_analyzer_shared/test/macros/util.dart
index df29629..2b59947 100644
--- a/pkg/_fe_analyzer_shared/test/macros/util.dart
+++ b/pkg/_fe_analyzer_shared/test/macros/util.dart
@@ -351,32 +351,14 @@
id: RemoteInstance.uniqueId,
isNullable: false,
namedFields: [
- RecordFieldDeclarationImpl(
- id: RemoteInstance.uniqueId,
- identifier:
- IdentifierImpl(id: RemoteInstance.uniqueId, name: 'world'),
- library: Fixtures.library,
- metadata: [],
- name: 'world',
- type: stringType),
+ RecordFieldImpl(
+ id: RemoteInstance.uniqueId, name: 'world', type: stringType),
],
positionalFields: [
- RecordFieldDeclarationImpl(
- id: RemoteInstance.uniqueId,
- identifier:
- IdentifierImpl(id: RemoteInstance.uniqueId, name: r'$1'),
- library: Fixtures.library,
- metadata: [],
- name: null,
- type: stringType),
- RecordFieldDeclarationImpl(
- id: RemoteInstance.uniqueId,
- identifier:
- IdentifierImpl(id: RemoteInstance.uniqueId, name: r'$2'),
- library: Fixtures.library,
- metadata: [],
- name: 'hello',
- type: nullableBoolType),
+ RecordFieldImpl(
+ id: RemoteInstance.uniqueId, name: null, type: stringType),
+ RecordFieldImpl(
+ id: RemoteInstance.uniqueId, name: 'hello', type: nullableBoolType),
]);
// Top level, non-class declarations.
diff --git a/pkg/analyzer/lib/src/summary2/macro_declarations.dart b/pkg/analyzer/lib/src/summary2/macro_declarations.dart
index 96dfee1..b716ddc 100644
--- a/pkg/analyzer/lib/src/summary2/macro_declarations.dart
+++ b/pkg/analyzer/lib/src/summary2/macro_declarations.dart
@@ -1911,24 +1911,13 @@
ast.RecordTypeAnnotation node,
TypeAnnotationLocation location,
) {
- final unitNode = node.thisOrAncestorOfType<ast.CompilationUnit>()!;
- final unitElement = unitNode.declaredElement!;
- final macroLibrary = library(unitElement);
-
- macro.RecordFieldDeclarationImpl buildField(
+ macro.RecordFieldImpl buildField(
ast.RecordTypeAnnotationField field,
TypeAnnotationLocation location,
) {
final name = field.name?.lexeme ?? '';
- return macro.RecordFieldDeclarationImpl(
+ return macro.RecordFieldImpl(
id: macro.RemoteInstance.uniqueId,
- identifier: IdentifierImplFromNode(
- id: macro.RemoteInstance.uniqueId,
- name: name,
- getElement: () => null,
- ),
- library: macroLibrary,
- metadata: const [],
name: name,
type: _typeAnnotationOrDynamic(field.type, location),
);
diff --git a/pkg/front_end/lib/src/fasta/kernel/macro/types.dart b/pkg/front_end/lib/src/fasta/kernel/macro/types.dart
index 6f256e7..db52b55 100644
--- a/pkg/front_end/lib/src/fasta/kernel/macro/types.dart
+++ b/pkg/front_end/lib/src/fasta/kernel/macro/types.dart
@@ -112,9 +112,9 @@
}
}
- /// Creates the [macro.RecordFieldDeclarationImpl]s corresponding to [fields]
+ /// Creates the [macro.RecordFieldImpl]s corresponding to [fields]
/// occurring in [libraryBuilder].
- List<macro.RecordFieldDeclarationImpl> _createRecordFields(
+ List<macro.RecordFieldImpl> _createRecordFields(
LibraryBuilder libraryBuilder, List<RecordTypeFieldBuilder>? fields) {
// TODO(johnniwinther): Support record fields once they are not required to
// be declarations.