Skip to content

Commit 4dc8f1f

Browse files
authored
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.
1 parent b4136e0 commit 4dc8f1f

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

source_gen/lib/src/constants/revive.dart

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,16 @@ Revivable reviveInstance(DartObject object, [LibraryElement? origin]) {
6565
}
6666

6767
// ignore: deprecated_member_use
68-
for (final e in origin!.definingCompilationUnit.types
69-
.expand((t) => t.fields)
70-
.where((f) => f.isConst && f.computeConstantValue() == object)) {
71-
final result = Revivable._(
72-
source: url.removeFragment(),
73-
accessor: '${clazz.name}.${e.name}',
74-
);
75-
if (tryResult(result)) {
76-
return result;
68+
for (final type in origin!.definingCompilationUnit.types) {
69+
for (final e in type.fields
70+
.where((f) => f.isConst && f.computeConstantValue() == object)) {
71+
final result = Revivable._(
72+
source: url.removeFragment(),
73+
accessor: '${type.name}.${e.name}',
74+
);
75+
if (tryResult(result)) {
76+
return result;
77+
}
7778
}
7879
}
7980
final i = (object as DartObjectImpl).getInvocation();

source_gen/test/constants_test.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,8 @@ void main() {
218218
@Wrapper(_privateFunction)
219219
class Example {}
220220
221-
class Int64Like {
222-
static const Int64Like ZERO = const Int64Like._bits(0, 0, 0);
221+
class Int64Like implements Int64LikeBase{
222+
static const Int64Like ZERO = const Int64LikeBase._bits(0, 0, 0);
223223
224224
final int _l;
225225
final int _m;
@@ -228,6 +228,14 @@ void main() {
228228
const Int64Like._bits(this._l, this._m, this._h);
229229
}
230230
231+
class Int64LikeBase {
232+
final int _l;
233+
final int _m;
234+
final int _h;
235+
236+
const Int64LikeBase._bits(this._l, this._m, this._h);
237+
}
238+
231239
enum Enum {
232240
field1,
233241
field2,

0 commit comments

Comments
 (0)