Reject cfg on expressions that cannot be safely removed#159580
Reject cfg on expressions that cannot be safely removed#159580Unique-Usman wants to merge 1 commit into
Conversation
Previously, expressions behind stmt_expr_attributes were only rejected when the cfg condition evaluated to false. If the condition was true, the code compiled successfully. This meant code like an attributed binary operand could compile on one platform but fail on another, even though removing the operand would leave an invalid expression. This change always rejects cfg in expression positions where removing the expression would produce invalid code. Expression positions where removal is safe continue to work as before. Signed-off-by: Usman Akinyemi <usmanakinyemi202@gmail.com>
|
rustbot has assigned @petrochenkov. Use Why was this reviewer chosen?The reviewer was selected based on:
|
| if self.expand_cfg_true(node, attr, pos).as_bool() { | ||
| if matches!( | ||
| Node::KIND, | ||
| AstFragmentKind::Expr | AstFragmentKind::MethodReceiverExpr | ||
| ) { | ||
| self.cx.dcx().emit_err(RemoveExprNotSupported { span }); | ||
| } | ||
| continue; | ||
| } |
There was a problem hiding this comment.
What this code does it emits this error if the cfg predicate is true, then if it's false we call expand_cfg_false and (if it's not the crate root) that emits the same error. Not only isn't that immediately obvious, it also suggests that there are still places where #[cfg(true)] is allowed but #[cfg(false)] is not.
Can you change it so there's only one place we emit this error (whether the predicate is true or not)?
Can you also change the error name and message to say that cfg is not supported in these positions?
There was a problem hiding this comment.
(this check could also be moved elsewhere, I'm not sure what the best place for it is)
There was a problem hiding this comment.
Can this file have some #[cfg(true)] as well?
|
I don't think we should do this.
|
|
@petrochenkov should the toolchain provide guidance for things that are likely to cause trouble because they are always a footgun regardless? Code like Edit: even if we don't emit an error, shouldn't this case in particular be at least deny-by-default lint, given that it is syntactically correct, but unlikely to be what the user intended? |
#159581 won't stabilize attribute macros on expressions, so should we move Also if we ever get a way for macros to overload based on what position (type, expr, etc) they're in (perhaps similar to |
Previously, expressions behind stmt_expr_attributes were only rejected when the cfg condition evaluated to false. If the condition was true, the code compiled successfully.
This meant code like an attributed binary operand could compile on one platform but fail on another, even though removing the operand would leave an invalid expression.
This change always rejects cfg in expression positions where removing the expression would produce invalid code. Expression positions where removal is safe continue to work as before.