Skip to content

Commit 51e414f

Browse files
committed
Combine spans into one error, deduplicate code
1 parent ae8a1ba commit 51e414f

File tree

3 files changed

+48
-112
lines changed

3 files changed

+48
-112
lines changed

compiler/rustc_builtin_macros/src/asm.rs

+14-21
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_parse_format as parse;
1010
use rustc_session::lint::{self, BuiltinLintDiagnostics};
1111
use rustc_span::symbol::Ident;
1212
use rustc_span::symbol::{kw, sym, Symbol};
13-
use rustc_span::{InnerSpan, Span};
13+
use rustc_span::{InnerSpan, MultiSpan, Span};
1414
use rustc_target::asm::InlineAsmArch;
1515
use smallvec::smallvec;
1616

@@ -523,26 +523,19 @@ fn expand_preparsed_asm(
523523
if found_labels.len() > 0 {
524524
let spans =
525525
found_labels.into_iter().filter_map(find_label_span).collect::<Vec<Span>>();
526-
if spans.len() > 0 {
527-
for span in spans.into_iter() {
528-
ecx.parse_sess().buffer_lint_with_diagnostic(
529-
lint::builtin::NAMED_ASM_LABELS,
530-
span,
531-
ecx.current_expansion.lint_node_id,
532-
"avoid using named labels in inline assembly",
533-
BuiltinLintDiagnostics::NamedAsmLabel("only local labels of the form `<number>:` should be used in inline asm".to_string()),
534-
);
535-
}
536-
} else {
537-
// If there were labels but we couldn't find a span, combine the warnings and use the template span
538-
ecx.parse_sess().buffer_lint_with_diagnostic(
539-
lint::builtin::NAMED_ASM_LABELS,
540-
template_sp,
541-
ecx.current_expansion.lint_node_id,
542-
"avoid using named labels in inline assembly",
543-
BuiltinLintDiagnostics::NamedAsmLabel("only local labels of the form `<number>:` should be used in inline asm".to_string()),
544-
);
545-
}
526+
// If there were labels but we couldn't find a span, combine the warnings and use the template span
527+
let target_spans: MultiSpan =
528+
if spans.len() > 0 { spans.into() } else { template_sp.into() };
529+
ecx.parse_sess().buffer_lint_with_diagnostic(
530+
lint::builtin::NAMED_ASM_LABELS,
531+
target_spans,
532+
ecx.current_expansion.lint_node_id,
533+
"avoid using named labels in inline assembly",
534+
BuiltinLintDiagnostics::NamedAsmLabel(
535+
"only local labels of the form `<number>:` should be used in inline asm"
536+
.to_string(),
537+
),
538+
);
546539
}
547540
}
548541

src/test/ui/asm/named-asm-labels.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,11 @@ fn main() {
1616
// Multiple labels on one line
1717
asm!("foo: bar1: nop");
1818
//~^ ERROR avoid using named labels
19-
//~| ERROR avoid using named labels
2019

2120
// Multiple lines
2221
asm!("foo1: nop", "nop"); //~ ERROR avoid using named labels
2322
asm!("foo2: foo3: nop", "nop");
2423
//~^ ERROR avoid using named labels
25-
//~| ERROR avoid using named labels
2624
asm!("nop", "foo4: nop"); //~ ERROR avoid using named labels
2725
asm!("foo5: nop", "foo6: nop");
2826
//~^ ERROR avoid using named labels
@@ -31,19 +29,16 @@ fn main() {
3129
// Statement separator
3230
asm!("foo7: nop; foo8: nop");
3331
//~^ ERROR avoid using named labels
34-
//~| ERROR avoid using named labels
3532
asm!("foo9: nop; nop"); //~ ERROR avoid using named labels
3633
asm!("nop; foo10: nop"); //~ ERROR avoid using named labels
3734

3835
// Escaped newline
3936
asm!("bar2: nop\n bar3: nop");
4037
//~^ ERROR avoid using named labels
41-
//~| ERROR avoid using named labels
4238
asm!("bar4: nop\n nop"); //~ ERROR avoid using named labels
4339
asm!("nop\n bar5: nop"); //~ ERROR avoid using named labels
4440
asm!("nop\n bar6: bar7: nop");
4541
//~^ ERROR avoid using named labels
46-
//~| ERROR avoid using named labels
4742

4843
// Raw strings
4944
asm!(
@@ -53,7 +48,7 @@ fn main() {
5348
"
5449
);
5550
//~^^^^ ERROR avoid using named labels
56-
//~^^^^ ERROR avoid using named labels
51+
5752
asm!(
5853
r###"
5954
nop

0 commit comments

Comments
 (0)