1
Fork 0

errors: simplify referring to fluent attributes

To render the message of a Fluent attribute, the identifier of the
Fluent message must be known. `DiagnosticMessage::FluentIdentifier`
contains both the message's identifier and optionally the identifier of
an attribute. Generated constants for each attribute would therefore
need to be named uniquely (amongst all error messages) or be able to
refer to only the attribute identifier which will be combined with a
message identifier later. In this commit, the latter strategy is
implemented as part of the `Diagnostic` type's functions for adding
subdiagnostics of various kinds.

Signed-off-by: David Wood <david.wood@huawei.com>
This commit is contained in:
David Wood 2022-05-24 15:09:47 +01:00
parent 855fc022fe
commit f669b78ffc
13 changed files with 190 additions and 150 deletions

View file

@ -11,7 +11,7 @@ use proc_macro::{Diagnostic, Level, Span};
use proc_macro2::TokenStream;
use quote::quote;
use std::{
collections::HashMap,
collections::{HashMap, HashSet},
fs::File,
io::Read,
path::{Path, PathBuf},
@ -100,6 +100,10 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
let ident_span = res.ident.span().unwrap();
let path_span = res.resource.span().unwrap();
// Set of Fluent attribute names already output, to avoid duplicate type errors - any given
// constant created for a given attribute is the same.
let mut previous_attrs = HashSet::new();
let relative_ftl_path = res.resource.value();
let absolute_ftl_path =
invocation_relative_path_to_absolute(ident_span, &relative_ftl_path);
@ -199,13 +203,15 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
});
for Attribute { id: Identifier { name: attr_name }, .. } in attributes {
let attr_snake_name = attr_name.replace("-", "_");
let snake_name = Ident::new(&format!("{snake_name}_{attr_snake_name}"), span);
let snake_name = Ident::new(&attr_name.replace("-", "_"), span);
if !previous_attrs.insert(snake_name.clone()) {
continue;
}
constants.extend(quote! {
pub const #snake_name: crate::DiagnosticMessage =
crate::DiagnosticMessage::FluentIdentifier(
std::borrow::Cow::Borrowed(#name),
Some(std::borrow::Cow::Borrowed(#attr_name))
pub const #snake_name: crate::SubdiagnosticMessage =
crate::SubdiagnosticMessage::FluentAttr(
std::borrow::Cow::Borrowed(#attr_name)
);
});
}