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
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@saethlin
Copy link
Member

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
@saethlin saethlin added the C-bug Category: This is a bug. label Jun 26, 2020
@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed C-bug Category: This is a bug. labels Jun 26, 2020
@estebank
Copy link
Contributor

@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 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 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
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants