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

Type ascription misdiagnosis when trying to use serde_json::json! #73777

Closed
saethlin opened this issue Jun 26, 2020 · 3 comments
Closed

Type ascription misdiagnosis when trying to use serde_json::json! #73777

saethlin opened this issue Jun 26, 2020 · 3 comments

Comments

@saethlin
Copy link
Contributor

@saethlin saethlin commented Jun 26, 2020

Hi @estebank I didn't forget about your encouragement on Twitter: https://twitter.com/ekuber/status/1255881826855313409?s=20

I forgot a comma at the end of the first key-value pair:

fn main() {
    let json = serde_json::json!({
        "domain_name": "redjack.com"
        "passive_provenance": [420, 69],
    });
}

On the latest nightly compiler this is explicitly diagnosed as an attempt to use type ascription:

error: expected type, found `"redjack.com"`
 --> src/main.rs:3:24
  |
3 |         "domain_name": "redjack.com"
  |                      - ^^^^^^^^^^^^^ expected type
  |                      |
  |                      tried to parse a type due to this type ascription
  |
  = note: `#![feature(type_ascription)]` lets you annotate an expression with a type: `<expr>: <type>`
  = note: see issue #23416 <https://github.com/rust-lang/rust/issues/23416> for more information

On stable, it's arguably much worse, the error only makes sense if you know the compiler thinks this is type ascription but feature is not mentioned:

error: expected type, found `"redjack.com"`
 --> src/main.rs:3:24
  |
3 |         "domain_name": "redjack.com"
  |                      - ^^^^^^^^^^^^^ expected type
  |                      |
  |                      tried to parse a type due to this
@estebank
Copy link
Contributor

@estebank estebank commented Jun 29, 2020

@dtolnay is this something that could be handled in serde_json? I had a cursory look and saw that there already macro cases exclusively for better diagnostics, but the macro was big enough that I couldn't disentangle how to write a macro case for ($expr : $expr*).

Ideally we would handle this in the compiler more gracefully, but changing the RHS here from a string literal to a type makes the compiler be a bit more helpful.

@dtolnay
Copy link
Member

@dtolnay dtolnay commented Jun 29, 2020

Yeah I opened a fix in serde-rs/json#688.

error: no rules expected the token `"passive_provenance"`
 --> $DIR/missing_comma.rs:4:9
  |
3 |         "domain_name": "redjack.com"
  |                                     - help: missing comma here
4 |         "passive_provenance": [420, 69],
  |         ^^^^^^^^^^^^^^^^^^^^ no rules expected this token in macro call
@estebank
Copy link
Contributor

@estebank estebank commented Jul 2, 2020

Closing as per the fix in serde and CCing #34255 which tracks the work around type ascription more generally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants