@@ -121,7 +121,33 @@ macro_rules! s_no_extra_traits {
121
121
) ;
122
122
}
123
123
124
- // This is pretty horrible
124
+ // This is a pretty horrible hack to allow us to conditionally mark
125
+ // some functions as 'const', without requiring users of this macro
126
+ // to care about the "extern-const-fn" feature.
127
+ //
128
+ // When 'extern-const-fn' is enabled, we emit the captured 'const' keyword
129
+ // in the expanded function.
130
+ //
131
+ // When 'extern-const-fn' is disabled, we always emit a plain 'pub unsafe extern fn'.
132
+ // Note that the expression matched by the macro is exactly the same - this allows
133
+ // users of this macro to work whether or not 'extern-const-fn' is enabled
134
+ //
135
+ // Unfortunately, we need to duplicate most of this macro between the 'cfg_if' blocks.
136
+ // This is because 'const unsafe extern fn' won't even parse on older compilers,
137
+ // so we need to avoid emitting it at all of 'extern-const-fn'.
138
+ //
139
+ // Specifically, moving the 'cfg_if' into the macro body will *not* work.
140
+ // Doing so would cause the '#[cfg(feature = "extern-const-fn")]' to be emiited
141
+ // into user code. The 'cfg' gate will not stop Rust from trying to parse the
142
+ // 'pub const unsafe extern fn', so users would get a compiler error even when
143
+ // the 'extern-const-fn' feature is disabled
144
+ //
145
+ // Note that users of this macro need to place 'const' in a weird position
146
+ // (after the closing ')' for the arguments, but before the return type).
147
+ // This was the only way I could satisfy the following two requirements:
148
+ // 1. Avoid ambuguity errors from 'macro_rules!' (which happen when writing '$foo:ident fn'
149
+ // 2. Allow users of this macro to mix 'pub fn foo' and 'pub const fn bar' within the same
150
+ // 'f!' block
125
151
cfg_if ! {
126
152
if #[ cfg( feature = "extern-const-fn" ) ] {
127
153
#[ allow( unused_macros) ]
0 commit comments