Allow in expression position #43
Labels
No labels
bug
dependencies
duplicate
enhancement
github_actions
help wanted
invalid
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
gh-rust-lang/cfg-if#43
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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_ifexpression: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.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?
There might be a way to make both work. If not, having separate
cfg_ifandcfg_if_exp/cfg_if_valuewould be somewhat reasonable.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
{-surrounding. I'd personally prefer, though, to avoid adding a second macro to this library.Maybe just documenting this approach in the main docs would be enough?
My approach to solving this problem is currently this:
I.e. define a variable
retin each branch and return it within a block. This block is an expression.I was writing another library but I believe it's important enough to provide
cfg_if_expr. I might do it within a couple of days.