-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Update function pointers proposal for binary operators. #3348
Conversation
@gafter addressed feedback. |
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.
> | ||
> In an unsafe context, several constructs are available for operating on all _funcptr\_type_s: | ||
> * The `&` operator may be used to obtain the address of static methods ([Allow address-of to target methods](function-pointers.md#allow-address-of-to-target-methods)) | ||
> * The `==`, `!=`, `<`, `>`, `<=`, and `=>` operators may be used to compare pointers ([Pointer comparison](unsafe-code.md#pointer-comparison)). |
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.
<
, >
, <=
, and =>
do not make sense for function pointers. I know that C/C++ allows it, but it is not necessarily a good prior art to follow.
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.
I wish we could block them, but they're defined on void*
, and function pointers are implicitly convertible.
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.
==
and !=
are questionable too. Function pointers in .NET are not stable. For example, ldftn
on same function can return two different pointer values in two places in the program. It does actually happen in practice, e.g. a typical example is ldftn M
before M was executed for the first time and after M was executed for the first time.
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.
Always emit a warning when any of these are used on function pointers?
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.
Hmm, that's interesting. Seems like a good candidate for a warning then. I'll add a note to the tracking issue.
> * The `fixed` statement may be used to temporarily fix a variable so its address can be obtained ([The fixed statement](unsafe-code.md#the-fixed-statement)). | ||
> | ||
> In an unsafe context, several constructs are available for operating on all _funcptr\_type_s: | ||
> * The `&` operator may be used to obtain the address of static methods ([Allow address-of to target methods](function-pointers.md#allow-address-of-to-target-methods)) |
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.
This not operating on funcptr type (contrary to what the previous line says).
Should this rather say that The
& operator may be used to obtain the address of a variable
?
ie. should the following be possible?
delegate*<int> p1 = ...;
var x = &p1;
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.
You can certainly do that, just like you can do that for any local. The operating on is taken from existing examples for pointer types.
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.
You can certainly do that, just like you can do that for any local.
Good, it is what I have expected.
The formatting of this section makes it sound like that it is not possible. Taking address of a local is explicitly mentioned in the pointers that are not function pointers section above; but it is not mentioned in this section.
This forbids all binary operators except comparison operators.