44
55import 'package:meta/meta.dart' ;
66
7+ import 'definition.dart' ;
78import 'helper.dart' ;
89import 'serialization_context.dart' ;
910import 'syntax.g.dart' ;
@@ -80,11 +81,13 @@ abstract class Constant extends MaybeConstant {
8081 )
8182 .toList (),
8283 ),
83- InstanceConstantSyntax (value: final value) => InstanceConstant (
84- fields: (value? .json ?? {}).map (
85- (key, value) => MapEntry (key, context.constants[value as int ]),
84+ InstanceConstantSyntax (value: final value, : final definitionIndex) =>
85+ InstanceConstant (
86+ definition: context.definitions[definitionIndex],
87+ fields: (value? .json ?? {}).map (
88+ (key, value) => MapEntry (key, context.constants[value as int ]),
89+ ),
8690 ),
87- ),
8891 UnsupportedConstantSyntax (: final message) => UnsupportedConstant (message),
8992 _ => throw UnimplementedError (
9093 '"${syntax .type }" is not a supported constant type' ,
@@ -371,15 +374,20 @@ final class MapConstant extends Constant {
371374/// Only as far as they can also be represented by constants. This is more or
372375/// less the same as a [MapConstant] .
373376final class InstanceConstant extends Constant {
377+ /// The definition of the class of this instance.
378+ final Definition definition;
379+
374380 /// The fields of this instance, mapped from field name to [Constant] value.
375381 final Map <String , Constant > fields;
376382
377- /// Creates an [InstanceConstant] object with the given [fields] .
378- const InstanceConstant ({required this .fields});
383+ /// Creates an [InstanceConstant] object with the given [definition] and
384+ /// [fields] .
385+ const InstanceConstant ({required this .definition, required this .fields});
379386
380387 @override
381388 InstanceConstantSyntax _toSyntax (SerializationContext context) =>
382389 InstanceConstantSyntax (
390+ definitionIndex: context.definitions[definition]! ,
383391 value: fields.isNotEmpty
384392 ? JsonObjectSyntax .fromJson (
385393 fields.map (
@@ -394,15 +402,17 @@ final class InstanceConstant extends Constant {
394402 bool operator == (Object other) {
395403 if (identical (this , other)) return true ;
396404
397- return other is InstanceConstant && deepEquals (other.fields, fields);
405+ return other is InstanceConstant &&
406+ other.definition == definition &&
407+ deepEquals (other.fields, fields);
398408 }
399409
400410 @override
401- int get hashCode => deepHash (fields);
411+ int get hashCode => Object . hash (definition, deepHash (fields) );
402412
403413 @override
404414 String toString () =>
405- 'InstanceConstant({'
415+ 'InstanceConstant($ definition , {'
406416 '${fields .entries .map ((e ) => '${e .key }: ${e .value }' ).join (', ' )}})' ;
407417
408418 @override
@@ -411,6 +421,8 @@ final class InstanceConstant extends Constant {
411421 bool allowPromotionOfUnsupported,
412422 ) {
413423 if (other is ! InstanceConstant ) return false ;
424+ // ignore: invalid_use_of_visible_for_testing_member
425+ if (! definition.semanticEquals (other.definition)) return false ;
414426 if (fields.length != other.fields.length) return false ;
415427 for (final entry in fields.entries) {
416428 final otherField = other.fields[entry.key];
0 commit comments