withoutboats / fehler

Rust doesn't have exceptions
Other
629 stars 24 forks source link

compiler errors sometimes made nugatory (closures, struct derive?) #60

Open ijackson opened 4 years ago

ijackson commented 4 years ago

Hi. Thanks again for this excellent package.

To reproduce:

#[allow(unused_imports)]
use fehler::throws;

fn parse_args(_f: &dyn Fn(&mut ())) { }

#[throws(std::io::Error)]
fn main() 
{
    #[derive(Default,Debug)]
    struct Args { }
    let args = parse_args(&|ma|{ });
    let spec = 42;
}

And run cargo build, with fehler="1" in your dependencies.

Actual output:

warning: unused variable: `ma`
  |
  = note: `#[warn(unused_variables)]` on by default

warning: unused variable: `args`

warning: unused variable: `spec`

warning: 3 warnings emitted

    Finished dev [unoptimized + debuginfo] target(s) in 0.01s

Note the very unhelpful error messages, with no location information and no suggestions.

Expected output:


warning: unused variable: `ma`
  --> src/bin/report.rs:11:29
   |
11 |     let args = parse_args(&|ma|{ });
   |                             ^^ help: if this is intentional, prefix it with an underscore: `_ma`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: unused variable: `args`
  --> src/bin/report.rs:11:9
   |
11 |     let args = parse_args(&|ma|{ });
   |         ^^^^ help: if this is intentional, prefix it with an underscore: `_args`

warning: unused variable: `spec`
  --> src/bin/report.rs:12:9
   |
12 |     let spec = 42;
   |         ^^^^ help: if this is intentional, prefix it with an underscore: `_spec`

warning: 3 warnings emitted

    Finished dev [unoptimized + debuginfo] target(s) in 0.17s

This rather odd test case was minimised from a more complicated program. Empirically, the issue goes away if I remove any of (a) #[throws] (b) the #[derive] on the struct (c) the closure. The problem can occur with errors as well as warnings. In one particular version of my actual program I got the very unhelpful output type annotations needed without any indication of where!

Thanks for your attention.

dtolnay commented 4 years ago

This is a consequence of this compiler bug: https://github.com/rust-lang/rust/issues/43081. When calling an attribute macro #[...] the compiler will randomly forget all the source code location information like this.

ijackson commented 3 years ago

I have not seen this happen to me in a very long time. I think that if this compiler bug is still present, it now does not come up when using fehler.

Not sure whether to leave this issue open in case it recurs for anyone, or simply close it.