Skip to content

Commit c712323

Browse files
committed
Propose @P1start's coercision rules
1 parent dc1044f commit c712323

File tree

1 file changed

+33
-27
lines changed

1 file changed

+33
-27
lines changed

text/0000-fun-vs-fun-ptr.md

+33-27
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
# Summary
77

88
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.
1414

1515
# Motivation
1616

@@ -41,37 +41,43 @@ function pointers fat, and moreover the sizes of functions are not in-general kn
4141
anyway (c.f. foreign functions).
4242

4343
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.
5051

5152
# Detailed design
5253

53-
Quite simply, change the syntax of function pointer types from
54+
1. Change the syntax of function pointer types from
5455

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+
```
5859

59-
to
60+
to
6061

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+
```
6465

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.
6768

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.
7172

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, these secret function types won't have a surface syntax. This change however
78+
brings the coercion rules for functions in line with that for DSTs, and will prevent what would
79+
be a much more auto-referencing if the types of functions were exposed in the
80+
future. [Thanks @P1start for this idea.]
7581

7682
# Drawbacks
7783

0 commit comments

Comments
 (0)