Skip to content

Removing duplicate Slice origins and surrounding empty lines #9

Closed

Description

I'm currently trying to move the suggestions of the following diagnostic over from Rust to annotate-snippet:

error[E0382]: use of moved value: `x`
  --> /code/rust/src/test/ui/annotate-snippet/suggestion.rs:4:5
   |
 4 |     let x = vec![1];
   |         - move occurs because `x` has type `std::vec::Vec<i32>`, which does not implement the `Copy` trait
 7 |     let y = x;
   |             - value moved here
 9 |     x; //~ ERROR use of moved value
   |     ^ value used here after move

In short, from Rust I can only get a Vec that contains the three lines with the annotations. This Vec contains only the lines with annotations and no other lines around it. Something similar to this:

[
  Line {
      line_index: 4,
      annotations: [
          Annotation {
              start_col: 8,
              end_col: 9,
              label: "move occurs because ...",
          },
      ],
  },
  Line {
      line_index: 7,
      annotations: [
          Annotation {
              start_col: 11,
              end_col: 12,
              label: "value moved here",
          },
      ],
  },
  // etc..
]

I'm currently mapping this to annotate-snippets by turning each Line into a Slice and then calling DisplayList::from(snippet) at the end.

The problem is that each Slice has the origin at the top:

error[E0382]: use of moved value: `x`
  --> /code/rust/src/test/ui/annotate-snippet/suggestion.rs:4:8
   |
 4 |     let x = vec![1];
   |         ^ move occurs because `x` has type `std::vec::Vec<i32>`, which does not implement the `Copy` trait
   |
  ::: /code/rust/src/test/ui/annotate-snippet/suggestion.rs:7:12
   |
 7 |     let y = x;
   |             ^ value moved here
   |
  ::: /code/rust/src/test/ui/annotate-snippet/suggestion.rs:9:4
   |
 9 |     x; //~ ERROR use of moved value
   |     ^ value used here after move

If I set the origin of consecutive slices to None there will still be extra empty padding lines:

error[E0382]: use of moved value: `x`
  --> /code/rust/src/test/ui/annotate-snippet/suggestion.rs:5:8                                                             
   |
LL |     let x = vec![1];
   |         ^ move occurs because `x` has type `std::vec::Vec<i32>`, which does not implement the `Copy` trait
   |
   |
LL |     let y = x;
   |             ^ value moved here
   |
   |
LL |     x; //~ ERROR use of moved value
   |     ^ value used here after move
   |

I'm not sure what the original idea for a Slice was, but it seems like the best approach for those rustc annotations?

I can see three ways to solve this currently:

  1. annotate-snippets doesn't add empty padding lines if the origin is None
  2. annotate-snippets removes the origin and empty padding lines of consecutive Slices that have the same origin. This means annotate-snippet would have to compare the origin of each Slice to origin of the previous Slice.
  3. We implement From<Snippet> for DisplayList by ourselves in rustc.

I would love to hear your thoughts on this before I continue with the annotations =)

cc rust-lang/rust#61809

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions