Skip to content

Commit e8c1d77

Browse files
committed
Rollup merge of #23428 - Manishearth:ast-doc, r=steveklabnik
I often have to run `ast-json` or look into the pretty-printer source to figure out what the fields of an AST enum mean. I've tried to document most of what I know (and some semi-obvious stuff). r? @steveklabnik f? @eddyb
2 parents 760f870 + c20c652 commit e8c1d77

File tree

1 file changed

+42
-15
lines changed

1 file changed

+42
-15
lines changed

src/libsyntax/ast.rs

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@ pub struct Lifetime {
215215
pub name: Name
216216
}
217217

218-
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
219218
/// A lifetime definition, eg `'a: 'b+'c+'d`
219+
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
220220
pub struct LifetimeDef {
221221
pub lifetime: Lifetime,
222222
pub bounds: Vec<Lifetime>
@@ -590,6 +590,7 @@ pub enum Pat_ {
590590
/// A PatIdent may either be a new bound variable,
591591
/// or a nullary enum (in which case the third field
592592
/// is None).
593+
///
593594
/// In the nullary enum case, the parser can't determine
594595
/// which it is. The resolver determines this, and
595596
/// records this pattern's NodeId in an auxiliary
@@ -786,18 +787,22 @@ pub enum Expr_ {
786787
/// An array (`[a, b, c, d]`)
787788
ExprVec(Vec<P<Expr>>),
788789
/// A function call
790+
///
789791
/// The first field resolves to the function itself,
790792
/// and the second field is the list of arguments
791793
ExprCall(P<Expr>, Vec<P<Expr>>),
792794
/// A method call (`x.foo::<Bar, Baz>(a, b, c, d)`)
793-
/// The `SpannedIdent` is the identifier for the method name
795+
///
796+
/// The `SpannedIdent` is the identifier for the method name.
794797
/// The vector of `Ty`s are the ascripted type parameters for the method
795-
/// (within the angle brackets)
798+
/// (within the angle brackets).
799+
///
796800
/// The first element of the vector of `Expr`s is the expression that evaluates
797801
/// to the object on which the method is being called on (the receiver),
798802
/// and the remaining elements are the rest of the arguments.
803+
///
799804
/// Thus, `x.foo::<Bar, Baz>(a, b, c, d)` is represented as
800-
/// `ExprMethodCall(foo, [Bar, Baz], [x, a, b, c, d])`
805+
/// `ExprMethodCall(foo, [Bar, Baz], [x, a, b, c, d])`.
801806
ExprMethodCall(SpannedIdent, Vec<P<Ty>>, Vec<P<Expr>>),
802807
/// A tuple (`(a, b, c ,d)`)
803808
ExprTup(Vec<P<Expr>>),
@@ -810,32 +815,41 @@ pub enum Expr_ {
810815
/// A cast (`foo as f64`)
811816
ExprCast(P<Expr>, P<Ty>),
812817
/// An `if` block, with an optional else block
818+
///
813819
/// `if expr { block } else { expr }`
814820
ExprIf(P<Expr>, P<Block>, Option<P<Expr>>),
815821
/// An `if let` expression with an optional else block
822+
///
816823
/// `if let pat = expr { block } else { expr }`
817-
/// This is desugared to a `match` expression
824+
///
825+
/// This is desugared to a `match` expression.
818826
ExprIfLet(P<Pat>, P<Expr>, P<Block>, Option<P<Expr>>),
819827
// FIXME #6993: change to Option<Name> ... or not, if these are hygienic.
820828
/// A while loop, with an optional label
829+
///
821830
/// `'label: while expr { block }`
822831
ExprWhile(P<Expr>, P<Block>, Option<Ident>),
823832
// FIXME #6993: change to Option<Name> ... or not, if these are hygienic.
824833
/// A while-let loop, with an optional label
834+
///
825835
/// `'label: while let pat = expr { block }`
826-
/// This is desugared to a combination of `loop` and `match` expressions
836+
///
837+
/// This is desugared to a combination of `loop` and `match` expressions.
827838
ExprWhileLet(P<Pat>, P<Expr>, P<Block>, Option<Ident>),
828839
// FIXME #6993: change to Option<Name> ... or not, if these are hygienic.
829840
/// A for loop, with an optional label
841+
///
830842
/// `'label: for pat in expr { block }`
831-
/// This is desugared to a combination of `loop` and `match` expressions
843+
///
844+
/// This is desugared to a combination of `loop` and `match` expressions.
832845
ExprForLoop(P<Pat>, P<Expr>, P<Block>, Option<Ident>),
833846
/// Conditionless loop (can be exited with break, continue, or return)
847+
///
834848
/// `'label: loop { block }`
835849
// FIXME #6993: change to Option<Name> ... or not, if these are hygienic.
836850
ExprLoop(P<Block>, Option<Ident>),
837851
/// A `match` block, with a source that indicates whether or not it is
838-
/// the result of a desugaring, and if so, which kind
852+
/// the result of a desugaring, and if so, which kind.
839853
ExprMatch(P<Expr>, Vec<Arm>, MatchSource),
840854
/// A closure (for example, `move |a, b, c| {a + b + c}`)
841855
ExprClosure(CaptureClause, P<FnDecl>, P<Block>),
@@ -845,20 +859,24 @@ pub enum Expr_ {
845859
/// An assignment (`a = foo()`)
846860
ExprAssign(P<Expr>, P<Expr>),
847861
/// An assignment with an operator
848-
/// For example, `a += 1`
862+
///
863+
/// For example, `a += 1`.
849864
ExprAssignOp(BinOp, P<Expr>, P<Expr>),
850865
/// Access of a named struct field (`obj.foo`)
851866
ExprField(P<Expr>, SpannedIdent),
852867
/// Access of an unnamed field of a struct or tuple-struct
853-
/// For example, `foo.0`
868+
///
869+
/// For example, `foo.0`.
854870
ExprTupField(P<Expr>, Spanned<usize>),
855871
/// An indexing operation (`foo[2]`)
856872
ExprIndex(P<Expr>, P<Expr>),
857873
/// A range (`1..2`, `1..`, or `..2`)
858874
ExprRange(Option<P<Expr>>, Option<P<Expr>>),
859875

860876
/// Variable reference, possibly containing `::` and/or type
861-
/// parameters, e.g. foo::bar::<baz>. Optionally "qualified",
877+
/// parameters, e.g. foo::bar::<baz>.
878+
///
879+
/// Optionally "qualified",
862880
/// e.g. `<Vec<T> as SomeTrait>::SomeType`.
863881
ExprPath(Option<QSelf>, Path),
864882

@@ -878,13 +896,15 @@ pub enum Expr_ {
878896
ExprMac(Mac),
879897

880898
/// A struct literal expression.
899+
///
881900
/// For example, `Foo {x: 1, y: 2}`, or
882-
/// `Foo {x: 1, .. base}`, where `base` is the `Option<Expr>`
901+
/// `Foo {x: 1, .. base}`, where `base` is the `Option<Expr>`.
883902
ExprStruct(Path, Vec<Field>, Option<P<Expr>>),
884903

885904
/// A vector literal constructed from one repeated element.
905+
///
886906
/// For example, `[1u8; 5]`. The first expression is the element
887-
/// to be repeated; the second is the number of times to repeat it
907+
/// to be repeated; the second is the number of times to repeat it.
888908
ExprRepeat(P<Expr>, P<Expr>),
889909

890910
/// No-op: used solely so we can pretty-print faithfully
@@ -1092,6 +1112,7 @@ pub type Mac = Spanned<Mac_>;
10921112
/// Represents a macro invocation. The Path indicates which macro
10931113
/// is being invoked, and the vector of token-trees contains the source
10941114
/// of the macro invocation.
1115+
///
10951116
/// There's only one flavor, now, so this could presumably be simplified.
10961117
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
10971118
pub enum Mac_ {
@@ -1105,6 +1126,7 @@ pub enum StrStyle {
11051126
/// A regular string, like `"foo"`
11061127
CookedStr,
11071128
/// A raw string, like `r##"foo"##`
1129+
///
11081130
/// The uint is the number of `#` symbols used
11091131
RawStr(usize)
11101132
}
@@ -1459,7 +1481,7 @@ impl Arg {
14591481
}
14601482
}
14611483

1462-
/// represents the header (not the body) of a function declaration
1484+
/// Represents the header (not the body) of a function declaration
14631485
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
14641486
pub struct FnDecl {
14651487
pub inputs: Vec<Arg>,
@@ -1505,7 +1527,9 @@ pub enum FunctionRetTy {
15051527
/// Functions with return type `!`that always
15061528
/// raise an error or exit (i.e. never return to the caller)
15071529
NoReturn(Span),
1508-
/// Return type is not specified. Functions default to `()` and
1530+
/// Return type is not specified.
1531+
///
1532+
/// Functions default to `()` and
15091533
/// closures default to inference. Span points to where return
15101534
/// type would be inserted.
15111535
DefaultReturn(Span),
@@ -1645,6 +1669,7 @@ pub struct Attribute_ {
16451669
}
16461670

16471671
/// TraitRef's appear in impls.
1672+
///
16481673
/// resolve maps each TraitRef's ref_id to its defining trait; that's all
16491674
/// that the ref_id is for. The impl_id maps to the "self type" of this impl.
16501675
/// If this impl is an ItemImpl, the impl_id is redundant (it could be the
@@ -1745,6 +1770,7 @@ pub struct Item {
17451770
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
17461771
pub enum Item_ {
17471772
/// An`extern crate` item, with optional original crate name,
1773+
///
17481774
/// e.g. `extern crate foo` or `extern crate "foo-bar" as foo`
17491775
ItemExternCrate(Option<(InternedString, StrStyle)>),
17501776
/// A `use` or `pub use` item
@@ -1773,6 +1799,7 @@ pub enum Item_ {
17731799
Vec<P<TraitItem>>),
17741800

17751801
// Default trait implementations
1802+
///
17761803
// `impl Trait for .. {}`
17771804
ItemDefaultImpl(Unsafety, TraitRef),
17781805
/// An implementation, eg `impl<A> Trait for Foo { .. }`

0 commit comments

Comments
 (0)