Skip to content

Commit c67006e

Browse files
authored
Rollup merge of #156797 - ariagivens:suggest-quotes, r=JonathanBrouwer
Suggest adding quotation marks to identifiers in attributes When the user provides an identifier when a literal was expected in an attribute value, suggest adding quotation marks to the identifier to make it a string literal. For instance: ``` 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"` ```
2 parents ac7ba99 + 1fa9548 commit c67006e

4 files changed

Lines changed: 22 additions & 9 deletions

File tree

compiler/rustc_attr_parsing/src/parser.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_ast::{
1212
AttrArgs, Expr, ExprKind, LitKind, MetaItemLit, Path, PathSegment, StmtKind, UnOp,
1313
};
1414
use rustc_ast_pretty::pprust;
15-
use rustc_errors::{Diag, PResult};
15+
use rustc_errors::{Applicability, Diag, PResult};
1616
use rustc_hir::{self as hir, AttrPath};
1717
use rustc_parse::exp;
1818
use rustc_parse::parser::{ForceCollect, Parser, PathStyle, Recovery, token_descr};
@@ -410,7 +410,20 @@ fn expr_to_lit<'sess>(
410410
// - `#[foo = include_str!("nonexistent-file.rs")]`:
411411
// results in `ast::ExprKind::Err`.
412412
let msg = "attribute value must be a literal";
413-
let err = psess.dcx().struct_span_err(span, msg);
413+
let mut err = psess.dcx().struct_span_err(span, msg);
414+
415+
// Suggest adding quotation marks to turn an identifier into a string literal
416+
if let ExprKind::Path(None, ref path) = expr.kind
417+
&& let [segment] = path.segments.as_slice()
418+
{
419+
err.span_suggestion(
420+
expr.span,
421+
"try adding quotation marks",
422+
&format!("\"{}\"", segment.ident),
423+
Applicability::MaybeIncorrect,
424+
);
425+
}
426+
414427
Err(err)
415428
}
416429
}

tests/ui/attributes/crate-type-non-crate.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ error: attribute value must be a literal
4040
--> $DIR/crate-type-non-crate.rs:9:16
4141
|
4242
LL | #[crate_type = lib]
43-
| ^^^
43+
| ^^^ help: try adding quotation marks: `"lib"`
4444

4545
error[E0539]: malformed `crate_type` attribute input
4646
--> $DIR/crate-type-non-crate.rs:12:1
@@ -72,7 +72,7 @@ error: attribute value must be a literal
7272
--> $DIR/crate-type-non-crate.rs:14:16
7373
|
7474
LL | #[crate_type = foo]
75-
| ^^^
75+
| ^^^ help: try adding quotation marks: `"foo"`
7676

7777
error[E0539]: malformed `crate_type` attribute input
7878
--> $DIR/crate-type-non-crate.rs:17:1

tests/ui/attributes/validation-on-associated-items-issue-121537.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: attribute value must be a literal
22
--> $DIR/validation-on-associated-items-issue-121537.rs:2:13
33
|
44
LL | #[doc = MyTrait]
5-
| ^^^^^^^
5+
| ^^^^^^^ help: try adding quotation marks: `"MyTrait"`
66

77
error: aborting due to 1 previous error
88

tests/ui/darwin-objc/darwin-objc-bad-arg.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ error: attribute value must be a literal
22
--> $DIR/darwin-objc-bad-arg.rs:12:18
33
|
44
LL | objc::class!(s);
5-
| ^
5+
| ^ help: try adding quotation marks: `"s"`
66

77
error: attribute value must be a literal
88
--> $DIR/darwin-objc-bad-arg.rs:15:18
99
|
1010
LL | objc::class!(NSObject);
11-
| ^^^^^^^^
11+
| ^^^^^^^^ help: try adding quotation marks: `"NSObject"`
1212

1313
error: `objc::class!` expected a string literal
1414
--> $DIR/darwin-objc-bad-arg.rs:18:18
@@ -26,13 +26,13 @@ error: attribute value must be a literal
2626
--> $DIR/darwin-objc-bad-arg.rs:25:21
2727
|
2828
LL | objc::selector!(s);
29-
| ^
29+
| ^ help: try adding quotation marks: `"s"`
3030

3131
error: attribute value must be a literal
3232
--> $DIR/darwin-objc-bad-arg.rs:28:21
3333
|
3434
LL | objc::selector!(alloc);
35-
| ^^^^^
35+
| ^^^^^ help: try adding quotation marks: `"alloc"`
3636

3737
error: `objc::selector!` expected a string literal
3838
--> $DIR/darwin-objc-bad-arg.rs:31:21

0 commit comments

Comments
 (0)