Skip to content

Commit f6b435d

Browse files
committed
Accurately portray raw identifiers in error messages
When refering to or suggesting raw identifiers, refer to them with `r#`. Fix #65634.
1 parent 7afe6d9 commit f6b435d

File tree

8 files changed

+59
-6
lines changed

8 files changed

+59
-6
lines changed

src/librustc/ty/print/pretty.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,9 @@ impl<F: fmt::Write> Printer<'tcx> for FmtPrinter<'_, 'tcx, F> {
12821282
if !self.empty_path {
12831283
write!(self, "::")?;
12841284
}
1285+
if ast::Ident::from_str(&name).is_raw_guess() {
1286+
write!(self, "r#")?;
1287+
}
12851288
write!(self, "{}", name)?;
12861289

12871290
// FIXME(eddyb) this will print e.g. `{{closure}}#3`, but it

src/libsyntax_pos/symbol.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,12 +840,18 @@ impl Hash for Ident {
840840

841841
impl fmt::Debug for Ident {
842842
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
843+
if self.is_raw_guess() {
844+
write!(f, "r#")?;
845+
}
843846
write!(f, "{}{:?}", self.name, self.span.ctxt())
844847
}
845848
}
846849

847850
impl fmt::Display for Ident {
848851
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
852+
if self.is_raw_guess() {
853+
write!(f, "r#")?;
854+
}
849855
fmt::Display::fmt(&self.name, f)
850856
}
851857
}

src/test/ui/parser/raw/raw-literal-keywords.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ fn test_union() {
1111
}
1212

1313
fn test_if_2() {
14-
let _ = r#if; //~ ERROR cannot find value `if` in this scope
14+
let _ = r#if; //~ ERROR cannot find value `r#if` in this scope
1515
}
1616

1717
fn test_struct_2() {
18-
let _ = r#struct; //~ ERROR cannot find value `struct` in this scope
18+
let _ = r#struct; //~ ERROR cannot find value `r#struct` in this scope
1919
}
2020

2121
fn test_union_2() {

src/test/ui/parser/raw/raw-literal-keywords.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found
1616
LL | r#union Test;
1717
| ^^^^ expected one of 8 possible tokens
1818

19-
error[E0425]: cannot find value `if` in this scope
19+
error[E0425]: cannot find value `r#if` in this scope
2020
--> $DIR/raw-literal-keywords.rs:14:13
2121
|
2222
LL | let _ = r#if;
2323
| ^^^^ not found in this scope
2424

25-
error[E0425]: cannot find value `struct` in this scope
25+
error[E0425]: cannot find value `r#struct` in this scope
2626
--> $DIR/raw-literal-keywords.rs:18:13
2727
|
2828
LL | let _ = r#struct;

src/test/ui/raw-ident-suggestion.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#![allow(non_camel_case_types)]
2+
3+
trait r#async {
4+
fn r#struct(&self) {
5+
println!("async");
6+
}
7+
}
8+
9+
trait r#await {
10+
fn r#struct(&self) {
11+
println!("await");
12+
}
13+
}
14+
15+
struct r#fn {}
16+
17+
impl r#async for r#fn {}
18+
impl r#await for r#fn {}
19+
20+
fn main() {
21+
r#fn {}.r#struct(); //~ ERROR multiple applicable items in scope
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0034]: multiple applicable items in scope
2+
--> $DIR/raw-ident-suggestion.rs:21:13
3+
|
4+
LL | r#fn {}.r#struct();
5+
| ^^^^^^^^ multiple `r#struct` found
6+
|
7+
note: candidate #1 is defined in an impl of the trait `async` for the type `r#fn`
8+
--> $DIR/raw-ident-suggestion.rs:4:5
9+
|
10+
LL | fn r#struct(&self) {
11+
| ^^^^^^^^^^^^^^^^^^
12+
= help: to disambiguate the method call, write `async::r#struct(r#fn {})` instead
13+
note: candidate #2 is defined in an impl of the trait `await` for the type `r#fn`
14+
--> $DIR/raw-ident-suggestion.rs:10:5
15+
|
16+
LL | fn r#struct(&self) {
17+
| ^^^^^^^^^^^^^^^^^^
18+
= help: to disambiguate the method call, write `await::r#struct(r#fn {})` instead
19+
20+
error: aborting due to previous error
21+
22+
For more information about this error, try `rustc --explain E0034`.

src/test/ui/suggestions/raw-name-use-suggestion.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ mod foo {
55

66
fn main() {
77
foo::let(); //~ ERROR expected identifier, found keyword `let`
8-
r#break(); //~ ERROR cannot find function `break` in this scope
8+
r#break(); //~ ERROR cannot find function `r#break` in this scope
99
}

src/test/ui/suggestions/raw-name-use-suggestion.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ help: you can escape reserved keywords to use them as identifiers
2020
LL | foo::r#let();
2121
| ^^^^^
2222

23-
error[E0425]: cannot find function `break` in this scope
23+
error[E0425]: cannot find function `r#break` in this scope
2424
--> $DIR/raw-name-use-suggestion.rs:8:5
2525
|
2626
LL | r#break();

0 commit comments

Comments
 (0)