-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
[cfe] noSuchMethod forwarder to private member not generated #47923
Comments
I'd like to push back on this. I see that the spec does indeed say this code should be accepted, and should produce a noSuchMethod forwarder (which in turn forwards to Object.noSuchMethod, which throws an exception). But is that a beneficial behavior for the user? Generally we prefer wrong code to lead to compile time errors rather than runtime failures, and since we're in that situation now, it feels like it would be a step backward to make the change suggested here. Maybe it would be better to simply codify the current behavior of the CFE in the spec (and update the analyzer to conform to it). Doing so would be 100% non-breaking for users, since the CFE already rejects the code in question. Tagging some of the language folks who usually discuss such things: @leafpetersen @lrhn @munificent |
I don't understand why there is a noSuchMethod forwarder implied here. @eernstg can you elaborate? This just looks like statically wrong code to me, that should be rejected. What am I missing? |
That noSuchMethod forwarder is 'forced by privacy':
In the given example "C" is One could say that there is little difference between inheriting a method that doesn't work, and not inheriting anything at all, so the situation is quite similar to this one, where we're also generating a noSuchMethod forwarder: // Library not_n009lib.dart.
class A {}
abstract class B { void _m(num n); }
// Library not_n009.dart.
import 'not_n009lib.dart';
class C extends A implements B {}
void main() => C(); The rationale would be something like "There's no way this class can implement We have recently discussed the approach to noSuchMethod forwarders (when we discussed ways to enable promotion of instance variables in special cases involving privacy), but I don't remember discussions about turning the situation into a compile-time error. Instead, we considered (and I strongly supported ;-) changing the generated member such that it would throw rather than forwarding to noSuchMethod. |
Just to be sure there's no confusion - please make no code changes around this right now. There's active discussion ongoing about what the best way forward is with this and related examples. |
I filed the following two issues to try to sort out the relevant questions here: |
I have added a proposed resolution for the core questions here in the two issues linked above. TL;DR:
I believe that resolves the core issue here. |
[Edit, May 24 2022: Added
implements A
such that we avoid using an example which will be an error with dart-lang/language#2020. Updated the CFE error message (it is essentially unchanged) and SHA1 accordingly.]Consider the following program, which is stored in the files
n004lib.dart
andn004.dart
:This program is rejected by
dart
from 94aa2ad with the following error messages:The program should actually be accepted (and a noSuchMethod forwarder for
_m
should be generated).There is a rule that prevents noSuchMethod forwarders from being generated in the case where the forwarder would override a non-noSuchMethod-forwarder (that is, a manually written method implementation), but that rule is only valid when the overridden manually written declaration is accessible:
So the change which is needed here is that the error should not be reported, and the generation of a noSuchMethod forwarder should be allowed to proceed.
The text was updated successfully, but these errors were encountered: