-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Improve suggestions for NonZeroT
<- T
coercion error
#99438
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
37c21d6
Do not suggest "wrapping the expression in `std::num::NonZeroU64`"
WaffleLapkin 7163e7f
Suggest a fix for `NonZero*` <- `*` coercion error
WaffleLapkin da2752e
check accessibility before suggesting wrapping expressions
WaffleLapkin 2edad7d
Apply suggestions from the review
WaffleLapkin 5bd88df
Add a note about privacy to wrapping suggestion
WaffleLapkin 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,9 @@ | ||
fn main() { | ||
let _: std::num::NonZeroU64 = 1; | ||
//~^ ERROR mismatched types | ||
//~| HELP consider calling `NonZeroU64::new` | ||
|
||
let _: Option<std::num::NonZeroU64> = 1; | ||
//~^ ERROR mismatched types | ||
//~| HELP consider calling `NonZeroU64::new` | ||
} |
31 changes: 31 additions & 0 deletions
31
src/test/ui/mismatched_types/non_zero_assigned_something.stderr
This file contains hidden or 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,31 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/non_zero_assigned_something.rs:2:35 | ||
| | ||
LL | let _: std::num::NonZeroU64 = 1; | ||
| -------------------- ^ expected struct `NonZeroU64`, found integer | ||
| | | ||
| expected due to this | ||
| | ||
help: consider calling `NonZeroU64::new` | ||
| | ||
LL | let _: std::num::NonZeroU64 = NonZeroU64::new(1).unwrap(); | ||
| ++++++++++++++++ ++++++++++ | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/non_zero_assigned_something.rs:6:43 | ||
| | ||
LL | let _: Option<std::num::NonZeroU64> = 1; | ||
| ---------------------------- ^ expected enum `Option`, found integer | ||
| | | ||
| expected due to this | ||
| | ||
= note: expected enum `Option<NonZeroU64>` | ||
found type `{integer}` | ||
help: consider calling `NonZeroU64::new` | ||
| | ||
LL | let _: Option<std::num::NonZeroU64> = NonZeroU64::new(1); | ||
| ++++++++++++++++ + | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
This file contains hidden or 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,24 @@ | ||
mod inner { | ||
pub struct Wrapper<T>(T); | ||
} | ||
|
||
fn needs_wrapper(t: inner::Wrapper<i32>) {} | ||
fn needs_wrapping(t: std::num::Wrapping<i32>) {} | ||
fn needs_ready(t: std::future::Ready<i32>) {} | ||
|
||
fn main() { | ||
// Suggest wrapping expression because type is local | ||
// and its privacy can be easily changed | ||
needs_wrapper(0); | ||
//~^ ERROR mismatched types | ||
//~| HELP try wrapping the expression in `inner::Wrapper` | ||
|
||
// Suggest wrapping expression because field is accessible | ||
needs_wrapping(0); | ||
//~^ ERROR mismatched types | ||
//~| HELP try wrapping the expression in `std::num::Wrapping` | ||
|
||
// Do not suggest wrapping expression | ||
needs_ready(Some(0)); | ||
//~^ ERROR mismatched types | ||
} |
59 changes: 59 additions & 0 deletions
59
src/test/ui/mismatched_types/wrap-suggestion-privacy.stderr
This file contains hidden or 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,59 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/wrap-suggestion-privacy.rs:12:19 | ||
| | ||
LL | needs_wrapper(0); | ||
| ------------- ^ expected struct `Wrapper`, found integer | ||
| | | ||
| arguments to this function are incorrect | ||
| | ||
= note: expected struct `Wrapper<i32>` | ||
found type `{integer}` | ||
note: function defined here | ||
--> $DIR/wrap-suggestion-privacy.rs:5:4 | ||
| | ||
LL | fn needs_wrapper(t: inner::Wrapper<i32>) {} | ||
| ^^^^^^^^^^^^^ ---------------------- | ||
help: try wrapping the expression in `inner::Wrapper` (its field is private, but it's local to this crate and its privacy can be changed) | ||
| | ||
LL | needs_wrapper(inner::Wrapper(0)); | ||
| +++++++++++++++ + | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/wrap-suggestion-privacy.rs:17:20 | ||
| | ||
LL | needs_wrapping(0); | ||
| -------------- ^ expected struct `Wrapping`, found integer | ||
| | | ||
| arguments to this function are incorrect | ||
| | ||
= note: expected struct `Wrapping<i32>` | ||
found type `{integer}` | ||
note: function defined here | ||
--> $DIR/wrap-suggestion-privacy.rs:6:4 | ||
| | ||
LL | fn needs_wrapping(t: std::num::Wrapping<i32>) {} | ||
| ^^^^^^^^^^^^^^ -------------------------- | ||
help: try wrapping the expression in `std::num::Wrapping` | ||
| | ||
LL | needs_wrapping(std::num::Wrapping(0)); | ||
| +++++++++++++++++++ + | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/wrap-suggestion-privacy.rs:22:17 | ||
| | ||
LL | needs_ready(Some(0)); | ||
| ----------- ^^^^^^^ expected struct `std::future::Ready`, found enum `Option` | ||
| | | ||
| arguments to this function are incorrect | ||
| | ||
= note: expected struct `std::future::Ready<i32>` | ||
found enum `Option<{integer}>` | ||
note: function defined here | ||
--> $DIR/wrap-suggestion-privacy.rs:7:4 | ||
| | ||
LL | fn needs_ready(t: std::future::Ready<i32>) {} | ||
| ^^^^^^^^^^^ -------------------------- | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
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.
Uh oh!
There was an error while loading. Please reload this page.