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

Arrays as struct members causes panic #15804

Closed
nathansizemore opened this issue Oct 26, 2023 · 8 comments · Fixed by #16112
Closed

Arrays as struct members causes panic #15804

nathansizemore opened this issue Oct 26, 2023 · 8 comments · Fixed by #16112
Labels
C-bug Category: bug I-panic

Comments

@nathansizemore
Copy link

Example file

const MAX_ONE_BYTE_PICTURE_ID: i16 = 0x7F; // 7 bits
const MAX_TWO_BYTE_PICTURE_ID: i16 = 0x7FFF; // 15 bits
const NO_SPATIAL_IDX: u8 = 0xFF;
const NO_GOF_IDX: u8 = 0xFF;
const NUM_VP9_BUFFERS: u8 = 8;
const MAX_VP9_REF_PICS: usize = 3;
const MAX_VP9_FRAMES_IN_GOF: usize = 0xFF; // 8 bits
const MAX_VP9_NUMBER_OF_SPATIAL_LAYERS: usize = 8;

const MIN_VP9_SPATIAL_LAYER_LONG_SIDE_LENGTH: usize = 240;
const MIN_VP9_SPATIAL_LAYER_SHORT_SIDE_LENGTH: usize = 135;

pub struct Gof {
    pub num_frames_in_gof: usize,
    pub temporal_idx: [u8; MAX_VP9_FRAMES_IN_GOF],
    pub temporal_up_switch: [bool; MAX_VP9_FRAMES_IN_GOF],
    pub num_ref_pics: [u8; MAX_VP9_FRAMES_IN_GOF],
    pub pid_diff: [[u8; MAX_VP9_REF_PICS]; MAX_VP9_FRAMES_IN_GOF],
    pub pid_start: u16,
}

ra version: 2023-10-23
rustc: rustc 1.73.0 (cc66ad468 2023-10-03)
Pop_OS, Emacs

Hovering a cursor anywhere over the array literals breaks the LSP.:

Saving file /home/nate/dev/icarus/lib/webrtc/src/video/codecs/vp9/gof.rs...
LSP :: No formatting changes provided
Wrote /home/nate/dev/icarus/lib/webrtc/src/video/codecs/vp9/gof.rs
LSP :: Error from the Language Server: request handler panicked: Can't find CONST_ARG@158..159 in AstIdMap:
[SyntaxNodePtr { kind: USE, range: 153..192 }, SyntaxNodePtr { kind: USE, range: 193..218 }, ...truncated, but should get the point across
lsp-request: request handler panicked: Can’t find CONST_ARG@158..159 in AstIdMap:
@nathansizemore nathansizemore added the C-bug Category: bug label Oct 26, 2023
@AndreySmirnov81
Copy link

AndreySmirnov81 commented Oct 26, 2023

The following simple example is sufficient to reproduce the problem:

struct T {
    arr: [u8; 16],
}

And this one too:

type Arr = [char; 16];

struct T {
    arr: Arr,
}

rust-analyzer version: 0.3.1705-standalone (1087295 2023-10-22)
rustc 1.72.0 (5680fa18f 2023-08-23)

@HKalbasi
Copy link
Member

I can't reproduce this. Is it reproducible in an empty project on vscode?

@lnicola
Copy link
Member

lnicola commented Oct 26, 2023

I can reproduce it in an empty project. There is a CONST_ARG in the AST, but @25..27, while the panic messages is about @156..157. No macros involved.

@nathansizemore
Copy link
Author

I can't reproduce this. Is it reproducible in an empty project on vscode?

Yes, can also do it in VS Code with a blank scratch proj:

image

@MarSik
Copy link

MarSik commented Dec 1, 2023

I believe I hit the same issue: #15968

@evertedsphere
Copy link
Contributor

Same here in Emacs + rust-analyzer.

@Veykril
Copy link
Member

Veykril commented Dec 12, 2023

I can't reproduce this on latest

@roife
Copy link
Contributor

roife commented Dec 12, 2023

Hey, I have locally identified and resolved the bug. The issue stems from an error transformation in the code action generate_delegate_trait with PathTransform, similar to #14534, but for some unknown reasons, it cannot be reliably reproduced.

Upon investigating generate_delegate_trait, I found numerous implementation issues with this code action, such as lack of support for generic structs and incorrect handling of the Self type. To address these issues, I have rewritten the entire code action and am currently addressing the final few issues. I may submit a pull request within the next day or two.

@bors bors closed this as completed in 34df296 Jan 2, 2024
bors added a commit that referenced this issue Jan 9, 2024
Resolve panic in `generate_delegate_methods`

Fixes #16276

This PR addresses two issues:

1. When using `PathTransform`, it searches for the node corresponding to the `path` in the `source_scope` during `make::fn_`. Therefore, we need to perform the transform before `make::fn_` (similar to the problem in issue #15804). Otherwise, even though the tokens are the same, their offsets (i.e., `span`) differ, resulting in the error "Can't find CONST_ARG@xxx."

2. As mentioned in the first point, `PathTransform` searches for the node corresponding to the `path` in the `source_scope`. Thus, when transforming paths, we should update nodes from right to left (i.e., use **reverse of preorder** (right -> left -> root) instead of **postorder** (left -> right -> root)). Reasons are as follows:

    In the red-green tree (rowan), we do not store absolute ranges but instead store the length of each node and dynamically calculate offsets (spans). Therefore, when modifying the left-side node (such as nodes are inserted or deleted), it causes all right-side nodes' spans to change. This, in turn, leads to PathTransform being unable to find nodes with the same paths (due to different spans), resulting in errors.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: bug I-panic
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants