Skip to content

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

Closed
@kdashg

Description

@kdashg

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions