Skip to content

Commit 6bdf9bd

Browse files
authored
Rollup merge of rust-lang#127935 - tgross35:binary_asm_labels-x86-only, r=estebank,Urgau
Change `binary_asm_labels` to only fire on x86 and x86_64 In <rust-lang#126922>, the `binary_asm_labels` lint was added which flags labels such as `0:` and `1:`. Before that change, LLVM was giving a confusing error on x86/x86_64 because of an incorrect interpretation. However, targets other than x86 and x86_64 never had the error message and have not been a problem. This means that the lint was causing code that previously worked to start failing (e.g. `compiler_builtins`), rather than only providing a more clear messages where there has always been an error. Adjust the lint to only fire on x86 and x86_64 assembly to avoid this regression. Also update the help message. Fixes: rust-lang#127821
2 parents 0641b37 + 5686720 commit 6bdf9bd

File tree

5 files changed

+77
-26
lines changed

5 files changed

+77
-26
lines changed

compiler/rustc_lint/messages.ftl

+3-2
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,9 @@ lint_inner_macro_attribute_unstable = inner macro attributes are unstable
403403
404404
lint_invalid_asm_label_binary = avoid using labels containing only the digits `0` and `1` in inline assembly
405405
.label = use a different label that doesn't start with `0` or `1`
406-
.note = an LLVM bug makes these labels ambiguous with a binary literal number
407-
.note = see <https://bugs.llvm.org/show_bug.cgi?id=36144> for more information
406+
.help = start numbering with `2` instead
407+
.note1 = an LLVM bug makes these labels ambiguous with a binary literal number on x86
408+
.note2 = see <https://github.com/llvm/llvm-project/issues/99547> for more information
408409
409410
lint_invalid_asm_label_format_arg = avoid using named labels in inline assembly
410411
.help = only local labels of the form `<number>:` should be used in inline asm

compiler/rustc_lint/src/builtin.rs

+39-18
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ use rustc_span::source_map::Spanned;
6666
use rustc_span::symbol::{kw, sym, Ident, Symbol};
6767
use rustc_span::{BytePos, InnerSpan, Span};
6868
use rustc_target::abi::Abi;
69+
use rustc_target::asm::InlineAsmArch;
6970
use rustc_trait_selection::infer::{InferCtxtExt, TyCtxtInferExt};
7071
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _;
7172
use rustc_trait_selection::traits::{self, misc::type_allowed_to_implement_copy};
@@ -2739,8 +2740,9 @@ declare_lint! {
27392740
///
27402741
/// ### Example
27412742
///
2742-
/// ```rust,compile_fail
2743-
/// # #![feature(asm_experimental_arch)]
2743+
/// ```rust,ignore (fails on non-x86_64)
2744+
/// #![cfg(target_arch = "x86_64")]
2745+
///
27442746
/// use std::arch::asm;
27452747
///
27462748
/// fn main() {
@@ -2750,19 +2752,32 @@ declare_lint! {
27502752
/// }
27512753
/// ```
27522754
///
2753-
/// {{produces}}
2755+
/// This will produce:
2756+
///
2757+
/// ```text
2758+
/// error: avoid using labels containing only the digits `0` and `1` in inline assembly
2759+
/// --> <source>:7:15
2760+
/// |
2761+
/// 7 | asm!("0: jmp 0b");
2762+
/// | ^ use a different label that doesn't start with `0` or `1`
2763+
/// |
2764+
/// = help: start numbering with `2` instead
2765+
/// = note: an LLVM bug makes these labels ambiguous with a binary literal number on x86
2766+
/// = note: see <https://github.com/llvm/llvm-project/issues/99547> for more information
2767+
/// = note: `#[deny(binary_asm_labels)]` on by default
2768+
/// ```
27542769
///
27552770
/// ### Explanation
27562771
///
2757-
/// A [LLVM bug] causes this code to fail to compile because it interprets the `0b` as a binary
2758-
/// literal instead of a reference to the previous local label `0`. Note that even though the
2759-
/// bug is marked as fixed, it only fixes a specific usage of intel syntax within standalone
2760-
/// files, not inline assembly. To work around this bug, don't use labels that could be
2761-
/// confused with a binary literal.
2772+
/// An [LLVM bug] causes this code to fail to compile because it interprets the `0b` as a binary
2773+
/// literal instead of a reference to the previous local label `0`. To work around this bug,
2774+
/// don't use labels that could be confused with a binary literal.
2775+
///
2776+
/// This behavior is platform-specific to x86 and x86-64.
27622777
///
27632778
/// See the explanation in [Rust By Example] for more details.
27642779
///
2765-
/// [LLVM bug]: https://bugs.llvm.org/show_bug.cgi?id=36144
2780+
/// [LLVM bug]: https://github.com/llvm/llvm-project/issues/99547
27662781
/// [Rust By Example]: https://doc.rust-lang.org/nightly/rust-by-example/unsafe/asm.html#labels
27672782
pub BINARY_ASM_LABELS,
27682783
Deny,
@@ -2908,16 +2923,22 @@ impl<'tcx> LateLintPass<'tcx> for AsmLabels {
29082923
InvalidAsmLabel::FormatArg { missing_precise_span },
29092924
);
29102925
}
2911-
AsmLabelKind::Binary => {
2912-
// the binary asm issue only occurs when using intel syntax
2913-
if !options.contains(InlineAsmOptions::ATT_SYNTAX) {
2914-
cx.emit_span_lint(
2915-
BINARY_ASM_LABELS,
2916-
span,
2917-
InvalidAsmLabel::Binary { missing_precise_span, span },
2918-
)
2919-
}
2926+
// the binary asm issue only occurs when using intel syntax on x86 targets
2927+
AsmLabelKind::Binary
2928+
if !options.contains(InlineAsmOptions::ATT_SYNTAX)
2929+
&& matches!(
2930+
cx.tcx.sess.asm_arch,
2931+
Some(InlineAsmArch::X86 | InlineAsmArch::X86_64) | None
2932+
) =>
2933+
{
2934+
cx.emit_span_lint(
2935+
BINARY_ASM_LABELS,
2936+
span,
2937+
InvalidAsmLabel::Binary { missing_precise_span, span },
2938+
)
29202939
}
2940+
// No lint on anything other than x86
2941+
AsmLabelKind::Binary => (),
29212942
};
29222943
}
29232944
}

