Skip to content
This repository has been archived by the owner on Nov 24, 2023. It is now read-only.

Error applying replacements from module files #92

Closed
killercup opened this issue May 6, 2018 · 3 comments
Closed

Error applying replacements from module files #92

killercup opened this issue May 6, 2018 · 3 comments
Labels

Comments

@killercup
Copy link
Member

It seems I have reintroduced a bug with regard to byte indices in module files.

Simple example: This change to rustfix itself

diff --git a/cargo-fix/src/cli.rs b/cargo-fix/src/cli.rs
index fbbdfe6..32a9bb4 100644
--- a/cargo-fix/src/cli.rs
+++ b/cargo-fix/src/cli.rs
@@ -107,6 +107,8 @@ fn log_for_human(kind: &str, msg: &str) -> Result<(), Error> {
     let mut stream = StandardStream::stderr(color_choice);
     stream.reset()?;
 
+    let mut i = 42;
+
     stream.set_color(ColorSpec::new().set_bold(true).set_fg(Some(Color::Cyan)))?;
     // Justify to 12 chars just like cargo
     write!(&mut stream, "{:>12}", kind)?;

leads to this output

Checking cargo-fix v0.2.0 (file:///Users/pascal/Projekte/rustfix/cargo-fix)
error: Could not find data slice that covers range 3823..3827
error: Could not compile `cargo-fix`.

That range looks an awful lot like the index in the whole crate's source, not in cli.rs. I'm pretty sure there is such a property in the compiler's json output, though, and I'm just not using it correctly.

@killercup killercup added the bug label May 6, 2018
@killercup
Copy link
Member Author

Yes indeed, this was tracked in https://github.com/rust-lang/rust/issues/35164apparently fixed in rust-lang/rust#42973 a year ago, and this looks like it's still there.

Looks like byte indices are actually legit and something else is wrong here?

@killercup
Copy link
Member Author

Yeah, I'm stupid. We get two suggestions for this specific case:

warning: unused variable: `i`
   --> cargo-fix/src/cli.rs:110:9
    |
110 |     let mut i = 42;
    |         ^^^^^ help: consider using `_i` instead
    |
    = note: #[warn(unused_variables)] on by default

warning: variable does not need to be mutable
   --> cargo-fix/src/cli.rs:110:9
    |
110 |     let mut i = 42;
    |         ----^
    |         |
    |         help: remove this `mut`
    |
    = note: #[warn(unused_mut)] on by default

This should of course not overlap! We can remove the mut and add the _ as two separate lints.

JSON
{
  "message": {
    "children": [
      {
        "children": [],
        "code": null,
        "level": "note",
        "message": "#[warn(unused_variables)] on by default",
        "rendered": null,
        "spans": []
      },
      {
        "children": [],
        "code": null,
        "level": "help",
        "message": "consider using `_i` instead",
        "rendered": null,
        "spans": [
          {
            "byte_end": 3828,
            "byte_start": 3823,
            "column_end": 14,
            "column_start": 9,
            "expansion": null,
            "file_name": "cargo-fix/src/cli.rs",
            "is_primary": true,
            "label": null,
            "line_end": 110,
            "line_start": 110,
            "suggested_replacement": "_i",
            "text": [
              {
                "highlight_end": 14,
                "highlight_start": 9,
                "text": "    let mut i = 42;"
              }
            ]
          }
        ]
      }
    ],
    "code": {
      "code": "unused_variables",
      "explanation": null
    },
    "level": "warning",
    "message": "unused variable: `i`",
    "rendered": "warning: unused variable: `i`\n   --> cargo-fix/src/cli.rs:110:9\n    |\n110 |     let mut i = 42;\n    |         ^^^^^ help: consider using `_i` instead\n    |\n    = note: #[warn(unused_variables)] on by default\n\n",
    "spans": [
      {
        "byte_end": 3828,
        "byte_start": 3823,
        "column_end": 14,
        "column_start": 9,
        "expansion": null,
        "file_name": "cargo-fix/src/cli.rs",
        "is_primary": true,
        "label": null,
        "line_end": 110,
        "line_start": 110,
        "suggested_replacement": null,
        "text": [
          {
            "highlight_end": 14,
            "highlight_start": 9,
            "text": "    let mut i = 42;"
          }
        ]
      }
    ]
  },
  "package_id": "cargo-fix 0.2.0 (path+file:///Users/pascal/Projekte/rustfix/cargo-fix)",
  "reason": "compiler-message",
  "target": {
    "crate_types": [
      "bin"
    ],
    "kind": [
      "bin"
    ],
    "name": "cargo-fix",
    "src_path": "/Users/pascal/Projekte/rustfix/cargo-fix/src/main.rs"
  }
}
{
  "message": {
    "children": [
      {
        "children": [],
        "code": null,
        "level": "note",
        "message": "#[warn(unused_mut)] on by default",
        "rendered": null,
        "spans": []
      },
      {
        "children": [],
        "code": null,
        "level": "help",
        "message": "remove this `mut`",
        "rendered": null,
        "spans": [
          {
            "byte_end": 3827,
            "byte_start": 3823,
            "column_end": 13,
            "column_start": 9,
            "expansion": null,
            "file_name": "cargo-fix/src/cli.rs",
            "is_primary": true,
            "label": null,
            "line_end": 110,
            "line_start": 110,
            "suggested_replacement": "",
            "text": [
              {
                "highlight_end": 13,
                "highlight_start": 9,
                "text": "    let mut i = 42;"
              }
            ]
          }
        ]
      }
    ],
    "code": {
      "code": "unused_mut",
      "explanation": null
    },
    "level": "warning",
    "message": "variable does not need to be mutable",
    "rendered": "warning: variable does not need to be mutable\n   --> cargo-fix/src/cli.rs:110:9\n |\n110 |     let mut i = 42;\n    |         ----^\n    |         |\n    |         help: remove this `mut`\n    |\n    = note: #[warn(unused_mut)] on by default\n\n",
    "spans": [
      {
        "byte_end": 3828,
        "byte_start": 3823,
        "column_end": 14,
        "column_start": 9,
        "expansion": null,
        "file_name": "cargo-fix/src/cli.rs",
        "is_primary": true,
        "label": null,
        "line_end": 110,
        "line_start": 110,
        "suggested_replacement": null,
        "text": [
          {
            "highlight_end": 14,
            "highlight_start": 9,
            "text": "    let mut i = 42;"
          }
        ]
      }
    ]
  },
  "package_id": "cargo-fix 0.2.0 (path+file:///Users/pascal/Projekte/rustfix/cargo-fix)",
  "reason": "compiler-message",
  "target": {
    "crate_types": [
      "bin"
    ],
    "kind": [
      "bin"
    ],
    "name": "cargo-fix",
    "src_path": "/Users/pascal/Projekte/rustfix/cargo-fix/src/main.rs"
  }
}

@killercup
Copy link
Member Author

Long story short this is actually rust-lang/rust#50472 and I should get some sleep.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

1 participant