-
Notifications
You must be signed in to change notification settings - Fork 26
DDC generates invalid const objects. #503
Comments
Note that this could have important implications, since the following (now valid) code wouldn't be valid anymore: class A<T> {
Iterator<T> _currentExpansion = const EmptyIterator();
}
class EmptyIterator<E> implements Iterator<E> {
const EmptyIterator();
bool moveNext() => false;
E get current => null;
} |
Yes. In DDC, const objects are hash-consed at runtime rather than at compile time, to deal with this exact issue. Since strong mode won't let a As we look at implementing strong mode on other platforms, we're going to have decide how to deal with this. Either we change the semantics of const more broadly to deal with this, or else we lose some useful code. For the |
There are two things happening here. Consider a slightly modified example: class A<T> {
const A();
List<T> foo() => <T>[];
}
class C<T> {
A<T> bar() => const A();
}
main() {
var a1 = new C<int>().bar();
var a2 = new C<String>().bar();
var a3 = new C<int>().bar();
print(a1 == a3);
print(a1 == a2);
} In DDC, this will print:
Where as the VM and dart2js will print:
So, the two things:
|
So strong mode changes |
Yeah, options aren't great in general. In this case, it may be better to reject the code as |
This is really a language issue: DDC is just implementing the current strong mode semantics. I've opened a bug here dart-lang/sdk#26291 for discussing this, closing this one. |
It must always be possible to allocate const objects at compile-time.
DDC doesn't respect this property:
In this example DDC will happily create two different 'const' instances in the
bar
function. It would be trivial to change the code to generate an infinity of different "const" objects (since the number of generic types is unbound in Dart).Generated code:
The text was updated successfully, but these errors were encountered: