Skip to main content

flux_macros/
lib.rs

1#![feature(proc_macro_diagnostic, never_type)]
2
3#[allow(clippy::all, clippy::semicolon_if_nothing_returned)] // copied from upstream, kept as is
4mod diagnostics;
5mod fold;
6mod primops;
7mod symbols;
8
9use quote::quote;
10use synstructure::decl_derive;
11
12decl_derive!(
13    [Diagnostic, attributes(
14        // struct and field attributes
15        diag,
16        help,
17        help_once,
18        note,
19        note_once,
20        warning,
21        // field attributes
22        primary_span,
23        label,
24        subdiagnostic,
25        suggestion,
26        suggestion_short,
27        suggestion_hidden,
28        suggestion_verbose)] => diagnostics::diagnostic_derive
29);
30
31decl_derive!(
32    [Subdiagnostic, attributes(
33        // struct/variant attributes
34        label,
35        help,
36        help_once,
37        note,
38        note_once,
39        warning,
40        subdiagnostic,
41        suggestion,
42        suggestion_short,
43        suggestion_hidden,
44        suggestion_verbose,
45        multipart_suggestion,
46        multipart_suggestion_short,
47        multipart_suggestion_hidden,
48        // field attributes
49        primary_span,
50        suggestion_part,
51        applicability)] => diagnostics::subdiagnostic_derive
52);
53
54/// Creates a `DiagMessage` from an inline Fluent pattern. The pattern is checked to be valid
55/// Fluent, but variables are not checked against the arguments set on the diagnostic.
56#[proc_macro]
57pub fn msg(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
58    diagnostics::msg_macro(input)
59}
60
61decl_derive!(
62    [TypeFoldable] => fold::type_foldable_derive
63);
64
65decl_derive!(
66    [TypeVisitable] => fold::type_visitable_derive
67);
68
69#[proc_macro]
70pub fn primop_rules(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
71    primops::primop_rules(input)
72}
73
74decl_derive!(
75    [DebugAsJson] => debug_as_json
76);
77
78fn debug_as_json(s: synstructure::Structure<'_>) -> proc_macro2::TokenStream {
79    s.gen_impl(quote! {
80        gen impl std::fmt::Debug for @Self {
81            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
82                let s = serde_json::to_string(&self).map_err(|_| std::fmt::Error::default())?;
83                write!(f, "{}", s)
84            }
85        }
86    })
87}
88
89#[proc_macro]
90pub fn symbols(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
91    symbols::symbols(input.into()).into()
92}