-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Conversation
r? @wesleywiser (rustbot has picked a reviewer for you, use r? to override) |
There was a problem hiding this 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.
&& boxx.ident.name == sym::Box | ||
&& new.ident.name == sym::new |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
@rustbot author |
327d116
to
efe4850
Compare
Some changes occurred in src/tools/clippy cc @rust-lang/clippy |
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 Since @rustbot ready |
efe4850
to
d845769
Compare
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.
Yeah if we want As a first step after A Such a feature would allow us to build Anyways, I'm getting offtopic. I think removing the |
@bors r+ |
…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
…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`
…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
…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
…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
…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
…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
Currently,
#[rustc_box]
can be applied to any call expression with a single argument. This PR only allows it to be applied to calls toBox::new