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

Panic when typing the names on a tuple: index out of bounds: the len is 0 but the index is 0 #16746

Closed
ThomasMiz opened this issue Mar 4, 2024 · 13 comments
Assignees
Labels
A-pattern pattern handling related things C-bug Category: bug

Comments

@ThomasMiz
Copy link

When deleting characters from the name of one element in a tuple, if you delete the last one a panic will occur.

To reproduce, create a new empty project and add this to your main.rs:

fn main() {
    let (a, b) = (0, 0);
}

Then place your cursor right after the 'b' character and press backspace, making your code look like this:

fn main() {
    let (a, ) = (0, 0);
}

This panic message is printed to stderr.

rust-analyzer version: 0.4.1867-standalone (037924c 2024-03-03)
rustc version: rustc 1.78.0-nightly (516b6162a 2024-03-03)

(sidenote: I know this is the third issue about panics from me in the past 5 days, I apologize if I'm getting annoying)

@ThomasMiz ThomasMiz added the C-bug Category: bug label Mar 4, 2024
@Young-Flash
Copy link
Member

seems relate to ra-ap-rustc_pattern_analysis-0.40.0, cc @Nadrieril

thread 'Worker' panicked at /github/home/.cargo/registry/src/index.crates.io-6f17d22bba15001f/ra-ap-rustc_pattern_analysis-0.40.0/src/usefulness.rs:954:18:
index out of bounds: the len is 0 but the index is 0
stack backtrace:
   0: rust_begin_unwind
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:645:5
   1: core::panicking::panic_fmt
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/panicking.rs:72:14
   2: core::panicking::panic_bounds_check
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/panicking.rs:208:5
   3: ra_ap_rustc_pattern_analysis::usefulness::PlaceInfo<Cx>::split_column_ctors
   4: ra_ap_rustc_pattern_analysis::usefulness::compute_exhaustiveness_and_usefulness
   5: ra_ap_rustc_pattern_analysis::usefulness::compute_exhaustiveness_and_usefulness
   6: ra_ap_rustc_pattern_analysis::usefulness::compute_exhaustiveness_and_usefulness
   7: ra_ap_rustc_pattern_analysis::usefulness::compute_match_usefulness
   8: hir_ty::diagnostics::expr::BodyValidationDiagnostic::collect

@Nadrieril
Copy link
Member

😭 I'll have a look tomorrow.

Fwiw: it would be really nice if the typechecker could be trusted to skip match checking when there's a type error, cause that's always the source of the pattern_analysis panics

@Veykril Veykril added the A-pattern pattern handling related things label Mar 4, 2024
jhpratt added a commit to jhpratt/rust that referenced this issue Mar 5, 2024
… r=compiler-errors

pattern analysis: abort on arity mismatch

This is one more PR replacing panics by `Err()` aborts. I recently audited all the `unwrap()` calls, but I had forgotten about array accesses. (Again [discovered by rust-analyzer](rust-lang/rust-analyzer#16746)).

r? `@compiler-errors`
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 5, 2024
… r=compiler-errors

pattern analysis: abort on arity mismatch

This is one more PR replacing panics by `Err()` aborts. I recently audited all the `unwrap()` calls, but I had forgotten about array accesses. (Again [discovered by rust-analyzer](rust-lang/rust-analyzer#16746)).

r? ``@compiler-errors``
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 5, 2024
… r=compiler-errors

pattern analysis: abort on arity mismatch

This is one more PR replacing panics by `Err()` aborts. I recently audited all the `unwrap()` calls, but I had forgotten about array accesses. (Again [discovered by rust-analyzer](rust-lang/rust-analyzer#16746)).

r? ```@compiler-errors```
@flodiebold
Copy link
Member

Fwiw: it would be really nice if the typechecker could be trusted to skip match checking when there's a type error, cause that's always the source of the pattern_analysis panics

We want as much as possible to work if there are errors in unrelated parts of the function though 😅 Of course if the actual match has type mismatches there's not much point in trying to do further checking.

@Veykril
Copy link
Member

Veykril commented Mar 5, 2024

It should probably suffice to skip it if the scrutinee contains an {error}?

rust-timer added a commit to rust-lang-ci/rust that referenced this issue Mar 5, 2024
Rollup merge of rust-lang#121987 - Nadrieril:abort-on-arity-mismatch, r=compiler-errors

pattern analysis: abort on arity mismatch

This is one more PR replacing panics by `Err()` aborts. I recently audited all the `unwrap()` calls, but I had forgotten about array accesses. (Again [discovered by rust-analyzer](rust-lang/rust-analyzer#16746)).

r? ```@compiler-errors```
@Nadrieril
Copy link
Member

I was assuming both of these things were already what we're doing, but I haven't actually checked

@Veykril
Copy link
Member

Veykril commented Mar 5, 2024

Only for error types, not for types that contain errors

if scrut_ty.is_unknown() {

And exhaustiveness checking for let's doesn't do any checks

@Nadrieril
Copy link
Member

Oh, that explains things :D

@target-san
Copy link

Sorry for poking my nose into discussion, though I'm getting quite similar error on a medium-sized project. Is there some way to determine what piece of source causes error?

In case anyone's interested, I'm getting following error trace:

thread 'Worker' panicked at /github/home/.cargo/registry/src/index.crates.io-6f17d22bba15001f/ra-ap-rustc_pattern_analysis-0.40.0/src/usefulness.rs:954:18:
index out of bounds: the len is 0 but the index is 0
stack backtrace:
   0: rust_begin_unwind
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:645:5
   1: core::panicking::panic_fmt
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/panicking.rs:72:14
   2: core::panicking::panic_bounds_check
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/panicking.rs:208:5
   3: ra_ap_rustc_pattern_analysis::usefulness::PlaceInfo<Cx>::split_column_ctors
   4: ra_ap_rustc_pattern_analysis::usefulness::compute_exhaustiveness_and_usefulness
   5: ra_ap_rustc_pattern_analysis::usefulness::compute_exhaustiveness_and_usefulness
   6: ra_ap_rustc_pattern_analysis::usefulness::compute_match_usefulness
   7: hir_ty::diagnostics::expr::BodyValidationDiagnostic::collect
   8: hir::DefWithBody::diagnostics
   9: hir::Module::diagnostics
  10: hir::DefWithBody::diagnostics
  11: hir::ModuleDef::diagnostics
  12: hir::Module::diagnostics
  13: ide_diagnostics::diagnostics
  14: salsa::Cancelled::catch
  15: core::ops::function::impls::<impl core::ops::function::FnMut<A> for &mut F>::call_mut
  16: <alloc::vec::Vec<T> as alloc::vec::spec_from_iter::SpecFromIter<T,I>>::from_iter
  17: rust_analyzer::diagnostics::fetch_native_diagnostics
  18: core::ops::function::FnOnce::call_once{{vtable.shim}}
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
2024-03-06T12:20:51.167479Z ERROR hir_ty::diagnostics::match_check::pat_analysis: Unexpected type for `Struct` constructor: AssocTypeId(4606)<[?0 := AdtId(StructId(StructId(7548)))<[]>]>
2024-03-06T12:20:51.168021Z ERROR hir_ty::diagnostics::match_check::pat_analysis: Unexpected type for `Struct` constructor: AssocTypeId(4606)<[?0 := AdtId(EnumId(EnumId(1412)))<[]>]>
2024-03-06T12:20:51.168142Z ERROR hir_ty::diagnostics::match_check::pat_analysis: Unexpected type for `Struct` constructor: AssocTypeId(4606)<[?0 := AdtId(StructId(StructId(7549)))<[]>]>
2024-03-06T12:20:51.168278Z ERROR hir_ty::diagnostics::match_check::pat_analysis: Unexpected type for `Struct` constructor: AssocTypeId(4606)<[?0 := AdtId(StructId(StructId(7550)))<[]>]>
2024-03-06T12:20:51.168386Z ERROR hir_ty::diagnostics::match_check::pat_analysis: Unexpected type for `Struct` constructor: AssocTypeId(4606)<[?0 := AdtId(StructId(StructId(7551)))<[]>]>
2024-03-06T12:20:51.168529Z ERROR hir_ty::diagnostics::match_check::pat_analysis: Unexpected type for `Struct` constructor: AssocTypeId(4606)<[?0 := AdtId(StructId(StructId(7552)))<[]>]>
2024-03-06T12:20:51.168605Z ERROR hir_ty::diagnostics::match_check::pat_analysis: Unexpected type for `Struct` constructor: AssocTypeId(4606)<[?0 := AdtId(StructId(StructId(7553)))<[]>]>
2024-03-06T12:20:51.168695Z ERROR hir_ty::diagnostics::match_check::pat_analysis: Unexpected type for `Struct` constructor: AssocTypeId(4606)<[?0 := AdtId(StructId(StructId(7554)))<[]>]>
2024-03-06T12:20:51.168769Z ERROR hir_ty::diagnostics::match_check::pat_analysis: Unexpected type for `Struct` constructor: AssocTypeId(4606)<[?0 := AdtId(StructId(StructId(7555)))<[]>]>
2024-03-06T12:20:51.168847Z ERROR hir_ty::diagnostics::match_check::pat_analysis: Unexpected type for `Struct` constructor: AssocTypeId(4606)<[?0 := AdtId(StructId(StructId(7556)))<[]>]>
2024-03-06T12:20:51.173609Z ERROR hir_ty::diagnostics::match_check::pat_analysis: Unexpected type for `Struct` constructor: AssocTypeId(4606)<[?0 := AdtId(EnumId(EnumId(1413)))<[]>]>
Panic context:
> fetch_native_diagnostics

@Nadrieril
Copy link
Member

Nadrieril commented Mar 6, 2024

There's no easy way to know which code triggered it unfortunately I think.

This has now been fixed on the rustc side, the fix will come to rust-analyzer next update i.e. within a week.

@Veykril if you know how to enforce the type check, please do, I haven't looked at the type-checking side of r-a at all

@Nadrieril Nadrieril self-assigned this Mar 6, 2024
@Veykril
Copy link
Member

Veykril commented Mar 6, 2024

should be a simple

-        if scrut_ty.is_unknown() {
+        if scrut_ty.contains_unknown() {

for

if scrut_ty.is_unknown() {

And adding the same check for the let checking

@lnicola
Copy link
Member

lnicola commented Mar 7, 2024

Fixed in #16778.

@Nadrieril
Copy link
Member

I forgot to add a regression test. Do you know where I can add on? I haven't figured out r-a's testing situation yet

@Veykril
Copy link
Member

Veykril commented Mar 7, 2024

https://github.com/rust-lang/rust-analyzer/blob/master/crates/ide-diagnostics/src/handlers/missing_match_arms.rs is where we test match checking (let exhaustiveness checking has its own diagnostic file)

bors added a commit that referenced this issue Mar 7, 2024
…r=Veykril

minor: Skip match diagnostics for partially unknown types

CC #16746 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-pattern pattern handling related things C-bug Category: bug
Projects
None yet
Development

No branches or pull requests

7 participants