Improve errors for generate_solution_type macro (#9553)

* add more errors for check attributes in npos elections solution type

* revert local env

* return Ok false if there are no attributes

* fmt

* Update primitives/npos-elections/solution-type/src/lib.rs

Co-authored-by: Squirrel <gilescope@gmail.com>

* Update primitives/npos-elections/solution-type/src/lib.rs

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

* Update primitives/npos-elections/solution-type/src/lib.rs

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

* Update primitives/npos-elections/solution-type/src/lib.rs

* improve span by giving extra attribute, nightly fmt

* fix test to test new error msg

Co-authored-by: Squirrel <gilescope@gmail.com>
Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>
This commit is contained in:
Amar Singh
2021-08-25 21:32:25 -04:00
committed by GitHub
parent 0f1896e7a2
commit 77489c3107
4 changed files with 20 additions and 50 deletions
@@ -134,20 +134,23 @@ struct SolutionDef {
}
fn check_attributes(input: ParseStream) -> syn::Result<bool> {
let attrs = input.call(syn::Attribute::parse_outer).unwrap_or_default();
let mut attrs = input.call(syn::Attribute::parse_outer).unwrap_or_default();
if attrs.len() > 1 {
return Err(syn_err("compact solution can accept only #[compact]"))
let extra_attr = attrs.pop().expect("attributes vec with len > 1 can be popped");
return Err(syn::Error::new_spanned(
extra_attr.clone(),
"compact solution can accept only #[compact]",
))
}
Ok(attrs.iter().any(|attr| {
if attr.path.segments.len() == 1 {
let segment = attr.path.segments.first().expect("Vec with len 1 can be popped.");
if segment.ident == Ident::new("compact", Span::call_site()) {
return true
if attrs.is_empty() {
return Ok(false)
}
let attr = attrs.pop().expect("attributes vec with len 1 can be popped.");
if attr.path.is_ident("compact") {
Ok(true)
} else {
Err(syn::Error::new_spanned(attr.clone(), "compact solution can accept only #[compact]"))
}
false
}))
}
impl Parse for SolutionDef {
@@ -0,0 +1,5 @@
error: compact solution can accept only #[compact]
--> $DIR/wrong_attribute.rs:4:2
|
4 | #[pages(1)] pub struct TestSolution::<
| ^^^^^^^^^^^
@@ -1,38 +0,0 @@
error[E0412]: cannot find type `Perbill` in this scope
--> $DIR/wrong_page.rs:7:14
|
7 | Accuracy = Perbill,
| ^^^^^^^ not found in this scope
|
help: consider importing this struct
|
1 | use sp_arithmetic::Perbill;
|
error[E0433]: failed to resolve: use of undeclared type `Perbill`
--> $DIR/wrong_page.rs:7:14
|
7 | Accuracy = Perbill,
| ^^^^^^^ not found in this scope
|
help: consider importing this struct
|
1 | use sp_arithmetic::Perbill;
|
error[E0119]: conflicting implementations of trait `std::convert::TryFrom<&[_npos::IndexAssignment<u8, u16, [type error]>]>` for type `TestSolution`
--> $DIR/wrong_page.rs:3:1
|
3 | / generate_solution_type!(
4 | | #[pages(1)] pub struct TestSolution::<
5 | | VoterIndex = u8,
6 | | TargetIndex = u16,
7 | | Accuracy = Perbill,
8 | | >(8)
9 | | );
| |__^
|
= note: conflicting implementation in crate `core`:
- impl<T, U> TryFrom<U> for T
where U: Into<T>;
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)