From 4dc8f1f9f51f9126aa50bad1736249bc3d8a0623 Mon Sep 17 00:00:00 2001 From: mbojey2 Date: Wed, 21 Jul 2021 10:30:05 -0700 Subject: [PATCH] Use correct class when reviving from a static field (#546) Fixes #533 Previously when searching for a static field matching a constant object we would use the class name from the type of the object always instead of the class name in which we found the field. This worked in many cases because constant static fields intended for use in other libraries are more often defined on the same class as their type. --- source_gen/lib/src/constants/revive.dart | 19 ++++++++++--------- source_gen/test/constants_test.dart | 12 ++++++++++-- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/source_gen/lib/src/constants/revive.dart b/source_gen/lib/src/constants/revive.dart index 95cdfb31..0e922230 100644 --- a/source_gen/lib/src/constants/revive.dart +++ b/source_gen/lib/src/constants/revive.dart @@ -65,15 +65,16 @@ Revivable reviveInstance(DartObject object, [LibraryElement? origin]) { } // ignore: deprecated_member_use - for (final e in origin!.definingCompilationUnit.types - .expand((t) => t.fields) - .where((f) => f.isConst && f.computeConstantValue() == object)) { - final result = Revivable._( - source: url.removeFragment(), - accessor: '${clazz.name}.${e.name}', - ); - if (tryResult(result)) { - return result; + for (final type in origin!.definingCompilationUnit.types) { + for (final e in type.fields + .where((f) => f.isConst && f.computeConstantValue() == object)) { + final result = Revivable._( + source: url.removeFragment(), + accessor: '${type.name}.${e.name}', + ); + if (tryResult(result)) { + return result; + } } } final i = (object as DartObjectImpl).getInvocation(); diff --git a/source_gen/test/constants_test.dart b/source_gen/test/constants_test.dart index 690251c1..21873e13 100644 --- a/source_gen/test/constants_test.dart +++ b/source_gen/test/constants_test.dart @@ -218,8 +218,8 @@ void main() { @Wrapper(_privateFunction) class Example {} - class Int64Like { - static const Int64Like ZERO = const Int64Like._bits(0, 0, 0); + class Int64Like implements Int64LikeBase{ + static const Int64Like ZERO = const Int64LikeBase._bits(0, 0, 0); final int _l; final int _m; @@ -228,6 +228,14 @@ void main() { const Int64Like._bits(this._l, this._m, this._h); } + class Int64LikeBase { + final int _l; + final int _m; + final int _h; + + const Int64LikeBase._bits(this._l, this._m, this._h); + } + enum Enum { field1, field2,