Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accurately portray raw identifiers in error messages #67010

Merged
merged 3 commits into from
Dec 6, 2019
Merged
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
3 changes: 3 additions & 0 deletions src/librustc/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1282,6 +1282,9 @@ impl<F: fmt::Write> 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
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_parse/parser/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,13 @@ impl<'a> Parser<'a> {
// `./<id>.rs` and `./<id>/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);
Expand Down
6 changes: 6 additions & 0 deletions src/libsyntax_pos/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
22 changes: 22 additions & 0 deletions src/test/ui/issues/issue-65634-raw-ident-suggestion.rs
Original file line number Diff line number Diff line change
@@ -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
}
22 changes: 22 additions & 0 deletions src/test/ui/issues/issue-65634-raw-ident-suggestion.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
error[E0034]: multiple applicable items in scope
--> $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`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r#async

--> $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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be r#async::r#struct(r#fn {}) instead of async::r#struct(r#fn {})

note: candidate #2 is defined in an impl of the trait `await` for the type `r#fn`
--> $DIR/issue-65634-raw-ident-suggestion.rs:10:5
|
LL | fn r#struct(&self) {
| ^^^^^^^^^^^^^^^^^^
= help: to disambiguate the method call, write `await::r#struct(r#fn {})` instead
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r#await::r#struct(r#fn {})


error: aborting due to previous error

For more information about this error, try `rustc --explain E0034`.
4 changes: 2 additions & 2 deletions src/test/ui/parser/raw/raw-literal-keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/parser/raw/raw-literal-keywords.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/suggestions/raw-name-use-suggestion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion src/test/ui/suggestions/raw-name-use-suggestion.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down