Skip to content

Commit 277a557

Browse files
authored
Unrolled build for rust-lang#118359
Rollup merge of rust-lang#118359 - hkmatsumoto:suggest-box-ref, r=TaKO8Ki Suggest swapping the order of `ref` and `box` It is not valid grammar to write `ref box <ident>` in patterns, but `box ref <ident>` is. This patch adds a diagnostic to suggest swapping them, analogous to what we do for `mut let`.
2 parents aa33051 + f4c2bde commit 277a557

File tree

6 files changed

+55
-2
lines changed

6 files changed

+55
-2
lines changed

compiler/rustc_parse/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,9 @@ parse_sugg_wrap_pattern_in_parens = wrap the pattern in parentheses
721721
parse_switch_mut_let_order =
722722
switch the order of `mut` and `let`
723723
724+
parse_switch_ref_box_order = switch the order of `ref` and `box`
725+
.suggestion = swap them
726+
724727
parse_ternary_operator = Rust has no ternary operator
725728
.help = use an `if-else` expression instead
726729

compiler/rustc_parse/src/errors.rs

+8
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,14 @@ pub(crate) enum InvalidVariableDeclarationSub {
137137
UseLetNotVar(#[primary_span] Span),
138138
}
139139

140+
#[derive(Diagnostic)]
141+
#[diag(parse_switch_ref_box_order)]
142+
pub(crate) struct SwitchRefBoxOrder {
143+
#[primary_span]
144+
#[suggestion(applicability = "machine-applicable", code = "box ref")]
145+
pub span: Span,
146+
}
147+
140148
#[derive(Diagnostic)]
141149
#[diag(parse_invalid_comparison_operator)]
142150
pub(crate) struct InvalidComparisonOperator {

compiler/rustc_parse/src/parser/pat.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use crate::errors::{
55
ExpectedCommaAfterPatternField, GenericArgsInPatRequireTurbofishSyntax,
66
InclusiveRangeExtraEquals, InclusiveRangeMatchArrow, InclusiveRangeNoEnd, InvalidMutInPattern,
77
PatternOnWrongSideOfAt, RefMutOrderIncorrect, RemoveLet, RepeatedMutInPattern,
8-
TopLevelOrPatternNotAllowed, TopLevelOrPatternNotAllowedSugg, TrailingVertNotAllowed,
9-
UnexpectedLifetimeInPattern, UnexpectedVertVertBeforeFunctionParam,
8+
SwitchRefBoxOrder, TopLevelOrPatternNotAllowed, TopLevelOrPatternNotAllowedSugg,
9+
TrailingVertNotAllowed, UnexpectedLifetimeInPattern, UnexpectedVertVertBeforeFunctionParam,
1010
UnexpectedVertVertInPattern,
1111
};
1212
use crate::{maybe_recover_from_interpolated_ty_qpath, maybe_whole};
@@ -374,6 +374,12 @@ impl<'a> Parser<'a> {
374374
} else if self.eat_keyword(kw::Mut) {
375375
self.parse_pat_ident_mut(syntax_loc)?
376376
} else if self.eat_keyword(kw::Ref) {
377+
if self.check_keyword(kw::Box) {
378+
// Suggest `box ref` and quit parsing pattern to prevent series of
379+
// misguided diagnostics from later stages of the compiler.
380+
let span = self.prev_token.span.to(self.token.span);
381+
return Err(self.sess.create_err(SwitchRefBoxOrder { span }));
382+
}
377383
// Parse ref ident @ pat / ref mut ident @ pat
378384
let mutbl = self.parse_mutability();
379385
self.parse_pat_ident(BindingAnnotation(ByRef::Yes, mutbl), syntax_loc)?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// run-rustfix
2+
3+
#![feature(box_patterns)]
4+
#![allow(dead_code)]
5+
6+
fn foo(f: Option<Box<i32>>) {
7+
match f {
8+
Some(box ref _i) => {},
9+
//~^ ERROR switch the order of `ref` and `box`
10+
None => {}
11+
}
12+
}
13+
14+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// run-rustfix
2+
3+
#![feature(box_patterns)]
4+
#![allow(dead_code)]
5+
6+
fn foo(f: Option<Box<i32>>) {
7+
match f {
8+
Some(ref box _i) => {},
9+
//~^ ERROR switch the order of `ref` and `box`
10+
None => {}
11+
}
12+
}
13+
14+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: switch the order of `ref` and `box`
2+
--> $DIR/pattern-bad-ref-box-order.rs:8:14
3+
|
4+
LL | Some(ref box _i) => {},
5+
| ^^^^^^^ help: swap them: `box ref`
6+
7+
error: aborting due to 1 previous error
8+

0 commit comments

Comments
 (0)