-
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
RFC: Function pointers reform #996
Conversation
Also change `ForeignType` to `ValueType`.
|
||
# Detailed design | ||
|
||
Make the current function pointer types unsized, and introduce function reference types of the form `&fn(arg_list) -> ret_type` and new (const) function pointer types of the form `&const fn(arg_list) -> ret_type`. |
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.
A quite misleading typo &const fn
-> *const fn
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.
@petrochenkov, thanks, fixed.
(EDIT: Posted a line note as a normal comment, removed. Please see the line note replying to @eddyb.) |
And also reinterpret them as "incomplete function items". This change is made in order to deal with the fact that all Rust types must have dynamically determinable sizes.
I'll be honest, I trend negative on this idea. It's not that it doesn't make sense, it's just that it's late-breaking changes to the type system that don't feel necessary. The current I am concerned that the change is somewhat invasive to the type system in that it introduces the idea of types that have no size or alignment. (This occurred to me while reading the RFC, but I see that @eddyb raised this same point on #883 and I just hadn't noticed it yet.) Basically, the type It may be that we just need to incorporate this notion ("Sizeable", as @eddyb called it, though I'd prefer another name). It is certainly important because I would like to eventually permit passing DST by value, so that we could do things like There is one other angle from which this RFC may make sense, though. I appreciate that it makes the "nullability and safety" of fn pointers part of the pointer type. @brson was recently discussing with me how to properly type a C function pointer that one receives from C code, you ought to write Some minor changes to the RFC as written I would like to see:
UPDATE: After a more careful reading of this RFC, I see that some of the things I said I wanted already existed. My bad. Perhaps they can just be made more prominent. :) |
The following rules will apply: | ||
|
||
1. `fn{f}`s are still function item types, or "function handles/proxies/values", whose representations and semantics remain unchanged. | ||
2. `fn`s are no longer function pointer types, but types representing "incomplete function items", which are unsized statically and zero-sized dynamically. |
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.
zero-sized dynamically doesn't seem right; they ought to have no dynamic size I think
OK, I did a more careful reading, and I see that the RFC already addressed my big concern about "unsized types". Sorry about that. Problem is that I don't like this answer of saying that boxes have zero-size dynamically. It seems to imply that
but if that were applied with That said, this design is not even at the RFC level yet, much less implemented. Nor do I think we have stabilized any intrinsics or other things that truly require all unsized types to have sizes yet (perhaps alignments, but that seems like it's not as big a problem). So we could probably say that size is zero for now, and later add some sort of traits to distinguish this case where the size of actually just not knowable or relevant. So maybe my objection isn't that strong: but it still seems like a possible "forwards compatibility" risk. |
@nikomatsakis A subtle change seems to have happened along the way from the previous RFC to this one: #883, acknowledging the time pressure, was originally specified as only a superficial syntactic change (from |
@glaebhoerl I personally consider having types that are written |
That's not in dispute. The trade would be having something worse than the current status quo for a short period (~1.0) in exchange for something better than it forever after. (Again, just as a stopgap if doing the whole thing at once would be too much.) |
My concern about this proposal was that we make |
Ah, it wasn't clear that your concerns spring directly from the fundamental idea, rather than from the details of how the sizing / unsizing / dynamically sizing aspects are handled (which I admit I don't really follow). Thanks for clarifying. |
@nikomatsakis Thanks for the reply! I'll update this RFC to address the problems you brought up. (I still think I am correct about some of them, so please again correct me if I am misunderstanding something.) You mentioned in #883 that the proposed So I am thinking, maybe we can somewhat "unify" the two special cases and say:
Maybe we need a |
@nikomatsakis (That said, there is potentially another way to make |
@nikomatsakis, on second thought, as Treating |
Can
|
I consider that our treatment of uninhabited types is subpar and a footgun in the presence of unsafe code, fwiw (easily leads to UB). And please, if we add some trait-based mechanism, let's think of the future and make it general enough to describe entire classes of types, not the issue at hand, alone. |
@eddyb, agreed. |
This RFC has been revised. |
And some other adjustments.
So somehow I missed @huonw's comment from before, or else it didn't sink in. I found it informative not only because of analyzing how commonly (or, more to the point, rarely) fn pointers are used but because it highlights more aspects of the stdlib (and language) that might benefit here, and in particular points out that e.g. However, this doesn't change my basic feeling that I don't want to introduce a new concept (types with no sizes) into the type system this soon before beta. It may be that support for such types is inevitable but that's not totally clear to me yet. I think this RFC has covered really interesting ground in the design space, and we may well want to adopt a design like this, but there is no reason we can't come back to this after 1.0 is released by making (After all, the same statistics that show that changing UPDATE: Edited slightly for clarity. |
Or we can turn this design 180 degrees!
|
@petrochenkov, good idea! I'll revise the RFC. (Or, if all wanted now is simply renaming |
I would refrain from complete rewriting of the RFC until at least one more person considers this idea acceptable :) |
👎 on adding a |
@eddyb, I know beta is approaching fast, but fwiw, I suppose |
@eddyb, on second thought, the "user visible" breaking change may not be smaller, but there should be less risk in the implementation and future proofing department. |
I noticed that we had
@petrochenkov, would this be an accaptable alternative to renaming @nikomatsakis @eddyb, does this align with the intended semantics of the Also I think we should at least do one terminology change, let's call the current function pointers "function handles", because they are like pointers but not considered as "true pointers" by the language. |
@CloudiDust There should be no |
@eddyb, I see, so, would |
Yet another possible syntax for function references/pointers may be: &'static [fn] (arg_list) -> ret_type
*const [fn] (arg_list) -> ret_type This looks better and shorter, but requires looking ahead for disambiguation. (Other possibilities like EDIT: Clarification and typo correction. |
So, as I've written earlier, I think RFC is on a good track, but we're not going to take it in the immediate future, because there are higher priorities at the moment and these changes can be done backwards compatibly. (And, as I mentioned, I'm particularly reluctant to introduce "truly unsized" types just now until we have some other aspects of DST better nailed down, though we should definitely keep them in mind.) Therefore I'm closing this RFC as postponed under #1037. Thanks @CloudiDust and others for putting in the effort into the (many iterations of...) this design. I feel fairly confident we will come back to the design advanced by this RFC at some point. |
Repurpose Rust's current function pointer types to denote "incomplete function items", and introduce function reference types and new function pointer types, so that function references/pointers work more like value references/pointers.
This is based on #883 and related discussions, where the design is already largely agreed upon. The author of #883, @Ericson2314 is too busy to update that RFC, so this RFC is created.
Rendered