-
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
Fix range literals borrowing suggestions #54734
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @varkor (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
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.
This is looking really good, thanks! I've left a few comments, but they're mostly minor things. Nice work!
src/librustc_typeck/check/demand.rs
Outdated
}; | ||
let sugg_expr = if needs_parens { format!("({})", src) } else { src }; |
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.
Generally prefer to split if
-else
across separate lines.
src/librustc_typeck/check/demand.rs
Outdated
} else { | ||
false | ||
} | ||
|
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.
Nit: delete blank line here.
src/librustc_typeck/check/demand.rs
Outdated
@@ -374,6 +379,77 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { | |||
None | |||
} | |||
|
|||
// This function checks if the specified expression is a built-in range literal |
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.
Use ///
for documentation comments for functions. (Nit: And full stops for the ends of sentences 😄)
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.
Right. Does nightly rustdoc support code references yet?
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.
As in references to source files? Not that I'm aware of.
src/librustc_typeck/check/demand.rs
Outdated
@@ -374,6 +379,77 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { | |||
None | |||
} | |||
|
|||
// This function checks if the specified expression is a built-in range literal | |||
// (See: librustc/hir/lowering.rs::LoweringContext::lower_expr() ) |
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.
Nit: I'd surround the path with backticks, like any piece of code.
// (via split on "|") | ||
let ops_path = ["{{root}}", "std|core", "ops"]; | ||
|
||
let is_range_path = |path: &Path| { |
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.)
let is_range_path = |path: &Path| {
let segs = path.segments.iter().map(|seg| seg.ident.as_str());
if let (Some("{{root}}"), std_core, "ops", range, None) = (segs.next(), segs.next(), segs.next(), segs.next(), segs.next()) {
(std_core == "std" || std_core == "ops") && range.starts_with("Range")
} else {
false
}
};
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 returning None
when there's no more elements.
.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 comment
The reason will be displayed to describe this comment to others. Learn more.
Ident.as_str()
returns a LocalInternedString
which can be compared with &str
, but I found it too verbose to use in Some()
.
src/librustc_typeck/check/demand.rs
Outdated
@@ -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: ``librustc::hir::lowering::LoweringContext::lower_expr()``). |
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.
Sorry, I meant single rather than double backticks; I should have clarified. On second thoughts, though, it might be more convenient to include the path:
/// (See `LoweringContext::lower_expr()` in `src/librustc/hir/lowering.rs`.)
as then someone can jump straight to the path. (Like in the following.)
https://github.com/rust-lang/rust/blob/f89622a7548f69c76e5c9857e34be568ba322d02/src/librustc_privacy/lib.rs#L1388
src/librustc_typeck/check/demand.rs
Outdated
} | ||
}; | ||
|
||
let is_range_literal = |span: &Span| { |
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.
As you pointed out, it would be clearer to give this a different name than the function it's in. Maybe span_is_range_literal
or something like that?
src/librustc_typeck/check/demand.rs
Outdated
|
||
let is_range_literal = |span: &Span| { | ||
// Tell if expression span snippet doesn't look like an explicit | ||
// Range struct or `new()` call. This is to allow inferring |
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.
"Inferring" has a very specific meaning, so I'd tend to avoid it in favour of something like:
// Check whether a span corresponding to a range is a range literal,
// rather than an explicit struct or `new()` call.
Some very minor comments to follow up, just to make things a little more readable. After this, it all looks good to me! |
Addressed them now -- thanks for review and please let me know if anything else. |
📌 Commit 611e5c4 has been approved by |
…tion, r=varkor Fix range literals borrowing suggestions Fixes rust-lang#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
@bors r- This is failing on wasm.
|
Uh oh! Will look into this as soon as I have some spare time. |
I have attempted a fix -- unfortunately having trouble getting the toolchain to work with wasm target, so can't test that at the moment. Any help with this would be appreciated. Alternatively, we can lean on the automated CI process to test with wasm target. |
@bors r+ |
📌 Commit 882e373 has been approved by |
⌛ Testing commit 882e373 with merge 5a7b2dec4ec3ea0c84b272799f81d090dc773263... |
💔 Test failed - status-appveyor |
Fixed that test for windows, tested successfully with |
@bors r+ |
📌 Commit 1f7dafb has been approved by |
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
💔 Test failed - status-appveyor |
Hmm, the failure appears unrelated at first glance. I'm seeing |
The error is confusing. It's referencing |
Let's just retry. @bors retry |
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
☀️ Test successful - status-appveyor, status-travis |
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