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

rustc save-analysis records variant struct-literal spans as referencing enum not variant #96985

Closed
kdashg opened this issue May 12, 2022 · 0 comments · Fixed by #96986
Closed
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

@kdashg
Copy link
Contributor

kdashg commented May 12, 2022

This came out of investigation into a bug in Mozilla's SearchFox tool output:
https://bugzilla.mozilla.org/show_bug.cgi?id=1623824
(I went into detail about the investigation there, as well as a proof-of-concept patch)

It looks like in a struct-literal construction such as MyEnum::MyVariant { ... }, the "MyVariant" span ends up recorded as a reference to MyEnum, not MyVariant.

Given the following code: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=e34b9b11c38581770e7cd82f1372b264

enum NativeSurfaceOperationDetails {
    CreateExternalSurface {
        id: usize,
        is_opaque: bool,
    },
}

// -

fn construct() -> NativeSurfaceOperationDetails {
    NativeSurfaceOperationDetails::CreateExternalSurface {
        id: 5,
        is_opaque: true,
    }
}

fn print(e: NativeSurfaceOperationDetails) {
    match e {
        NativeSurfaceOperationDetails::CreateExternalSurface { id, is_opaque } => {
            println!("id: {}, is_opaque: {}", id, is_opaque);
        }
    }
}

// -

fn main() {
    let e = construct();
    print(e);
}

The current output is:

    {
      "kind": "Type",
      "span": {
        "file_name": "enum-variant-ctor.rs",
        "byte_start": 177,
        "byte_end": 206,
        "line_start": 11,
        "line_end": 11,
        "column_start": 4,
        "column_end": 33
      },
      "ref_id": {
        "krate": 0,
        "index": 3
      }
    },
    {
      "kind": "Type",
      "span": {
        "file_name": "enum-variant-ctor.rs",
        "byte_start": 208,
        "byte_end": 229,
        "line_start": 11,
        "line_end": 11,
        "column_start": 35,
        "column_end": 56
      },
      "ref_id": {
        "krate": 0,
        "index": 3
      }
    },

Ideally the output should look like:

    {
      "kind": "Type",
      "span": {
        "file_name": "enum-variant-ctor.rs",
        "byte_start": 177,
        "byte_end": 206,
        "line_start": 11,
        "line_end": 11,
        "column_start": 4,
        "column_end": 33
      },
      "ref_id": {
        "krate": 0,
        "index": 3
      }
    },
    {
      "kind": "Type",
      "span": {
        "file_name": "enum-variant-ctor.rs",
        "byte_start": 208,
        "byte_end": 229,
        "line_start": 11,
        "line_end": 11,
        "column_start": 35,
        "column_end": 56
      },
      "ref_id": {
        "krate": 0,
        "index": 4   // <--
      }
    },

When searching for references to a particular variant, save-analysis should give us the information to enumerate them, including when used as struct literals.
The downstream effect of this in our SearchFox tool is that we get more hits for the string "CreateExternalSurface" than we do for the enum variant symbol NativeSurfaceOperationDetails::CreateExternalSurface:
https://searchfox.org/mozilla-central/search?q=CreateExternalSurface&path=*.rs
https://searchfox.org/mozilla-central/search?q=symbol:webrender%3A%3Acomposite%3A%3ANativeSurfaceOperationDetails%3A%3ACreateExternalSurface&redirect=false

@kdashg kdashg 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 May 12, 2022
JohnTitor added a commit to JohnTitor/rust that referenced this issue May 14, 2022
[save-analysis] Reference the variant not enum at struct-literal cons…

…truction.

Closes rust-lang#96985
@bors bors closed this as completed in 5fde765 May 14, 2022
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.

1 participant