Skip to content

Commit 320d70f

Browse files
authored
Merge pull request #164 from dtolnay/frombacktrace
Fix miscounting fields when from and backtrace are same field
2 parents 7966eb3 + c4d7c2b commit 320d70f

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

impl/src/valid.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,11 @@ fn check_field_attrs(fields: &[Field]) -> Result<()> {
180180
}
181181
}
182182
if let Some(from_field) = from_field {
183-
if fields.len() > 1 + has_backtrace as usize {
183+
let max_expected_fields = match backtrace_field {
184+
Some(backtrace_field) => 1 + !same_member(from_field, backtrace_field) as usize,
185+
None => 1 + has_backtrace as usize,
186+
};
187+
if fields.len() > max_expected_fields {
184188
return Err(Error::new_spanned(
185189
from_field.attrs.from,
186190
"deriving From requires no fields other than source and backtrace",

tests/ui/from-backtrace-backtrace.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// https://github.com/dtolnay/thiserror/issues/163
2+
3+
#![feature(backtrace)]
4+
5+
use std::backtrace::Backtrace;
6+
use thiserror::Error;
7+
8+
#[derive(Error, Debug)]
9+
#[error("...")]
10+
pub struct Error(#[from] #[backtrace] std::io::Error, Backtrace);
11+
12+
fn main() {}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: deriving From requires no fields other than source and backtrace
2+
--> tests/ui/from-backtrace-backtrace.rs:10:18
3+
|
4+
10 | pub struct Error(#[from] #[backtrace] std::io::Error, Backtrace);
5+
| ^^^^^^^

0 commit comments

Comments
 (0)