Skip to content

Commit e916641

Browse files
committed
Address review comments
1 parent 3548be9 commit e916641

File tree

1 file changed

+21
-7
lines changed
  • compiler/rustc_parse/src/parser

1 file changed

+21
-7
lines changed

compiler/rustc_parse/src/parser/ty.rs

+21-7
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ pub(super) enum RecoverQPath {
4343
No,
4444
}
4545

46+
/// Signals whether parsing a type should recover `->`.
47+
///
48+
/// More specifically, when parsing a function like:
49+
/// ```rust
50+
/// fn foo() => u8 { 0 }
51+
/// fn bar(): u8 { 0 }
52+
/// ```
53+
/// The compiler will try to recover interpreting `foo() => u8` as `foo() -> u8` when calling
54+
/// `parse_ty` with anything except `RecoverReturnSign::No`, and it will try to recover `bar(): u8`
55+
/// as `bar() -> u8` when passing `RecoverReturnSign::Yes` to `parse_ty`
4656
#[derive(Copy, Clone, PartialEq)]
4757
pub(super) enum RecoverReturnSign {
4858
Yes,
@@ -51,6 +61,10 @@ pub(super) enum RecoverReturnSign {
5161
}
5262

5363
impl RecoverReturnSign {
64+
/// [RecoverReturnSign::Yes] allows for recovering `fn foo() => u8` and `fn foo(): u8`,
65+
/// [RecoverReturnSign::OnlyFatArrow] allows for recovering only `fn foo() => u8` (recovering
66+
/// colons can cause problems when parsing where clauses), and
67+
/// [RecoverReturnSign::No] doesn't allow for any recovery of the return type arrow
5468
fn can_recover(self, token: &TokenKind) -> bool {
5569
match self {
5670
Self::Yes => matches!(token, token::FatArrow | token::Colon),
@@ -81,8 +95,8 @@ impl<'a> Parser<'a> {
8195
pub fn parse_ty(&mut self) -> PResult<'a, P<Ty>> {
8296
self.parse_ty_common(
8397
AllowPlus::Yes,
84-
RecoverQPath::Yes,
8598
AllowCVariadic::No,
99+
RecoverQPath::Yes,
86100
RecoverReturnSign::Yes,
87101
)
88102
}
@@ -93,8 +107,8 @@ impl<'a> Parser<'a> {
93107
pub(super) fn parse_ty_for_param(&mut self) -> PResult<'a, P<Ty>> {
94108
self.parse_ty_common(
95109
AllowPlus::Yes,
96-
RecoverQPath::Yes,
97110
AllowCVariadic::Yes,
111+
RecoverQPath::Yes,
98112
RecoverReturnSign::Yes,
99113
)
100114
}
@@ -108,8 +122,8 @@ impl<'a> Parser<'a> {
108122
pub(super) fn parse_ty_no_plus(&mut self) -> PResult<'a, P<Ty>> {
109123
self.parse_ty_common(
110124
AllowPlus::No,
111-
RecoverQPath::Yes,
112125
AllowCVariadic::No,
126+
RecoverQPath::Yes,
113127
RecoverReturnSign::Yes,
114128
)
115129
}
@@ -118,8 +132,8 @@ impl<'a> Parser<'a> {
118132
pub(super) fn parse_ty_for_where_clause(&mut self) -> PResult<'a, P<Ty>> {
119133
self.parse_ty_common(
120134
AllowPlus::Yes,
121-
RecoverQPath::Yes,
122135
AllowCVariadic::Yes,
136+
RecoverQPath::Yes,
123137
RecoverReturnSign::OnlyFatArrow,
124138
)
125139
}
@@ -135,8 +149,8 @@ impl<'a> Parser<'a> {
135149
// FIXME(Centril): Can we unconditionally `allow_plus`?
136150
let ty = self.parse_ty_common(
137151
allow_plus,
138-
recover_qpath,
139152
AllowCVariadic::No,
153+
recover_qpath,
140154
recover_return_sign,
141155
)?;
142156
FnRetTy::Ty(ty)
@@ -154,8 +168,8 @@ impl<'a> Parser<'a> {
154168
.emit();
155169
let ty = self.parse_ty_common(
156170
allow_plus,
157-
recover_qpath,
158171
AllowCVariadic::No,
172+
recover_qpath,
159173
recover_return_sign,
160174
)?;
161175
FnRetTy::Ty(ty)
@@ -167,8 +181,8 @@ impl<'a> Parser<'a> {
167181
fn parse_ty_common(
168182
&mut self,
169183
allow_plus: AllowPlus,
170-
recover_qpath: RecoverQPath,
171184
allow_c_variadic: AllowCVariadic,
185+
recover_qpath: RecoverQPath,
172186
recover_return_sign: RecoverReturnSign,
173187
) -> PResult<'a, P<Ty>> {
174188
let allow_qpath_recovery = recover_qpath == RecoverQPath::Yes;

0 commit comments

Comments
 (0)