From f6b435d923e5979cd3579427901d2140a932dfc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Tue, 3 Dec 2019 19:01:42 -0800 Subject: [PATCH 1/3] Accurately portray raw identifiers in error messages When refering to or suggesting raw identifiers, refer to them with `r#`. Fix #65634. --- src/librustc/ty/print/pretty.rs | 3 +++ src/libsyntax_pos/symbol.rs | 6 +++++ .../ui/parser/raw/raw-literal-keywords.rs | 4 ++-- .../ui/parser/raw/raw-literal-keywords.stderr | 4 ++-- src/test/ui/raw-ident-suggestion.rs | 22 +++++++++++++++++++ src/test/ui/raw-ident-suggestion.stderr | 22 +++++++++++++++++++ .../ui/suggestions/raw-name-use-suggestion.rs | 2 +- .../raw-name-use-suggestion.stderr | 2 +- 8 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 src/test/ui/raw-ident-suggestion.rs create mode 100644 src/test/ui/raw-ident-suggestion.stderr diff --git a/src/librustc/ty/print/pretty.rs b/src/librustc/ty/print/pretty.rs index fff2f06e87b8e..745f7d0276d80 100644 --- a/src/librustc/ty/print/pretty.rs +++ b/src/librustc/ty/print/pretty.rs @@ -1282,6 +1282,9 @@ impl Printer<'tcx> for FmtPrinter<'_, 'tcx, F> { if !self.empty_path { write!(self, "::")?; } + if ast::Ident::from_str(&name).is_raw_guess() { + write!(self, "r#")?; + } write!(self, "{}", name)?; // FIXME(eddyb) this will print e.g. `{{closure}}#3`, but it diff --git a/src/libsyntax_pos/symbol.rs b/src/libsyntax_pos/symbol.rs index 88a325112ac6c..73df24a836dc3 100644 --- a/src/libsyntax_pos/symbol.rs +++ b/src/libsyntax_pos/symbol.rs @@ -840,12 +840,18 @@ impl Hash for Ident { impl fmt::Debug for Ident { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if self.is_raw_guess() { + write!(f, "r#")?; + } write!(f, "{}{:?}", self.name, self.span.ctxt()) } } impl fmt::Display for Ident { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if self.is_raw_guess() { + write!(f, "r#")?; + } fmt::Display::fmt(&self.name, f) } } diff --git a/src/test/ui/parser/raw/raw-literal-keywords.rs b/src/test/ui/parser/raw/raw-literal-keywords.rs index bf9cbcdab2e89..a986980fab97b 100644 --- a/src/test/ui/parser/raw/raw-literal-keywords.rs +++ b/src/test/ui/parser/raw/raw-literal-keywords.rs @@ -11,11 +11,11 @@ fn test_union() { } fn test_if_2() { - let _ = r#if; //~ ERROR cannot find value `if` in this scope + let _ = r#if; //~ ERROR cannot find value `r#if` in this scope } fn test_struct_2() { - let _ = r#struct; //~ ERROR cannot find value `struct` in this scope + let _ = r#struct; //~ ERROR cannot find value `r#struct` in this scope } fn test_union_2() { diff --git a/src/test/ui/parser/raw/raw-literal-keywords.stderr b/src/test/ui/parser/raw/raw-literal-keywords.stderr index fd8eda3770d27..f7b6c894a90fe 100644 --- a/src/test/ui/parser/raw/raw-literal-keywords.stderr +++ b/src/test/ui/parser/raw/raw-literal-keywords.stderr @@ -16,13 +16,13 @@ error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found LL | r#union Test; | ^^^^ expected one of 8 possible tokens -error[E0425]: cannot find value `if` in this scope +error[E0425]: cannot find value `r#if` in this scope --> $DIR/raw-literal-keywords.rs:14:13 | LL | let _ = r#if; | ^^^^ not found in this scope -error[E0425]: cannot find value `struct` in this scope +error[E0425]: cannot find value `r#struct` in this scope --> $DIR/raw-literal-keywords.rs:18:13 | LL | let _ = r#struct; diff --git a/src/test/ui/raw-ident-suggestion.rs b/src/test/ui/raw-ident-suggestion.rs new file mode 100644 index 0000000000000..b928510258b2f --- /dev/null +++ b/src/test/ui/raw-ident-suggestion.rs @@ -0,0 +1,22 @@ +#![allow(non_camel_case_types)] + +trait r#async { + fn r#struct(&self) { + println!("async"); + } +} + +trait r#await { + fn r#struct(&self) { + println!("await"); + } +} + +struct r#fn {} + +impl r#async for r#fn {} +impl r#await for r#fn {} + +fn main() { + r#fn {}.r#struct(); //~ ERROR multiple applicable items in scope +} diff --git a/src/test/ui/raw-ident-suggestion.stderr b/src/test/ui/raw-ident-suggestion.stderr new file mode 100644 index 0000000000000..fddd9427ab786 --- /dev/null +++ b/src/test/ui/raw-ident-suggestion.stderr @@ -0,0 +1,22 @@ +error[E0034]: multiple applicable items in scope + --> $DIR/raw-ident-suggestion.rs:21:13 + | +LL | r#fn {}.r#struct(); + | ^^^^^^^^ multiple `r#struct` found + | +note: candidate #1 is defined in an impl of the trait `async` for the type `r#fn` + --> $DIR/raw-ident-suggestion.rs:4:5 + | +LL | fn r#struct(&self) { + | ^^^^^^^^^^^^^^^^^^ + = help: to disambiguate the method call, write `async::r#struct(r#fn {})` instead +note: candidate #2 is defined in an impl of the trait `await` for the type `r#fn` + --> $DIR/raw-ident-suggestion.rs:10:5 + | +LL | fn r#struct(&self) { + | ^^^^^^^^^^^^^^^^^^ + = help: to disambiguate the method call, write `await::r#struct(r#fn {})` instead + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0034`. diff --git a/src/test/ui/suggestions/raw-name-use-suggestion.rs b/src/test/ui/suggestions/raw-name-use-suggestion.rs index 6c01383d9610d..0a8073c0be2ea 100644 --- a/src/test/ui/suggestions/raw-name-use-suggestion.rs +++ b/src/test/ui/suggestions/raw-name-use-suggestion.rs @@ -5,5 +5,5 @@ mod foo { fn main() { foo::let(); //~ ERROR expected identifier, found keyword `let` - r#break(); //~ ERROR cannot find function `break` in this scope + r#break(); //~ ERROR cannot find function `r#break` in this scope } diff --git a/src/test/ui/suggestions/raw-name-use-suggestion.stderr b/src/test/ui/suggestions/raw-name-use-suggestion.stderr index 58eb87c00a411..62b76318e09b5 100644 --- a/src/test/ui/suggestions/raw-name-use-suggestion.stderr +++ b/src/test/ui/suggestions/raw-name-use-suggestion.stderr @@ -20,7 +20,7 @@ help: you can escape reserved keywords to use them as identifiers LL | foo::r#let(); | ^^^^^ -error[E0425]: cannot find function `break` in this scope +error[E0425]: cannot find function `r#break` in this scope --> $DIR/raw-name-use-suggestion.rs:8:5 | LL | r#break(); From b5ad0cb03302d679e5dc7d7d1157c42ccd72b5b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Tue, 3 Dec 2019 22:25:15 -0800 Subject: [PATCH 2/3] review comments: move test --- .../issue-65634-raw-ident-suggestion.rs} | 0 .../issue-65634-raw-ident-suggestion.stderr} | 6 +++--- 2 files changed, 3 insertions(+), 3 deletions(-) rename src/test/ui/{raw-ident-suggestion.rs => issues/issue-65634-raw-ident-suggestion.rs} (100%) rename src/test/ui/{raw-ident-suggestion.stderr => issues/issue-65634-raw-ident-suggestion.stderr} (81%) diff --git a/src/test/ui/raw-ident-suggestion.rs b/src/test/ui/issues/issue-65634-raw-ident-suggestion.rs similarity index 100% rename from src/test/ui/raw-ident-suggestion.rs rename to src/test/ui/issues/issue-65634-raw-ident-suggestion.rs diff --git a/src/test/ui/raw-ident-suggestion.stderr b/src/test/ui/issues/issue-65634-raw-ident-suggestion.stderr similarity index 81% rename from src/test/ui/raw-ident-suggestion.stderr rename to src/test/ui/issues/issue-65634-raw-ident-suggestion.stderr index fddd9427ab786..c7bb653dc1f14 100644 --- a/src/test/ui/raw-ident-suggestion.stderr +++ b/src/test/ui/issues/issue-65634-raw-ident-suggestion.stderr @@ -1,17 +1,17 @@ error[E0034]: multiple applicable items in scope - --> $DIR/raw-ident-suggestion.rs:21:13 + --> $DIR/issue-65634-raw-ident-suggestion.rs:21:13 | LL | r#fn {}.r#struct(); | ^^^^^^^^ multiple `r#struct` found | note: candidate #1 is defined in an impl of the trait `async` for the type `r#fn` - --> $DIR/raw-ident-suggestion.rs:4:5 + --> $DIR/issue-65634-raw-ident-suggestion.rs:4:5 | LL | fn r#struct(&self) { | ^^^^^^^^^^^^^^^^^^ = help: to disambiguate the method call, write `async::r#struct(r#fn {})` instead note: candidate #2 is defined in an impl of the trait `await` for the type `r#fn` - --> $DIR/raw-ident-suggestion.rs:10:5 + --> $DIR/issue-65634-raw-ident-suggestion.rs:10:5 | LL | fn r#struct(&self) { | ^^^^^^^^^^^^^^^^^^ From 0103308ad3745109600541e139af5571838b8791 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Tue, 3 Dec 2019 22:25:44 -0800 Subject: [PATCH 3/3] Account for raw idents in module file finding --- src/librustc_parse/parser/module.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/librustc_parse/parser/module.rs b/src/librustc_parse/parser/module.rs index 59d7c2b423972..807defc51c1d3 100644 --- a/src/librustc_parse/parser/module.rs +++ b/src/librustc_parse/parser/module.rs @@ -212,13 +212,13 @@ impl<'a> Parser<'a> { // `./.rs` and `.//mod.rs`. let relative_prefix_string; let relative_prefix = if let Some(ident) = relative { - relative_prefix_string = format!("{}{}", ident, path::MAIN_SEPARATOR); + relative_prefix_string = format!("{}{}", ident.name, path::MAIN_SEPARATOR); &relative_prefix_string } else { "" }; - let mod_name = id.to_string(); + let mod_name = id.name.to_string(); let default_path_str = format!("{}{}.rs", relative_prefix, mod_name); let secondary_path_str = format!("{}{}{}mod.rs", relative_prefix, mod_name, path::MAIN_SEPARATOR);