Skip to content
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

Gracefully handle python-esque sequence slicing #108215

Closed
hkmatsumoto opened this issue Feb 18, 2023 · 3 comments · Fixed by #111133
Closed

Gracefully handle python-esque sequence slicing #108215

hkmatsumoto opened this issue Feb 18, 2023 · 3 comments · Fixed by #111133
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@hkmatsumoto
Copy link
Member

hkmatsumoto commented Feb 18, 2023

Code

fn main() {
    let a = &[1, 2, 3][1:2];
}

Current output

Compiling playground v0.0.1 (/playground)
error: expected type, found `2`
 --> src/main.rs:2:26
  |
2 |     let a = &[1, 2, 3][1:2];
  |                         -^ expected type
  |                         |
  |                         tried to parse a type due to this type ascription
  |
  = note: `#![feature(type_ascription)]` lets you annotate an expression with a type: `<expr>: <type>`
  = note: see issue #23416 <https://github.com/rust-lang/rust/issues/23416> for more information

error: could not compile `playground` due to previous error

Desired output

help: did you mean [1..2]?

Rationale and extra context

Having written Python for several hours, I made this mistake and couldn't realize what was wrong for a while.

Other cases

No response

Anything else?

@rustbot claim

@hkmatsumoto hkmatsumoto added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 18, 2023
@Noratrieb
Copy link
Member

I'm not sure whether it's worth to fix this right now. #101728 will significantly change this code, basically requiring a rewrite of this diagnostic.

@hkmatsumoto
Copy link
Member Author

Agreed, it's reasonable to wait for type ascription removal.
Thanks for the info!

@chenyukang
Copy link
Member

chenyukang commented May 3, 2023

@hkmatsumoto
Now we should fix this error message:

error: expected one of `.`, `?`, `]`, or an operator, found `:`
 --> src/main.rs:2:25
  |
2 |     let a = &[1, 2, 3][1:2];
  |                         ^ expected one of `.`, `?`, `]`, or an operator
  |
  = note: type ascription syntax has been removed, [see issue #101728 <https://github.com/rust-lang/rust/issues/101728>](https://github.com/rust-lang/rust/issues/101728)
help: maybe write a path separator here
  |
2 |     let a = &[1, 2, 3][1::2];

help: maybe write a path separator here is not OK, we'd better suggest to use [1..2].
I think you need to add proper logic for this part of code:

"maybe write a path separator here",

hkmatsumoto added a commit to hkmatsumoto/rust that referenced this issue May 16, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Nov 27, 2023
… r=TaKO8Ki

Detect Python-like slicing and suggest how to fix

Fix rust-lang#108215
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Nov 27, 2023
… r=TaKO8Ki

Detect Python-like slicing and suggest how to fix

Fix rust-lang#108215
compiler-errors added a commit to compiler-errors/rust that referenced this issue Nov 28, 2023
… r=TaKO8Ki

Detect Python-like slicing and suggest how to fix

Fix rust-lang#108215
@bors bors closed this as completed in e65c060 Nov 28, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Nov 28, 2023
Rollup merge of rust-lang#111133 - hkmatsumoto:handle-python-slicing, r=TaKO8Ki

Detect Python-like slicing and suggest how to fix

Fix rust-lang#108215
kjetilkjeka pushed a commit to kjetilkjeka/rust that referenced this issue Nov 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants