Skip to content
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

Restrict #[rustc_box] to Box::new calls #108516

Merged
merged 1 commit into from
Mar 2, 2023

Conversation

clubby789
Copy link
Contributor

Currently, #[rustc_box] can be applied to any call expression with a single argument. This PR only allows it to be applied to calls to Box::new

@rustbot
Copy link
Collaborator

rustbot commented Feb 27, 2023

r? @wesleywiser

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 27, 2023
Copy link
Member

@jhpratt jhpratt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than how Box::new is detected, this looks good.

Comment on lines 101 to 102
&& boxx.ident.name == sym::Box
&& new.ident.name == sym::new
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would allow any Box, not just the standard library's.

As Box is already a lang item (owned_box), you should be able to restrict it to methods of the type without any additional lang items. Alternatively, you could make Box::new a lang item and check for that specifically, which would limit it to only the one method.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, you can get the Box lang item, get its tcx.associated_items, and then just use https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/assoc/struct.AssocItems.html#method.find_by_name_and_kind to grab the new item.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, that would work as well. I wasn't sure on what was preferred, as I know effort is taken to avoid hard-coding names (even if that name cannot ever change).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, you could make Box::new a lang item and check for that specifically, which would limit it to only the one method.

Or a diagnostic item, which #104363 is adding for Box::new.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's the best idea to use diagnostic items for a hard error like this. The compiler should be able to operate totally fine with all of the diagnostic items cfg'd out.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh -- we also could do an expr macro... rustc_box!(expr)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potentially, but I was hoping to remove ExprKind::Box from the AST with #108471

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm... ideally we'd remove box syntax from the parser, but there's nothing necessarily wrong with keeping it around in AST, like we have with type ascription via a macro.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding the suggestion to move the error to later in the compiler, it might be possible to move the lowering to HIR->THIR lowering in rustc_mir_build, but I'm not sure one can emit errors at that point. It's certainly worth a try tho.

Basically one would add a test here whether there are any attributes, and if there is #[rustc_box] then one would create a THIR ExprKind::Box as done down below in the same method. As an example for code that checks for attributes, you can look for the #[custom_mir] attribute where the attr check is here.

Regarding rustc_box!(expr), I'm not a fan of using the privileged access that builtin-macros have for such purposes, but it seems to be the status quo (for now, unless we get builtin#). I think that the proposals to replace placement new go in the direction of using syntax that is very similar to function calls though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding the suggestion to move the error to later in the compiler, it might be possible to move the lowering to HIR->THIR lowering in rustc_mir_build, but I'm not sure one can emit errors at that point. It's certainly worth a try tho.

Yeah, we definitely have errors emitted at and past this point. Notably pattern exhaustiveness errors, but others too.

@compiler-errors
Copy link
Member

r? @compiler-errors

@compiler-errors
Copy link
Member

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 28, 2023
@clubby789 clubby789 force-pushed the rustc-box-restrict branch 3 times, most recently from 327d116 to efe4850 Compare March 2, 2023 01:44
@rustbot
Copy link
Collaborator

rustbot commented Mar 2, 2023

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

@clubby789
Copy link
Contributor Author

clubby789 commented Mar 2, 2023

As suggested, moved this to check the attribute and call target at HIR->THIR lowering. A clippy lint needed to be updated since it used hir::ExprKind::Box to detect a builtin macro.

Since hir::ExprKind::Box is now no longer created, it may be worth it to remove it entirely, which would require some changes to tools which exhaustively match on HIR exprs - unless builtin# becomes a thing and needs to re-add it?

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 2, 2023
@est31
Copy link
Member

est31 commented Mar 2, 2023

it may be worth it to remove it entirely

That will require both this PR and #108471. This one should be quick to merge, so if we remove it, it should be done in #108471 after it is rebased to a merged version of this PR. Or alternatively, a follow-up.

unless builtin# becomes a thing and needs to re-add it?

Yeah if we want builtin#box, we have to re-add it. We could by then also have changed the feature so much that we have breakage anyways: of the, as of #108476 merged, two current users of #[rustc_box], vec![] is using it in more of a hacky way.

