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
This commit is contained in:
bors 2018-10-09 22:36:14 +00:00
commit eae47a4048
9 changed files with 664 additions and 3 deletions

View file

@ -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 }
}

View file

@ -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 }
}

View file

@ -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`.

View file

@ -0,0 +1,57 @@
// error-pattern: `#[panic_handler]` function required, but not found
// Regression test for #54505 - range borrowing suggestion had
// incorrect syntax (missing parentheses).
// This test doesn't use std
// (so all Ranges resolve to core::ops::Range...)
#![no_std]
#![feature(lang_items)]
use core::ops::RangeBounds;
#[cfg(not(target_arch = "wasm32"))]
#[lang = "eh_personality"]
extern fn eh_personality() {}
#[cfg(target_os = "windows")]
#[lang = "eh_unwind_resume"]
extern fn eh_unwind_resume() {}
// take a reference to any built-in range
fn take_range(_r: &impl RangeBounds<i8>) {}
fn main() {
take_range(0..1);
//~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here
//~| SUGGESTION &(0..1)
take_range(1..);
//~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here
//~| SUGGESTION &(1..)
take_range(..);
//~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here
//~| SUGGESTION &(..)
take_range(0..=1);
//~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here
//~| SUGGESTION &(0..=1)
take_range(..5);
//~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here
//~| SUGGESTION &(..5)
take_range(..=42);
//~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here
//~| SUGGESTION &(..=42)
}

View file

@ -0,0 +1,77 @@
error: `#[panic_handler]` function required, but not found
error[E0308]: mismatched types
--> $DIR/issue-54505-no-std.rs:28:16
|
LL | take_range(0..1);
| ^^^^
| |
| expected reference, found struct `core::ops::Range`
| help: consider borrowing here: `&(0..1)`
|
= note: expected type `&_`
found type `core::ops::Range<{integer}>`
error[E0308]: mismatched types
--> $DIR/issue-54505-no-std.rs:33:16
|
LL | take_range(1..);
| ^^^
| |
| expected reference, found struct `core::ops::RangeFrom`
| help: consider borrowing here: `&(1..)`
|
= note: expected type `&_`
found type `core::ops::RangeFrom<{integer}>`
error[E0308]: mismatched types
--> $DIR/issue-54505-no-std.rs:38:16
|
LL | take_range(..);
| ^^
| |
| expected reference, found struct `core::ops::RangeFull`
| help: consider borrowing here: `&(..)`
|
= note: expected type `&_`
found type `core::ops::RangeFull`
error[E0308]: mismatched types
--> $DIR/issue-54505-no-std.rs:43:16
|
LL | take_range(0..=1);
| ^^^^^
| |
| expected reference, found struct `core::ops::RangeInclusive`
| help: consider borrowing here: `&(0..=1)`
|
= note: expected type `&_`
found type `core::ops::RangeInclusive<{integer}>`
error[E0308]: mismatched types
--> $DIR/issue-54505-no-std.rs:48:16
|
LL | take_range(..5);
| ^^^
| |
| expected reference, found struct `core::ops::RangeTo`
| help: consider borrowing here: `&(..5)`
|
= note: expected type `&_`
found type `core::ops::RangeTo<{integer}>`
error[E0308]: mismatched types
--> $DIR/issue-54505-no-std.rs:53:16
|
LL | take_range(..=42);
| ^^^^^
| |
| expected reference, found struct `core::ops::RangeToInclusive`
| help: consider borrowing here: `&(..=42)`
|
= note: expected type `&_`
found type `core::ops::RangeToInclusive<{integer}>`
error: aborting due to 7 previous errors
For more information about this error, try `rustc --explain E0308`.

View file

@ -0,0 +1,43 @@
// run-rustfix
// Regression test for #54505 - range borrowing suggestion had
// incorrect syntax (missing parentheses).
use std::ops::RangeBounds;
// take a reference to any built-in range
fn take_range(_r: &impl RangeBounds<i8>) {}
fn main() {
take_range(&(0..1));
//~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here
//~| SUGGESTION &(0..1)
take_range(&(1..));
//~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here
//~| SUGGESTION &(1..)
take_range(&(..));
//~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here
//~| SUGGESTION &(..)
take_range(&(0..=1));
//~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here
//~| SUGGESTION &(0..=1)
take_range(&(..5));
//~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here
//~| SUGGESTION &(..5)
take_range(&(..=42));
//~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here
//~| SUGGESTION &(..=42)
}

View file

@ -0,0 +1,43 @@
// run-rustfix
// Regression test for #54505 - range borrowing suggestion had
// incorrect syntax (missing parentheses).
use std::ops::RangeBounds;
// take a reference to any built-in range
fn take_range(_r: &impl RangeBounds<i8>) {}
fn main() {
take_range(0..1);
//~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here
//~| SUGGESTION &(0..1)
take_range(1..);
//~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here
//~| SUGGESTION &(1..)
take_range(..);
//~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here
//~| SUGGESTION &(..)
take_range(0..=1);
//~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here
//~| SUGGESTION &(0..=1)
take_range(..5);
//~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here
//~| SUGGESTION &(..5)
take_range(..=42);
//~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here
//~| SUGGESTION &(..=42)
}

View file

@ -0,0 +1,75 @@
error[E0308]: mismatched types
--> $DIR/issue-54505.rs:14:16
|
LL | take_range(0..1);
| ^^^^
| |
| expected reference, found struct `std::ops::Range`
| help: consider borrowing here: `&(0..1)`
|
= note: expected type `&_`
found type `std::ops::Range<{integer}>`
error[E0308]: mismatched types
--> $DIR/issue-54505.rs:19:16
|
LL | take_range(1..);
| ^^^
| |
| expected reference, found struct `std::ops::RangeFrom`
| help: consider borrowing here: `&(1..)`
|
= note: expected type `&_`
found type `std::ops::RangeFrom<{integer}>`
error[E0308]: mismatched types
--> $DIR/issue-54505.rs:24:16
|
LL | take_range(..);
| ^^
| |
| expected reference, found struct `std::ops::RangeFull`
| help: consider borrowing here: `&(..)`
|
= note: expected type `&_`
found type `std::ops::RangeFull`
error[E0308]: mismatched types
--> $DIR/issue-54505.rs:29:16
|
LL | take_range(0..=1);
| ^^^^^
| |
| expected reference, found struct `std::ops::RangeInclusive`
| help: consider borrowing here: `&(0..=1)`
|
= note: expected type `&_`
found type `std::ops::RangeInclusive<{integer}>`
error[E0308]: mismatched types
--> $DIR/issue-54505.rs:34:16
|
LL | take_range(..5);
| ^^^
| |
| expected reference, found struct `std::ops::RangeTo`
| help: consider borrowing here: `&(..5)`
|
= note: expected type `&_`
found type `std::ops::RangeTo<{integer}>`
error[E0308]: mismatched types
--> $DIR/issue-54505.rs:39:16
|
LL | take_range(..=42);
| ^^^^^
| |
| expected reference, found struct `std::ops::RangeToInclusive`
| help: consider borrowing here: `&(..=42)`
|
= note: expected type `&_`
found type `std::ops::RangeToInclusive<{integer}>`
error: aborting due to 6 previous errors
For more information about this error, try `rustc --explain E0308`.