Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
6c9bd8d
fix indexing and remove some unwraps from arg mismatch diagnostic
compiler-errors May 30, 2022
fc93461
fix typo
compiler-errors May 30, 2022
4b26d41
add regression test
compiler-errors May 30, 2022
5adef6c
Add std::alloc::set_alloc_error_hook example
LucasDumont Jun 7, 2022
2ae1ec9
Don't suggest adding let in certain if conditions
compiler-errors Jun 8, 2022
c1b1ec7
Suggest escaping `box` as identifier
ChayimFriedman2 Jun 8, 2022
456f1ff
Suggest using `iter()` or `into_iter()` for `Vec`
ChayimFriedman2 Jun 8, 2022
0f2a2fb
Add regresion test for #67498
JohnTitor Jun 8, 2022
93c210a
Remove `ignore-compare-mode-nll` annotations from tests
JohnTitor Jun 8, 2022
0f9cc06
Update books
ehuss Jun 8, 2022
bcfced8
Fix polonius compare mode.
ehuss Jun 8, 2022
1922f0b
Rollup merge of #97557 - compiler-errors:arg-mismatch-mini, r=jackh726
compiler-errors Jun 8, 2022
888d72c
Rollup merge of #97830 - LucasDumont:add-example-alloc, r=yaahc
compiler-errors Jun 8, 2022
e040920
Rollup merge of #97856 - compiler-errors:bad-let-suggestions, r=estebank
compiler-errors Jun 8, 2022
f12a1c2
Rollup merge of #97857 - ChayimFriedman2:box-identifier-help, r=compi…
compiler-errors Jun 8, 2022
1577838
Rollup merge of #97871 - ChayimFriedman2:vec-iterator-unimplemented, …
compiler-errors Jun 8, 2022
a36671a
Rollup merge of #97882 - JohnTitor:issue-67498, r=compiler-errors
compiler-errors Jun 8, 2022
cf3dd7b
Rollup merge of #97883 - JohnTitor:no-more-compare-mode-nll, r=jackh726
compiler-errors Jun 8, 2022
7331527
Rollup merge of #97891 - ehuss:update-books, r=ehuss
compiler-errors Jun 8, 2022
cfc0677
Rollup merge of #97894 - ehuss:fix-polonius-compare-mode, r=jackh726
compiler-errors Jun 8, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 57 additions & 4 deletions compiler/rustc_parse/src/parser/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,10 +360,7 @@ impl<'a> Parser<'a> {
let mutbl = self.parse_mutability();
self.parse_pat_ident(BindingMode::ByRef(mutbl))?
} else if self.eat_keyword(kw::Box) {
// Parse `box pat`
let pat = self.parse_pat_with_range_pat(false, None)?;
self.sess.gated_spans.gate(sym::box_patterns, lo.to(self.prev_token.span));
PatKind::Box(pat)
self.parse_pat_box()?
} else if self.check_inline_const(0) {
// Parse `const pat`
let const_expr = self.parse_const_block(lo.to(self.token.span), true)?;
Expand Down Expand Up @@ -915,6 +912,62 @@ impl<'a> Parser<'a> {
Ok(PatKind::TupleStruct(qself, path, fields))
}

/// Are we sure this could not possibly be the start of a pattern?
///
/// Currently, this only accounts for tokens that can follow identifiers
/// in patterns, but this can be extended as necessary.
fn isnt_pattern_start(&self) -> bool {
[
token::Eq,
token::Colon,
token::Comma,
token::Semi,
token::At,
token::OpenDelim(Delimiter::Brace),
token::CloseDelim(Delimiter::Brace),
token::CloseDelim(Delimiter::Parenthesis),
]
.contains(&self.token.kind)
}

/// Parses `box pat`
fn parse_pat_box(&mut self) -> PResult<'a, PatKind> {
let box_span = self.prev_token.span;

if self.isnt_pattern_start() {
self.struct_span_err(
self.token.span,
format!("expected pattern, found {}", super::token_descr(&self.token)),
)
.span_note(box_span, "`box` is a reserved keyword")
.span_suggestion_verbose(
box_span.shrink_to_lo(),
"escape `box` to use it as an identifier",
"r#",
Applicability::MaybeIncorrect,
)
.emit();

// We cannot use `parse_pat_ident()` since it will complain `box`
// is not an identifier.
let sub = if self.eat(&token::At) {
Some(self.parse_pat_no_top_alt(Some("binding pattern"))?)
} else {
None
};

Ok(PatKind::Ident(
BindingMode::ByValue(Mutability::Not),
Ident::new(kw::Box, box_span),
sub,
))
} else {
let pat = self.parse_pat_with_range_pat(false, None)?;
self.sess.gated_spans.gate(sym::box_patterns, box_span.to(self.prev_token.span));
Ok(PatKind::Box(pat))
}
}

/// Parses the fields of a struct-like pattern.
fn parse_pat_fields(&mut self) -> PResult<'a, (Vec<PatField>, bool)> {
let mut fields = Vec::new();
Expand Down
9 changes: 8 additions & 1 deletion src/test/ui/parser/keyword-box-as-identifier.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
fn main() {
let box = "foo"; //~ error: expected pattern, found `=`
let box = 0;
//~^ ERROR expected pattern, found `=`
let box: bool;
//~^ ERROR expected pattern, found `:`
let mut box = 0;
//~^ ERROR expected pattern, found `=`
let (box,) = (0,);
//~^ ERROR expected pattern, found `,`
}
64 changes: 61 additions & 3 deletions src/test/ui/parser/keyword-box-as-identifier.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,66 @@
error: expected pattern, found `=`
--> $DIR/keyword-box-as-identifier.rs:2:13
|
LL | let box = "foo";
| ^ expected pattern
LL | let box = 0;
| ^
|
note: `box` is a reserved keyword
--> $DIR/keyword-box-as-identifier.rs:2:9
|
LL | let box = 0;
| ^^^
help: escape `box` to use it as an identifier
|
LL | let r#box = 0;
| ++

error: expected pattern, found `:`
--> $DIR/keyword-box-as-identifier.rs:4:12
|
LL | let box: bool;
| ^
|
note: `box` is a reserved keyword
--> $DIR/keyword-box-as-identifier.rs:4:9
|
LL | let box: bool;
| ^^^
help: escape `box` to use it as an identifier
|
LL | let r#box: bool;
| ++

error: expected pattern, found `=`
--> $DIR/keyword-box-as-identifier.rs:6:17
|
LL | let mut box = 0;
| ^
|
note: `box` is a reserved keyword
--> $DIR/keyword-box-as-identifier.rs:6:13
|
LL | let mut box = 0;
| ^^^
help: escape `box` to use it as an identifier
|
LL | let mut r#box = 0;
| ++

error: expected pattern, found `,`
--> $DIR/keyword-box-as-identifier.rs:8:13
|
LL | let (box,) = (0,);
| ^
|
note: `box` is a reserved keyword
--> $DIR/keyword-box-as-identifier.rs:8:10
|
LL | let (box,) = (0,);
| ^^^
help: escape `box` to use it as an identifier
|
LL | let (r#box,) = (0,);
| ++

error: aborting due to previous error
error: aborting due to 4 previous errors