Skip to content

Commit 8c8af6c

Browse files
committed
Avoid too many expected symbols and reduce Nones
1 parent 41e85c3 commit 8c8af6c

File tree

12 files changed

+58
-64
lines changed

12 files changed

+58
-64
lines changed

compiler/rustc_parse/src/parser/attr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ impl<'a> Parser<'a> {
260260
item
261261
} else {
262262
let do_parse = |this: &mut Self| {
263-
let path = this.parse_path(PathStyle::Mod, None)?;
263+
let path = this.parse_path(PathStyle::Mod)?;
264264
let args = this.parse_attr_args()?;
265265
Ok(ast::AttrItem { path, args, tokens: None })
266266
};
@@ -387,7 +387,7 @@ impl<'a> Parser<'a> {
387387
}
388388

389389
let lo = self.token.span;
390-
let path = self.parse_path(PathStyle::Mod, None)?;
390+
let path = self.parse_path(PathStyle::Mod)?;
391391
let kind = self.parse_meta_item_kind()?;
392392
let span = lo.to(self.prev_token.span);
393393
Ok(ast::MetaItem { path, kind, span })

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1579,7 +1579,7 @@ impl<'a> Parser<'a> {
15791579
self.expect(&token::ModSep)?;
15801580

15811581
let mut path = ast::Path { segments: ThinVec::new(), span: DUMMY_SP, tokens: None };
1582-
self.parse_path_segments(&mut path.segments, T::PATH_STYLE, None, None)?;
1582+
self.parse_path_segments(&mut path.segments, T::PATH_STYLE, None)?;
15831583
path.span = ty_span.to(self.prev_token.span);
15841584

15851585
let ty_str = self.span_to_snippet(ty_span).unwrap_or_else(|_| pprust::ty_to_string(&ty));

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ impl<'a> Parser<'a> {
775775
_ => {}
776776
}
777777

778-
match self.parse_path(PathStyle::Expr, None) {
778+
match self.parse_path(PathStyle::Expr) {
779779
Ok(path) => {
780780
let span_after_type = parser_snapshot_after_type.token.span;
781781
let expr = mk_expr(
@@ -1314,7 +1314,7 @@ impl<'a> Parser<'a> {
13141314
}
13151315

13161316
let fn_span_lo = self.token.span;
1317-
let mut seg = self.parse_path_segment(PathStyle::Expr, None, None)?;
1317+
let mut seg = self.parse_path_segment(PathStyle::Expr, None)?;
13181318
self.check_trailing_angle_brackets(&seg, &[&token::OpenDelim(Delimiter::Parenthesis)]);
13191319
self.check_turbofish_missing_angle_brackets(&mut seg);
13201320

@@ -1544,7 +1544,7 @@ impl<'a> Parser<'a> {
15441544
})?;
15451545
(Some(qself), path)
15461546
} else {
1547-
(None, self.parse_path(PathStyle::Expr, None)?)
1547+
(None, self.parse_path(PathStyle::Expr)?)
15481548
};
15491549

15501550
// `!`, as an operator, is prefix, so we know this isn't that.

compiler/rustc_parse/src/parser/item.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ impl<'a> Parser<'a> {
452452

453453
/// Parses an item macro, e.g., `item!();`.
454454
fn parse_item_macro(&mut self, vis: &Visibility) -> PResult<'a, MacCall> {
455-
let path = self.parse_path(PathStyle::Mod, None)?; // `foo::bar`
455+
let path = self.parse_path(PathStyle::Mod)?; // `foo::bar`
456456
self.expect(&token::Not)?; // `!`
457457
match self.parse_delim_args() {
458458
// `( .. )` or `[ .. ]` (followed by `;`), or `{ .. }`.
@@ -976,7 +976,7 @@ impl<'a> Parser<'a> {
976976
self.parse_use_tree_glob_or_nested()?
977977
} else {
978978
// `use path::*;` or `use path::{...};` or `use path;` or `use path as bar;`
979-
prefix = self.parse_path(PathStyle::Mod, None)?;
979+
prefix = self.parse_path(PathStyle::Mod)?;
980980

981981
if self.eat(&token::ModSep) {
982982
self.parse_use_tree_glob_or_nested()?
@@ -987,7 +987,7 @@ impl<'a> Parser<'a> {
987987
.emit_err(errors::SingleColonImportPath { span: self.prev_token.span });
988988

989989
// We parse the rest of the path and append it to the original prefix.
990-
self.parse_path_segments(&mut prefix.segments, PathStyle::Mod, None, None)?;
990+
self.parse_path_segments(&mut prefix.segments, PathStyle::Mod, None)?;
991991
prefix.span = lo.to(self.prev_token.span);
992992
}
993993

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,7 +1413,7 @@ impl<'a> Parser<'a> {
14131413
// Parse `pub(in path)`.
14141414
self.bump(); // `(`
14151415
self.bump(); // `in`
1416-
let path = self.parse_path(PathStyle::Mod, None)?; // `path`
1416+
let path = self.parse_path(PathStyle::Mod)?; // `path`
14171417
self.expect(&token::CloseDelim(Delimiter::Parenthesis))?; // `)`
14181418
let vis = VisibilityKind::Restricted {
14191419
path: P(path),
@@ -1430,7 +1430,7 @@ impl<'a> Parser<'a> {
14301430
{
14311431
// Parse `pub(crate)`, `pub(self)`, or `pub(super)`.
14321432
self.bump(); // `(`
1433-
let path = self.parse_path(PathStyle::Mod, None)?; // `crate`/`super`/`self`
1433+
let path = self.parse_path(PathStyle::Mod)?; // `crate`/`super`/`self`
14341434
self.expect(&token::CloseDelim(Delimiter::Parenthesis))?; // `)`
14351435
let vis = VisibilityKind::Restricted {
14361436
path: P(path),
@@ -1456,7 +1456,7 @@ impl<'a> Parser<'a> {
14561456
/// Recovery for e.g. `pub(something) fn ...` or `struct X { pub(something) y: Z }`
14571457
fn recover_incorrect_vis_restriction(&mut self) -> PResult<'a, ()> {
14581458
self.bump(); // `(`
1459-
let path = self.parse_path(PathStyle::Mod, None)?;
1459+
let path = self.parse_path(PathStyle::Mod)?;
14601460
self.expect(&token::CloseDelim(Delimiter::Parenthesis))?; // `)`
14611461

14621462
let path_str = pprust::path_to_string(&path);

compiler/rustc_parse/src/parser/nonterminal.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ impl<'a> Parser<'a> {
168168
}.into_diagnostic(&self.sess.span_diagnostic));
169169
}
170170
NonterminalKind::Path => token::NtPath(
171-
P(self.collect_tokens_no_attrs(|this| this.parse_path(PathStyle::Type, None))?),
171+
P(self.collect_tokens_no_attrs(|this| this.parse_path(PathStyle::Type))?),
172172
),
173173
NonterminalKind::Meta => token::NtMeta(P(self.parse_attr_item(true)?)),
174174
NonterminalKind::Vis => token::NtVis(

compiler/rustc_parse/src/parser/pat.rs

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ use super::{ForceCollect, Parser, PathStyle, TrailingToken};
22
use crate::errors::{
33
self, AmbiguousRangePattern, DotDotDotForRemainingFields, DotDotDotRangeToPatternNotAllowed,
44
DotDotDotRestPattern, EnumPatternInsteadOfIdentifier, ExpectedBindingLeftOfAt,
5-
ExpectedCommaAfterPatternField, InclusiveRangeExtraEquals, InclusiveRangeMatchArrow,
6-
InclusiveRangeNoEnd, InvalidMutInPattern, PatternOnWrongSideOfAt, RefMutOrderIncorrect,
7-
RemoveLet, RepeatedMutInPattern, TopLevelOrPatternNotAllowed, TopLevelOrPatternNotAllowedSugg,
8-
TrailingVertNotAllowed, UnexpectedLifetimeInPattern, UnexpectedVertVertBeforeFunctionParam,
5+
ExpectedCommaAfterPatternField, GenericArgsInPatRequireTurbofishSyntax,
6+
InclusiveRangeExtraEquals, InclusiveRangeMatchArrow, InclusiveRangeNoEnd, InvalidMutInPattern,
7+
PatternOnWrongSideOfAt, RefMutOrderIncorrect, RemoveLet, RepeatedMutInPattern,
8+
TopLevelOrPatternNotAllowed, TopLevelOrPatternNotAllowedSugg, TrailingVertNotAllowed,
9+
UnexpectedLifetimeInPattern, UnexpectedVertVertBeforeFunctionParam,
910
UnexpectedVertVertInPattern,
1011
};
1112
use crate::{maybe_recover_from_interpolated_ty_qpath, maybe_whole};
@@ -366,11 +367,11 @@ impl<'a> Parser<'a> {
366367
// Parse _
367368
PatKind::Wild
368369
} else if self.eat_keyword(kw::Mut) {
369-
self.parse_pat_ident_mut()?
370+
self.parse_pat_ident_mut(syntax_loc)?
370371
} else if self.eat_keyword(kw::Ref) {
371372
// Parse ref ident @ pat / ref mut ident @ pat
372373
let mutbl = self.parse_mutability();
373-
self.parse_pat_ident(BindingAnnotation(ByRef::Yes, mutbl))?
374+
self.parse_pat_ident(BindingAnnotation(ByRef::Yes, mutbl), syntax_loc)?
374375
} else if self.eat_keyword(kw::Box) {
375376
self.parse_pat_box()?
376377
} else if self.check_inline_const(0) {
@@ -392,7 +393,7 @@ impl<'a> Parser<'a> {
392393
// Parse `ident @ pat`
393394
// This can give false positives and parse nullary enums,
394395
// they are dealt with later in resolve.
395-
self.parse_pat_ident(BindingAnnotation::NONE)?
396+
self.parse_pat_ident(BindingAnnotation::NONE, syntax_loc)?
396397
} else if self.is_start_of_pat_with_path() {
397398
// Parse pattern starting with a path
398399
let (qself, path) = if self.eat_lt() {
@@ -401,7 +402,7 @@ impl<'a> Parser<'a> {
401402
(Some(qself), path)
402403
} else {
403404
// Parse an unqualified path
404-
(None, self.parse_path(PathStyle::Pat, syntax_loc)?)
405+
(None, self.parse_path(PathStyle::Pat)?)
405406
};
406407
let span = lo.to(self.prev_token.span);
407408

@@ -574,12 +575,12 @@ impl<'a> Parser<'a> {
574575
}
575576

576577
/// Parse a mutable binding with the `mut` token already eaten.
577-
fn parse_pat_ident_mut(&mut self) -> PResult<'a, PatKind> {
578+
fn parse_pat_ident_mut(&mut self, syntax_loc: Option<PatternLocation>) -> PResult<'a, PatKind> {
578579
let mut_span = self.prev_token.span;
579580

580581
if self.eat_keyword(kw::Ref) {
581582
self.sess.emit_err(RefMutOrderIncorrect { span: mut_span.to(self.prev_token.span) });
582-
return self.parse_pat_ident(BindingAnnotation::REF_MUT);
583+
return self.parse_pat_ident(BindingAnnotation::REF_MUT, syntax_loc);
583584
}
584585

585586
self.recover_additional_muts();
@@ -784,7 +785,7 @@ impl<'a> Parser<'a> {
784785
(Some(qself), path)
785786
} else {
786787
// Parse an unqualified path
787-
(None, self.parse_path(PathStyle::Pat, None)?)
788+
(None, self.parse_path(PathStyle::Pat)?)
788789
};
789790
let hi = self.prev_token.span;
790791
Ok(self.mk_expr(lo.to(hi), ExprKind::Path(qself, path)))
@@ -813,16 +814,28 @@ impl<'a> Parser<'a> {
813814
| token::DotDotDot | token::DotDotEq | token::DotDot // A range pattern.
814815
| token::ModSep // A tuple / struct variant pattern.
815816
| token::Not)) // A macro expanding to a pattern.
816-
// May suggest the turbofish syntax for generics, only valid for recoveries.
817-
&& !(self.look_ahead(1, |t| t.kind == token::Lt)
818-
&& self.look_ahead(2, |t| t.can_begin_type()))
819817
}
820818

821819
/// Parses `ident` or `ident @ pat`.
822820
/// Used by the copy foo and ref foo patterns to give a good
823821
/// error message when parsing mistakes like `ref foo(a, b)`.
824-
fn parse_pat_ident(&mut self, binding_annotation: BindingAnnotation) -> PResult<'a, PatKind> {
822+
fn parse_pat_ident(
823+
&mut self,
824+
binding_annotation: BindingAnnotation,
825+
syntax_loc: Option<PatternLocation>,
826+
) -> PResult<'a, PatKind> {
825827
let ident = self.parse_ident()?;
828+
829+
if !matches!(syntax_loc, Some(PatternLocation::FunctionParameter))
830+
&& self.check_noexpect(&token::Lt)
831+
&& self.look_ahead(1, |t| t.can_begin_type())
832+
{
833+
return Err(self.sess.create_err(GenericArgsInPatRequireTurbofishSyntax {
834+
span: self.token.span,
835+
suggest_turbofish: self.token.span.shrink_to_lo(),
836+
}));
837+
}
838+
826839
let sub = if self.eat(&token::At) {
827840
Some(self.parse_pat_no_top_alt(Some(Expected::BindingPattern), None)?)
828841
} else {

compiler/rustc_parse/src/parser/path.rs

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use super::pat::PatternLocation;
21
use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign};
32
use super::{Parser, Restrictions, TokenType};
4-
use crate::errors::{GenericArgsInPatRequireTurbofishSyntax, PathSingleColon};
3+
use crate::errors::PathSingleColon;
54
use crate::{errors, maybe_whole};
65
use rustc_ast::ptr::P;
76
use rustc_ast::token::{self, Delimiter, Token, TokenKind};
@@ -80,7 +79,7 @@ impl<'a> Parser<'a> {
8079
let (mut path, path_span);
8180
if self.eat_keyword(kw::As) {
8281
let path_lo = self.token.span;
83-
path = self.parse_path(PathStyle::Type, None)?;
82+
path = self.parse_path(PathStyle::Type)?;
8483
path_span = path_lo.to(self.prev_token.span);
8584
} else {
8685
path_span = self.token.span.to(self.token.span);
@@ -99,7 +98,7 @@ impl<'a> Parser<'a> {
9998
}
10099

101100
let qself = P(QSelf { ty, path_span, position: path.segments.len() });
102-
self.parse_path_segments(&mut path.segments, style, None, None)?;
101+
self.parse_path_segments(&mut path.segments, style, None)?;
103102

104103
Ok((
105104
qself,
@@ -140,12 +139,8 @@ impl<'a> Parser<'a> {
140139
true
141140
}
142141

143-
pub(super) fn parse_path(
144-
&mut self,
145-
style: PathStyle,
146-
syntax_loc: Option<PatternLocation>,
147-
) -> PResult<'a, Path> {
148-
self.parse_path_inner(style, None, syntax_loc)
142+
pub(super) fn parse_path(&mut self, style: PathStyle) -> PResult<'a, Path> {
143+
self.parse_path_inner(style, None)
149144
}
150145

151146
/// Parses simple paths.
@@ -162,7 +157,6 @@ impl<'a> Parser<'a> {
162157
&mut self,
163158
style: PathStyle,
164159
ty_generics: Option<&Generics>,
165-
syntax_loc: Option<PatternLocation>,
166160
) -> PResult<'a, Path> {
167161
let reject_generics_if_mod_style = |parser: &Parser<'_>, path: &Path| {
168162
// Ensure generic arguments don't end up in attribute paths, such as:
@@ -207,7 +201,7 @@ impl<'a> Parser<'a> {
207201
if self.eat(&token::ModSep) {
208202
segments.push(PathSegment::path_root(lo.shrink_to_lo().with_ctxt(mod_sep_ctxt)));
209203
}
210-
self.parse_path_segments(&mut segments, style, ty_generics, syntax_loc)?;
204+
self.parse_path_segments(&mut segments, style, ty_generics)?;
211205
Ok(Path { segments, span: lo.to(self.prev_token.span), tokens: None })
212206
}
213207

@@ -216,10 +210,9 @@ impl<'a> Parser<'a> {
216210
segments: &mut ThinVec<PathSegment>,
217211
style: PathStyle,
218212
ty_generics: Option<&Generics>,
219-
syntax_loc: Option<PatternLocation>,
220213
) -> PResult<'a, ()> {
221214
loop {
222-
let segment = self.parse_path_segment(style, ty_generics, syntax_loc)?;
215+
let segment = self.parse_path_segment(style, ty_generics)?;
223216
if style.has_generic_ambiguity() {
224217
// In order to check for trailing angle brackets, we must have finished
225218
// recursing (`parse_path_segment` can indirectly call this function),
@@ -274,7 +267,6 @@ impl<'a> Parser<'a> {
274267
&mut self,
275268
style: PathStyle,
276269
ty_generics: Option<&Generics>,
277-
syntax_loc: Option<PatternLocation>,
278270
) -> PResult<'a, PathSegment> {
279271
let ident = self.parse_path_segment_ident()?;
280272
let is_args_start = |token: &Token| {
@@ -294,17 +286,6 @@ impl<'a> Parser<'a> {
294286
is_args_start(&this.token)
295287
};
296288

297-
if let Some(PatternLocation::FunctionParameter) = syntax_loc {
298-
} else if style == PathStyle::Pat
299-
&& self.check_noexpect(&token::Lt)
300-
&& self.look_ahead(1, |t| t.can_begin_type())
301-
{
302-
return Err(self.sess.create_err(GenericArgsInPatRequireTurbofishSyntax {
303-
span: self.token.span,
304-
suggest_turbofish: self.token.span.shrink_to_lo(),
305-
}));
306-
}
307-
308289
Ok(
309290
if style == PathStyle::Type && check_args_start(self)
310291
|| style != PathStyle::Mod

compiler/rustc_parse/src/parser/stmt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl<'a> Parser<'a> {
149149

150150
fn parse_stmt_path_start(&mut self, lo: Span, attrs: AttrWrapper) -> PResult<'a, Stmt> {
151151
let stmt = self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| {
152-
let path = this.parse_path(PathStyle::Expr, None)?;
152+
let path = this.parse_path(PathStyle::Expr)?;
153153

154154
if this.eat(&token::Not) {
155155
let stmt_mac = this.parse_stmt_mac(lo, attrs, path)?;

compiler/rustc_parse/src/parser/ty.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ impl<'a> Parser<'a> {
289289
recover_return_sign,
290290
)?
291291
} else {
292-
let path = self.parse_path(PathStyle::Type, None)?;
292+
let path = self.parse_path(PathStyle::Type)?;
293293
let parse_plus = allow_plus == AllowPlus::Yes && self.check_plus();
294294
self.parse_remaining_bounds_path(lifetime_defs, path, lo, parse_plus)?
295295
}
@@ -649,7 +649,7 @@ impl<'a> Parser<'a> {
649649
ty_generics: Option<&Generics>,
650650
) -> PResult<'a, TyKind> {
651651
// Simple path
652-
let path = self.parse_path_inner(PathStyle::Type, ty_generics, None)?;
652+
let path = self.parse_path_inner(PathStyle::Type, ty_generics)?;
653653
if self.eat(&token::Not) {
654654
// Macro invocation in type position
655655
Ok(TyKind::MacCall(P(MacCall { path, args: self.parse_delim_args()? })))
@@ -865,7 +865,7 @@ impl<'a> Parser<'a> {
865865

866866
path
867867
} else {
868-
self.parse_path(PathStyle::Type, None)?
868+
self.parse_path(PathStyle::Type)?
869869
};
870870

871871
if self.may_recover() && self.token == TokenKind::OpenDelim(Delimiter::Parenthesis) {

tests/ui/span/issue-34264.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: expected one of `!`, `(`, `...`, `..=`, `..`, `::`, `:`, `{`, or `|`, found `<`
1+
error: expected one of `:`, `@`, or `|`, found `<`
22
--> $DIR/issue-34264.rs:1:14
33
|
44
LL | fn foo(Option<i32>, String) {}
5-
| ^ expected one of 9 possible tokens
5+
| ^ expected one of `:`, `@`, or `|`
66
|
77
= note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
88
help: if this is a `self` type, give it a parameter name

tests/ui/suggestions/issue-64252-self-type.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: expected one of `!`, `(`, `...`, `..=`, `..`, `::`, `:`, `{`, or `|`, found `<`
1+
error: expected one of `:`, `@`, or `|`, found `<`
22
--> $DIR/issue-64252-self-type.rs:4:15
33
|
44
LL | pub fn foo(Box<Self>) { }
5-
| ^ expected one of 9 possible tokens
5+
| ^ expected one of `:`, `@`, or `|`
66
|
77
= note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
88
help: if this is a `self` type, give it a parameter name
@@ -14,11 +14,11 @@ help: if this is a type, explicitly ignore the parameter name
1414
LL | pub fn foo(_: Box<Self>) { }
1515
| ++
1616

17-
error: expected one of `!`, `(`, `...`, `..=`, `..`, `::`, `:`, `{`, or `|`, found `<`
17+
error: expected one of `:`, `@`, or `|`, found `<`
1818
--> $DIR/issue-64252-self-type.rs:8:15
1919
|
2020
LL | fn bar(Box<Self>) { }
21-
| ^ expected one of 9 possible tokens
21+
| ^ expected one of `:`, `@`, or `|`
2222
|
2323
= note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
2424
help: if this is a `self` type, give it a parameter name

0 commit comments

Comments
 (0)