As a first step after box syntax removal, I'd suggest a #[rustc_ptr_write] ptr::write(...) feature, taking in the second argument in-place: right now the desugaring code constructs an allocation, writes into it in-place, and then returns it, we could move the allocation construction into explicit code.

A builtin# equivalent would be builtin#ptr_write, and the AST/HIR node would take two items instead of one, so it would generate some churn anyways I guess.

Such a feature would allow us to build box!(), ptr_write!(), etc macros that expose placement on stable, but IMO for 99% of users, it would be nicer if the optimization just turned on with normal calls to Box::new and ptr::write, without needing any changes to user code. But this is more inside RFC territory I think. I'm open to co-authors, contact me on zulip.

Anyways, I'm getting offtopic. I think removing the ExprKind should be okay, but not in this PR.

@compiler-errors
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Mar 2, 2023

📌 Commit d845769 has been approved by compiler-errors

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 2, 2023
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 2, 2023
…iaskrgr

Rollup of 5 pull requests

Successful merges:

 - rust-lang#108516 (Restrict `#[rustc_box]` to `Box::new` calls)
 - rust-lang#108575 (Erase **all** regions when probing for associated types on ambiguity in astconv)
 - rust-lang#108585 (Run compiler test suite in parallel on Fuchsia)
 - rust-lang#108606 (Add test case for mismatched open/close delims)
 - rust-lang#108609 (Highlight whole expression for E0599)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit c9c9283 into rust-lang:master Mar 2, 2023
@rustbot rustbot added this to the 1.69.0 milestone Mar 2, 2023
flip1995 pushed a commit to flip1995/rust that referenced this pull request Mar 10, 2023
…mpiler-errors

Restrict `#[rustc_box]` to `Box::new` calls

Currently, `#[rustc_box]` can be applied to any call expression with a single argument. This PR only allows it to be applied to calls to `Box::new`
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Mar 16, 2023
…r-errors

Remove box expressions from HIR

After rust-lang#108516, `#[rustc_box]` is used at HIR->THIR lowering and this is no longer emitted, so it can be removed.

This is based on top of rust-lang#108471 to help with conflicts, so 43490488ccacd1a822e9c621f5ed6fca99959a0b is the only relevant commit (sorry for all the duplicated pings!)

`@rustbot` label +S-blocked
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Mar 17, 2023
…r-errors

Remove box expressions from HIR

After rust-lang#108516, `#[rustc_box]` is used at HIR->THIR lowering and this is no longer emitted, so it can be removed.

This is based on top of rust-lang#108471 to help with conflicts, so 43490488ccacd1a822e9c621f5ed6fca99959a0b is the only relevant commit (sorry for all the duplicated pings!)

``@rustbot`` label +S-blocked
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Mar 17, 2023
…r-errors

Remove box expressions from HIR

After rust-lang#108516, `#[rustc_box]` is used at HIR->THIR lowering and this is no longer emitted, so it can be removed.

This is based on top of rust-lang#108471 to help with conflicts, so 43490488ccacd1a822e9c621f5ed6fca99959a0b is the only relevant commit (sorry for all the duplicated pings!)

```@rustbot``` label +S-blocked
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Mar 17, 2023
…r-errors

Remove box expressions from HIR

After rust-lang#108516, `#[rustc_box]` is used at HIR->THIR lowering and this is no longer emitted, so it can be removed.

This is based on top of rust-lang#108471 to help with conflicts, so 43490488ccacd1a822e9c621f5ed6fca99959a0b is the only relevant commit (sorry for all the duplicated pings!)

````@rustbot```` label +S-blocked
flip1995 pushed a commit to flip1995/rust that referenced this pull request Mar 24, 2023
…r-errors

Remove box expressions from HIR

After rust-lang#108516, `#[rustc_box]` is used at HIR->THIR lowering and this is no longer emitted, so it can be removed.

This is based on top of rust-lang#108471 to help with conflicts, so 43490488ccacd1a822e9c621f5ed6fca99959a0b is the only relevant commit (sorry for all the duplicated pings!)

````@rustbot```` label +S-blocked
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants