Skip to content

Commit 2b92f9f

Browse files
committed
Parser: Document restrictions
I had trouble easily understanding what these various flags do. This is my attempt to try to explain what these do.
1 parent 0fbb922 commit 2b92f9f

File tree

1 file changed

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

1 file changed

+49
-0
lines changed

compiler/rustc_parse/src/parser/mod.rs

+49
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,62 @@ mod mut_visit {
6161
}
6262

6363
bitflags::bitflags! {
64+
/// Restrictions applied while parsing.
65+
///
66+
/// The parser maintains a bitset of restrictions it will honor while
67+
/// parsing. This is essentially used as a way of tracking state of what
68+
/// is being parsed and to change behavior based on that.
6469
#[derive(Clone, Copy, Debug)]
6570
struct Restrictions: u8 {
71+
/// Restricts expressions for use in statement position.
72+
///
73+
/// When expressions are used in various places, like statements or
74+
/// match arms, this is used to stop parsing once certain tokens are
75+
/// reached.
76+
///
77+
/// For example, `if true {} & 1` with `STMT_EXPR` in effect is parsed
78+
/// as two separate expression statements (`if` and a reference to 1).
79+
/// Otherwise it is parsed as a bitwise AND where `if` is on the left
80+
/// and 1 is on the right.
6681
const STMT_EXPR = 1 << 0;
82+
/// Do not allow struct literals.
83+
///
84+
/// There are several places in the grammar where we don't want to
85+
/// allow struct literals because they can require lookahead, or
86+
/// otherwise could be ambiguous or cause confusion. For example,
87+
/// `if Foo {} {}` isn't clear if it is `Foo{}` struct literal, or
88+
/// just `Foo` is the condition, followed by a consequent block,
89+
/// followed by an empty block.
90+
///
91+
/// See [RFC 92](https://rust-lang.github.io/rfcs/0092-struct-grammar.html).
6792
const NO_STRUCT_LITERAL = 1 << 1;
93+
/// Used to provide better error messages for const generic arguments.
94+
///
95+
/// An un-braced const generic argument is limited to a very small
96+
/// subset of expressions. This is used to detect the situation where
97+
/// an expression outside of that subset is used, and to suggest to
98+
/// wrap the expression in braces.
6899
const CONST_EXPR = 1 << 2;
100+
/// Allows `let` expressions.
101+
///
102+
/// `let pattern = scrutinee` is parsed as an expression, but it is
103+
/// only allowed in let chains (`if` and `while` conditions).
104+
/// Otherwise it is not an expression (note that `let` in statement
105+
/// positions is treated as a `StmtKind::Let` statement, which has a
106+
/// slightly different grammar).
69107
const ALLOW_LET = 1 << 3;
108+
/// Used to detect a missing `=>` in a match guard.
109+
///
110+
/// This is used for error handling in a match guard to give a better
111+
/// error message if the `=>` is missing. It is set when parsing the
112+
/// guard expression.
70113
const IN_IF_GUARD = 1 << 4;
114+
/// Used to detect the incorrect use of expressions in patterns.
115+
///
116+
/// This is used for error handling while parsing a pattern. During
117+
/// error recovery, this will be set to try to parse the pattern as an
118+
/// expression, but halts parsing the expression when reaching certain
119+
/// tokens like `=`.
71120
const IS_PAT = 1 << 5;
72121
}
73122
}

0 commit comments

Comments
 (0)