Skip to content

Commit c0b7b41

Browse files
committed
parse_ty_common: use enums instead of bools.
1 parent c9290dc commit c0b7b41

File tree

5 files changed

+52
-29
lines changed

5 files changed

+52
-29
lines changed

src/librustc_parse/parser/diagnostics.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use super::ty::AllowPlus;
12
use super::{BlockMode, Parser, PathStyle, SemiColonMode, SeqSep, TokenExpectType, TokenType};
23

34
use rustc_ast_pretty::pprust;
@@ -693,11 +694,11 @@ impl<'a> Parser<'a> {
693694

694695
pub(super) fn maybe_report_ambiguous_plus(
695696
&mut self,
696-
allow_plus: bool,
697+
allow_plus: AllowPlus,
697698
impl_dyn_multi: bool,
698699
ty: &Ty,
699700
) {
700-
if !allow_plus && impl_dyn_multi {
701+
if matches!(allow_plus, AllowPlus::No) && impl_dyn_multi {
701702
let sum_with_parens = format!("({})", pprust::ty_to_string(&ty));
702703
self.struct_span_err(ty.span, "ambiguous `+` in a type")
703704
.span_suggestion(
@@ -712,11 +713,11 @@ impl<'a> Parser<'a> {
712713

713714
pub(super) fn maybe_recover_from_bad_type_plus(
714715
&mut self,
715-
allow_plus: bool,
716+
allow_plus: AllowPlus,
716717
ty: &Ty,
717718
) -> PResult<'a, ()> {
718719
// Do not add `+` to expected tokens.
719-
if !allow_plus || !self.token.is_like_plus() {
720+
if matches!(allow_plus, AllowPlus::No) || !self.token.is_like_plus() {
720721
return Ok(());
721722
}
722723

src/librustc_parse/parser/expr.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::pat::{GateOr, PARAM_EXPECTED};
2+
use super::ty::{AllowPlus, RecoverQPath};
23
use super::{BlockMode, Parser, PathStyle, PrevTokenKind, Restrictions, TokenType};
34
use super::{SemiColonMode, SeqSep, TokenExpectType};
45
use crate::maybe_recover_from_interpolated_ty_qpath;
@@ -1399,7 +1400,7 @@ impl<'a> Parser<'a> {
13991400
self.expect_or()?;
14001401
args
14011402
};
1402-
let output = self.parse_ret_ty(true, true)?;
1403+
let output = self.parse_ret_ty(AllowPlus::Yes, RecoverQPath::Yes)?;
14031404

14041405
Ok(P(FnDecl { inputs, output }))
14051406
}

src/librustc_parse/parser/item.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::diagnostics::{dummy_arg, ConsumeClosingDelim, Error};
2+
use super::ty::{AllowPlus, RecoverQPath};
23
use super::{FollowedByType, Parser, PathStyle};
34

45
use crate::maybe_whole;
@@ -1839,7 +1840,7 @@ impl<'a> Parser<'a> {
18391840
fn parse_fn_sig(&mut self, cfg: &ParamCfg) -> PResult<'a, (Ident, P<FnDecl>, Generics)> {
18401841
let ident = self.parse_ident()?;
18411842
let mut generics = self.parse_generics()?;
1842-
let decl = self.parse_fn_decl(cfg, true)?;
1843+
let decl = self.parse_fn_decl(cfg, AllowPlus::Yes)?;
18431844
generics.where_clause = self.parse_where_clause()?;
18441845
Ok((ident, decl, generics))
18451846
}
@@ -1848,11 +1849,11 @@ impl<'a> Parser<'a> {
18481849
pub(super) fn parse_fn_decl(
18491850
&mut self,
18501851
cfg: &ParamCfg,
1851-
ret_allow_plus: bool,
1852+
ret_allow_plus: AllowPlus,
18521853
) -> PResult<'a, P<FnDecl>> {
18531854
Ok(P(FnDecl {
18541855
inputs: self.parse_fn_params(cfg)?,
1855-
output: self.parse_ret_ty(ret_allow_plus, true)?,
1856+
output: self.parse_ret_ty(ret_allow_plus, RecoverQPath::Yes)?,
18561857
}))
18571858
}
18581859

src/librustc_parse/parser/path.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use super::ty::{AllowPlus, RecoverQPath};
12
use super::{Parser, TokenType};
23
use crate::maybe_whole;
34
use rustc_errors::{pluralize, Applicability, PResult};
@@ -224,7 +225,7 @@ impl<'a> Parser<'a> {
224225
// `(T, U) -> R`
225226
let (inputs, _) = self.parse_paren_comma_seq(|p| p.parse_ty())?;
226227
let span = ident.span.to(self.prev_span);
227-
let output = self.parse_ret_ty(false, false)?;
228+
let output = self.parse_ret_ty(AllowPlus::No, RecoverQPath::No)?;
228229
ParenthesizedArgs { inputs, output, span }.into()
229230
};
230231

src/librustc_parse/parser/ty.rs

+39-20
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,23 @@ impl BoundModifiers {
3636
}
3737
}
3838

39+
#[derive(Copy, Clone)]
40+
pub(super) enum AllowPlus {
41+
Yes,
42+
No,
43+
}
44+
45+
pub(super) enum RecoverQPath {
46+
Yes,
47+
No,
48+
}
49+
50+
// Is `...` (`CVarArgs`) legal at this level of type parsing?
51+
enum AllowCVariadic {
52+
Yes,
53+
No,
54+
}
55+
3956
/// Returns `true` if `IDENT t` can start a type -- `IDENT::a::b`, `IDENT<u8, u8>`,
4057
/// `IDENT<<u8 as Trait>::AssocTy>`.
4158
///
@@ -48,14 +65,14 @@ fn can_continue_type_after_non_fn_ident(t: &Token) -> bool {
4865
impl<'a> Parser<'a> {
4966
/// Parses a type.
5067
pub fn parse_ty(&mut self) -> PResult<'a, P<Ty>> {
51-
self.parse_ty_common(true, true, false)
68+
self.parse_ty_common(AllowPlus::Yes, RecoverQPath::Yes, AllowCVariadic::No)
5269
}
5370

5471
/// Parse a type suitable for a function or function pointer parameter.
5572
/// The difference from `parse_ty` is that this version allows `...`
5673
/// (`CVarArgs`) at the top level of the the type.
5774
pub(super) fn parse_ty_for_param(&mut self) -> PResult<'a, P<Ty>> {
58-
self.parse_ty_common(true, true, true)
75+
self.parse_ty_common(AllowPlus::Yes, RecoverQPath::Yes, AllowCVariadic::Yes)
5976
}
6077

6178
/// Parses a type in restricted contexts where `+` is not permitted.
@@ -65,30 +82,31 @@ impl<'a> Parser<'a> {
6582
/// Example 2: `value1 as TYPE + value2`
6683
/// `+` is prohibited to avoid interactions with expression grammar.
6784
pub(super) fn parse_ty_no_plus(&mut self) -> PResult<'a, P<Ty>> {
68-
self.parse_ty_common(false, true, false)
85+
self.parse_ty_common(AllowPlus::No, RecoverQPath::Yes, AllowCVariadic::No)
6986
}
7087

7188
/// Parses an optional return type `[ -> TY ]` in a function declaration.
7289
pub(super) fn parse_ret_ty(
7390
&mut self,
74-
allow_plus: bool,
75-
allow_qpath_recovery: bool,
91+
allow_plus: AllowPlus,
92+
recover_qpath: RecoverQPath,
7693
) -> PResult<'a, FunctionRetTy> {
7794
Ok(if self.eat(&token::RArrow) {
7895
// FIXME(Centril): Can we unconditionally `allow_plus`?
79-
FunctionRetTy::Ty(self.parse_ty_common(allow_plus, allow_qpath_recovery, false)?)
96+
let ty = self.parse_ty_common(allow_plus, recover_qpath, AllowCVariadic::No)?;
97+
FunctionRetTy::Ty(ty)
8098
} else {
8199
FunctionRetTy::Default(self.token.span.shrink_to_lo())
82100
})
83101
}
84102

85103
fn parse_ty_common(
86104
&mut self,
87-
allow_plus: bool,
88-
allow_qpath_recovery: bool,
89-
// Is `...` (`CVarArgs`) legal in the immediate top level call?
90-
allow_c_variadic: bool,
105+
allow_plus: AllowPlus,
106+
recover_qpath: RecoverQPath,
107+
allow_c_variadic: AllowCVariadic,
91108
) -> PResult<'a, P<Ty>> {
109+
let allow_qpath_recovery = matches!(recover_qpath, RecoverQPath::Yes);
92110
maybe_recover_from_interpolated_ty_qpath!(self, allow_qpath_recovery);
93111
maybe_whole!(self, NtTy, |x| x);
94112

@@ -124,7 +142,7 @@ impl<'a> Parser<'a> {
124142
self.parse_ty_bare_fn(lifetime_defs)?
125143
} else {
126144
let path = self.parse_path(PathStyle::Type)?;
127-
let parse_plus = allow_plus && self.check_plus();
145+
let parse_plus = matches!(allow_plus, AllowPlus::Yes) && self.check_plus();
128146
self.parse_remaining_bounds(lifetime_defs, path, lo, parse_plus)?
129147
}
130148
} else if self.eat_keyword(kw::Impl) {
@@ -144,7 +162,7 @@ impl<'a> Parser<'a> {
144162
} else if self.token.is_path_start() {
145163
self.parse_path_start_ty(lo, allow_plus)?
146164
} else if self.eat(&token::DotDotDot) {
147-
if allow_c_variadic {
165+
if let AllowCVariadic::Yes = allow_c_variadic {
148166
TyKind::CVarArgs
149167
} else {
150168
// FIXME(Centril): Should we just allow `...` syntactically
@@ -172,7 +190,7 @@ impl<'a> Parser<'a> {
172190
/// Parses either:
173191
/// - `(TYPE)`, a parenthesized type.
174192
/// - `(TYPE,)`, a tuple with a single field of type TYPE.
175-
fn parse_ty_tuple_or_parens(&mut self, lo: Span, allow_plus: bool) -> PResult<'a, TyKind> {
193+
fn parse_ty_tuple_or_parens(&mut self, lo: Span, allow_plus: AllowPlus) -> PResult<'a, TyKind> {
176194
let mut trailing_plus = false;
177195
let (ts, trailing) = self.parse_paren_comma_seq(|p| {
178196
let ty = p.parse_ty()?;
@@ -182,7 +200,7 @@ impl<'a> Parser<'a> {
182200

183201
if ts.len() == 1 && !trailing {
184202
let ty = ts.into_iter().nth(0).unwrap().into_inner();
185-
let maybe_bounds = allow_plus && self.token.is_like_plus();
203+
let maybe_bounds = matches!(allow_plus, AllowPlus::Yes) && self.token.is_like_plus();
186204
match ty.kind {
187205
// `(TY_BOUND_NOPAREN) + BOUND + ...`.
188206
TyKind::Path(None, path) if maybe_bounds => {
@@ -288,7 +306,8 @@ impl<'a> Parser<'a> {
288306
let unsafety = self.parse_unsafety();
289307
let ext = self.parse_extern()?;
290308
self.expect_keyword(kw::Fn)?;
291-
let decl = self.parse_fn_decl(&ParamCfg { is_name_required: |_| false }, false)?;
309+
let cfg = ParamCfg { is_name_required: |_| false };
310+
let decl = self.parse_fn_decl(&cfg, AllowPlus::No)?;
292311
Ok(TyKind::BareFn(P(BareFnTy { ext, unsafety, generic_params, decl })))
293312
}
294313

@@ -326,7 +345,7 @@ impl<'a> Parser<'a> {
326345
/// 1. a type macro, `mac!(...)`,
327346
/// 2. a bare trait object, `B0 + ... + Bn`,
328347
/// 3. or a path, `path::to::MyType`.
329-
fn parse_path_start_ty(&mut self, lo: Span, allow_plus: bool) -> PResult<'a, TyKind> {
348+
fn parse_path_start_ty(&mut self, lo: Span, allow_plus: AllowPlus) -> PResult<'a, TyKind> {
330349
// Simple path
331350
let path = self.parse_path(PathStyle::Type)?;
332351
if self.eat(&token::Not) {
@@ -336,7 +355,7 @@ impl<'a> Parser<'a> {
336355
args: self.parse_mac_args()?,
337356
prior_type_ascription: self.last_type_ascription,
338357
}))
339-
} else if allow_plus && self.check_plus() {
358+
} else if matches!(allow_plus, AllowPlus::Yes) && self.check_plus() {
340359
// `Trait1 + Trait2 + 'a`
341360
self.parse_remaining_bounds(Vec::new(), path, lo, true)
342361
} else {
@@ -359,15 +378,15 @@ impl<'a> Parser<'a> {
359378
&mut self,
360379
colon_span: Option<Span>,
361380
) -> PResult<'a, GenericBounds> {
362-
self.parse_generic_bounds_common(true, colon_span)
381+
self.parse_generic_bounds_common(AllowPlus::Yes, colon_span)
363382
}
364383

365384
/// Parses bounds of a type parameter `BOUND + BOUND + ...`, possibly with trailing `+`.
366385
///
367386
/// See `parse_generic_bound` for the `BOUND` grammar.
368387
fn parse_generic_bounds_common(
369388
&mut self,
370-
allow_plus: bool,
389+
allow_plus: AllowPlus,
371390
colon_span: Option<Span>,
372391
) -> PResult<'a, GenericBounds> {
373392
let mut bounds = Vec::new();
@@ -377,7 +396,7 @@ impl<'a> Parser<'a> {
377396
Ok(bound) => bounds.push(bound),
378397
Err(neg_sp) => negative_bounds.push(neg_sp),
379398
}
380-
if !allow_plus || !self.eat_plus() {
399+
if matches!(allow_plus, AllowPlus::No) || !self.eat_plus() {
381400
break;
382401
}
383402
}

0 commit comments

Comments
 (0)