Skip to content

Commit 6a37f83

Browse files
committed
Add comment
1 parent 6a7c2dd commit 6a37f83

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/macros.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,33 @@ macro_rules! s_no_extra_traits {
121121
);
122122
}
123123

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
125151
cfg_if! {
126152
if #[cfg(feature = "extern-const-fn")] {
127153
#[allow(unused_macros)]

0 commit comments

Comments
 (0)