about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias KrΓΌger <matthias.krueger@famsik.de>2021-12-09 05:02:20 +0100
committerGitHub <noreply@github.com>2021-12-09 05:02:20 +0100
commitdc834f08ba3ac4dbfade58c8639b933bd81c6a82 (patch)
treebdee14570a4974ef900fb2bc7b89e77dee6c75c6
parent793648ae836f80443db143d0aafc3504afdeb2d2 (diff)
parent9d535b4c04b81a54ba4b6143829349fb46fadc4c (diff)
downloadrust-dc834f08ba3ac4dbfade58c8639b933bd81c6a82.tar.gz
rust-dc834f08ba3ac4dbfade58c8639b933bd81c6a82.zip
Rollup merge of #91476 - m-ou-se:ferris-identifier, r=estebank
Improve 'cannot contain emoji' error.

Before:

```
error: identifiers cannot contain emoji: `πŸ¦€`
 --> src/main.rs:2:9
  |
2 |     let πŸ¦€ = 1;
  |         ^^
```

After:
```
error: Ferris cannot be used as an identifier
 --> src/main.rs:2:9
  |
2 |     let πŸ¦€ = 1;
  |         ^^ help: try using their name instead: `ferris`
```

r? `@estebank`
-rw-r--r--compiler/rustc_interface/src/passes.rs26
-rw-r--r--compiler/rustc_span/src/symbol.rs1
-rw-r--r--src/test/ui/parser/emoji-identifiers.rs3
-rw-r--r--src/test/ui/parser/emoji-identifiers.stderr10
4 files changed, 34 insertions, 6 deletions
diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs
index dbee92cf598..da76f221269 100644
--- a/compiler/rustc_interface/src/passes.rs
+++ b/compiler/rustc_interface/src/passes.rs
@@ -10,7 +10,7 @@ use rustc_codegen_ssa::traits::CodegenBackend;
 use rustc_data_structures::parallel;
 use rustc_data_structures::sync::{Lrc, OnceCell, WorkerLocal};
 use rustc_data_structures::temp_dir::MaybeTempDir;
-use rustc_errors::{ErrorReported, PResult};
+use rustc_errors::{Applicability, ErrorReported, PResult};
 use rustc_expand::base::ExtCtxt;
 use rustc_hir::def_id::{StableCrateId, LOCAL_CRATE};
 use rustc_hir::Crate;
@@ -456,10 +456,26 @@ pub fn configure_and_expand(
         identifiers.sort_by_key(|&(key, _)| key);
         for (ident, mut spans) in identifiers.into_iter() {
             spans.sort();
-            sess.diagnostic().span_err(
-                MultiSpan::from(spans),
-                &format!("identifiers cannot contain emoji: `{}`", ident),
-            );
+            if ident == sym::ferris {
+                let first_span = spans[0];
+                sess.diagnostic()
+                    .struct_span_err(
+                        MultiSpan::from(spans),
+                        "Ferris cannot be used as an identifier",
+                    )
+                    .span_suggestion(
+                        first_span,
+                        "try using their name instead",
+                        "ferris".to_string(),
+                        Applicability::MaybeIncorrect,
+                    )
+                    .emit();
+            } else {
+                sess.diagnostic().span_err(
+                    MultiSpan::from(spans),
+                    &format!("identifiers cannot contain emoji: `{}`", ident),
+                );
+            }
         }
     });
 
diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs
index a4280047c70..492cecf7d67 100644
--- a/compiler/rustc_span/src/symbol.rs
+++ b/compiler/rustc_span/src/symbol.rs
@@ -630,6 +630,7 @@ symbols! {
         fdiv_fast,
         feature,
         fence,
+        ferris: "πŸ¦€",
         fetch_update,
         ffi,
         ffi_const,
diff --git a/src/test/ui/parser/emoji-identifiers.rs b/src/test/ui/parser/emoji-identifiers.rs
index ef18939bbb8..b50c046bcb2 100644
--- a/src/test/ui/parser/emoji-identifiers.rs
+++ b/src/test/ui/parser/emoji-identifiers.rs
@@ -13,4 +13,7 @@ fn main() {
     let _ = i_like_to_πŸ˜„_a_lot() βž– 4; //~ ERROR cannot find function `i_like_to_πŸ˜„_a_lot` in this scope
     //~^ ERROR identifiers cannot contain emoji
     //~| ERROR unknown start of token: \u{2796}
+
+    let πŸ¦€ = 1;//~ ERROR Ferris cannot be used as an identifier
+    dbg!(πŸ¦€);
 }
diff --git a/src/test/ui/parser/emoji-identifiers.stderr b/src/test/ui/parser/emoji-identifiers.stderr
index 5f9263c4c13..7dc589e5563 100644
--- a/src/test/ui/parser/emoji-identifiers.stderr
+++ b/src/test/ui/parser/emoji-identifiers.stderr
@@ -18,6 +18,14 @@ LL | fn i_like_to_πŸ˜…_a_lot() -> πŸ‘€ {
 LL |     let _ = i_like_to_πŸ˜„_a_lot() βž– 4;
    |             ^^^^^^^^^^^^^^^^^^ help: a function with a similar name exists: `i_like_to_πŸ˜…_a_lot`
 
+error: Ferris cannot be used as an identifier
+  --> $DIR/emoji-identifiers.rs:17:9
+   |
+LL |     let πŸ¦€ = 1;
+   |         ^^ help: try using their name instead: `ferris`
+LL |     dbg!(πŸ¦€);
+   |          ^^
+
 error: identifiers cannot contain emoji: `ABigπŸ‘©πŸ‘©πŸ‘§πŸ‘§Family`
   --> $DIR/emoji-identifiers.rs:1:8
    |
@@ -77,7 +85,7 @@ LL |     πŸ‘€::full_of✨()
    |         function or associated item not found in `πŸ‘€`
    |         help: there is an associated function with a similar name: `full_of_✨`
 
-error: aborting due to 9 previous errors
+error: aborting due to 10 previous errors
 
 Some errors have detailed explanations: E0425, E0599.
 For more information about an error, try `rustc --explain E0425`.