Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle more SCALE attributes: skip, index #44

Merged
merged 36 commits into from
Mar 30, 2021
Merged

Conversation

dvdplm
Copy link
Contributor

@dvdplm dvdplm commented Jan 4, 2021

Handle #[codec(skip)] and #[codec(index = $int)] (on variants) and use the utility code copied from parity-scale-codec-derive.

In parity-scale-codec-derive there is a fair amount of validation made on attributes being in the right place. This PR does not include those checks – I assume that a type containing any of these attributes will derive Encode and so run the validation so duplicating them seems dumb.

Builds on #42
Closes #43
Closes #45

ascjones and others added 17 commits December 7, 2020 11:49
By default, parity-scale-codec does not provide Encode/Decode impls for an owned String.
This is only provided under the "full" feature which is not used by the substrate runtime,
because it should not be used for consensus critical code. So in order for the CompactForm
to be integrated into the substrate runtime, or wherever the "full" feature cannot be used,
then we must parameterize the `String` type so that it can be both an `&'static str` on the
runtime side where it is encoded, and a `String` in client/consuming code where it is decoded.
Add a `compact` member to `Field` to indicate it is `scale_codec::Compact`
Handle `codec(skip)` and `codec(index = $int)` attributes
@dvdplm dvdplm requested a review from ascjones January 4, 2021 22:08
@ascjones ascjones mentioned this pull request Jan 5, 2021
8 tasks
@dvdplm dvdplm marked this pull request as ready for review February 17, 2021 12:55
@dvdplm dvdplm self-assigned this Feb 17, 2021
.github/workflows/rust.yml Outdated Show resolved Hide resolved
Copy link
Contributor

@ascjones ascjones left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need to add the index to regular enum variants too.

derive/src/utils.rs Outdated Show resolved Hide resolved
derive/src/utils.rs Outdated Show resolved Hide resolved
derive/src/lib.rs Show resolved Hide resolved
derive/src/utils.rs Outdated Show resolved Hide resolved
derive/src/utils.rs Show resolved Hide resolved
src/tests.rs Outdated Show resolved Hide resolved
test_suite/tests/derive.rs Show resolved Hide resolved
test_suite/tests/ui/fail_with_invalid_codec_attrs.stderr Outdated Show resolved Hide resolved
derive/src/utils.rs Outdated Show resolved Hide resolved
dvdplm and others added 3 commits March 23, 2021 11:17
Co-authored-by: Andrew Jones <[email protected]>
  Better error message
  Remove redundant tests
  Test more enum cases to document index/discriminant interaction
@@ -16,4 +16,4 @@ error: proc-macro derive panicked
11 | #[derive(TypeInfo, Encode)]
| ^^^^^^^^
|
= help: message: Internal error, parse_meta must have been checked: Error("expected literal")
= help: message: Internal error. `#[codec(…)]` attributes must be checked in `parity-scale-codec`. This is a bug.: Error("expected literal")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still don't 100% get this - the errors above were generated by parity-scale-codec so the check appears to have happened correctly - so this error is not necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, it’s there to chastise sloppy developers and should never reach users. It means that the attribute syntax checks never happened or are buggy; the derive author has to explicitly make a method call so there’s nothing stopping human error.
We could unwrap here but that’s not better.
The trybuild is here as a guard against buggy parity-scale-codec releases.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should never reach users

Forgive me 🙈 , but doesn't this trybuild demonstrate that this error does in fact reach the user even though the syntax checks did happen?

Copy link
Contributor Author

@dvdplm dvdplm Mar 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right and I am wrong. First of all I'm heaping on a bunch of different syntax errors in the same enum which makes it hard to understand what is going on. If I use a single-syntax error at a time it's easier to follow.

#[derive(Encode, TypeInfo)]
enum EnumAttrsValidation {
    #[codec(index = a)]
    Thing(u32),
}

Gives this error:

error: proc-macro derive panicked
  --> $DIR/fail_with_invalid_codec_attrs.rs:17:18
   |
17 | #[derive(Encode, TypeInfo)]
   |                  ^^^^^^^^
   |
   = help: message: Internal error. `#[codec(…)]` attributes must be checked in `parity-scale-codec`. This is a bug.: Error("expected literal")

error: expected literal
  --> $DIR/fail_with_invalid_codec_attrs.rs:19:21
   |
19 |     #[codec(index = a)]
   |                     ^

So parity-scale-codec is giving the proper error message back, whereas scale-info just panics. What I was trying to say with this test is that scale-info will not try to check attributes from parity-scale-codec.

We have no way of knowing if the Encode macro ran or how it went, so we can either panic or re-implement their attribute checking and print the proper error twice.

I think we should panic but my error message must change. How about: scale-info: Bad index in `#[codec(index = …)]`: Error("expected literal").

Wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, maybe could mention somehow to see the other error generated by parity-scale-codec.

Copy link
Contributor

@ascjones ascjones left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@dvdplm
Copy link
Contributor Author

dvdplm commented Mar 24, 2021

@Robbepop This one should be pretty non-controversial.

@ascjones ascjones merged commit a02b151 into master Mar 30, 2021
@ascjones ascjones deleted the dp-handle-scale-skip branch March 30, 2021 09:27
@ascjones ascjones mentioned this pull request Jun 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Handle #[codec(index)] on variants Handle #[codec(skip)]
2 participants