Skip to content

Commit 406579a

Browse files
committed
macros: introduce build_field_mapping
Move the logic for building a field mapping (which is used by the building of format strings in `suggestion` annotations) into a helper function. Signed-off-by: David Wood <[email protected]>
1 parent 540eaf9 commit 406579a

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

compiler/rustc_macros/src/diagnostics/diagnostic.rs

+3-18
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use crate::diagnostics::error::{
55
SessionDiagnosticDeriveError,
66
};
77
use crate::diagnostics::utils::{
8-
report_error_if_not_applied_to_span, report_type_error, type_is_unit, type_matches_path,
9-
Applicability, FieldInfo, FieldInnerTy, HasFieldMap, SetOnce,
8+
build_field_mapping, report_error_if_not_applied_to_span, report_type_error, type_is_unit,
9+
type_matches_path, Applicability, FieldInfo, FieldInnerTy, HasFieldMap, SetOnce,
1010
};
1111
use proc_macro2::{Ident, TokenStream};
1212
use quote::{format_ident, quote};
@@ -25,26 +25,11 @@ pub(crate) struct SessionDiagnosticDerive<'a> {
2525

2626
impl<'a> SessionDiagnosticDerive<'a> {
2727
pub(crate) fn new(diag: syn::Ident, sess: syn::Ident, structure: Structure<'a>) -> Self {
28-
// Build the mapping of field names to fields. This allows attributes to peek values from
29-
// other fields.
30-
let mut fields_map = HashMap::new();
31-
32-
// Convenience bindings.
33-
let ast = structure.ast();
34-
35-
if let syn::Data::Struct(syn::DataStruct { fields, .. }) = &ast.data {
36-
for field in fields.iter() {
37-
if let Some(ident) = &field.ident {
38-
fields_map.insert(ident.to_string(), quote! { &self.#ident });
39-
}
40-
}
41-
}
42-
4328
Self {
4429
builder: SessionDiagnosticDeriveBuilder {
4530
diag,
4631
sess,
47-
fields: fields_map,
32+
fields: build_field_mapping(&structure),
4833
kind: None,
4934
code: None,
5035
slug: None,

compiler/rustc_macros/src/diagnostics/utils.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ use crate::diagnostics::error::{span_err, throw_span_err, SessionDiagnosticDeriv
22
use proc_macro::Span;
33
use proc_macro2::TokenStream;
44
use quote::{format_ident, quote, ToTokens};
5-
use std::collections::BTreeSet;
5+
use std::collections::{BTreeSet, HashMap};
66
use std::str::FromStr;
77
use syn::{spanned::Spanned, Attribute, Meta, Type, TypeTuple};
8-
use synstructure::BindingInfo;
8+
use synstructure::{BindingInfo, Structure};
99

1010
/// Checks whether the type name of `ty` matches `name`.
1111
///
@@ -325,3 +325,20 @@ impl quote::ToTokens for Applicability {
325325
});
326326
}
327327
}
328+
329+
/// Build the mapping of field names to fields. This allows attributes to peek values from
330+
/// other fields.
331+
pub(crate) fn build_field_mapping<'a>(structure: &Structure<'a>) -> HashMap<String, TokenStream> {
332+
let mut fields_map = HashMap::new();
333+
334+
let ast = structure.ast();
335+
if let syn::Data::Struct(syn::DataStruct { fields, .. }) = &ast.data {
336+
for field in fields.iter() {
337+
if let Some(ident) = &field.ident {
338+
fields_map.insert(ident.to_string(), quote! { &self.#ident });
339+
}
340+
}
341+
}
342+
343+
fields_map
344+
}

0 commit comments

Comments
 (0)