Skip to content

Commit 24cde0d

Browse files
authored
Rollup merge of #98428 - davidtwco:translation-derive-typed-identifiers, r=oli-obk
macros: use typed identifiers in diag and subdiag derive Using typed identifiers instead of strings with the Fluent identifiers in the diagnostic and subdiagnostic derives - this enables the diagnostic derive to benefit from the compile-time validation that comes with typed identifiers, namely that use of a non-existent Fluent identifier will not compile. r? `@oli-obk`
2 parents cc793cd + dc90d1d commit 24cde0d

File tree

15 files changed

+971
-669
lines changed

15 files changed

+971
-669
lines changed

compiler/rustc_builtin_macros/src/cfg.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ pub fn expand_cfg(
3636
}
3737

3838
#[derive(SessionDiagnostic)]
39-
#[error(slug = "builtin-macros-requires-cfg-pattern")]
39+
#[error(builtin_macros::requires_cfg_pattern)]
4040
struct RequiresCfgPattern {
4141
#[primary_span]
4242
#[label]
4343
span: Span,
4444
}
4545

4646
#[derive(SessionDiagnostic)]
47-
#[error(slug = "builtin-macros-expected-one-cfg-pattern")]
47+
#[error(builtin_macros::expected_one_cfg_pattern)]
4848
struct OneCfgPattern {
4949
#[primary_span]
5050
span: Span,
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
builtin-macros-requires-cfg-pattern =
1+
builtin_macros-requires-cfg-pattern =
22
macro requires a cfg-pattern as an argument
33
.label = cfg-pattern required
44
5-
builtin-macros-expected-one-cfg-pattern = expected 1 cfg-pattern
5+
builtin_macros-expected-one-cfg-pattern = expected 1 cfg-pattern

compiler/rustc_error_messages/src/lib.rs

+21-17
Original file line numberDiff line numberDiff line change
@@ -259,18 +259,6 @@ pub enum SubdiagnosticMessage {
259259
FluentAttr(FluentId),
260260
}
261261

262-
impl SubdiagnosticMessage {
263-
/// Create a `SubdiagnosticMessage` for the provided Fluent attribute.
264-
pub fn attr(id: impl Into<FluentId>) -> Self {
265-
SubdiagnosticMessage::FluentAttr(id.into())
266-
}
267-
268-
/// Create a `SubdiagnosticMessage` for the provided Fluent identifier.
269-
pub fn message(id: impl Into<FluentId>) -> Self {
270-
SubdiagnosticMessage::FluentIdentifier(id.into())
271-
}
272-
}
273-
274262
/// `From` impl that enables existing diagnostic calls to functions which now take
275263
/// `impl Into<SubdiagnosticMessage>` to continue to work as before.
276264
impl<S: Into<String>> From<S> for SubdiagnosticMessage {
@@ -333,11 +321,6 @@ impl DiagnosticMessage {
333321
_ => panic!("expected non-translatable diagnostic message"),
334322
}
335323
}
336-
337-
/// Create a `DiagnosticMessage` for the provided Fluent identifier.
338-
pub fn new(id: impl Into<FluentId>) -> Self {
339-
DiagnosticMessage::FluentIdentifier(id.into(), None)
340-
}
341324
}
342325

343326
/// `From` impl that enables existing diagnostic calls to functions which now take
@@ -348,6 +331,27 @@ impl<S: Into<String>> From<S> for DiagnosticMessage {
348331
}
349332
}
350333

334+
/// Translating *into* a subdiagnostic message from a diagnostic message is a little strange - but
335+
/// the subdiagnostic functions (e.g. `span_label`) take a `SubdiagnosticMessage` and the
336+
/// subdiagnostic derive refers to typed identifiers that are `DiagnosticMessage`s, so need to be
337+
/// able to convert between these, as much as they'll be converted back into `DiagnosticMessage`
338+
/// using `with_subdiagnostic_message` eventually. Don't use this other than for the derive.
339+
impl Into<SubdiagnosticMessage> for DiagnosticMessage {
340+
fn into(self) -> SubdiagnosticMessage {
341+
match self {
342+
DiagnosticMessage::Str(s) => SubdiagnosticMessage::Str(s),
343+
DiagnosticMessage::FluentIdentifier(id, None) => {
344+
SubdiagnosticMessage::FluentIdentifier(id)
345+
}
346+
// There isn't really a sensible behaviour for this because it loses information but
347+
// this is the most sensible of the behaviours.
348+
DiagnosticMessage::FluentIdentifier(_, Some(attr)) => {
349+
SubdiagnosticMessage::FluentAttr(attr)
350+
}
351+
}
352+
}
353+
}
354+
351355
/// A span together with some additional data.
352356
#[derive(Clone, Debug)]
353357
pub struct SpanLabel {

0 commit comments

Comments
 (0)