Skip to content

Commit 6960ad9

Browse files
authored
Rollup merge of rust-lang#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 e629404 + dc90d1d commit 6960ad9

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
@@ -258,18 +258,6 @@ pub enum SubdiagnosticMessage {
258258
FluentAttr(FluentId),
259259
}
260260

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

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

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

0 commit comments

Comments
 (0)