From 6ba97357d81dbb8c1c05b4022e5c589be514563e Mon Sep 17 00:00:00 2001 From: Ted Driggs Date: Wed, 26 Oct 2022 12:07:27 -0700 Subject: [PATCH] Only include error locations when necessary When spans are available, locations in the message add noise. --- CHANGELOG.md | 1 + core/src/error/mod.rs | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7362b78..2833e43 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased - Derived impls of `FromMeta` will now error on literals, rather than silently ignoring them. [#193](https://github.com/TedDriggs/darling/pull/193) +- Don't include property paths in compile errors when spans are available. ## v0.14.1 (April 28, 2022) diff --git a/core/src/error/mod.rs b/core/src/error/mod.rs index 87dbdff..cc4b15b 100644 --- a/core/src/error/mod.rs +++ b/core/src/error/mod.rs @@ -446,7 +446,16 @@ impl From for Error { impl From for syn::Error { fn from(e: Error) -> Self { if e.len() == 1 { - syn::Error::new(e.span(), e) + if let Some(span) = e.explicit_span() { + // Don't include the location path if the error has an explicit span, + // since it will be redundant and isn't consistent with how rustc + // exposes errors. + syn::Error::new(span, e.kind) + } else { + // If the error's span is going to be the macro call site, include + // the location information to try and help the user pinpoint the issue. + syn::Error::new(e.span(), e) + } } else { let mut syn_errors = e.flatten().into_iter().map(syn::Error::from); let mut error = syn_errors