Rollup merge of #111133 - hkmatsumoto:handle-python-slicing, r=TaKO8Ki
Detect Python-like slicing and suggest how to fix Fix #108215
This commit is contained in:
commit
203aaf62a9
4 changed files with 53 additions and 11 deletions
|
@ -756,6 +756,11 @@ impl Token {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns `true` if the token is the integer literal.
|
||||||
|
pub fn is_integer_lit(&self) -> bool {
|
||||||
|
matches!(self.kind, Literal(Lit { kind: LitKind::Integer, .. }))
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns `true` if the token is a non-raw identifier for which `pred` holds.
|
/// Returns `true` if the token is a non-raw identifier for which `pred` holds.
|
||||||
pub fn is_non_raw_ident_where(&self, pred: impl FnOnce(Ident) -> bool) -> bool {
|
pub fn is_non_raw_ident_where(&self, pred: impl FnOnce(Ident) -> bool) -> bool {
|
||||||
match self.ident() {
|
match self.ident() {
|
||||||
|
|
|
@ -567,20 +567,37 @@ impl<'a> Parser<'a> {
|
||||||
snapshot.recover_diff_marker();
|
snapshot.recover_diff_marker();
|
||||||
}
|
}
|
||||||
if self.token == token::Colon {
|
if self.token == token::Colon {
|
||||||
// if next token is following a colon, it's likely a path
|
// if a previous and next token of the current one is
|
||||||
// and we can suggest a path separator
|
// integer literal (e.g. `1:42`), it's likely a range
|
||||||
self.bump();
|
// expression for Pythonistas and we can suggest so.
|
||||||
if self.token.span.lo() == self.prev_token.span.hi() {
|
if self.prev_token.is_integer_lit()
|
||||||
|
&& self.may_recover()
|
||||||
|
&& self.look_ahead(1, |token| token.is_integer_lit())
|
||||||
|
{
|
||||||
|
// FIXME(hkmatsumoto): Might be better to trigger
|
||||||
|
// this only when parsing an index expression.
|
||||||
err.span_suggestion_verbose(
|
err.span_suggestion_verbose(
|
||||||
self.prev_token.span,
|
self.token.span,
|
||||||
"maybe write a path separator here",
|
"you might have meant a range expression",
|
||||||
"::",
|
"..",
|
||||||
Applicability::MaybeIncorrect,
|
Applicability::MaybeIncorrect,
|
||||||
);
|
);
|
||||||
}
|
} else {
|
||||||
if self.sess.unstable_features.is_nightly_build() {
|
// if next token is following a colon, it's likely a path
|
||||||
// FIXME(Nilstrieb): Remove this again after a few months.
|
// and we can suggest a path separator
|
||||||
err.note("type ascription syntax has been removed, see issue #101728 <https://github.com/rust-lang/rust/issues/101728>");
|
self.bump();
|
||||||
|
if self.token.span.lo() == self.prev_token.span.hi() {
|
||||||
|
err.span_suggestion_verbose(
|
||||||
|
self.prev_token.span,
|
||||||
|
"maybe write a path separator here",
|
||||||
|
"::",
|
||||||
|
Applicability::MaybeIncorrect,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if self.sess.unstable_features.is_nightly_build() {
|
||||||
|
// FIXME(Nilstrieb): Remove this again after a few months.
|
||||||
|
err.note("type ascription syntax has been removed, see issue #101728 <https://github.com/rust-lang/rust/issues/101728>");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
7
tests/ui/suggestions/range-index-instead-of-colon.rs
Normal file
7
tests/ui/suggestions/range-index-instead-of-colon.rs
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
// edition:2021
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
&[1, 2, 3][1:2];
|
||||||
|
//~^ ERROR: expected one of
|
||||||
|
//~| HELP: you might have meant a range expression
|
||||||
|
}
|
13
tests/ui/suggestions/range-index-instead-of-colon.stderr
Normal file
13
tests/ui/suggestions/range-index-instead-of-colon.stderr
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
error: expected one of `.`, `?`, `]`, or an operator, found `:`
|
||||||
|
--> $DIR/range-index-instead-of-colon.rs:4:17
|
||||||
|
|
|
||||||
|
LL | &[1, 2, 3][1:2];
|
||||||
|
| ^ expected one of `.`, `?`, `]`, or an operator
|
||||||
|
|
|
||||||
|
help: you might have meant a range expression
|
||||||
|
|
|
||||||
|
LL | &[1, 2, 3][1..2];
|
||||||
|
| ~~
|
||||||
|
|
||||||
|
error: aborting due to 1 previous error
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue