Skip to content

Commit f1b1ed7

Browse files
committed
Auto merge of #108471 - clubby789:unbox-the-syntax, r=Nilstrieb,est31
Remove `box_syntax` r? `@Nilstrieb` This removes the feature `box_syntax`, which allows the use of `box <expr>` to create a Box, and finalises removing use of the feature from the compiler. `box_patterns` (allowing the use of `box <pat>` in a pattern) is unaffected. It also removes `ast::ExprKind::Box` - the only way to create a 'box' expression now is with the rustc-internal `#[rustc_box]` attribute. As a temporary measure to help users move away, `box <expr>` now parses the inner expression, and emits a `MachineApplicable` lint to replace it with `Box::new` Closes #49733
2 parents d610b0c + 8b186df commit f1b1ed7

File tree

110 files changed

+979
-1054
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

110 files changed

+979
-1054
lines changed

compiler/rustc_ast/src/ast.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -1230,7 +1230,6 @@ impl Expr {
12301230

12311231
pub fn precedence(&self) -> ExprPrecedence {
12321232
match self.kind {
1233-
ExprKind::Box(_) => ExprPrecedence::Box,
12341233
ExprKind::Array(_) => ExprPrecedence::Array,
12351234
ExprKind::ConstBlock(_) => ExprPrecedence::ConstBlock,
12361235
ExprKind::Call(..) => ExprPrecedence::Call,
@@ -1291,8 +1290,7 @@ impl Expr {
12911290
/// To a first-order approximation, is this a pattern?
12921291
pub fn is_approximately_pattern(&self) -> bool {
12931292
match &self.peel_parens().kind {
1294-
ExprKind::Box(_)
1295-
| ExprKind::Array(_)
1293+
ExprKind::Array(_)
12961294
| ExprKind::Call(_, _)
12971295
| ExprKind::Tup(_)
12981296
| ExprKind::Lit(_)
@@ -1363,8 +1361,6 @@ pub struct StructExpr {
13631361

13641362
#[derive(Clone, Encodable, Decodable, Debug)]
13651363
pub enum ExprKind {
1366-
/// A `box x` expression.
1367-
Box(P<Expr>),
13681364
/// An array (`[a, b, c, d]`)
13691365
Array(ThinVec<P<Expr>>),
13701366
/// Allow anonymous constants from an inline `const` block

compiler/rustc_ast/src/mut_visit.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1316,7 +1316,6 @@ pub fn noop_visit_expr<T: MutVisitor>(
13161316
vis: &mut T,
13171317
) {
13181318
match kind {
1319-
ExprKind::Box(expr) => vis.visit_expr(expr),
13201319
ExprKind::Array(exprs) => visit_thin_exprs(exprs, vis),
13211320
ExprKind::ConstBlock(anon_const) => {
13221321
vis.visit_anon_const(anon_const);

compiler/rustc_ast/src/util/classify.rs

-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ pub fn expr_trailing_brace(mut expr: &ast::Expr) -> Option<&ast::Expr> {
3535
| Assign(_, e, _)
3636
| AssignOp(_, _, e)
3737
| Binary(_, _, e)
38-
| Box(e)
3938
| Break(_, Some(e))
4039
| Let(_, e, _)
4140
| Range(_, Some(e), _)

compiler/rustc_ast/src/visit.rs

-1
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,6 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
772772
walk_list!(visitor, visit_attribute, expression.attrs.iter());
773773

774774
match &expression.kind {
775-
ExprKind::Box(subexpression) => visitor.visit_expr(subexpression),
776775
ExprKind::Array(subexpressions) => {
777776
walk_list!(visitor, visit_expr, subexpressions);
778777
}

compiler/rustc_ast_lowering/src/expr.rs

-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
7070
self.lower_attrs(hir_id, &e.attrs);
7171

7272
let kind = match &e.kind {
73-
ExprKind::Box(inner) => hir::ExprKind::Box(self.lower_expr(inner)),
7473
ExprKind::Array(exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)),
7574
ExprKind::ConstBlock(anon_const) => {
7675
let anon_const = self.lower_anon_const(anon_const);

compiler/rustc_ast_passes/src/feature_gate.rs

-9
Original file line numberDiff line numberDiff line change
@@ -392,14 +392,6 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
392392

393393
fn visit_expr(&mut self, e: &'a ast::Expr) {
394394
match e.kind {
395-
ast::ExprKind::Box(_) => {
396-
gate_feature_post!(
397-
&self,
398-
box_syntax,
399-
e.span,
400-
"box expression syntax is experimental; you can call `Box::new` instead"
401-
);
402-
}
403395
ast::ExprKind::Type(..) => {
404396
if self.sess.parse_sess.span_diagnostic.err_count() == 0 {
405397
// To avoid noise about type ascription in common syntax errors,
@@ -604,7 +596,6 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session) {
604596
gate_all!(box_patterns, "box pattern syntax is experimental");
605597
gate_all!(exclusive_range_pattern, "exclusive range pattern syntax is experimental");
606598
gate_all!(try_blocks, "`try` blocks are unstable");
607-
gate_all!(box_syntax, "box expression syntax is experimental; you can call `Box::new` instead");
608599
gate_all!(type_ascription, "type ascription is experimental");
609600

610601
visit::walk_crate(&mut visitor, krate);

compiler/rustc_ast_pretty/src/pprust/state/expr.rs

-4
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,6 @@ impl<'a> State<'a> {
296296
self.ibox(INDENT_UNIT);
297297
self.ann.pre(self, AnnNode::Expr(expr));
298298
match &expr.kind {
299-
ast::ExprKind::Box(expr) => {
300-
self.word_space("box");
301-
self.print_expr_maybe_paren(expr, parser::PREC_PREFIX);
302-
}
303299
ast::ExprKind::Array(exprs) => {
304300
self.print_expr_vec(exprs);
305301
}

compiler/rustc_builtin_macros/src/assert/context.rs

-1
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,6 @@ impl<'cx, 'a> Context<'cx, 'a> {
290290
| ExprKind::Async(_, _, _)
291291
| ExprKind::Await(_)
292292
| ExprKind::Block(_, _)
293-
| ExprKind::Box(_)
294293
| ExprKind::Break(_, _)
295294
| ExprKind::Closure(_)
296295
| ExprKind::ConstBlock(_)

compiler/rustc_codegen_cranelift/example/alloc_example.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(start, core_intrinsics, alloc_error_handler, box_syntax)]
1+
#![feature(start, core_intrinsics, alloc_error_handler)]
22
#![no_std]
33

44
extern crate alloc;
@@ -29,7 +29,7 @@ fn alloc_error_handler(_: alloc::alloc::Layout) -> ! {
2929

3030
#[start]
3131
fn main(_argc: isize, _argv: *const *const u8) -> isize {
32-
let world: Box<&str> = box "Hello World!\0";
32+
let world: Box<&str> = Box::new("Hello World!\0");
3333
unsafe {
3434
puts(*world as *const str as *const u8);
3535
}

compiler/rustc_codegen_cranelift/example/mini_core_hello_world.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(no_core, lang_items, never_type, linkage, extern_types, thread_local, box_syntax)]
1+
#![feature(no_core, lang_items, never_type, linkage, extern_types, thread_local)]
22
#![no_core]
33
#![allow(dead_code, non_camel_case_types)]
44

@@ -178,7 +178,7 @@ fn main() {
178178
let ptr: *const i8 = hello as *const [u8] as *const i8;
179179
puts(ptr);
180180

181-
let world: Box<&str> = box "World!\0";
181+
let world: Box<&str> = Box::new("World!\0");
182182
puts(*world as *const str as *const i8);
183183
world as Box<dyn SomeTrait>;
184184

@@ -238,10 +238,10 @@ fn main() {
238238
}
239239
}
240240

241-
let _ = box NoisyDrop {
241+
let _ = Box::new(NoisyDrop {
242242
text: "Boxed outer got dropped!\0",
243243
inner: NoisyDropInner,
244-
} as Box<dyn SomeTrait>;
244+
}) as Box<dyn SomeTrait>;
245245

246246
const FUNC_REF: Option<fn()> = Some(main);
247247
match FUNC_REF {

compiler/rustc_codegen_gcc/example/alloc_example.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(start, box_syntax, core_intrinsics, alloc_error_handler, lang_items)]
1+
#![feature(start, core_intrinsics, alloc_error_handler, lang_items)]
22
#![no_std]
33

44
extern crate alloc;
@@ -38,7 +38,7 @@ unsafe extern "C" fn _Unwind_Resume() {
3838

3939
#[start]
4040
fn main(_argc: isize, _argv: *const *const u8) -> isize {
41-
let world: Box<&str> = box "Hello World!\0";
41+
let world: Box<&str> = Box::new("Hello World!\0");
4242
unsafe {
4343
puts(*world as *const str as *const u8);
4444
}

compiler/rustc_codegen_gcc/example/mini_core_hello_world.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Adapted from https://github.com/sunfishcode/mir2cranelift/blob/master/rust-examples/nocore-hello-world.rs
22

33
#![feature(
4-
no_core, unboxed_closures, start, lang_items, box_syntax, never_type, linkage,
4+
no_core, unboxed_closures, start, lang_items, never_type, linkage,
55
extern_types, thread_local
66
)]
77
#![no_core]
@@ -163,7 +163,7 @@ fn main() {
163163
let ptr: *const u8 = hello as *const [u8] as *const u8;
164164
puts(ptr);
165165

166-
let world: Box<&str> = box "World!\0";
166+
let world: Box<&str> = Box::new("World!\0");
167167
puts(*world as *const str as *const u8);
168168
world as Box<dyn SomeTrait>;
169169

@@ -223,10 +223,10 @@ fn main() {
223223
}
224224
}
225225

226-
let _ = box NoisyDrop {
226+
let _ = Box::new(NoisyDrop {
227227
text: "Boxed outer got dropped!\0",
228228
inner: NoisyDropInner,
229-
} as Box<dyn SomeTrait>;
229+
}) as Box<dyn SomeTrait>;
230230

231231
const FUNC_REF: Option<fn()> = Some(main);
232232
#[allow(unreachable_code)]

compiler/rustc_codegen_gcc/example/mod_bench.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(start, box_syntax, core_intrinsics, lang_items)]
1+
#![feature(start, core_intrinsics, lang_items)]
22
#![no_std]
33

44
#[link(name = "c")]

compiler/rustc_error_codes/src/error_codes/E0010.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,5 @@ the heap at runtime, and therefore cannot be done at compile time.
55
Erroneous code example:
66

77
```compile_fail,E0010
8-
#![feature(box_syntax)]
9-
10-
const CON : Box<i32> = box 0;
8+
const CON : Vec<i32> = vec![1, 2, 3];
119
```

compiler/rustc_feature/src/active.rs

-2
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,6 @@ declare_features! (
200200
(active, auto_traits, "1.50.0", Some(13231), None),
201201
/// Allows using `box` in patterns (RFC 469).
202202
(active, box_patterns, "1.0.0", Some(29641), None),
203-
/// Allows using the `box $expr` syntax.
204-
(active, box_syntax, "1.0.0", Some(49733), None),
205203
/// Allows `#[doc(notable_trait)]`.
206204
/// Renamed from `doc_spotlight`.
207205
(active, doc_notable_trait, "1.52.0", Some(45040), None),

compiler/rustc_feature/src/removed.rs

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ declare_features! (
5252
(removed, allow_fail, "1.19.0", Some(46488), None, Some("removed due to no clear use cases")),
5353
(removed, await_macro, "1.38.0", Some(50547), None,
5454
Some("subsumed by `.await` syntax")),
55+
/// Allows using the `box $expr` syntax.
56+
(removed, box_syntax, "CURRENT_RUSTC_VERSION", Some(49733), None, Some("replaced with `#[rustc_box]`")),
5557
/// Allows capturing disjoint fields in a closure/generator (RFC 2229).
5658
(removed, capture_disjoint_fields, "1.49.0", Some(53488), None, Some("stabilized in Rust 2021")),
5759
/// Allows comparing raw pointers during const eval.

compiler/rustc_hir_pretty/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1367,8 +1367,8 @@ impl<'a> State<'a> {
13671367
self.ann.pre(self, AnnNode::Expr(expr));
13681368
match expr.kind {
13691369
hir::ExprKind::Box(expr) => {
1370-
self.word_space("box");
1371-
self.print_expr_maybe_paren(expr, parser::PREC_PREFIX);
1370+
self.word_space("Box::new");
1371+
self.print_call_post(std::slice::from_ref(expr));
13721372
}
13731373
hir::ExprKind::Array(exprs) => {
13741374
self.print_expr_vec(exprs);

compiler/rustc_lint/src/unused.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1371,7 +1371,6 @@ declare_lint_pass!(UnusedAllocation => [UNUSED_ALLOCATION]);
13711371
impl<'tcx> LateLintPass<'tcx> for UnusedAllocation {
13721372
fn check_expr(&mut self, cx: &LateContext<'_>, e: &hir::Expr<'_>) {
13731373
match e.kind {
1374-
hir::ExprKind::Box(_) => {}
13751374
hir::ExprKind::Call(path_expr, [_])
13761375
if let hir::ExprKind::Path(qpath) = &path_expr.kind
13771376
&& let Some(did) = cx.qpath_res(qpath, path_expr.hir_id).opt_def_id()

compiler/rustc_mir_transform/src/large_enums.rs

+25-20
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,12 @@ impl EnumSizeOpt {
158158
tmp_ty,
159159
),
160160
};
161-
let rval = Rvalue::Use(Operand::Constant(box (constant_vals)));
161+
let rval = Rvalue::Use(Operand::Constant(Box::new(constant_vals)));
162162

163-
let const_assign =
164-
Statement { source_info, kind: StatementKind::Assign(box (place, rval)) };
163+
let const_assign = Statement {
164+
source_info,
165+
kind: StatementKind::Assign(Box::new((place, rval))),
166+
};
165167

166168
let discr_place = Place::from(
167169
local_decls
@@ -170,48 +172,51 @@ impl EnumSizeOpt {
170172

171173
let store_discr = Statement {
172174
source_info,
173-
kind: StatementKind::Assign(box (discr_place, Rvalue::Discriminant(*rhs))),
175+
kind: StatementKind::Assign(Box::new((
176+
discr_place,
177+
Rvalue::Discriminant(*rhs),
178+
))),
174179
};
175180

176181
let discr_cast_place =
177182
Place::from(local_decls.push(LocalDecl::new(tcx.types.usize, span)));
178183

179184
let cast_discr = Statement {
180185
source_info,
181-
kind: StatementKind::Assign(box (
186+
kind: StatementKind::Assign(Box::new((
182187
discr_cast_place,
183188
Rvalue::Cast(
184189
CastKind::IntToInt,
185190
Operand::Copy(discr_place),
186191
tcx.types.usize,
187192
),
188-
)),
193+
))),
189194
};
190195

191196
let size_place =
192197
Place::from(local_decls.push(LocalDecl::new(tcx.types.usize, span)));
193198

194199
let store_size = Statement {
195200
source_info,
196-
kind: StatementKind::Assign(box (
201+
kind: StatementKind::Assign(Box::new((
197202
size_place,
198203
Rvalue::Use(Operand::Copy(Place {
199204
local: size_array_local,
200205
projection: tcx
201206
.mk_place_elems(&[PlaceElem::Index(discr_cast_place.local)]),
202207
})),
203-
)),
208+
))),
204209
};
205210

206211
let dst =
207212
Place::from(local_decls.push(LocalDecl::new(tcx.mk_mut_ptr(ty), span)));
208213

209214
let dst_ptr = Statement {
210215
source_info,
211-
kind: StatementKind::Assign(box (
216+
kind: StatementKind::Assign(Box::new((
212217
dst,
213218
Rvalue::AddressOf(Mutability::Mut, *lhs),
214-
)),
219+
))),
215220
};
216221

217222
let dst_cast_ty = tcx.mk_mut_ptr(tcx.types.u8);
@@ -220,21 +225,21 @@ impl EnumSizeOpt {
220225

221226
let dst_cast = Statement {
222227
source_info,
223-
kind: StatementKind::Assign(box (
228+
kind: StatementKind::Assign(Box::new((
224229
dst_cast_place,
225230
Rvalue::Cast(CastKind::PtrToPtr, Operand::Copy(dst), dst_cast_ty),
226-
)),
231+
))),
227232
};
228233

229234
let src =
230235
Place::from(local_decls.push(LocalDecl::new(tcx.mk_imm_ptr(ty), span)));
231236

232237
let src_ptr = Statement {
233238
source_info,
234-
kind: StatementKind::Assign(box (
239+
kind: StatementKind::Assign(Box::new((
235240
src,
236241
Rvalue::AddressOf(Mutability::Not, *rhs),
237-
)),
242+
))),
238243
};
239244

240245
let src_cast_ty = tcx.mk_imm_ptr(tcx.types.u8);
@@ -243,24 +248,24 @@ impl EnumSizeOpt {
243248

244249
let src_cast = Statement {
245250
source_info,
246-
kind: StatementKind::Assign(box (
251+
kind: StatementKind::Assign(Box::new((
247252
src_cast_place,
248253
Rvalue::Cast(CastKind::PtrToPtr, Operand::Copy(src), src_cast_ty),
249-
)),
254+
))),
250255
};
251256

252257
let deinit_old =
253-
Statement { source_info, kind: StatementKind::Deinit(box dst) };
258+
Statement { source_info, kind: StatementKind::Deinit(Box::new(dst)) };
254259

255260
let copy_bytes = Statement {
256261
source_info,
257-
kind: StatementKind::Intrinsic(
258-
box NonDivergingIntrinsic::CopyNonOverlapping(CopyNonOverlapping {
262+
kind: StatementKind::Intrinsic(Box::new(
263+
NonDivergingIntrinsic::CopyNonOverlapping(CopyNonOverlapping {
259264
src: Operand::Copy(src_cast_place),
260265
dst: Operand::Copy(dst_cast_place),
261266
count: Operand::Copy(size_place),
262267
}),
263-
),
268+
)),
264269
};
265270

266271
let store_dead = Statement {

compiler/rustc_mir_transform/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#![allow(rustc::potential_query_instability)]
22
#![feature(box_patterns)]
33
#![feature(drain_filter)]
4-
#![feature(box_syntax)]
54
#![feature(let_chains)]
65
#![feature(map_try_insert)]
76
#![feature(min_specialization)]

compiler/rustc_parse/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -731,3 +731,6 @@ parse_unknown_start_of_token = unknown start of token: {$escaped}
731731
[one] once more
732732
*[other] {$repeats} more times
733733
}
734+
735+
parse_box_syntax_removed = `box_syntax` has been removed
736+
.suggestion = use `Box::new()` instead

0 commit comments

Comments
 (0)