-
Couldn't load subscription status.
- Fork 227
Open
Labels
requestRequests to resolve a particular developer problemRequests to resolve a particular developer problem
Description
Consider the following code (from dart-lang/sdk#61145):
class A {}
class B {}
class C implements A, B {}
abstract class D {
A get foo;
}
abstract class E {
B get foo;
}
mixin M implements E {}
abstract class F extends D with M {
C get foo;
}
main() {}The analyzer accepts this code; the CFE rejects it. The CFE says:
../../tmp/proj/test.dart:11:16: Error: Class 'D with M' inherits multiple members named 'foo' with incompatible signatures.
Try adding a declaration of 'foo' to 'D with M'.
abstract class F extends D with M {
^
../../tmp/proj/test.dart:5:9: Context: This is one of the overridden members.
A get foo;
^^^
../../tmp/proj/test.dart:8:9: Context: This is one of the overridden members.
B get foo;
^^^
Based on my understanding of the spec, the CFE is correct in rejecting the code, because:
- The declaration
abstract class F extends D with Mshould be understood as syntactic sugar forabstract class F extends DwithM, whereDwithMis a synthetic mixin application, defined asclass DwithM = D with M. - The interface for class
DwithMinherits two declarations offoo: one with typeA(fromD), and one with typeB(fromM, viaE). - Since neither
AnorBis a subtype of the other, the compiler cannot assign a type tofooin the interface forDwithM.
This is a particularly frustrating situation because:
- The error can't be conveniently worked around; the CFE's suggestion to "Try adding a declaration of 'foo' to 'D with M'" is impossible.
- It's not obvious why this code needs to be rejected at all; after all, class
Fprovides a declaration offoowith a type that's compatible with the interfaces of bothDandM, and it's impossible for any expression to have a static type ofDwithM, so there's no reason the compiler would ever need to look up the type offooinDwithMat all.More generally, it's impossible for an expression to have a static type that is a synthetic mixin application. (The proof of this is subtle; I'll make a follow-up comment with details).Edit: this is wrong in some cases involving generics. See Proposal: don't require synthetic mixin applications to have a well-typed interface. #4476 (comment).
I propose that we drop the requirement for synthetic mixin applications to have well-defined interfaces, in which case the analyzer's behavior will be considered correct, and the example code above will be allowed.
CC @dart-lang/language-team
Wdestroier, FMorschel, scheglov and albertms10
Metadata
Metadata
Assignees
Labels
requestRequests to resolve a particular developer problemRequests to resolve a particular developer problem