[cfe] Pass ReferenceFromIndex to value class transformation
This fixes fuzz testing for value_class/empty,
value_class/explicit_mixin, and value_class/value_mixin_error
Change-Id: Ia098b3ed5b8a028f1a28c5579fe5df11e5a92ff0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/217185
Reviewed-by: Jens Johansen <jensj@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
diff --git a/pkg/front_end/lib/src/fasta/kernel/kernel_target.dart b/pkg/front_end/lib/src/fasta/kernel/kernel_target.dart
index 3eac256..9e0e011 100644
--- a/pkg/front_end/lib/src/fasta/kernel/kernel_target.dart
+++ b/pkg/front_end/lib/src/fasta/kernel/kernel_target.dart
@@ -1407,8 +1407,8 @@
if (loader.target.context.options
.isExperimentEnabledGlobally(ExperimentalFlag.valueClass)) {
- valueClass.transformComponent(
- component!, loader.coreTypes, loader.hierarchy, environment);
+ valueClass.transformComponent(component!, loader.coreTypes,
+ loader.hierarchy, loader.referenceFromIndex, environment);
ticker.logMs("Lowered value classes");
}
diff --git a/pkg/kernel/lib/transformations/value_class.dart b/pkg/kernel/lib/transformations/value_class.dart
index 2a96ffd..f69a386 100644
--- a/pkg/kernel/lib/transformations/value_class.dart
+++ b/pkg/kernel/lib/transformations/value_class.dart
@@ -9,6 +9,7 @@
import '../ast.dart';
import '../core_types.dart' show CoreTypes;
import '../class_hierarchy.dart' show ClassHierarchy;
+import '../reference_from_index.dart';
import './scanner.dart';
class ValueClassScanner extends ClassScanner<Null> {
@@ -64,12 +65,23 @@
}
}
-void transformComponent(Component node, CoreTypes coreTypes,
- ClassHierarchy hierarchy, TypeEnvironment typeEnvironment) {
+void transformComponent(
+ Component node,
+ CoreTypes coreTypes,
+ ClassHierarchy hierarchy,
+ ReferenceFromIndex? referenceFromIndex,
+ TypeEnvironment typeEnvironment) {
ValueClassScanner scanner = new ValueClassScanner();
ScanResult<Class, Null> valueClasses = scanner.scan(node);
for (Class valueClass in valueClasses.targets.keys) {
- transformValueClass(valueClass, coreTypes, hierarchy, typeEnvironment);
+ transformValueClass(
+ valueClass,
+ coreTypes,
+ hierarchy,
+ referenceFromIndex
+ ?.lookupLibrary(valueClass.enclosingLibrary)
+ ?.lookupIndexedClass(valueClass.name),
+ typeEnvironment);
}
treatCopyWithCallSites(node, coreTypes, typeEnvironment, hierarchy);
@@ -79,8 +91,12 @@
}
}
-void transformValueClass(Class cls, CoreTypes coreTypes,
- ClassHierarchy hierarchy, TypeEnvironment typeEnvironment) {
+void transformValueClass(
+ Class cls,
+ CoreTypes coreTypes,
+ ClassHierarchy hierarchy,
+ IndexedClass? indexedClass,
+ TypeEnvironment typeEnvironment) {
Constructor? syntheticConstructor = null;
for (Constructor constructor in cls.constructors) {
if (constructor.isSynthetic) {
@@ -93,11 +109,11 @@
allVariablesList.sort((a, b) => a.name!.compareTo(b.name!));
addConstructor(cls, coreTypes, syntheticConstructor!);
- addEqualsOperator(cls, coreTypes, hierarchy, allVariablesList);
- addHashCode(cls, coreTypes, hierarchy, allVariablesList);
- addToString(cls, coreTypes, hierarchy, allVariablesList);
- addCopyWith(cls, coreTypes, hierarchy, allVariablesList, syntheticConstructor,
- typeEnvironment);
+ addEqualsOperator(cls, coreTypes, hierarchy, indexedClass, allVariablesList);
+ addHashCode(cls, coreTypes, hierarchy, indexedClass, allVariablesList);
+ addToString(cls, coreTypes, hierarchy, indexedClass, allVariablesList);
+ addCopyWith(cls, coreTypes, hierarchy, indexedClass, allVariablesList,
+ syntheticConstructor, typeEnvironment);
}
void addConstructor(
@@ -137,7 +153,7 @@
}
void addEqualsOperator(Class cls, CoreTypes coreTypes, ClassHierarchy hierarchy,
- List<VariableDeclaration> allVariablesList) {
+ IndexedClass? indexedClass, List<VariableDeclaration> allVariablesList) {
List<VariableDeclaration> allVariables = allVariablesList.toList();
for (Procedure procedure in cls.procedures) {
if (procedure.kind == ProcedureKind.Operator &&
@@ -167,8 +183,9 @@
targets[variable] = target;
}
+ Name name = Name("==");
Procedure equalsOperator = Procedure(
- Name("=="),
+ name,
ProcedureKind.Operator,
FunctionNode(
ReturnStatement(allVariables
@@ -184,13 +201,14 @@
previousValue!, LogicalExpressionOperator.AND, element))),
returnType: returnType,
positionalParameters: [other]),
- fileUri: cls.fileUri)
+ fileUri: cls.fileUri,
+ reference: indexedClass?.lookupGetterReference(name))
..fileOffset = cls.fileOffset;
cls.addProcedure(equalsOperator);
}
void addHashCode(Class cls, CoreTypes coreTypes, ClassHierarchy hierarchy,
- List<VariableDeclaration> allVariablesList) {
+ IndexedClass? indexedClass, List<VariableDeclaration> allVariablesList) {
List<VariableDeclaration> allVariables = allVariablesList.toList();
for (Procedure procedure in cls.procedures) {
if (procedure.kind == ProcedureKind.Getter &&
@@ -234,8 +252,9 @@
targetsHashcode[variable] = targetHashcode;
targets[variable] = target;
}
+ Name name = Name("hashCode");
cls.addProcedure(Procedure(
- Name("hashCode"),
+ name,
ProcedureKind.Getter,
FunctionNode(
ReturnStatement(StaticInvocation(
@@ -259,12 +278,13 @@
hashCombine!, Arguments([previousValue, element])))
]))),
returnType: returnType),
- fileUri: cls.fileUri)
+ fileUri: cls.fileUri,
+ reference: indexedClass?.lookupGetterReference(name))
..fileOffset = cls.fileOffset);
}
void addToString(Class cls, CoreTypes coreTypes, ClassHierarchy hierarchy,
- List<VariableDeclaration> allVariablesList) {
+ IndexedClass? indexedClass, List<VariableDeclaration> allVariablesList) {
List<Expression> wording = [StringLiteral("${cls.name}(")];
for (VariableDeclaration variable in allVariablesList) {
@@ -291,12 +311,14 @@
}
DartType returnType =
coreTypes.stringRawType(cls.enclosingLibrary.nonNullable);
+ Name name = Name("toString");
cls.addProcedure(Procedure(
- Name("toString"),
+ name,
ProcedureKind.Method,
FunctionNode(ReturnStatement(StringConcatenation(wording)),
returnType: returnType),
- fileUri: cls.fileUri)
+ fileUri: cls.fileUri,
+ reference: indexedClass?.lookupGetterReference(name))
..fileOffset = cls.fileOffset);
}
@@ -304,6 +326,7 @@
Class cls,
CoreTypes coreTypes,
ClassHierarchy hierarchy,
+ IndexedClass? indexedClass,
List<VariableDeclaration> allVariablesList,
Constructor syntheticConstructor,
TypeEnvironment typeEnvironment) {
@@ -324,8 +347,9 @@
targets[variable] = target;
}
+ Name name = Name("copyWith");
cls.addProcedure(Procedure(
- Name("copyWith"),
+ name,
ProcedureKind.Method,
FunctionNode(
ReturnStatement(ConstructorInvocation(
@@ -335,7 +359,8 @@
.map((f) => NamedExpression(f.name!, VariableGet(f)))
.toList()))),
namedParameters: allVariables),
- fileUri: cls.fileUri)
+ fileUri: cls.fileUri,
+ reference: indexedClass?.lookupGetterReference(name))
..fileOffset = cls.fileOffset);
}