-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Type inference is dependent on the order that type parameters are defined #40423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@leafpetersen @vsmenon @Markzipan @nshahan this is the inference issue I was showing you all yesterday. |
@a-siva Sorry if I wasn't clear enough, this isn't restricted to the web. If you run my reproduction on the VM, the same error occurs.
|
The error also appears in the analyzer, which I didn't think was using the front end, but that would imply both the CFE and analyzer have the exact same bug. |
@eernstg Is this the issue that was fixed recently with dart-lang/language#3009 ? I haven't looked at enabling that experiment personally... |
As of Dart SDK 3.7.0-183.0.dev, this is still broken from a quick test in DartPad. |
Yeah I don't think dartpad has that experiment enabled. Even on the main branch. |
This does look like the kind of example that shows how #3009 and '--enable-experiment=inference-using-bounds' gives rise to improved type inference. The feature is enabled by default in Dart 3.7, which means that it is available in the DartPad main channel, but not beta or stable. However, the example does not match precisely. First, in the two cases where the variable type is declared as a raw type (that is, without actual type arguments), the inference process is controlled by the declared type, and the declared type is obtained by instantiation to bound (not inference), which forces the value of the type argument In these cases the ordering of the type variables is irrelevant because the outcome of the inference step is done by "remote control" by specifying the variables to have the types Here is the remaining example program, renamed to put the emphasis on the order: class Value {}
class MyValue implements Value {}
class Renderer<T> {}
class BeforeBoundOrder<V extends Value, R extends Renderer<V>> {
final V value;
BeforeBoundOrder(this.value);
}
class AfterBoundOrder<R extends Renderer<V>, V extends Value> {
final V value;
AfterBoundOrder(this.value);
}
void main() {
var v1 = BeforeBoundOrder(MyValue());
print(v1.runtimeType); // 'BeforeBoundOrder<MyValue, Renderer<MyValue>>'.
// ERROR: Tried to infer 'Renderer<Object?>' for 'R' which doesn't work: ...
var v2 = AfterBoundOrder(MyValue());
} The failure occurs even when 'inference-using-bounds' is enabled, because we're computing The change to type inference which will eliminate the ordering dependency hasn't been implemented yet (and some discussions about how to do it may still be needed). @chloestefantsova, perhaps you can say more about this? The example shows another thing, though. If the error line is commented out then the program runs and prints |
Thanks for the investigation, @eernstg ! |
Versions:
Here's a working reproduction https://dartpad.dev/a8a92c56149499dd479488f1cca5bff9.
var
versus the raw type - this is unexpected.var
appears to be dependent on the order in which type parameters are declared on the resulting type, when one of those type parameters depends on the other.The text was updated successfully, but these errors were encountered: