Skip to content

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

Closed
@saethlin

Description

@saethlin

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions