Skip to content

Commit 3e00278

Browse files
committed
accept cfg on the first template
1 parent 56a9de8 commit 3e00278

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

compiler/rustc_builtin_macros/src/asm.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,23 @@ pub fn parse_asm_args<'a>(
140140
return Err(dcx.create_err(errors::AsmRequiresTemplate { span: sp }));
141141
}
142142

143-
let _attributes = AsmAttrVec::parse(ecx, p)?;
144-
let first_template = p.parse_expr()?;
143+
let first_template = loop {
144+
let attributes = AsmAttrVec::parse(ecx, p)?;
145+
let is_configured_out =
146+
ecx.ecfg.features.asm_cfg() && strip_unconfigured.configure(attributes).is_none();
147+
148+
let template = p.parse_expr()?;
149+
150+
if !is_configured_out {
151+
break template;
152+
}
153+
154+
if !p.eat(exp!(Comma)) {
155+
// After a template string, we always expect *only* a comma...
156+
return Err(dcx.create_err(errors::AsmExpectedComma { span: p.token.span }));
157+
}
158+
};
159+
145160
let mut args = AsmArgs {
146161
templates: vec![first_template],
147162
operands: vec![],

tests/ui/asm/cfg.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,32 @@ fn template_allowed() {
8383
}
8484
}
8585

86+
#[unsafe(naked)]
87+
extern "C" fn first_template() -> u64 {
88+
naked_asm!(
89+
#[cfg(reva)]
90+
"mov rax, 5",
91+
#[cfg(revb)]
92+
"mov rax, 10",
93+
"ret",
94+
)
95+
}
96+
8697
pub fn main() {
8798
std::cfg_match! {
8899
reva => {
89100
assert_eq!(const_operand(), 5);
90101
assert_eq!(ignore_const_operand_cfg_attr(), 5);
91102
assert_eq!(ignore_const_operand(), 5);
103+
assert_eq!(first_template(), 5);
92104

93105
}
94106
revb => {
95107
assert_eq!(const_operand(), 10);
96108
assert_eq!(ignore_const_operand_cfg_attr(), 10);
97109
assert_eq!(ignore_const_operand(), 10);
110+
assert_eq!(first_template(), 10);
111+
98112
}
99113
}
100114
options();

0 commit comments

Comments
 (0)