Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error span if arg to asm!() is a macro call #130917

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion compiler/rustc_builtin_macros/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,7 @@ fn expand_preparsed_asm(

let msg = "asm template must be a string literal";
let template_sp = template_expr.span;
let template_is_mac_call = matches!(template_expr.kind, ast::ExprKind::MacCall(_));
let (template_str, template_style, template_span) = {
let ExpandResult::Ready(mac) = expr_to_spanned_string(ecx, template_expr, msg) else {
return ExpandResult::Retry(());
Expand Down Expand Up @@ -596,7 +597,14 @@ fn expand_preparsed_asm(

if !parser.errors.is_empty() {
let err = parser.errors.remove(0);
let err_sp = template_span.from_inner(InnerSpan::new(err.span.start, err.span.end));
let err_sp = if template_is_mac_call {
// If the template is a macro call we can't reliably point to the error's
// span so just use the template's span as the error span (fixes #129503)
template_span
} else {
template_span.from_inner(InnerSpan::new(err.span.start, err.span.end))
};

let msg = format!("invalid asm template string: {}", err.description);
let mut e = ecx.dcx().struct_span_err(err_sp, msg);
e.span_label(err_sp, err.label + " in asm template string");
Expand Down
7 changes: 0 additions & 7 deletions tests/crashes/129503.rs

This file was deleted.

26 changes: 26 additions & 0 deletions tests/ui/asm/ice-bad-err-span-in-template-129503.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Regression test for ICE #129503


// Tests that we come up with decent error spans
// when the template fed to `asm!()` is itself a
// macro call like `concat!()` and should not ICE

use std::arch::asm;

fn main() {
// Should not ICE
asm!(concat!(r#"lJ𐏿Æ�.𐏿�"#, "r} {}"));
//~^ ERROR invalid asm template string: unmatched `}` found


// Macro call template: should point to
// everything within `asm!()` as error span
asm!(concat!("abc", "r} {}"));
//~^ ERROR invalid asm template string: unmatched `}` found


// Literal template: should point precisely to
// just the `}` as error span
asm!("abc", "r} {}");
//~^ ERROR invalid asm template string: unmatched `}` found
}
28 changes: 28 additions & 0 deletions tests/ui/asm/ice-bad-err-span-in-template-129503.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
error: invalid asm template string: unmatched `}` found
--> $DIR/ice-bad-err-span-in-template-129503.rs:12:10
|
LL | asm!(concat!(r#"lJ𐏿Æ�.𐏿�"#, "r} {}"));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unmatched `}` in asm template string
|
= note: if you intended to print `}`, you can escape it using `}}`
= note: this error originates in the macro `concat` (in Nightly builds, run with -Z macro-backtrace for more info)

error: invalid asm template string: unmatched `}` found
--> $DIR/ice-bad-err-span-in-template-129503.rs:18:10
|
LL | asm!(concat!("abc", "r} {}"));
| ^^^^^^^^^^^^^^^^^^^^^^^ unmatched `}` in asm template string
|
= note: if you intended to print `}`, you can escape it using `}}`
= note: this error originates in the macro `concat` (in Nightly builds, run with -Z macro-backtrace for more info)

error: invalid asm template string: unmatched `}` found
--> $DIR/ice-bad-err-span-in-template-129503.rs:24:19
|
LL | asm!("abc", "r} {}");
| ^ unmatched `}` in asm template string
|
= note: if you intended to print `}`, you can escape it using `}}`

error: aborting due to 3 previous errors

Loading