-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Fix range literals borrowing suggestions #54734
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
bors
merged 6 commits into
rust-lang:master
from
pawroman:fix_range_borrowing_suggestion
Oct 10, 2018
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0626afb
First stab at fixing #54505
pawroman f7d35cc
Fix range borrowing suggestions logic
pawroman 87bf9e2
Address review comments
pawroman 611e5c4
Address review comments
pawroman 882e373
Attempt to fix #54505 tests for wasm
pawroman 1f7dafb
Fix test for windows os
pawroman 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -309,11 +309,20 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { | |
}; | ||
if self.can_coerce(ref_ty, expected) { | ||
if let Ok(src) = cm.span_to_snippet(sp) { | ||
let sugg_expr = match expr.node { // parenthesize if needed (Issue #46756) | ||
let needs_parens = match expr.node { | ||
// parenthesize if needed (Issue #46756) | ||
hir::ExprKind::Cast(_, _) | | ||
hir::ExprKind::Binary(_, _, _) => format!("({})", src), | ||
_ => src, | ||
hir::ExprKind::Binary(_, _, _) => true, | ||
// parenthesize borrows of range literals (Issue #54505) | ||
_ if self.is_range_literal(expr) => true, | ||
_ => false, | ||
}; | ||
let sugg_expr = if needs_parens { | ||
format!("({})", src) | ||
} else { | ||
src | ||
}; | ||
|
||
if let Some(sugg) = self.can_use_as_ref(expr) { | ||
return Some(sugg); | ||
} | ||
|
@@ -374,6 +383,66 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { | |
None | ||
} | ||
|
||
/// This function checks if the specified expression is a built-in range literal. | ||
/// (See: `LoweringContext::lower_expr()` in `src/librustc/hir/lowering.rs`). | ||
fn is_range_literal(&self, expr: &hir::Expr) -> bool { | ||
use hir::{Path, QPath, ExprKind, TyKind}; | ||
|
||
// We support `::std::ops::Range` and `::core::ops::Range` prefixes | ||
let is_range_path = |path: &Path| { | ||
let mut segs = path.segments.iter() | ||
.map(|seg| seg.ident.as_str()); | ||
|
||
if let (Some(root), Some(std_core), Some(ops), Some(range), None) = | ||
(segs.next(), segs.next(), segs.next(), segs.next(), segs.next()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
{ | ||
// "{{root}}" is the equivalent of `::` prefix in Path | ||
root == "{{root}}" && (std_core == "std" || std_core == "core") | ||
&& ops == "ops" && range.starts_with("Range") | ||
} else { | ||
false | ||
} | ||
}; | ||
|
||
let span_is_range_literal = |span: &Span| { | ||
// Check whether a span corresponding to a range expression | ||
// is a range literal, rather than an explicit struct or `new()` call. | ||
let source_map = self.tcx.sess.source_map(); | ||
let end_point = source_map.end_point(*span); | ||
|
||
if let Ok(end_string) = source_map.span_to_snippet(end_point) { | ||
!(end_string.ends_with("}") || end_string.ends_with(")")) | ||
} else { | ||
false | ||
} | ||
}; | ||
|
||
match expr.node { | ||
// All built-in range literals but `..=` and `..` desugar to Structs | ||
ExprKind::Struct(QPath::Resolved(None, ref path), _, _) | | ||
// `..` desugars to its struct path | ||
ExprKind::Path(QPath::Resolved(None, ref path)) => { | ||
pawroman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return is_range_path(&path) && span_is_range_literal(&expr.span); | ||
} | ||
|
||
// `..=` desugars into `::std::ops::RangeInclusive::new(...)` | ||
ExprKind::Call(ref func, _) => { | ||
if let ExprKind::Path(QPath::TypeRelative(ref ty, ref segment)) = func.node { | ||
if let TyKind::Path(QPath::Resolved(None, ref path)) = ty.node { | ||
let call_to_new = segment.ident.as_str() == "new"; | ||
|
||
return is_range_path(&path) && span_is_range_literal(&expr.span) | ||
&& call_to_new; | ||
} | ||
} | ||
} | ||
|
||
_ => {} | ||
} | ||
|
||
false | ||
varkor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
pub fn check_for_cast(&self, | ||
err: &mut DiagnosticBuilder<'tcx>, | ||
expr: &hir::Expr, | ||
|
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,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 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,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 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,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.
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.
I might be alone in this (cc @estebank), but I think for a short path like this, a more explicit approach (like below) is more readable. (I specifically don't like splitting on "|" because it feels a little too ad hoc.)
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.
That's a great suggestion! I was looking for something concise like this syntax-wise, it didn't occur to me that I could use
.next()
and rely on it returningNone
when there's no more elements.