Skip to content

Commit cfc21de

Browse files
committed
Add #[rustc_box]
This commit adds an alternative content boxing syntax, and uses it inside alloc. The usage inside the very performance relevant code in liballoc is the only remaining relevant usage of box syntax in the compiler (outside of tests, which are comparatively easy to port). box syntax was originally designed to be used by all Rust developers. This introduces a replacement syntax more tailored to only being used inside the Rust compiler, and with it, lays the groundwork for eventually removing box syntax.
1 parent e810f75 commit cfc21de

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,19 @@ impl<'hir> LoweringContext<'_, 'hir> {
4141
}
4242
ExprKind::Tup(ref elts) => hir::ExprKind::Tup(self.lower_exprs(elts)),
4343
ExprKind::Call(ref f, ref args) => {
44-
if let Some(legacy_args) = self.resolver.legacy_const_generic_args(f) {
44+
if e.attrs.get(0).map_or(false, |a| a.has_name(sym::rustc_box)) {
45+
if let [inner] = &args[..] {
46+
hir::ExprKind::Box(self.lower_expr(&inner))
47+
} else {
48+
self.sess
49+
.struct_span_err(
50+
e.span,
51+
"rustc_box requires precisely one argument",
52+
)
53+
.emit();
54+
hir::ExprKind::Err
55+
}
56+
} else if let Some(legacy_args) = self.resolver.legacy_const_generic_args(f) {
4557
self.lower_legacy_const_generics((**f).clone(), args.clone(), &legacy_args)
4658
} else {
4759
let f = self.lower_expr(f);

compiler/rustc_feature/src/builtin_attrs.rs

+6
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,12 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
675675
"#[rustc_has_incoherent_inherent_impls] allows the addition of incoherent inherent impls for \
676676
the given type by annotating all impl items with #[rustc_allow_incoherent_impl]."
677677
),
678+
rustc_attr!(
679+
rustc_box, AttributeType::Normal, template!(Word), ErrorFollowing,
680+
"#[rustc_box] allows creating boxes \
681+
and it is only intended to be used in `alloc`."
682+
),
683+
678684
BuiltinAttribute {
679685
name: sym::rustc_diagnostic_item,
680686
// FIXME: This can be `true` once we always use `tcx.is_diagnostic_item`.

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,7 @@ symbols! {
11731173
rustc_allow_const_fn_unstable,
11741174
rustc_allow_incoherent_impl,
11751175
rustc_attrs,
1176+
rustc_box,
11761177
rustc_builtin_macro,
11771178
rustc_capture_analysis,
11781179
rustc_clean,

src/test/ui/type/ascription/issue-47666.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: expected type, found `<[_]>::into_vec(box [0, 1])`
1+
error: expected type, found `<[_]>::into_vec(#[rustc_box] ::alloc::boxed::Box::new([0, 1]))`
22
--> $DIR/issue-47666.rs:3:25
33
|
44
LL | let _ = Option:Some(vec![0, 1]);

0 commit comments

Comments
 (0)