Skip to content

Commit

Permalink
feat: improved error message for conflicting generics with from
Browse files Browse the repository at this point in the history
  • Loading branch information
0x00002a committed Jan 15, 2024
1 parent 82e7880 commit c539182
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ readme = "docs/README.md"

[dependencies]
impl-tools = "0.9.0"
typesum-macros = { version= "0.2.0", default_features = false }
typesum-macros = { version= "0.2.1", default_features = false, path = "./typesum-macros" }

[features]
sumtype = ["typesum-macros/sumtype"]
Expand Down
2 changes: 1 addition & 1 deletion typesum-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "typesum-macros"
version = "0.2.0"
version = "0.2.1"
edition = "2021"
license = "MIT"
description = "Macros for typesum"
Expand Down
22 changes: 21 additions & 1 deletion typesum-macros/src/sum_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ where
);
generate(as_.as_slice(), bs.as_slice(), is.as_slice())
}
const EXPLICITLY_DISABLE_FROM_MSG: &str = "You need to explicitly disable the ones you don't want with #[sumtype(from = false)]. See the docs on #[sumtype] for more information";

pub fn sumtype_attr(mut attrs: Attrs, input: syn::DeriveInput) -> TokenStream {
let syn::Data::Enum(data) = &input.data else {
return syn::Error::new_spanned(&input, "sumtype can only act on enums").to_compile_error();
Expand Down Expand Up @@ -354,9 +356,27 @@ pub fn sumtype_attr(mut attrs: Attrs, input: syn::DeriveInput) -> TokenStream {
.filter(|((a, _), _)| a.add_from_impl)
.collect::<Vec<_>>();
let mut seen_from = Vec::new();
let mut seen_generics = 0;
for (_, (_, candidate)) in &from_candidates {
if let syn::Type::Path(p) = candidate {
if tys
.type_params()
.find(|t| p.path.is_ident(&t.ident))
.is_some()
{
seen_generics += 1;
if seen_generics > 1 {
return syn::Error::new_spanned(candidate, format!("Multiple generic's with From implementations found, these will conflict. {EXPLICITLY_DISABLE_FROM_MSG}")).to_compile_error();
}
}
}

if seen_from.contains(candidate) {
return syn::Error::new_spanned(candidate, "Multiple valid From candidates found. You need to explicitly disable the ones you don't want with #[sumtype(from = false)]. See the docs on #[sumtype] for more information").to_compile_error();
return syn::Error::new_spanned(
candidate,
"Multiple valid From candidates found. {EXPLICITLY_DISABLE_FROM_MSG}",
)
.to_compile_error();
} else {
seen_from.push(&candidate);
}
Expand Down

0 comments on commit c539182

Please sign in to comment.