Skip to content

Commit 6199592

Browse files
committed
Add better comments for FnParseMode
1 parent 74437e4 commit 6199592

File tree

1 file changed

+49
-2
lines changed
  • compiler/rustc_parse/src/parser

1 file changed

+49
-2
lines changed

compiler/rustc_parse/src/parser/item.rs

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1706,14 +1706,61 @@ impl<'a> Parser<'a> {
17061706
/// The parsing configuration used to parse a parameter list (see `parse_fn_params`).
17071707
///
17081708
/// 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.
17091712
type ReqName = fn(Edition) -> bool;
17101713

17111714
/// 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.
17141721
#[derive(Clone, Copy)]
17151722
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.
17161745
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.
17171764
pub req_body: bool,
17181765
}
17191766

0 commit comments

Comments
 (0)