compiler/rustc_lint/src/lints.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -2074,7 +2074,9 @@ pub enum InvalidAsmLabel {
20742074
missing_precise_span: bool,
20752075
},
20762076
#[diag(lint_invalid_asm_label_binary)]
2077-
#[note]
2077+
#[help]
2078+
#[note(lint_note1)]
2079+
#[note(lint_note2)]
20782080
Binary {
20792081
#[note(lint_invalid_asm_label_no_span)]
20802082
missing_precise_span: bool,

tests/ui/asm/binary_asm_labels.stderr

+15-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ error: avoid using labels containing only the digits `0` and `1` in inline assem
44
LL | asm!("0: jmp 0b");
55
| ^ use a different label that doesn't start with `0` or `1`
66
|
7-
= note: an LLVM bug makes these labels ambiguous with a binary literal number
7+
= help: start numbering with `2` instead
8+
= note: an LLVM bug makes these labels ambiguous with a binary literal number on x86
9+
= note: see <https://github.com/llvm/llvm-project/issues/99547> for more information
810
= note: `#[deny(binary_asm_labels)]` on by default
911

1012
error: avoid using labels containing only the digits `0` and `1` in inline assembly
@@ -13,31 +15,39 @@ error: avoid using labels containing only the digits `0` and `1` in inline assem
1315
LL | asm!("1: jmp 1b");
1416
| ^ use a different label that doesn't start with `0` or `1`
1517
|
16-
= note: an LLVM bug makes these labels ambiguous with a binary literal number
18+
= help: start numbering with `2` instead
19+
= note: an LLVM bug makes these labels ambiguous with a binary literal number on x86
20+
= note: see <https://github.com/llvm/llvm-project/issues/99547> for more information
1721

1822
error: avoid using labels containing only the digits `0` and `1` in inline assembly
1923
--> $DIR/binary_asm_labels.rs:13:15
2024
|
2125
LL | asm!("10: jmp 10b");
2226
| ^^ use a different label that doesn't start with `0` or `1`
2327
|
24-
= note: an LLVM bug makes these labels ambiguous with a binary literal number
28+
= help: start numbering with `2` instead
29+
= note: an LLVM bug makes these labels ambiguous with a binary literal number on x86
30+
= note: see <https://github.com/llvm/llvm-project/issues/99547> for more information
2531

2632
error: avoid using labels containing only the digits `0` and `1` in inline assembly
2733
--> $DIR/binary_asm_labels.rs:14:15
2834
|
2935
LL | asm!("01: jmp 01b");
3036
| ^^ use a different label that doesn't start with `0` or `1`
3137
|
32-
= note: an LLVM bug makes these labels ambiguous with a binary literal number
38+
= help: start numbering with `2` instead
39+
= note: an LLVM bug makes these labels ambiguous with a binary literal number on x86
40+
= note: see <https://github.com/llvm/llvm-project/issues/99547> for more information
3341

3442
error: avoid using labels containing only the digits `0` and `1` in inline assembly
3543
--> $DIR/binary_asm_labels.rs:15:15
3644
|
3745
LL | asm!("1001101: jmp 1001101b");
3846
| ^^^^^^^ use a different label that doesn't start with `0` or `1`
3947
|
40-
= note: an LLVM bug makes these labels ambiguous with a binary literal number
48+
= help: start numbering with `2` instead
49+
= note: an LLVM bug makes these labels ambiguous with a binary literal number on x86
50+
= note: see <https://github.com/llvm/llvm-project/issues/99547> for more information
4151

4252
error: aborting due to 5 previous errors
4353

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//@ build-pass
2+
//@ only-aarch64
3+
4+
// The `binary_asm_labels` lint should only be raised on `x86`. Make sure it
5+
// doesn't get raised on other platforms.
6+
7+
use std::arch::asm;
8+
9+
fn main() {
10+
unsafe {
11+
asm!("0: bl 0b");
12+
asm!("1: bl 1b");
13+
asm!("10: bl 10b");
14+
asm!("01: bl 01b");
15+
asm!("1001101: bl 1001101b");
16+
}
17+
}

0 commit comments

Comments
 (0)