Skip to content

Commit

Permalink
Use correct class when reviving from a static field (#546)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mbojey2 authored Jul 21, 2021
1 parent b4136e0 commit 4dc8f1f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
19 changes: 10 additions & 9 deletions source_gen/lib/src/constants/revive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
12 changes: 10 additions & 2 deletions source_gen/test/constants_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down

0 comments on commit 4dc8f1f

Please sign in to comment.