-
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
[WIP] Also lint Box::new in unused_allocation #97774
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1037,6 +1037,7 @@ symbols! { | |
out, | ||
overlapping_marker_traits, | ||
owned_box, | ||
owned_box_new, | ||
packed, | ||
panic, | ||
panic_2015, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#![feature(box_syntax)] | ||
#![deny(unused_allocation)] | ||
|
||
fn main() { | ||
let foo = (box [1, 2, 3]).len(); //~ ERROR: unnecessary allocation | ||
let one = (box vec![1]).pop(); //~ ERROR: unnecessary allocation | ||
|
||
let foo = Box::new([1, 2, 3]).len(); //~ ERROR: unnecessary allocation | ||
let one = Box::new(vec![1]).pop(); //~ ERROR: unnecessary allocation | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
error: unnecessary allocation, use `&` instead | ||
--> $DIR/unused-allocation.rs:5:15 | ||
| | ||
LL | let foo = (box [1, 2, 3]).len(); | ||
| ^^^^^^^^^^^^^^^ | ||
| | ||
note: the lint level is defined here | ||
--> $DIR/unused-allocation.rs:2:9 | ||
| | ||
LL | #![deny(unused_allocation)] | ||
| ^^^^^^^^^^^^^^^^^ | ||
|
||
error: unnecessary allocation, use `&mut` instead | ||
--> $DIR/unused-allocation.rs:6:15 | ||
| | ||
LL | let one = (box vec![1]).pop(); | ||
| ^^^^^^^^^^^^^ | ||
|
||
error: unnecessary allocation, use `&` instead | ||
--> $DIR/unused-allocation.rs:8:15 | ||
| | ||
LL | let foo = Box::new([1, 2, 3]).len(); | ||
| ^^^^^^^^ | ||
|
||
error: unnecessary allocation, use `&mut` instead | ||
--> $DIR/unused-allocation.rs:9:15 | ||
| | ||
LL | let one = Box::new(vec![1]).pop(); | ||
| ^^^^^^^^ | ||
|
||
error: aborting due to 4 previous errors | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
// run-pass | ||
#![allow(unused_allocation)] | ||
|
||
use std::rc::Rc; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
// run-pass | ||
#![allow(dead_code)] | ||
#![allow(unused_allocation)] | ||
|
||
use std::mem; | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Making it a diagnostic item instead of a lang item would be preferable in this case.
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've been planning, as a followup to this PR, to also use this for the
#[rustc_box]
feature. Right now,#[rustc_box] foobar(foo)
is lowered tobox foo
, irrespective of whatfoobar
is, even if it isn'tBox::new
. In order to make it error if it's notBox::new
, I would be needing something that gives me a def id i suppose. Is a diagnostic item a good choice for such an error as well?To explain, in a
box_syntax
free world, some users might still want to use#[rustc_box]
overBox::new
and they should get a more polished feature.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.
Which seems fine to me, given that
#[rustc_box]
is an internal feature that is supposed to be removed in the future.If it's supposed to be used by people and be polished, then perhaps it needs a better syntax than
#[rustc_box] Box::new(expr)
, for examplebox expr
, oh wait.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 didn't follow the story behind #97293 but the state in which you need to write
#[rustc_box] Box::new(expr)
instead of justBox::new(expr)
orbox expr
(*) seems like the worse from both worlds.(*) Preferably the former, because whatever made
Box::new(expr)
slow in #97293 can also make other much more common things likeVec::from()
/String::from()
slow, so it may be a good win if it's fixed.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.
Box::new(foo)
is not slow in most of cases, thanks to LLVM being really good, especially post version 12 (minimum LLVM is 12 now). The issue is just that switchingvec![]
toBox::new
would also switch it for the remaining few cases where it does cause regressions. For these, the PR added#[rustc_box]
as an alternative tobox foo
. My ultimate goal is to removebox foo
support from the compiler, this PR is another preparation of it. Eventually this will need a lang team FCP and a "go forward" from them. Anyways, the general future ofbox
syntax is not discussed here but in the tracking issue (still need to make a proposal there).It might be better if instead of doing empty promises, I add the lang item in a separate PR first that actually needs it, and then switch this PR to it.