|
6 | 6 | # Summary |
7 | 7 |
|
8 | 8 | It is likely that Rust will want to someday have a notion of an unboxed function type to be used |
9 | | -with custom pointer types.<sup>1</sup> Ideally then today's function pointers would become `&'static`s of |
10 | | -functions. But this is a breaking syntactic change, and unboxed functions can probably not be exposed |
11 | | -by 1.0. This RFC proposals making the needed breaking syntactic change now, so unboxed functions can |
12 | | -be added---and function pointers turned to borrowed pointers---all backwards compatibly in the |
13 | | -future. |
| 9 | +with custom pointer types.<sup>1</sup> Ideally then today's function pointers would become |
| 10 | +`&'static`s of functions. But this is a breaking syntactic change, and unboxed functions can |
| 11 | +probably not be exposed by 1.0. This RFC proposals making the needed breaking changes now, so |
| 12 | +unboxed functions can be added---and function pointers turned to borrowed pointers---all backwards |
| 13 | +compatibly in the future. |
14 | 14 |
|
15 | 15 | # Motivation |
16 | 16 |
|
@@ -41,37 +41,43 @@ function pointers fat, and moreover the sizes of functions are not in-general kn |
41 | 41 | anyway (c.f. foreign functions). |
42 | 42 |
|
43 | 43 | So if this requires a breaking change to make nice, yet is too big to do before 1.0, what can be |
44 | | -done? My suggestion is simply to simply change the syntax of function pointer types to make clear |
45 | | -they act like `&'statics`. This doesn't force any semantic changes in the future, but opens to door |
46 | | -to reusing `fn(...)...` for unboxed functions by allowing function pointers to silently become |
47 | | -actual `&statics` if/when function types are added in the future. Meanwhile Rust, while slightly |
48 | | -less ergonomic---function pointers are quite rare, is more forthright about its current |
49 | | -expressiveness today. |
| 44 | +done? My suggestion is simply to make just the breaking changes now, and leave the rest for later: |
| 45 | +change the syntax of function pointer types to make clear they act like `&'statics`, and make it so |
| 46 | +that only "borrowed functions" coerce to function pointers. This doesn't force any semantic changes |
| 47 | +in the future, but opens to door to reusing `fn(...)...` for unboxed functions by allowing function |
| 48 | +pointers to silently become actual `&statics` if/when function types are added in the |
| 49 | +future. Meanwhile Rust, while slightly less ergonomic---function pointers are quite rare, is more |
| 50 | +forthright about its current expressiveness today. |
50 | 51 |
|
51 | 52 | # Detailed design |
52 | 53 |
|
53 | | -Quite simply, change the syntax of function pointer types from |
| 54 | +1. Change the syntax of function pointer types from |
54 | 55 |
|
55 | | -```rust |
56 | | -('extern' STRING_LIT?)? 'fn' '(' (IDEN ':' TYPE (',' IDEN ':' TYPE)* ','?)? ')' ('->' TYPE)? |
57 | | -``` |
| 56 | + ```rust |
| 57 | + ('extern' STRING_LIT?)? 'fn' '(' (IDEN ':' TYPE (',' IDEN ':' TYPE)* ','?)? ')' ('->' TYPE)? |
| 58 | + ``` |
58 | 59 |
|
59 | | -to |
| 60 | + to |
60 | 61 |
|
61 | | -```rust |
62 | | -'&' '\'static' ('extern' STRING_LIT?)? 'fn' '(' (IDEN ':' TYPE (',' IDEN ':' TYPE)* ','?)? ')' ('->' TYPE)? |
63 | | -``` |
| 62 | + ```rust |
| 63 | + '&' '\'static' ('extern' STRING_LIT?)? 'fn' '(' (IDEN ':' TYPE (',' IDEN ':' TYPE)* ','?)? ')' ('->' TYPE)? |
| 64 | + ``` |
64 | 65 |
|
65 | | -Like slices before DST, the `fn(...) ...` itself will not denote a type on its own and need not even |
66 | | -parse. |
| 66 | + Like slices before DST, the `fn(...) ...` itself will not denote a type on its own and need not |
| 67 | + even parse. |
67 | 68 |
|
68 | | -In the future, `fn(...) ...` can be made legal syntax and given semantics, in which case the current |
69 | | -syntax would no longer be its own grammatical production but remain valid, and actually denote the |
70 | | -type of a static, immutable borrowed pointer to a function. |
| 69 | + In the future, `fn(...) ...` can be made legal syntax and given semantics, in which case the |
| 70 | + current syntax would no longer be its own grammatical production but remain valid, and actually |
| 71 | + denote the type of a static, immutable borrowed pointer to a function. |
71 | 72 |
|
72 | | -Just as today, function items may have unique function types, but those types won't have a surface |
73 | | -syntax, and expressions (in practice just identifiers/paths) of those types will coerce to function |
74 | | -pointers (see [1]). |
| 73 | + |
| 74 | +2. Change the coercion rules so that values of the secret function types to not coerce to function |
| 75 | + pointers, but values of immutably borrowed secret function types do instead. |
| 76 | + |
| 77 | + Just as today, function items may have unique function types, but those types won't have a |
| 78 | + surface syntax. This change however brings the coercion rules for functions in line with that for |
| 79 | + DSTs, and will prevent what would be a much more auto-referencing if the types of functions were |
| 80 | + exposed in the future. [Thanks @P1start for this idea.] |
75 | 81 |
|
76 | 82 | # Drawbacks |
77 | 83 |
|
|
0 commit comments