Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 2bf64d6

Browse files
committed
macros: update comments
Various small changes to comments, like wrapping code in backticks, changing comments to doc comments and adding newlines. Signed-off-by: David Wood <david.wood@huawei.com>
1 parent d5119c5 commit 2bf64d6

File tree

1 file changed

+34
-27
lines changed

1 file changed

+34
-27
lines changed

compiler/rustc_macros/src/session_diagnostic.rs

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ use syn::spanned::Spanned;
55

66
use std::collections::{BTreeSet, HashMap};
77

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+
///
1011
/// ```ignore (pseudo-rust)
1112
/// # extern crate rustc_errors;
1213
/// # use rustc_errors::Applicability;
@@ -28,6 +29,7 @@ use std::collections::{BTreeSet, HashMap};
2829
/// pub opt_sugg: Option<(Span, Applicability)>
2930
/// }
3031
/// ```
32+
///
3133
/// Then, later, to emit the error:
3234
///
3335
/// ```ignore (pseudo-rust)
@@ -47,10 +49,10 @@ pub fn session_diagnostic_derive(s: synstructure::Structure<'_>) -> proc_macro2:
4749
SessionDiagnosticDerive::new(diag, sess, s).into_tokens()
4850
}
4951

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.
5456
fn type_matches_path(ty: &syn::Type, name: &[&str]) -> bool {
5557
if let syn::Type::Path(ty) = ty {
5658
ty.path
@@ -65,7 +67,7 @@ fn type_matches_path(ty: &syn::Type, name: &[&str]) -> bool {
6567
}
6668
}
6769

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.
6971
struct SessionDiagnosticDerive<'a> {
7072
structure: synstructure::Structure<'a>,
7173
builder: SessionDiagnosticDeriveBuilder<'a>,
@@ -77,7 +79,7 @@ impl std::convert::From<syn::Error> for SessionDiagnosticDeriveError {
7779
}
7880
}
7981

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
8183
/// initialise the code with.
8284
enum DiagnosticId {
8385
Error(proc_macro2::TokenStream),
@@ -109,18 +111,19 @@ fn span_err(span: impl proc_macro::MultiSpan, msg: &str) -> proc_macro::Diagnost
109111
Diagnostic::spanned(span, proc_macro::Level::Error, msg)
110112
}
111113

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)`.
115118
macro_rules! throw_span_err {
116119
($span:expr, $msg:expr) => {{ throw_span_err!($span, $msg, |diag| diag) }};
117120
($span:expr, $msg:expr, $f:expr) => {{
118121
return Err(_throw_span_err($span, $msg, $f));
119122
}};
120123
}
121124

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`.
124127
fn _throw_span_err(
125128
span: impl proc_macro::MultiSpan,
126129
msg: &str,
@@ -240,8 +243,8 @@ impl<'a> SessionDiagnosticDerive<'a> {
240243
}
241244
}
242245

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.
245248
struct FieldInfo<'a> {
246249
vis: &'a syn::Visibility,
247250
binding: &'a synstructure::BindingInfo<'a>,
@@ -250,22 +253,22 @@ struct FieldInfo<'a> {
250253
}
251254

252255
/// 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.
256259
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.
258261
sess: syn::Ident,
259262

260263
/// Store a map of field name to its corresponding field. This is built on construction of the
261264
/// derive builder.
262265
fields: HashMap<String, &'a syn::Field>,
263266

264-
/// The identifier to use for the generated DiagnosticBuilder instance.
267+
/// The identifier to use for the generated `DiagnosticBuilder` instance.
265268
diag: syn::Ident,
266269

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
269272
/// was multiply specified).
270273
kind: Option<(DiagnosticId, proc_macro2::Span)>,
271274
}
@@ -560,20 +563,24 @@ impl<'a> SessionDiagnosticDeriveBuilder<'a> {
560563
}
561564

562565
/// 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+
///
564568
/// ```ignore (not-usage-example)
565569
/// struct Point {
566570
/// #[error = "Expected a point greater than ({x}, {y})"]
567571
/// x: i32,
568572
/// y: i32,
569573
/// }
570574
/// ```
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+
///
573579
/// ```ignore (not-usage-example)
574580
/// format!("Expected a point greater than ({x}, {y})", x = self.x, y = self.y)
575581
/// ```
576-
/// This function builds the entire call to format!.
582+
///
583+
/// This function builds the entire call to `format!`.
577584
fn build_format(&self, input: &str, span: proc_macro2::Span) -> proc_macro2::TokenStream {
578585
// This set is used later to generate the final format string. To keep builds reproducible,
579586
// the iteration order needs to be deterministic, hence why we use a BTreeSet here instead
@@ -646,7 +653,7 @@ impl<'a> SessionDiagnosticDeriveBuilder<'a> {
646653
}
647654
}
648655

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`.
650657
fn option_inner_ty(ty: &syn::Type) -> Option<&syn::Type> {
651658
if type_matches_path(ty, &["std", "option", "Option"]) {
652659
if let syn::Type::Path(ty_path) = ty {

0 commit comments

Comments
 (0)