Skip to content
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

Merged
merged 2 commits into from
Apr 9, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions proposals/function-pointers.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,29 @@ Restrictions of this feature:
deliberately not specified by the language. This includes whether they are static vs. instance or
exactly what signature they are emitted with.


### Operators on Function Pointer Types

The section in unsafe code on operators is modified as such:

> In an unsafe context, several constructs are available for operating on all _pointer\_type_s that are not _funcptr\_type_s:
>
> * The `*` operator may be used to perform pointer indirection ([Pointer indirection](unsafe-code.md#pointer-indirection)).
> * The `->` operator may be used to access a member of a struct through a pointer ([Pointer member access](unsafe-code.md#pointer-member-access)).
> * The `[]` operator may be used to index a pointer ([Pointer element access](unsafe-code.md#pointer-element-access)).
> * The `&` operator may be used to obtain the address of a variable ([The address-of operator](unsafe-code.md#the-address-of-operator)).
> * The `++` and `--` operators may be used to increment and decrement pointers ([Pointer increment and decrement](unsafe-code.md#pointer-increment-and-decrement)).
> * The `+` and `-` operators may be used to perform pointer arithmetic ([Pointer arithmetic](unsafe-code.md#pointer-arithmetic)).
> * The `==`, `!=`, `<`, `>`, `<=`, and `=>` operators may be used to compare pointers ([Pointer comparison](unsafe-code.md#pointer-comparison)).
> * The `stackalloc` operator may be used to allocate memory from the call stack ([Fixed size buffers](unsafe-code.md#fixed-size-buffers)).
> * 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))
Copy link
Member

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;

Copy link
Member Author

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.

Copy link
Member

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.

> * The `==`, `!=`, `<`, `>`, `<=`, and `=>` operators may be used to compare pointers ([Pointer comparison](unsafe-code.md#pointer-comparison)).
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

@jkotas jkotas Apr 21, 2020

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.

Copy link
Member

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?

Copy link
Member Author

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.


Additionally, we modify all the sections in `Pointers in expressions` to forbid function pointer types, except `Pointer comparison` and `The sizeof operator`.

### Better function member

The better function member specification will be changed to include the following line:
Expand Down