google / reflectable.dart

Reflectable is a Dart library that allows programmers to eliminate certain usages of dynamic reflection by specialization of reflective code to an equivalent implementation using only static techniques. The use of dynamic reflection is constrained in order to ensure that the specialized code can be generated and will have a reasonable size.
https://pub.dev/packages/reflectable
BSD 3-Clause "New" or "Revised" License
383 stars 57 forks source link

Bug: Constructor invocations in platform code do not get correct default values; metadata is null #180

Open eernstg opened 5 years ago

eernstg commented 5 years ago

With the upcoming version 2.1.0 of reflectable the dependencies have been updated to allow for analyzer 0.37.0 (and many other dependencies have been updated as well). One of the consequences of doing this is that reflectable cannot get information from the analyzer about source code constructs (that is, AstNodes) for code in platform libraries (but note that it only affects platform libraries).

Platform libraries are the ones which are imported using 'dart:N' for some name N (e.g., 'dart:html' or 'dart:math', except 'dart:ui' which is not a platform library).

This means that reflectable cannot generate code for supporting constructor invocations with the correct default values, for constructors declared in a platform library. Instead, every optional parameter declared in a platform library is given the default value null. This means that constructor invocation via a class mirror will not faithfully mimic a regular (non-reflective) constructor invocation when optional parameters with a default value different from null are omitted.

Similarly, when querying the metadata of a declaration which is declared in a platform library, reflectable gets an error on attempts to detect the value of that metadata, and currently this is handled by pretending that the metadata does not exist.

This issue is blocked on getting a bit more flexibility from the build package (in particular, https://github.com/dart-lang/build/issues/2345 is relevant, and https://github.com/dart-lang/build/issues/733 as well as #172 need a similar kind of flexibility). This would allow reflectable to request a resolver which uses full libraries rather than summaries for the SDK.

fertrig commented 5 years ago

Could you please confirm that this a new issue present in 2.1.0? I think this functionality was working on previous versions, like 2.0.12.

eernstg commented 5 years ago

Yes, it was working, it is a new issue: The new analyzer used by the build package uses summaries rather than full libraries for platform libraries, so there is no information about metadata nor about default values of parameters. (Note that platform libraries are only the ones imported using dart:..., and these problems do not occur with declarations in regular libraries.)