From 6bf3fbe9333cd38e5a2de3049fc8828c107a5ba0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Fri, 5 Jun 2026 22:27:46 +0000 Subject: [PATCH] Use multipart suggestion for finding ident when literal was expected in attr --- compiler/rustc_attr_parsing/src/parser.rs | 12 ++++---- .../ui/attributes/crate-type-non-crate.stderr | 14 ++++++++-- ...on-on-associated-items-issue-121537.stderr | 7 ++++- .../ui/darwin-objc/darwin-objc-bad-arg.stderr | 28 ++++++++++++++++--- 4 files changed, 49 insertions(+), 12 deletions(-) diff --git a/compiler/rustc_attr_parsing/src/parser.rs b/compiler/rustc_attr_parsing/src/parser.rs index 1e2523e42f3a9..c388b69af6484 100644 --- a/compiler/rustc_attr_parsing/src/parser.rs +++ b/compiler/rustc_attr_parsing/src/parser.rs @@ -478,12 +478,14 @@ fn expr_to_lit<'sess>( // Suggest adding quotation marks to turn an identifier into a string literal if let ExprKind::Path(None, ref path) = expr.kind - && let [segment] = path.segments.as_slice() + && let [_] = path.segments.as_slice() { - err.span_suggestion( - expr.span, - "try adding quotation marks", - &format!("\"{}\"", segment.ident), + err.multipart_suggestion( + "you might have meant to write a string literal", + vec![ + (expr.span.shrink_to_lo(), "\"".to_string()), + (expr.span.shrink_to_hi(), "\"".to_string()), + ], Applicability::MaybeIncorrect, ); } diff --git a/tests/ui/attributes/crate-type-non-crate.stderr b/tests/ui/attributes/crate-type-non-crate.stderr index d27d81f784254..8edfebdd918df 100644 --- a/tests/ui/attributes/crate-type-non-crate.stderr +++ b/tests/ui/attributes/crate-type-non-crate.stderr @@ -40,7 +40,12 @@ error: attribute value must be a literal --> $DIR/crate-type-non-crate.rs:9:16 | LL | #[crate_type = lib] - | ^^^ help: try adding quotation marks: `"lib"` + | ^^^ + | +help: you might have meant to write a string literal + | +LL | #[crate_type = "lib"] + | + + error[E0539]: malformed `crate_type` attribute input --> $DIR/crate-type-non-crate.rs:12:1 @@ -72,7 +77,12 @@ error: attribute value must be a literal --> $DIR/crate-type-non-crate.rs:14:16 | LL | #[crate_type = foo] - | ^^^ help: try adding quotation marks: `"foo"` + | ^^^ + | +help: you might have meant to write a string literal + | +LL | #[crate_type = "foo"] + | + + error[E0539]: malformed `crate_type` attribute input --> $DIR/crate-type-non-crate.rs:17:1 diff --git a/tests/ui/attributes/validation-on-associated-items-issue-121537.stderr b/tests/ui/attributes/validation-on-associated-items-issue-121537.stderr index 750ca2e3aaca2..94a77253ae409 100644 --- a/tests/ui/attributes/validation-on-associated-items-issue-121537.stderr +++ b/tests/ui/attributes/validation-on-associated-items-issue-121537.stderr @@ -2,7 +2,12 @@ error: attribute value must be a literal --> $DIR/validation-on-associated-items-issue-121537.rs:2:13 | LL | #[doc = MyTrait] - | ^^^^^^^ help: try adding quotation marks: `"MyTrait"` + | ^^^^^^^ + | +help: you might have meant to write a string literal + | +LL | #[doc = "MyTrait"] + | + + error: aborting due to 1 previous error diff --git a/tests/ui/darwin-objc/darwin-objc-bad-arg.stderr b/tests/ui/darwin-objc/darwin-objc-bad-arg.stderr index f75f07945a165..5c9ca56701eb0 100644 --- a/tests/ui/darwin-objc/darwin-objc-bad-arg.stderr +++ b/tests/ui/darwin-objc/darwin-objc-bad-arg.stderr @@ -2,13 +2,23 @@ error: attribute value must be a literal --> $DIR/darwin-objc-bad-arg.rs:12:18 | LL | objc::class!(s); - | ^ help: try adding quotation marks: `"s"` + | ^ + | +help: you might have meant to write a string literal + | +LL | objc::class!("s"); + | + + error: attribute value must be a literal --> $DIR/darwin-objc-bad-arg.rs:15:18 | LL | objc::class!(NSObject); - | ^^^^^^^^ help: try adding quotation marks: `"NSObject"` + | ^^^^^^^^ + | +help: you might have meant to write a string literal + | +LL | objc::class!("NSObject"); + | + + error: `objc::class!` expected a string literal --> $DIR/darwin-objc-bad-arg.rs:18:18 @@ -26,13 +36,23 @@ error: attribute value must be a literal --> $DIR/darwin-objc-bad-arg.rs:25:21 | LL | objc::selector!(s); - | ^ help: try adding quotation marks: `"s"` + | ^ + | +help: you might have meant to write a string literal + | +LL | objc::selector!("s"); + | + + error: attribute value must be a literal --> $DIR/darwin-objc-bad-arg.rs:28:21 | LL | objc::selector!(alloc); - | ^^^^^ help: try adding quotation marks: `"alloc"` + | ^^^^^ + | +help: you might have meant to write a string literal + | +LL | objc::selector!("alloc"); + | + + error: `objc::selector!` expected a string literal --> $DIR/darwin-objc-bad-arg.rs:31:21