Skip to content
Open
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
17 changes: 15 additions & 2 deletions compiler/rustc_attr_parsing/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use rustc_ast::{
AttrArgs, Expr, ExprKind, LitKind, MetaItemLit, Path, PathSegment, StmtKind, UnOp,
};
use rustc_ast_pretty::pprust;
use rustc_errors::{Diag, PResult};
use rustc_errors::{Applicability, Diag, PResult};
use rustc_hir::{self as hir, AttrPath};
use rustc_parse::exp;
use rustc_parse::parser::{ForceCollect, Parser, PathStyle, Recovery, token_descr};
Expand Down Expand Up @@ -410,7 +410,20 @@ fn expr_to_lit<'sess>(
// - `#[foo = include_str!("nonexistent-file.rs")]`:
// results in `ast::ExprKind::Err`.
let msg = "attribute value must be a literal";
let err = psess.dcx().struct_span_err(span, msg);
let mut err = psess.dcx().struct_span_err(span, msg);

// 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()
{
err.span_suggestion(
expr.span,
"try adding quotation marks",
&format!("\"{}\"", segment.ident),
Applicability::MaybeIncorrect,
);
}

Err(err)
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/attributes/crate-type-non-crate.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ 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"`

error[E0539]: malformed `crate_type` attribute input
--> $DIR/crate-type-non-crate.rs:12:1
Expand Down Expand Up @@ -72,7 +72,7 @@ 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"`

error[E0539]: malformed `crate_type` attribute input
--> $DIR/crate-type-non-crate.rs:17:1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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"`

error: aborting due to 1 previous error

Loading