Add recovery for use of removed box syntax

This commit is contained in:
clubby789 2023-02-27 13:22:43 +00:00
parent a8d5950b4d
commit 8b186dfdb7
5 changed files with 105 additions and 10 deletions

View File

@ -8,6 +8,7 @@
use crate::errors;
use crate::maybe_recover_from_interpolated_ty_qpath;
use ast::{Path, PathSegment};
use core::mem;
use rustc_ast::ptr::P;
use rustc_ast::token::{self, Delimiter, Token, TokenKind};
@ -29,6 +30,7 @@
use rustc_session::lint::builtin::BREAK_WITH_LABEL_AND_LOOP;
use rustc_session::lint::BuiltinLintDiagnostics;
use rustc_span::source_map::{self, Span, Spanned};
use rustc_span::symbol::kw::PathRoot;
use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::{BytePos, Pos};
use thin_vec::{thin_vec, ThinVec};
@ -607,6 +609,9 @@ macro_rules! make_it {
let operand_expr = this.parse_expr_dot_or_call(Default::default())?;
this.recover_from_prefix_increment(operand_expr, pre_span, starts_stmt)
}
token::Ident(..) if this.token.is_keyword(kw::Box) => {
make_it!(this, attrs, |this, _| this.parse_expr_box(lo))
}
token::Ident(..) if this.may_recover() && this.is_mistaken_not_ident_negation() => {
make_it!(this, attrs, |this, _| this.recover_not_expr(lo))
}
@ -633,6 +638,29 @@ fn recover_tilde_expr(&mut self, lo: Span) -> PResult<'a, (Span, ExprKind)> {
self.parse_expr_unary(lo, UnOp::Not)
}
/// Parse `box expr` - this syntax has been removed, but we still parse this
/// for now to provide an automated way to fix usages of it
fn parse_expr_box(&mut self, lo: Span) -> PResult<'a, (Span, ExprKind)> {
let (span, expr) = self.parse_expr_prefix_common(lo)?;
let code = self.sess.source_map().span_to_snippet(span.with_lo(lo.hi())).unwrap();
self.sess.emit_err(errors::BoxSyntaxRemoved { span, code: code.trim() });
// So typechecking works, parse `box <expr>` as `::std::boxed::Box::new(expr)`
let path = Path {
span,
segments: [
PathSegment::from_ident(Ident::with_dummy_span(PathRoot)),
PathSegment::from_ident(Ident::with_dummy_span(sym::std)),
PathSegment::from_ident(Ident::from_str("boxed")),
PathSegment::from_ident(Ident::from_str("Box")),
PathSegment::from_ident(Ident::with_dummy_span(sym::new)),
]
.into(),
tokens: None,
};
let path = self.mk_expr(span, ExprKind::Path(None, path));
Ok((span, self.mk_call(path, ThinVec::from([expr]))))
}
fn is_mistaken_not_ident_negation(&self) -> bool {
let token_cannot_continue_expr = |t: &Token| match t.uninterpolate().kind {
// These tokens can start an expression after `!`, but

View File

@ -0,0 +1,14 @@
// run-rustfix
fn main() {
#[allow(dead_code)]
struct T {
a: u8,
b: u8,
}
let _ = Box::new(()); //~ ERROR `box_syntax` has been removed
let _ = Box::new(1); //~ ERROR `box_syntax` has been removed
let _ = Box::new(T { a: 12, b: 18 }); //~ ERROR `box_syntax` has been removed
let _ = Box::new([5; 30]); //~ ERROR `box_syntax` has been removed
let _: Box<()> = Box::new(()); //~ ERROR `box_syntax` has been removed
}

View File

@ -1,10 +1,14 @@
// run-rustfix
fn main() {
#[allow(dead_code)]
struct T {
a: u8,
b: u8,
}
let _ = box () //~ ERROR expected expression, found reserved keyword `box`
let _ = box 1;
let _ = box T { a: 12, b: 18 };
let _ = box [5; 30];
let _ = box (); //~ ERROR `box_syntax` has been removed
let _ = box 1; //~ ERROR `box_syntax` has been removed
let _ = box T { a: 12, b: 18 }; //~ ERROR `box_syntax` has been removed
let _ = box [5; 30]; //~ ERROR `box_syntax` has been removed
let _: Box<()> = box (); //~ ERROR `box_syntax` has been removed
}

View File

@ -1,8 +1,57 @@
error: expected expression, found reserved keyword `box`
--> $DIR/removed-syntax-box.rs:6:13
error: `box_syntax` has been removed
--> $DIR/removed-syntax-box.rs:9:13
|
LL | let _ = box ()
| ^^^ expected expression
LL | let _ = box ();
| ^^^^^^
|
help: use `Box::new()` instead
|
LL | let _ = Box::new(());
| ~~~~~~~~~~~~
error: aborting due to previous error
error: `box_syntax` has been removed
--> $DIR/removed-syntax-box.rs:10:13
|
LL | let _ = box 1;
| ^^^^^
|
help: use `Box::new()` instead
|
LL | let _ = Box::new(1);
| ~~~~~~~~~~~
error: `box_syntax` has been removed
--> $DIR/removed-syntax-box.rs:11:13
|
LL | let _ = box T { a: 12, b: 18 };
| ^^^^^^^^^^^^^^^^^^^^^^
|
help: use `Box::new()` instead
|
LL | let _ = Box::new(T { a: 12, b: 18 });
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: `box_syntax` has been removed
--> $DIR/removed-syntax-box.rs:12:13
|
LL | let _ = box [5; 30];
| ^^^^^^^^^^^
|
help: use `Box::new()` instead
|
LL | let _ = Box::new([5; 30]);
| ~~~~~~~~~~~~~~~~~
error: `box_syntax` has been removed
--> $DIR/removed-syntax-box.rs:13:22
|
LL | let _: Box<()> = box ();
| ^^^^^^
|
help: use `Box::new()` instead
|
LL | let _: Box<()> = Box::new(());
| ~~~~~~~~~~~~
error: aborting due to 5 previous errors

View File

@ -10,5 +10,5 @@ extern crate std;
fn main() {
let _ =
#[rustc_box]
Box::new (1);
Box::new(1);
}