[Pre-RFC] Include scope names in diagnostics


#1

So far, rustc has only printed about filenames and line numbers for warnings and errors. I think it is rather missed, compared to gcc and other compilers, that useful context information such as function names and structs is not included.

I’d like to discuss these possible changes to rustc. I’ve also submitted a reference implementation ( https://github.com/rust-lang/rust/pull/49898 ) that I will rewrite following a merged RFC about this.

Short example:

    In function `test`:
    warning: unused variable: `a`

A larger example (still a bit buggy, for now):

struct OkStruct {
    i: u32,
}

enum OkEnum {
    ItemA,
    ItemB,
}

impl OkStruct {
    fn method() {
        let a = 1;
    }
}

fn test() {
    let a = 1;
}

fn test_nested() {
    let a = 1;

    fn nested() {
        let b = 2;
    }
}

fn main() {
    let a = 1;

    println!("{:?}", OkEnum::ItemA);
}

The output is:

In function `test`:
warning: unused variable: `a`
  --> various-warnings.rs:18:9
   |
18 |     let a = 1;
   |         ^ help: consider using `_a` instead
   |
   = note: #[warn(unused_variables)] on by default

In function `test_nested`:
warning: unused variable: `a`
  --> various-warnings.rs:22:9
   |
22 |     let a = 1;
   |         ^ help: consider using `_a` instead

In function `test_nested::nested`:
warning: unused variable: `b`
  --> various-warnings.rs:25:13
   |
25 |         let b = 2;
   |             ^ help: consider using `_b` instead

In function `main`:
warning: unused variable: `a`
  --> various-warnings.rs:30:9
   |
30 |     let a = 1;
   |         ^ help: consider using `_a` instead

In method `method::method`:
warning: unused variable: `a`
  --> various-warnings.rs:13:13
   |
13 |         let a = 1;
   |             ^ help: consider using `_a` instead

In struct `OkStruct`:
warning: struct is never used: `OkStruct`
 --> various-warnings.rs:1:1
  |
1 | struct OkStruct {
  | ^^^^^^^^^^^^^^^
  |
  = note: #[warn(dead_code)] on by default

In enum `OkEnum`:
warning: variant is never constructed: `ItemB`
 --> various-warnings.rs:8:5
  |
8 |     ItemB,
  |     ^^^^^

In method `method::method`:
warning: method is never used: `method`
  --> various-warnings.rs:12:5
   |
12 |     fn method() {
   |     ^^^^^^^^^^^

In function `test`:
warning: function is never used: `test`
  --> various-warnings.rs:17:1
   |
17 | fn test() {
   | ^^^^^^^^^

In function `test_nested`:
warning: function is never used: `test_nested`
  --> various-warnings.rs:21:1
   |
21 | fn test_nested() {
   | ^^^^^^^^^^^^^^^^


#2

This would be very helpful for thinking about where it happens. I would also say that maybe also including the name of any named scopes, ie inner': would also be beneficial.


#3

I’m all for more information, but extra lines are not great. How about adding it to the file/line information?

various-warnings.rs:30:9 in fn main