Skip to content

Commit c1a9841

Browse files
committed
Migrate emoji identifier diagnostics to SessionDiagnostic
1 parent a39bdb1 commit c1a9841

File tree

5 files changed

+31
-18
lines changed

5 files changed

+31
-18
lines changed

Diff for: Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4011,6 +4011,7 @@ dependencies = [
40114011
"rustc_hir",
40124012
"rustc_incremental",
40134013
"rustc_lint",
4014+
"rustc_macros",
40144015
"rustc_metadata",
40154016
"rustc_middle",
40164017
"rustc_mir_build",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
interface_ferris_identifier =
2+
Ferris cannot be used as an identifier
3+
.suggestion = try using their name instead
4+
5+
interface_emoji_identifier =
6+
identifiers cannot contain emoji: `{$ident}`

Diff for: compiler/rustc_error_messages/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ fluent_messages! {
3434
builtin_macros => "../locales/en-US/builtin_macros.ftl",
3535
const_eval => "../locales/en-US/const_eval.ftl",
3636
expand => "../locales/en-US/expand.ftl",
37+
interface => "../locales/en-US/interface.ftl",
3738
lint => "../locales/en-US/lint.ftl",
3839
parser => "../locales/en-US/parser.ftl",
3940
passes => "../locales/en-US/passes.ftl",

Diff for: compiler/rustc_interface/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ rustc_attr = { path = "../rustc_attr" }
1717
rustc_borrowck = { path = "../rustc_borrowck" }
1818
rustc_builtin_macros = { path = "../rustc_builtin_macros" }
1919
rustc_expand = { path = "../rustc_expand" }
20+
rustc_macros = { path = "../rustc_macros" }
2021
rustc_parse = { path = "../rustc_parse" }
2122
rustc_session = { path = "../rustc_session" }
2223
rustc_span = { path = "../rustc_span" }

Diff for: compiler/rustc_interface/src/passes.rs

+22-18
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ use rustc_borrowck as mir_borrowck;
88
use rustc_codegen_ssa::traits::CodegenBackend;
99
use rustc_data_structures::parallel;
1010
use rustc_data_structures::sync::{Lrc, OnceCell, WorkerLocal};
11-
use rustc_errors::{Applicability, ErrorGuaranteed, MultiSpan, PResult};
11+
use rustc_errors::{ErrorGuaranteed, PResult};
1212
use rustc_expand::base::{ExtCtxt, LintStoreExpand, ResolverExpand};
1313
use rustc_hir::def_id::StableCrateId;
1414
use rustc_hir::definitions::Definitions;
1515
use rustc_lint::{BufferedEarlyLint, EarlyCheckNode, LintStore};
16+
use rustc_macros::SessionDiagnostic;
1617
use rustc_metadata::creader::CStore;
1718
use rustc_middle::arena::Arena;
1819
use rustc_middle::dep_graph::DepGraph;
@@ -30,7 +31,7 @@ use rustc_session::output::filename_for_input;
3031
use rustc_session::search_paths::PathKind;
3132
use rustc_session::{Limit, Session};
3233
use rustc_span::symbol::{sym, Symbol};
33-
use rustc_span::FileName;
34+
use rustc_span::{FileName, Span};
3435
use rustc_trait_selection::traits;
3536
use rustc_typeck as typeck;
3637
use tracing::{info, warn};
@@ -263,6 +264,23 @@ impl LintStoreExpand for LintStoreExpandImpl<'_> {
263264
}
264265
}
265266

267+
#[derive(SessionDiagnostic)]
268+
#[error(interface::ferris_identifier)]
269+
struct FerrisIdentifier {
270+
#[primary_span]
271+
spans: Vec<Span>,
272+
#[suggestion(code = "ferris", applicability = "maybe-incorrect")]
273+
first_span: Span,
274+
}
275+
276+
#[derive(SessionDiagnostic)]
277+
#[error(interface::emoji_identifier)]
278+
struct EmojiIdentifier {
279+
#[primary_span]
280+
spans: Vec<Span>,
281+
ident: Symbol,
282+
}
283+
266284
/// Runs the "early phases" of the compiler: initial `cfg` processing, loading compiler plugins,
267285
/// syntax expansion, secondary `cfg` expansion, synthesis of a test
268286
/// harness if one is to be provided, injection of a dependency on the
@@ -443,23 +461,9 @@ pub fn configure_and_expand(
443461
spans.sort();
444462
if ident == sym::ferris {
445463
let first_span = spans[0];
446-
sess.diagnostic()
447-
.struct_span_err(
448-
MultiSpan::from(spans),
449-
"Ferris cannot be used as an identifier",
450-
)
451-
.span_suggestion(
452-
first_span,
453-
"try using their name instead",
454-
"ferris",
455-
Applicability::MaybeIncorrect,
456-
)
457-
.emit();
464+
sess.emit_err(FerrisIdentifier { spans, first_span });
458465
} else {
459-
sess.diagnostic().span_err(
460-
MultiSpan::from(spans),
461-
&format!("identifiers cannot contain emoji: `{}`", ident),
462-
);
466+
sess.emit_err(EmojiIdentifier { spans, ident });
463467
}
464468
}
465469
});

0 commit comments

Comments
 (0)