-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tracking issue for box_syntax #49733
Comments
Do I understand it correctly that the Personally I like the idea of having some syntactic sugar for creating something as common as a box (it does look nice). But obviously it would need to be built on something that works. |
@spacekookie yes, the big deal for box syntax is placement new. |
It doesn't optimize well, and even if it did, you would need some other way around it to avoid blowing up the stack in debug mode. |
So this is perhaps a bit of a weird usecase, but it is the only reason I have used The obvious alternative is to use the allocator directly and unsafely construct a Box from it, but that's annoying and ugly. See example here: https://github.com/mark-i-m/os2/blob/2ab9d589a4463bd3133157b5c1a2f9f6735a3432/kernel/process/sched.rs#L76-L83 |
In most cases people just want to simplify the |
I think that there really isn't a convenient alternative for destructuring boxes without adding a lot of extra nesting. Personally, in very My thought then is that we should separate the tracking issue out for |
This could possible alleviate my usecase: https://crates.io/crates/copyless |
@vadixidav box destructuring(/patterns) is already tracked at #29641 (see mention of 'box patterns' in the issue description) |
I was looking at (box x).into() vs x.box.into() At least, that is what came to my mind. Sorry if this is the wrong ticket or off topic, I just want to see rust getting continuously better and better 😃 |
You could easily make a trait that adds a method and call it x.boxed().into() Implemented like: trait Boxed {
fn boxed(self) -> Box<Self>;
}
impl<T> Boxed for T {
fn boxed(self) -> Box<Self> {
Box::new(self)
}
} (adding type restrictions as needed) |
For my use case, I'm less concerned with syntactic sugar ( If I have a struct like: const SIZE: usize = 4096*4096;
#[derive(Copy, Clone)]
#[repr(C, align(4096))]
pub struct Pages([u8; SIZE]);
impl Default for Pages {
fn default() -> Self {
Pages([0u8; SIZE])
}
} Creating a pub fn with_new() -> Box<Pages> {
Box::new(Pages([0u8; SIZE]))
}
pub fn with_box_syntax() -> Box<Pages> {
box Pages([0u8; SIZE])
}
pub fn with_default() -> Box<Pages> {
Default::default()
}
pub fn with_raw_alloc() -> Box<Pages> {
use std::alloc::{alloc_zeroed, Layout, handle_alloc_error};
let layout = Layout::new::<Pages>();
let ptr = unsafe { alloc_zeroed(layout) };
if ptr.is_null() {
handle_alloc_error(layout);
}
unsafe { Box::from_raw(ptr as *mut Pages) }
}
So even something like @mark-i-m's example isn't guaranteed to work. Should the |
I'm sort of confused on the state of this feature. Will it ever be stabilized? What are the reasons it cannot be stabilized in its current form if so? |
I believe there is a bunch of design work left. In particular, it's not clear how adjacent features like a hypothetical "placement new" syntax would work, and how they would interact with box syntax. |
I'm working on a competing compiler to rustc, and I am particularily aiming to make |
Never mind, with
|
FTR, deref patterns (#87121) don't help with removal of
But yeah, going via the deprecation lint route might be useful because I suppose a lot of people are using box syntax. Unconditional warnings are equivalent to unconditional errors, because you can't turn them off. But a deprecation lint might work, idk. Given that it's a nightly feature, it could even start with deny by default. |
I've opened #97293 to figure out a replacement syntax. In there, I introduce Note that I have edited this comment, it didn't say at the start what I intended. Sorry for the trouble! |
I've been wondering about The alternative that I could think of would be a lang-item function like |
@est31 Yet another alternative would be to have a |
If it's going to be a public macro, it should be called `boxed!()` instead.
…On Mon, 3 Oct 2022 at 10:46, Esteban Kuber ***@***.***> wrote:
The alternative that I could think of would be a lang-item function like
Box::new_in_place or a free function alloc::boxed::box_in_place with its
own feature gate. What do people think?
@est31 <https://github.com/est31> Yet another alternative would be to
have a rustc_box!() macro or similar, but I don't know if that buys us
much beyond a lang-item (beyond making it slightly clear that the macro
intrinsic might be doing something beyond a pure function call).
—
Reply to this email directly, view it on GitHub
<#49733 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABGLD25BQUMBRZWFA3QW7SDWBLWTTANCNFSM4EZIVXJQ>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
@estebank Personally, I see the attribute on the function more as a guaranteed optimization. But admittedly you can see macros in the same way, as one of the things they provide is guaranteed inlining. I've recently been drawn towards So maybe the |
Apologies for the ping on this question for anyone else, but I haven't heard of |
It took me an awful long time to hunt down the status of this issue, and the history is kind of confusing. So to save anyone time who's not familiar with this issue, here are the answers to the questions I had:
|
Do not use box syntax in `std` See rust-lang#94970 and rust-lang#49733. About half of the `box` instances in `std` do not even need to allocate, the other half can simply be replaced with `Box::new`. `@rustbot` label +T-libs r? rust-lang/libs
…ieb,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 rust-lang#49733
It was dropped in rust-lang/rust#49733 and isn't supported by new Syn. box unpacking looks like a good substitute
It was dropped in rust-lang/rust#49733 and isn't supported by new Syn. box unpacking looks like a good substitute
It was dropped in rust-lang/rust#49733 and isn't supported by new Syn. box unpacking looks like a good substitute
This is a tracking issue for the box_syntax feature, the special syntax for creating boxes, e.g.
box 1usize
(note: box patterns is separate and is tracked at at #29641)This was previously under the same tracking issues as placement (#22181/#27779) but I didn't want to remove it (yet) due to widespread usage in the compiler (
Box::new
is implemented in terms of it) and in the wider ecosystem (unlike<-
etc which only had a handful of crates using it).Things to decide:
box
syntax be phased out?Box::new
is inline always, or does that not work?box
andin
expressions (tracking issue for RFC 809) #22181 about the syntax behaviourNote: when removing this, be careful not to remove the syntax entirely in order to not cause another case of #50832
The text was updated successfully, but these errors were encountered: