Skip to content

Commit

Permalink
Merge pull request #19 from dtolnay/workaround
Browse files Browse the repository at this point in the history
Remove 'rustc is so bad at spans' workaround
  • Loading branch information
dtolnay authored Oct 25, 2022
2 parents 1ff370f + e0e7625 commit d55f794
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 33 deletions.
18 changes: 1 addition & 17 deletions src/emit.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::quote;
use syn::Error;

Expand All @@ -11,13 +10,7 @@ pub enum Kind {
Let,
}

pub fn emit(err: Error, kind: Kind, output: TokenStream) -> TokenStream {
let mut err = err;
if !probably_has_spans(kind) {
// Otherwise the error is printed without any line number.
err = Error::new(Span::call_site(), err);
}

pub fn emit(err: &Error, kind: Kind, output: TokenStream) -> TokenStream {
let err = err.to_compile_error();
let output = proc_macro2::TokenStream::from(output);

Expand All @@ -28,12 +21,3 @@ pub fn emit(err: Error, kind: Kind, output: TokenStream) -> TokenStream {

TokenStream::from(expanded)
}

// Rustc is so bad at spans.
// https://github.com/rust-lang/rust/issues/43081
fn probably_has_spans(kind: Kind) -> bool {
match kind {
Kind::Enum | Kind::Struct => true,
Kind::Match | Kind::Let => false,
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ pub fn sorted(args: TokenStream, input: TokenStream) -> TokenStream {

match result {
Ok(_) => output,
Err(err) => emit(err, kind, output),
Err(err) => emit(&err, kind, output),
}
}

Expand Down
8 changes: 3 additions & 5 deletions tests/ui/let-unstable.stderr
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
error: E::Bbb should sort before E::Ccc
--> tests/ui/let-unstable.rs:15:5
--> tests/ui/let-unstable.rs:20:9
|
15 | #[sorted]
| ^^^^^^^^^
|
= note: this error originates in the attribute macro `sorted` (in Nightly builds, run with -Z macro-backtrace for more info)
20 | E::Bbb(_, _) => {}
| ^^^^^^
8 changes: 3 additions & 5 deletions tests/ui/match-unstable.stderr
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
error: E::Bbb should sort before E::Ccc
--> tests/ui/match-unstable.rs:15:5
--> tests/ui/match-unstable.rs:20:9
|
15 | #[sorted]
| ^^^^^^^^^
|
= note: this error originates in the attribute macro `sorted` (in Nightly builds, run with -Z macro-backtrace for more info)
20 | E::Bbb(_, _) => {}
| ^^^^^^
8 changes: 3 additions & 5 deletions tests/ui/unsorted-match-unstable.stderr
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
error: E::Bbb should sort before E::Ddd
--> tests/ui/unsorted-match-unstable.rs:15:5
--> tests/ui/unsorted-match-unstable.rs:21:9
|
15 | #[sorted]
| ^^^^^^^^^
|
= note: this error originates in the attribute macro `sorted` (in Nightly builds, run with -Z macro-backtrace for more info)
21 | E::Bbb(_, _) => {}
| ^^^^^^

0 comments on commit d55f794

Please sign in to comment.