-
Notifications
You must be signed in to change notification settings - Fork 42
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
Allow in expression position #43
Comments
Just came to open an issue as well. I misread the documentation and thought it works for statements as well. Wouldn't wrapping at the macro level make it stop working in a statement like the one in the readme? cfg_if::cfg_if! {
if #[cfg(unix)] {
fn foo() { /* unix specific functionality */ }
} else if #[cfg(target_pointer_width = "32")] {
fn foo() { /* non-unix, 32-bit functionality */ }
} else {
fn foo() { /* fallback implementation */ }
}
}
fn main() {
foo();
} |
There might be a way to make both work. If not, having separate |
I agree that it's be awesome to support this, but to do so in the main macro would require the macro knowing what context it's being expanded in, which is information not availble to Rust macros. Otherwise this would, I believe, require a separate macro which does the |
Maybe just documenting this approach in the main docs would be enough? |
My approach to solving this problem is currently this: macro_rules! wrap {
($($tt:tt)+) => {
{
cfg_if!(
if #[cfg(feature = "python")] {
let ret = async move {
let value = {
$($tt)+
};
PyResult::Ok(value)
};
} else {
let ret = $($tt)+;
}
);
ret
}
};
} I.e. define a variable I was writing another library but I believe it's important enough to provide |
Summary:
This macro is currently allowed for statements, but is not allowed as an expression. But, I think it could work as an expression.
Repro:
Try this short program:
It will fail with this long and vaguely alarming set of messages:
Analysis:
You can actually make this work now by just wrapping an extra
{}
around thecfg_if
expression:If you do this, rather than the
cfg_if!
being an expression, it is a statement inside of a block expression.Expected behavior:
The
cfg_if!
macro should just wrap that extra{}
around its result itself, thus making it allowed in an expression context.The text was updated successfully, but these errors were encountered: