Skip to content

Commit 9b4e2a5

Browse files
authored
Auto merge of #34682 - CensoredUsername:clobber-docs, r=eddyb
Correct inline assembly clobber formatting. Fixes the formatting for inline assembly clobbers used in the book. As this causes llvm to silently ignore the clobber an error is also added to catch cases in which the wrong formatting was used. Additionally a test case is added to confirm that this error works. This fixes #34458 Note: this is only one out of a few possible ways to fix the issue depending on how the asm! macro formatting is wanted. Additionally, it'd be nicer to have some kind of test or feedback from llvm if the clobber constraints are valid, but I do not know enough about llvm to say if or how this is possible.
2 parents 182bcdb + e32da12 commit 9b4e2a5

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

src/doc/book/inline-assembly.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ but you must add the right number of `:` if you skip them:
5757
asm!("xor %eax, %eax"
5858
:
5959
:
60-
: "{eax}"
60+
: "eax"
6161
);
6262
# } }
6363
```
@@ -68,7 +68,7 @@ Whitespace also doesn't matter:
6868
# #![feature(asm)]
6969
# #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
7070
# fn main() { unsafe {
71-
asm!("xor %eax, %eax" ::: "{eax}");
71+
asm!("xor %eax, %eax" ::: "eax");
7272
# } }
7373
```
7474

@@ -127,7 +127,7 @@ stay valid.
127127
# #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
128128
# fn main() { unsafe {
129129
// Put the value 0x200 in eax
130-
asm!("mov $$0x200, %eax" : /* no outputs */ : /* no inputs */ : "{eax}");
130+
asm!("mov $$0x200, %eax" : /* no outputs */ : /* no inputs */ : "eax");
131131
# } }
132132
```
133133

src/libsyntax_ext/asm.rs

+3
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,10 @@ pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[tokenstream::Token
192192

193193
if OPTIONS.iter().any(|&opt| s == opt) {
194194
cx.span_warn(p.last_span, "expected a clobber, found an option");
195+
} else if s.starts_with("{") || s.ends_with("}") {
196+
cx.span_err(p.last_span, "clobber should not be surrounded by braces");
195197
}
198+
196199
clobs.push(s);
197200
}
198201
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// ignore-android
12+
// ignore-arm
13+
// ignore-aarch64
14+
15+
#![feature(asm, rustc_attrs)]
16+
17+
#[cfg(any(target_arch = "x86",
18+
target_arch = "x86_64"))]
19+
#[rustc_error]
20+
pub fn main() {
21+
unsafe {
22+
// clobber formatted as register input/output
23+
asm!("xor %eax, %eax" : : : "{eax}");
24+
//~^ ERROR clobber should not be surrounded by braces
25+
}
26+
}

0 commit comments

Comments
 (0)