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

Improve 'cannot contain emoji' error. #91476

Merged
merged 2 commits into from
Dec 9, 2021
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
26 changes: 21 additions & 5 deletions compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
);
}
}
});

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,7 @@ symbols! {
fdiv_fast,
feature,
fence,
ferris: "🦀",
fetch_update,
ffi,
ffi_const,
Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/parser/emoji-identifiers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(🦀);
}
10 changes: 9 additions & 1 deletion src/test/ui/parser/emoji-identifiers.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
|
Expand Down Expand Up @@ -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`.