-
Notifications
You must be signed in to change notification settings - Fork 228
Specify function object canonicalization #1686
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
Conversation
…f constant functions
|
@johnniwinther, @mraleph, do you think this change could be a front-end only thing? Would it potentially be a substantial effort? |
|
What is a "function closurization"? Can you give an example of what this change should support? |
|
A function closurization is a tearoff when the denoted function is a top-level function, a static method, or a local function, so it's a tearoff of anything-not-an-instance-method. The best example is probably the tests: https://dart-review.googlesource.com/c/sdk/+/202243. The current failures in those tests are mentioned in a comment from me. Here's a copy of the ones that are related to constants, for convenient access: A complete example could be the following: class CheckIdentical {
const CheckIdentical(Object? o1, Object? o2): assert(identical(o1, o2));
}
X f<X>(X x) => x;
void main() {
const int Function(int) g = f; // Generic function instantiation, "f<int>".
const int Function(int) g2 = f; // Ditto.
print(identical(g, g2)); // 'true' on the vm and dart2js.
CheckIdentical(g, g2); // Succeeds on the analyzer, and elsewhere.
// But canonicalization fails to occur when the constant
// expression is not in a constant context.
int Function(int) g3 = f;
print(identical(g, g3)); // 'false' with `dart`, true with `dart2js`.
} |
|
Ah. We currently eagerly constantify static tearoffs - regardless of static context. We could just extend this to instantiations of those tearoffs when the type arguments are constant. We probably even need this for the constructor tearoffs. So yes, this can be done in the cfe. |
|
Sounds good, thanks! |
|
@lrhn, given that the language team decided at the meeting June 23rd that function objects should be canonicalized, the basic question raised by this PR has been resolved. Do you have further comments on the PR? |
lrhn
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
Do we need to address equality too?
|
Thanks! Equality is specified, and the question is whether we'd want to ask for the specified behavior to be fully implemented, or we'd rather change the specification, cf. #1712. So we'll change the specification about equality in another PR, or we'll keep the current rules and ask for an implementation, and this PR should is separate from those efforts. |
This PR adds a couple of paragraphs to the language specification, specifying that constant expressions whose evaluation is a function closurization or a generic function instantiation are canonicalized.