Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Raise error if struct field name is greater than 30 chars (#1239)
### What Raise compile error if struct field name is greater than 30 chars, instead of 32 chars. ### Why As it happens the contract spec restricts the length of the struct field name to 30 characters. This is an oversight. The type is a StringM<30>, but we should have made it a ScSymbol in the XDR so that it picked up the max length of a Symbol. We did this with other parts of the spec, but not this one it seems. It's probably too late to change the type in a backwards compatible way, at least now. And there isn't significant value in trying to fix that because struct fields over 30 characters long but still under 32 characters long are rare. The code change introduces a constant in the code that matches the const generic expected on the StringM type. This probably looks a little odd but is done to introduce a compiler check/guarantee that the length of the value we print out can never diverge from the max size of the StringM that stores the value. If the XDR ever changes to use a larger value, this code in the SDK will fail to compile until we update it. Close #1228 ### Other Problems This Creates This discrepancy does create some problems. It's possible using the raw `Map` type to store key-value pairs where the keys are `Symbol`s up to 32-bytes long, that cannot be transferred into a `contracttype` because they cannot have field names greater than 30-bytes long. This is quite an edge case, and so unlikely to be a problem someone encounters, but it is unfortunate. ### Example #### Before No error. An empty field name is written to spec XDR. #### After ``` error: struct field name is too long: 31, max is 30 --> tests/empty/src/lib.rs:16:9 | 16 | pub a234567890123456789001234567890: u64, | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ```
- Loading branch information