@@ -5,8 +5,9 @@ use syn::spanned::Spanned;
5
5
6
6
use std:: collections:: { BTreeSet , HashMap } ;
7
7
8
- /// Implements #[derive(SessionDiagnostic)], which allows for errors to be specified as a struct, independent
9
- /// from the actual diagnostics emitting code.
8
+ /// Implements `#[derive(SessionDiagnostic)]`, which allows for errors to be specified as a struct,
9
+ /// independent from the actual diagnostics emitting code.
10
+ ///
10
11
/// ```ignore (pseudo-rust)
11
12
/// # extern crate rustc_errors;
12
13
/// # use rustc_errors::Applicability;
@@ -28,6 +29,7 @@ use std::collections::{BTreeSet, HashMap};
28
29
/// pub opt_sugg: Option<(Span, Applicability)>
29
30
/// }
30
31
/// ```
32
+ ///
31
33
/// Then, later, to emit the error:
32
34
///
33
35
/// ```ignore (pseudo-rust)
@@ -47,10 +49,10 @@ pub fn session_diagnostic_derive(s: synstructure::Structure<'_>) -> proc_macro2:
47
49
SessionDiagnosticDerive :: new ( diag, sess, s) . into_tokens ( )
48
50
}
49
51
50
- // Checks whether the type name of `ty` matches `name`.
51
- //
52
- // Given some struct at a::b::c::Foo, this will return true for c::Foo, b::c::Foo, or
53
- // a::b::c::Foo. This reasonably allows qualified names to be used in the macro.
52
+ /// Checks whether the type name of `ty` matches `name`.
53
+ ///
54
+ /// Given some struct at ` a::b::c::Foo` , this will return true for ` c::Foo`, ` b::c::Foo` , or
55
+ /// ` a::b::c::Foo` . This reasonably allows qualified names to be used in the macro.
54
56
fn type_matches_path ( ty : & syn:: Type , name : & [ & str ] ) -> bool {
55
57
if let syn:: Type :: Path ( ty) = ty {
56
58
ty. path
@@ -65,7 +67,7 @@ fn type_matches_path(ty: &syn::Type, name: &[&str]) -> bool {
65
67
}
66
68
}
67
69
68
- /// The central struct for constructing the as_error method from an annotated struct.
70
+ /// The central struct for constructing the ` as_error` method from an annotated struct.
69
71
struct SessionDiagnosticDerive < ' a > {
70
72
structure : synstructure:: Structure < ' a > ,
71
73
builder : SessionDiagnosticDeriveBuilder < ' a > ,
@@ -77,7 +79,7 @@ impl std::convert::From<syn::Error> for SessionDiagnosticDeriveError {
77
79
}
78
80
}
79
81
80
- /// Equivalent to rustc:errors::diagnostic::DiagnosticId, except stores the quoted expression to
82
+ /// Equivalent to ` rustc:errors::diagnostic::DiagnosticId` , except stores the quoted expression to
81
83
/// initialise the code with.
82
84
enum DiagnosticId {
83
85
Error ( proc_macro2:: TokenStream ) ,
@@ -109,18 +111,19 @@ fn span_err(span: impl proc_macro::MultiSpan, msg: &str) -> proc_macro::Diagnost
109
111
Diagnostic :: spanned ( span, proc_macro:: Level :: Error , msg)
110
112
}
111
113
112
- /// For methods that return a Result<_, SessionDiagnosticDeriveError>: emit a diagnostic on
113
- /// span $span with msg $msg (and, optionally, perform additional decoration using the FnOnce
114
- /// passed in `diag`). Then, return Err(ErrorHandled).
114
+ /// For methods that return a `Result<_, SessionDiagnosticDeriveError>`:
115
+ ///
116
+ /// Emit a diagnostic on span `$span` with msg `$msg` (optionally performing additional decoration
117
+ /// using the `FnOnce` passed in `diag`) and return `Err(ErrorHandled)`.
115
118
macro_rules! throw_span_err {
116
119
( $span: expr, $msg: expr) => { { throw_span_err!( $span, $msg, |diag| diag) } } ;
117
120
( $span: expr, $msg: expr, $f: expr) => { {
118
121
return Err ( _throw_span_err( $span, $msg, $f) ) ;
119
122
} } ;
120
123
}
121
124
122
- /// When possible, prefer using throw_span_err! over using this function directly. This only exists
123
- /// as a function to constrain `f` to an impl FnOnce.
125
+ /// When possible, prefer using ` throw_span_err!` over using this function directly. This only
126
+ /// exists as a function to constrain `f` to an ` impl FnOnce` .
124
127
fn _throw_span_err (
125
128
span : impl proc_macro:: MultiSpan ,
126
129
msg : & str ,
@@ -240,8 +243,8 @@ impl<'a> SessionDiagnosticDerive<'a> {
240
243
}
241
244
}
242
245
243
- /// Field information passed to the builder. Deliberately omits attrs to discourage the generate_*
244
- /// methods from walking the attributes themselves.
246
+ /// Field information passed to the builder. Deliberately omits attrs to discourage the
247
+ /// `generate_*` methods from walking the attributes themselves.
245
248
struct FieldInfo < ' a > {
246
249
vis : & ' a syn:: Visibility ,
247
250
binding : & ' a synstructure:: BindingInfo < ' a > ,
@@ -250,22 +253,22 @@ struct FieldInfo<'a> {
250
253
}
251
254
252
255
/// Tracks persistent information required for building up the individual calls to diagnostic
253
- /// methods for the final generated method. This is a separate struct to SessionDerive only to be
254
- /// able to destructure and split self.builder and the self.structure up to avoid a double mut
255
- /// borrow later on.
256
+ /// methods for the final generated method. This is a separate struct to `SessionDiagnosticDerive`
257
+ /// only to be able to destructure and split ` self.builder` and the ` self.structure` up to avoid a
258
+ /// double mut borrow later on.
256
259
struct SessionDiagnosticDeriveBuilder < ' a > {
257
- /// Name of the session parameter that's passed in to the as_error method.
260
+ /// Name of the session parameter that's passed in to the ` as_error` method.
258
261
sess : syn:: Ident ,
259
262
260
263
/// Store a map of field name to its corresponding field. This is built on construction of the
261
264
/// derive builder.
262
265
fields : HashMap < String , & ' a syn:: Field > ,
263
266
264
- /// The identifier to use for the generated DiagnosticBuilder instance.
267
+ /// The identifier to use for the generated ` DiagnosticBuilder` instance.
265
268
diag : syn:: Ident ,
266
269
267
- /// Whether this is a lint or an error. This dictates how the diag will be initialised. Span
268
- /// stores at what Span the kind was first set at (for error reporting purposes, if the kind
270
+ /// Whether this is a lint or an error. This dictates how the diag will be initialised. ` Span`
271
+ /// stores at what ` Span` the kind was first set at (for error reporting purposes, if the kind
269
272
/// was multiply specified).
270
273
kind : Option < ( DiagnosticId , proc_macro2:: Span ) > ,
271
274
}
@@ -560,20 +563,24 @@ impl<'a> SessionDiagnosticDeriveBuilder<'a> {
560
563
}
561
564
562
565
/// In the strings in the attributes supplied to this macro, we want callers to be able to
563
- /// reference fields in the format string. Take this, for example:
566
+ /// reference fields in the format string. For example:
567
+ ///
564
568
/// ```ignore (not-usage-example)
565
569
/// struct Point {
566
570
/// #[error = "Expected a point greater than ({x}, {y})"]
567
571
/// x: i32,
568
572
/// y: i32,
569
573
/// }
570
574
/// ```
571
- /// We want to automatically pick up that {x} refers `self.x` and {y} refers to `self.y`, then
572
- /// generate this call to format!:
575
+ ///
576
+ /// We want to automatically pick up that `{x}` refers `self.x` and `{y}` refers to `self.y`,
577
+ /// then generate this call to `format!`:
578
+ ///
573
579
/// ```ignore (not-usage-example)
574
580
/// format!("Expected a point greater than ({x}, {y})", x = self.x, y = self.y)
575
581
/// ```
576
- /// This function builds the entire call to format!.
582
+ ///
583
+ /// This function builds the entire call to `format!`.
577
584
fn build_format ( & self , input : & str , span : proc_macro2:: Span ) -> proc_macro2:: TokenStream {
578
585
// This set is used later to generate the final format string. To keep builds reproducible,
579
586
// the iteration order needs to be deterministic, hence why we use a BTreeSet here instead
@@ -646,7 +653,7 @@ impl<'a> SessionDiagnosticDeriveBuilder<'a> {
646
653
}
647
654
}
648
655
649
- /// If `ty` is an Option, returns Some(inner type). Else, returns None.
656
+ /// If `ty` is an Option, returns ` Some(inner type)`, otherwise returns ` None` .
650
657
fn option_inner_ty ( ty : & syn:: Type ) -> Option < & syn:: Type > {
651
658
if type_matches_path ( ty, & [ "std" , "option" , "Option" ] ) {
652
659
if let syn:: Type :: Path ( ty_path) = ty {
0 commit comments