@@ -1706,14 +1706,61 @@ impl<'a> Parser<'a> {
1706
1706
/// The parsing configuration used to parse a parameter list (see `parse_fn_params`).
1707
1707
///
1708
1708
/// The function decides if, per-parameter `p`, `p` must have a pattern or just a type.
1709
+ ///
1710
+ /// This function pointer accepts an edition, because in edition 2015, trait declarations
1711
+ /// were allowed to omit parameter names. In 2018, they became required.
1709
1712
type ReqName = fn ( Edition ) -> bool ;
1710
1713
1711
1714
/// Parsing configuration for functions.
1712
- /// This include the edition-specific name requirements, plus information on whether the
1713
- /// function is allowed to go without a body.
1715
+ ///
1716
+ /// The syntax of function items is slightly different within trait definitions,
1717
+ /// impl blocks, and modules. It is still parsed using the same code, just with
1718
+ /// different flags set, so that even when the input is wrong and produces a parse
1719
+ /// error, it still gets into the AST and the rest of the parser and
1720
+ /// type checker can run.
1714
1721
#[ derive( Clone , Copy ) ]
1715
1722
pub ( crate ) struct FnParseMode {
1723
+ /// A function pointer that decides if, per-parameter `p`, `p` must have a
1724
+ /// pattern or just a type. This field affects parsing of the parameters list.
1725
+ ///
1726
+ /// ```text
1727
+ /// fn foo(alef: A) -> X { X::new() }
1728
+ /// -----^^ affects parsing this part of the function signature
1729
+ /// |
1730
+ /// if req_name returns false, then this name is optional
1731
+ ///
1732
+ /// fn bar(A) -> X;
1733
+ /// ^
1734
+ /// |
1735
+ /// if req_name returns true, this is an error
1736
+ /// ```
1737
+ ///
1738
+ /// Calling this function pointer should only return false if:
1739
+ ///
1740
+ /// * The item is being parsed inside of a trait definition.
1741
+ /// Within an impl block or a module, it should always evaluate
1742
+ /// to true.
1743
+ /// * The span is from Edition 2015. In particular, you can get a
1744
+ /// 2015 span inside a 2021 crate using macros.
1716
1745
pub req_name : ReqName ,
1746
+ /// If this flag is set to `true`, then plain, semicolon-terminated function
1747
+ /// prototypes are not allowed here.
1748
+ ///
1749
+ /// ```text
1750
+ /// fn foo(alef: A) -> X { X::new() }
1751
+ /// ^^^^^^^^^^^^
1752
+ /// |
1753
+ /// this is always allowed
1754
+ ///
1755
+ /// fn bar(alef: A, bet: B) -> X;
1756
+ /// ^
1757
+ /// |
1758
+ /// if req_body is set to true, this is an error
1759
+ /// ```
1760
+ ///
1761
+ /// This field should only be set to false if the item is inside of a trait
1762
+ /// definition or extern block. Within an impl block or a module, it should
1763
+ /// always be set to true.
1717
1764
pub req_body : bool ,
1718
1765
}
1719
1766
0 commit comments