-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #54734 - pawroman:fix_range_borrowing_suggestion, r=varkor
Fix range literals borrowing suggestions Fixes #54505. The compiler issued incorrect range borrowing suggestions (missing `()` around borrows of range literals). This was not correct syntax (see the issue for an example). With changes in this PR, this is fixed for all types of `Range` literals. Thanks again to @varkor and @estebank for their invaluable help and guidance. r? @varkor
- Loading branch information
Showing
9 changed files
with
664 additions
and
3 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// run-rustfix | ||
|
||
// Regression test for changes introduced while fixing #54505 | ||
|
||
// This test uses non-literals for Ranges | ||
// (expecting no parens with borrow suggestion) | ||
|
||
use std::ops::RangeBounds; | ||
|
||
|
||
// take a reference to any built-in range | ||
fn take_range(_r: &impl RangeBounds<i8>) {} | ||
|
||
|
||
fn main() { | ||
take_range(&std::ops::Range { start: 0, end: 1 }); | ||
//~^ ERROR mismatched types [E0308] | ||
//~| HELP consider borrowing here | ||
//~| SUGGESTION &std::ops::Range { start: 0, end: 1 } | ||
|
||
take_range(&::std::ops::Range { start: 0, end: 1 }); | ||
//~^ ERROR mismatched types [E0308] | ||
//~| HELP consider borrowing here | ||
//~| SUGGESTION &::std::ops::Range { start: 0, end: 1 } | ||
|
||
take_range(&std::ops::RangeFrom { start: 1 }); | ||
//~^ ERROR mismatched types [E0308] | ||
//~| HELP consider borrowing here | ||
//~| SUGGESTION &std::ops::RangeFrom { start: 1 } | ||
|
||
take_range(&::std::ops::RangeFrom { start: 1 }); | ||
//~^ ERROR mismatched types [E0308] | ||
//~| HELP consider borrowing here | ||
//~| SUGGESTION &::std::ops::RangeFrom { start: 1 } | ||
|
||
take_range(&std::ops::RangeFull {}); | ||
//~^ ERROR mismatched types [E0308] | ||
//~| HELP consider borrowing here | ||
//~| SUGGESTION &std::ops::RangeFull {} | ||
|
||
take_range(&::std::ops::RangeFull {}); | ||
//~^ ERROR mismatched types [E0308] | ||
//~| HELP consider borrowing here | ||
//~| SUGGESTION &::std::ops::RangeFull {} | ||
|
||
take_range(&std::ops::RangeInclusive::new(0, 1)); | ||
//~^ ERROR mismatched types [E0308] | ||
//~| HELP consider borrowing here | ||
//~| SUGGESTION &std::ops::RangeInclusive::new(0, 1) | ||
|
||
take_range(&::std::ops::RangeInclusive::new(0, 1)); | ||
//~^ ERROR mismatched types [E0308] | ||
//~| HELP consider borrowing here | ||
//~| SUGGESTION &::std::ops::RangeInclusive::new(0, 1) | ||
|
||
take_range(&std::ops::RangeTo { end: 5 }); | ||
//~^ ERROR mismatched types [E0308] | ||
//~| HELP consider borrowing here | ||
//~| SUGGESTION &std::ops::RangeTo { end: 5 } | ||
|
||
take_range(&::std::ops::RangeTo { end: 5 }); | ||
//~^ ERROR mismatched types [E0308] | ||
//~| HELP consider borrowing here | ||
//~| SUGGESTION &::std::ops::RangeTo { end: 5 } | ||
|
||
take_range(&std::ops::RangeToInclusive { end: 5 }); | ||
//~^ ERROR mismatched types [E0308] | ||
//~| HELP consider borrowing here | ||
//~| SUGGESTION &std::ops::RangeToInclusive { end: 5 } | ||
|
||
take_range(&::std::ops::RangeToInclusive { end: 5 }); | ||
//~^ ERROR mismatched types [E0308] | ||
//~| HELP consider borrowing here | ||
//~| SUGGESTION &::std::ops::RangeToInclusive { end: 5 } | ||
} |
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,75 @@ | ||
// run-rustfix | ||
|
||
// Regression test for changes introduced while fixing #54505 | ||
|
||
// This test uses non-literals for Ranges | ||
// (expecting no parens with borrow suggestion) | ||
|
||
use std::ops::RangeBounds; | ||
|
||
|
||
// take a reference to any built-in range | ||
fn take_range(_r: &impl RangeBounds<i8>) {} | ||
|
||
|
||
fn main() { | ||
take_range(std::ops::Range { start: 0, end: 1 }); | ||
//~^ ERROR mismatched types [E0308] | ||
//~| HELP consider borrowing here | ||
//~| SUGGESTION &std::ops::Range { start: 0, end: 1 } | ||
|
||
take_range(::std::ops::Range { start: 0, end: 1 }); | ||
//~^ ERROR mismatched types [E0308] | ||
//~| HELP consider borrowing here | ||
//~| SUGGESTION &::std::ops::Range { start: 0, end: 1 } | ||
|
||
take_range(std::ops::RangeFrom { start: 1 }); | ||
//~^ ERROR mismatched types [E0308] | ||
//~| HELP consider borrowing here | ||
//~| SUGGESTION &std::ops::RangeFrom { start: 1 } | ||
|
||
take_range(::std::ops::RangeFrom { start: 1 }); | ||
//~^ ERROR mismatched types [E0308] | ||
//~| HELP consider borrowing here | ||
//~| SUGGESTION &::std::ops::RangeFrom { start: 1 } | ||
|
||
take_range(std::ops::RangeFull {}); | ||
//~^ ERROR mismatched types [E0308] | ||
//~| HELP consider borrowing here | ||
//~| SUGGESTION &std::ops::RangeFull {} | ||
|
||
take_range(::std::ops::RangeFull {}); | ||
//~^ ERROR mismatched types [E0308] | ||
//~| HELP consider borrowing here | ||
//~| SUGGESTION &::std::ops::RangeFull {} | ||
|
||
take_range(std::ops::RangeInclusive::new(0, 1)); | ||
//~^ ERROR mismatched types [E0308] | ||
//~| HELP consider borrowing here | ||
//~| SUGGESTION &std::ops::RangeInclusive::new(0, 1) | ||
|
||
take_range(::std::ops::RangeInclusive::new(0, 1)); | ||
//~^ ERROR mismatched types [E0308] | ||
//~| HELP consider borrowing here | ||
//~| SUGGESTION &::std::ops::RangeInclusive::new(0, 1) | ||
|
||
take_range(std::ops::RangeTo { end: 5 }); | ||
//~^ ERROR mismatched types [E0308] | ||
//~| HELP consider borrowing here | ||
//~| SUGGESTION &std::ops::RangeTo { end: 5 } | ||
|
||
take_range(::std::ops::RangeTo { end: 5 }); | ||
//~^ ERROR mismatched types [E0308] | ||
//~| HELP consider borrowing here | ||
//~| SUGGESTION &::std::ops::RangeTo { end: 5 } | ||
|
||
take_range(std::ops::RangeToInclusive { end: 5 }); | ||
//~^ ERROR mismatched types [E0308] | ||
//~| HELP consider borrowing here | ||
//~| SUGGESTION &std::ops::RangeToInclusive { end: 5 } | ||
|
||
take_range(::std::ops::RangeToInclusive { end: 5 }); | ||
//~^ ERROR mismatched types [E0308] | ||
//~| HELP consider borrowing here | ||
//~| SUGGESTION &::std::ops::RangeToInclusive { end: 5 } | ||
} |
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,147 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/issue-54505-no-literals.rs:16:16 | ||
| | ||
LL | take_range(std::ops::Range { start: 0, end: 1 }); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| expected reference, found struct `std::ops::Range` | ||
| help: consider borrowing here: `&std::ops::Range { start: 0, end: 1 }` | ||
| | ||
= note: expected type `&_` | ||
found type `std::ops::Range<{integer}>` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/issue-54505-no-literals.rs:21:16 | ||
| | ||
LL | take_range(::std::ops::Range { start: 0, end: 1 }); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| expected reference, found struct `std::ops::Range` | ||
| help: consider borrowing here: `&::std::ops::Range { start: 0, end: 1 }` | ||
| | ||
= note: expected type `&_` | ||
found type `std::ops::Range<{integer}>` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/issue-54505-no-literals.rs:26:16 | ||
| | ||
LL | take_range(std::ops::RangeFrom { start: 1 }); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| expected reference, found struct `std::ops::RangeFrom` | ||
| help: consider borrowing here: `&std::ops::RangeFrom { start: 1 }` | ||
| | ||
= note: expected type `&_` | ||
found type `std::ops::RangeFrom<{integer}>` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/issue-54505-no-literals.rs:31:16 | ||
| | ||
LL | take_range(::std::ops::RangeFrom { start: 1 }); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| expected reference, found struct `std::ops::RangeFrom` | ||
| help: consider borrowing here: `&::std::ops::RangeFrom { start: 1 }` | ||
| | ||
= note: expected type `&_` | ||
found type `std::ops::RangeFrom<{integer}>` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/issue-54505-no-literals.rs:36:16 | ||
| | ||
LL | take_range(std::ops::RangeFull {}); | ||
| ^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| expected reference, found struct `std::ops::RangeFull` | ||
| help: consider borrowing here: `&std::ops::RangeFull {}` | ||
| | ||
= note: expected type `&_` | ||
found type `std::ops::RangeFull` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/issue-54505-no-literals.rs:41:16 | ||
| | ||
LL | take_range(::std::ops::RangeFull {}); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| expected reference, found struct `std::ops::RangeFull` | ||
| help: consider borrowing here: `&::std::ops::RangeFull {}` | ||
| | ||
= note: expected type `&_` | ||
found type `std::ops::RangeFull` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/issue-54505-no-literals.rs:46:16 | ||
| | ||
LL | take_range(std::ops::RangeInclusive::new(0, 1)); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| expected reference, found struct `std::ops::RangeInclusive` | ||
| help: consider borrowing here: `&std::ops::RangeInclusive::new(0, 1)` | ||
| | ||
= note: expected type `&_` | ||
found type `std::ops::RangeInclusive<{integer}>` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/issue-54505-no-literals.rs:51:16 | ||
| | ||
LL | take_range(::std::ops::RangeInclusive::new(0, 1)); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| expected reference, found struct `std::ops::RangeInclusive` | ||
| help: consider borrowing here: `&::std::ops::RangeInclusive::new(0, 1)` | ||
| | ||
= note: expected type `&_` | ||
found type `std::ops::RangeInclusive<{integer}>` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/issue-54505-no-literals.rs:56:16 | ||
| | ||
LL | take_range(std::ops::RangeTo { end: 5 }); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| expected reference, found struct `std::ops::RangeTo` | ||
| help: consider borrowing here: `&std::ops::RangeTo { end: 5 }` | ||
| | ||
= note: expected type `&_` | ||
found type `std::ops::RangeTo<{integer}>` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/issue-54505-no-literals.rs:61:16 | ||
| | ||
LL | take_range(::std::ops::RangeTo { end: 5 }); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| expected reference, found struct `std::ops::RangeTo` | ||
| help: consider borrowing here: `&::std::ops::RangeTo { end: 5 }` | ||
| | ||
= note: expected type `&_` | ||
found type `std::ops::RangeTo<{integer}>` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/issue-54505-no-literals.rs:66:16 | ||
| | ||
LL | take_range(std::ops::RangeToInclusive { end: 5 }); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| expected reference, found struct `std::ops::RangeToInclusive` | ||
| help: consider borrowing here: `&std::ops::RangeToInclusive { end: 5 }` | ||
| | ||
= note: expected type `&_` | ||
found type `std::ops::RangeToInclusive<{integer}>` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/issue-54505-no-literals.rs:71:16 | ||
| | ||
LL | take_range(::std::ops::RangeToInclusive { end: 5 }); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| expected reference, found struct `std::ops::RangeToInclusive` | ||
| help: consider borrowing here: `&::std::ops::RangeToInclusive { end: 5 }` | ||
| | ||
= note: expected type `&_` | ||
found type `std::ops::RangeToInclusive<{integer}>` | ||
|
||
error: aborting due to 12 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
Oops, something went wrong.