Detect multiple variants having same From type

This commit is contained in:
David Tolnay 2019-10-12 17:21:21 -07:00
parent f3b38c3a8a
commit 2492735b67
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
1 changed files with 14 additions and 0 deletions

View File

@ -1,5 +1,7 @@
use crate::ast::{Enum, Field, Input, Struct, Variant};
use crate::attr::Attrs;
use quote::ToTokens;
use std::collections::BTreeSet as Set;
use syn::{Error, Member, Result};
pub(crate) const CHECKED: &str = "checked in validation";
@ -37,6 +39,18 @@ impl Enum<'_> {
));
}
}
let mut from_types = Set::new();
for variant in &self.variants {
if let Some(from_field) = variant.from_field() {
let repr = from_field.ty.to_token_stream().to_string();
if !from_types.insert(repr) {
return Err(Error::new_spanned(
from_field.original,
"cannot derive From because another variant has the same source type",
));
}
}
}
Ok(())
}
}