Skip to content

Commit dc834f0

Browse files
authored
Rollup merge of rust-lang#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`
2 parents 793648a + 9d535b4 commit dc834f0

File tree

4 files changed

+34
-6
lines changed

4 files changed

+34
-6
lines changed

β€Žcompiler/rustc_interface/src/passes.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_codegen_ssa::traits::CodegenBackend;
1010
use rustc_data_structures::parallel;
1111
use rustc_data_structures::sync::{Lrc, OnceCell, WorkerLocal};
1212
use rustc_data_structures::temp_dir::MaybeTempDir;
13-
use rustc_errors::{ErrorReported, PResult};
13+
use rustc_errors::{Applicability, ErrorReported, PResult};
1414
use rustc_expand::base::ExtCtxt;
1515
use rustc_hir::def_id::{StableCrateId, LOCAL_CRATE};
1616
use rustc_hir::Crate;
@@ -456,10 +456,26 @@ pub fn configure_and_expand(
456456
identifiers.sort_by_key(|&(key, _)| key);
457457
for (ident, mut spans) in identifiers.into_iter() {
458458
spans.sort();
459-
sess.diagnostic().span_err(
460-
MultiSpan::from(spans),
461-
&format!("identifiers cannot contain emoji: `{}`", ident),
462-
);
459+
if ident == sym::ferris {
460+
let first_span = spans[0];
461+
sess.diagnostic()
462+
.struct_span_err(
463+
MultiSpan::from(spans),
464+
"Ferris cannot be used as an identifier",
465+
)
466+
.span_suggestion(
467+
first_span,
468+
"try using their name instead",
469+
"ferris".to_string(),
470+
Applicability::MaybeIncorrect,
471+
)
472+
.emit();
473+
} else {
474+
sess.diagnostic().span_err(
475+
MultiSpan::from(spans),
476+
&format!("identifiers cannot contain emoji: `{}`", ident),
477+
);
478+
}
463479
}
464480
});
465481

β€Žcompiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,7 @@ symbols! {
630630
fdiv_fast,
631631
feature,
632632
fence,
633+
ferris: "πŸ¦€",
633634
fetch_update,
634635
ffi,
635636
ffi_const,

β€Žsrc/test/ui/parser/emoji-identifiers.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,7 @@ fn main() {
1313
let _ = i_like_to_πŸ˜„_a_lot() βž– 4; //~ ERROR cannot find function `i_like_to_πŸ˜„_a_lot` in this scope
1414
//~^ ERROR identifiers cannot contain emoji
1515
//~| ERROR unknown start of token: \u{2796}
16+
17+
let πŸ¦€ = 1;//~ ERROR Ferris cannot be used as an identifier
18+
dbg!(πŸ¦€);
1619
}

β€Žsrc/test/ui/parser/emoji-identifiers.stderr

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ LL | fn i_like_to_πŸ˜…_a_lot() -> πŸ‘€ {
1818
LL | let _ = i_like_to_πŸ˜„_a_lot() βž– 4;
1919
| ^^^^^^^^^^^^^^^^^^ help: a function with a similar name exists: `i_like_to_πŸ˜…_a_lot`
2020

21+
error: Ferris cannot be used as an identifier
22+
--> $DIR/emoji-identifiers.rs:17:9
23+
|
24+
LL | let πŸ¦€ = 1;
25+
| ^^ help: try using their name instead: `ferris`
26+
LL | dbg!(πŸ¦€);
27+
| ^^
28+
2129
error: identifiers cannot contain emoji: `ABigπŸ‘©πŸ‘©πŸ‘§πŸ‘§Family`
2230
--> $DIR/emoji-identifiers.rs:1:8
2331
|
@@ -77,7 +85,7 @@ LL | πŸ‘€::full_of✨()
7785
| function or associated item not found in `πŸ‘€`
7886
| help: there is an associated function with a similar name: `full_of_✨`
7987

80-
error: aborting due to 9 previous errors
88+
error: aborting due to 10 previous errors
8189

8290
Some errors have detailed explanations: E0425, E0599.
8391
For more information about an error, try `rustc --explain E0425`.

0 commit comments

Comments
Β (0)