Marwes / schemafy

Crate for generating rust types from a json schema
MIT License
242 stars 51 forks source link

Proc macro panics on identifier beginning with a number. #36

Closed Pfeil closed 4 years ago

Pfeil commented 4 years ago

Hi. I just tried this library the first time with a schema from a datatype registry, and the macro panics. I did everything like in https://github.com/Marwes/debugserver-types . The problem is the identifier. "asdf" as identifier makes it work. I believe that the identifier is valid, though. Also, I can not change it (except for this test). Those schemes are already used in other software to validate json files (I think this is one of the libraries that is used for validation: https://github.com/everit-org/json-schema/). This issue is currently not very important/urgent to me, but in case this is really a bug I thought I better report it.

Output of cargo build:

> cargo build
   Compiling schemafy_test v0.1.0 (/Users/verpfeilt/git/schemafy_test)
thread 'rustc' panicked at '`"21T111481F586Ca9A6Acbda92809"` is not a valid identifier', src/librustc_expand/proc_macro_server.rs:329:13
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

error: internal compiler error: unexpected panic

note: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports

note: rustc 1.44.1 (c7087fe00 2020-06-17) running on x86_64-apple-darwin

note: compiler flags: -C debuginfo=2 -C incremental --crate-type lib

note: some of the compiler flags provided by cargo are hidden

error: proc macro panicked
 --> src/lib.rs:3:1
  |
3 | schemafy::schemafy!("src/schema.json");
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: message: `"21T111481F586Ca9A6Acbda92809"` is not a valid identifier

warning: unused imports: `Deserialize`, `Serialize`
 --> src/lib.rs:1:13
  |
1 | use serde::{Deserialize, Serialize};
  |             ^^^^^^^^^^^  ^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

error: aborting due to previous error; 1 warning emitted

error: could not compile `schemafy_test`.

To learn more, run the command again with --verbose.

My schema.json:

{
    "definitions": {
        "21.T11148_1f586ca9a6acbda92809": {
            "pattern": "^([0-9]{4})(-)?([0][1-9]|1[0-2])(-)?([0-2][0-9]|3[0-1]) ([0-1][0-9]|2[0-3])(:)?([0-5][0-9])(:)?([0-5][0-9](\\.[0-9]*)?)( UTC)?$",
            "type": "string",
            "description": "date-time-handle-utc@21.T11148/1f586ca9a6acbda92809"
        }
    },
    "$schema": "http://json-schema.org/draft-04/schema#",
    "description": "dateCreated@21.T11148/29f92bd203dd3eaa5a1f",
    "$ref": "#/definitions/21.T11148_1f586ca9a6acbda92809"
}
Pfeil commented 4 years ago

I can confirm that the issue is the fact that the identifier begins with a number, like stated in #35 . So I added schemafy like this: schemafy = { git = "https://github.com/Marwes/schemafy"}, but the issue is still there. Am I missing something? This should use the latest commit, right?

Marwes commented 4 years ago

You can run cargo update to ensure it has the latest, and you can check Cargo.lock to see which git revision it uses to be safe. Not sure what is going on though, not sure I will have a chance to look at it soon

Pfeil commented 4 years ago

It works with a separate project, even though I tried cargo update on the first project and the cargo lock had the correct commit. I'm a little confused, but probably a clean build or so will solve it. Thanks!