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

Syntactically invalid JSON from -Z ast-json #54880

Closed
dtolnay opened this issue Oct 6, 2018 · 3 comments
Closed

Syntactically invalid JSON from -Z ast-json #54880

dtolnay opened this issue Oct 6, 2018 · 3 comments
Labels
C-bug Category: This is a bug.

Comments

@dtolnay
Copy link
Member

dtolnay commented Oct 6, 2018

The JSON emitted for the following AST is not legal JSON.

struct Struct;
trait Trait {}

macro_rules! impl_trait_for {
    ($s:ident) => {
        impl Trait for $s {}
    };
}

impl_trait_for!(Struct);

To reproduce:

rustc repro.rs -Z ast-json | rg ',]'

With indentation added, part of the JSON output looks like this. The problem is with the last , in this snippet.

{
  "variant": "Token",
  "fields": [
    {
      "lo": 104,
      "hi": 106
    },
    {
      "variant": "Interpolated",
      "fields": [
        [
          {
            "variant": "NtIdent",
            "fields": [
              "Struct",
              false
            ]
          },
        ]
      ]
    }
  ]
}
@dtolnay
Copy link
Member Author

dtolnay commented Oct 6, 2018

impl Encodable for LazyTokenStream {
fn encode<S: Encoder>(&self, _: &mut S) -> Result<(), S::Error> {
Ok(())
}
}
impl Decodable for LazyTokenStream {
fn decode<D: Decoder>(_: &mut D) -> Result<LazyTokenStream, D::Error> {
Ok(LazyTokenStream::new())
}
}

@Havvy Havvy added the C-bug Category: This is a bug. label Oct 7, 2018
@jryans
Copy link
Contributor

jryans commented Dec 12, 2020

I cannot reproduce this issue with 1.50.0-nightly (2225ee1 2020-12-11). The JSON output is now syntactically valid.

JSON output
{
    "module": {
        "inner": {
            "lo": 0,
            "hi": 144
        },
        "unsafety": "No",
        "items": [
            {
                "attrs": [
                    {
                        "kind": {
                            "variant": "Normal",
                            "fields": [
                                {
                                    "path": {
                                        "span": {
                                            "lo": 0,
                                            "hi": 0
                                        },
                                        "segments": [
                                            {
                                                "ident": {
                                                    "name": "prelude_import",
                                                    "span": {
                                                        "lo": 0,
                                                        "hi": 0
                                                    }
                                                },
                                                "id": 3,
                                                "args": null
                                            }
                                        ],
                                        "tokens": null
                                    },
                                    "args": "Empty",
                                    "tokens": null
                                },
                                null
                            ]
                        },
                        "id": null,
                        "style": "Outer",
                        "span": {
                            "lo": 0,
                            "hi": 0
                        }
                    }
                ],
                "id": 4,
                "span": {
                    "lo": 0,
                    "hi": 0
                },
                "vis": {
                    "kind": "Inherited",
                    "span": {
                        "lo": 0,
                        "hi": 0
                    },
                    "tokens": null
                },
                "ident": {
                    "name": "",
                    "span": {
                        "lo": 0,
                        "hi": 0
                    }
                },
                "kind": {
                    "variant": "Use",
                    "fields": [
                        {
                            "prefix": {
                                "span": {
                                    "lo": 0,
                                    "hi": 0
                                },
                                "segments": [
                                    {
                                        "ident": {
                                            "name": "{{root}}",
                                            "span": {
                                                "lo": 0,
                                                "hi": 0
                                            }
                                        },
                                        "id": 5,
                                        "args": null
                                    },
                                    {
                                        "ident": {
                                            "name": "std",
                                            "span": {
                                                "lo": 0,
                                                "hi": 0
                                            }
                                        },
                                        "id": 6,
                                        "args": null
                                    },
                                    {
                                        "ident": {
                                            "name": "prelude",
                                            "span": {
                                                "lo": 0,
                                                "hi": 0
                                            }
                                        },
                                        "id": 7,
                                        "args": null
                                    },
                                    {
                                        "ident": {
                                            "name": "v1",
                                            "span": {
                                                "lo": 0,
                                                "hi": 0
                                            }
                                        },
                                        "id": 8,
                                        "args": null
                                    }
                                ],
                                "tokens": null
                            },
                            "kind": "Glob",
                            "span": {
                                "lo": 0,
                                "hi": 0
                            }
                        }
                    ]
                },
                "tokens": null
            },
            {
                "attrs": [
                    {
                        "kind": {
                            "variant": "Normal",
                            "fields": [
                                {
                                    "path": {
                                        "span": {
                                            "lo": 0,
                                            "hi": 0
                                        },
                                        "segments": [
                                            {
                                                "ident": {
                                                    "name": "macro_use",
                                                    "span": {
                                                        "lo": 0,
                                                        "hi": 0
                                                    }
                                                },
                                                "id": 9,
                                                "args": null
                                            }
                                        ],
                                        "tokens": null
                                    },
                                    "args": "Empty",
                                    "tokens": null
                                },
                                null
                            ]
                        },
                        "id": null,
                        "style": "Outer",
                        "span": {
                            "lo": 0,
                            "hi": 0
                        }
                    }
                ],
                "id": 10,
                "span": {
                    "lo": 0,
                    "hi": 0
                },
                "vis": {
                    "kind": "Inherited",
                    "span": {
                        "lo": 0,
                        "hi": 0
                    },
                    "tokens": null
                },
                "ident": {
                    "name": "std",
                    "span": {
                        "lo": 0,
                        "hi": 0
                    }
                },
                "kind": {
                    "variant": "ExternCrate",
                    "fields": [
                        null
                    ]
                },
                "tokens": null
            },
            {
                "attrs": [],
                "id": 11,
                "span": {
                    "lo": 0,
                    "hi": 14
                },
                "vis": {
                    "kind": "Inherited",
                    "span": {
                        "lo": 0,
                        "hi": 0
                    },
                    "tokens": null
                },
                "ident": {
                    "name": "Struct",
                    "span": {
                        "lo": 7,
                        "hi": 13
                    }
                },
                "kind": {
                    "variant": "Struct",
                    "fields": [
                        {
                            "variant": "Unit",
                            "fields": [
                                12
                            ]
                        },
                        {
                            "params": [],
                            "where_clause": {
                                "has_where_token": false,
                                "predicates": [],
                                "span": {
                                    "lo": 13,
                                    "hi": 13
                                }
                            },
                            "span": {
                                "lo": 13,
                                "hi": 13
                            }
                        }
                    ]
                },
                "tokens": null
            },
            {
                "attrs": [],
                "id": 13,
                "span": {
                    "lo": 15,
                    "hi": 29
                },
                "vis": {
                    "kind": "Inherited",
                    "span": {
                        "lo": 15,
                        "hi": 15
                    },
                    "tokens": null
                },
                "ident": {
                    "name": "Trait",
                    "span": {
                        "lo": 21,
                        "hi": 26
                    }
                },
                "kind": {
                    "variant": "Trait",
                    "fields": [
                        "No",
                        "No",
                        {
                            "params": [],
                            "where_clause": {
                                "has_where_token": false,
                                "predicates": [],
                                "span": {
                                    "lo": 26,
                                    "hi": 26
                                }
                            },
                            "span": {
                                "lo": 26,
                                "hi": 26
                            }
                        },
                        [],
                        []
                    ]
                },
                "tokens": null
            },
            {
                "attrs": [],
                "id": 14,
                "span": {
                    "lo": 31,
                    "hi": 118
                },
                "vis": {
                    "kind": "Inherited",
                    "span": {
                        "lo": 31,
                        "hi": 31
                    },
                    "tokens": null
                },
                "ident": {
                    "name": "impl_trait_for",
                    "span": {
                        "lo": 44,
                        "hi": 58
                    }
                },
                "kind": {
                    "variant": "MacroDef",
                    "fields": [
                        {
                            "body": {
                                "variant": "Delimited",
                                "fields": [
                                    {
                                        "open": {
                                            "lo": 59,
                                            "hi": 60
                                        },
                                        "close": {
                                            "lo": 117,
                                            "hi": 118
                                        }
                                    },
                                    "Brace",
                                    {
                                        "0": [
                                            [
                                                {
                                                    "variant": "Delimited",
                                                    "fields": [
                                                        {
                                                            "open": {
                                                                "lo": 65,
                                                                "hi": 66
                                                            },
                                                            "close": {
                                                                "lo": 74,
                                                                "hi": 75
                                                            }
                                                        },
                                                        "Paren",
                                                        {
                                                            "0": [
                                                                [
                                                                    {
                                                                        "variant": "Token",
                                                                        "fields": [
                                                                            {
                                                                                "kind": "Dollar",
                                                                                "span": {
                                                                                    "lo": 66,
                                                                                    "hi": 67
                                                                                }
                                                                            }
                                                                        ]
                                                                    },
                                                                    "Alone"
                                                                ],
                                                                [
                                                                    {
                                                                        "variant": "Token",
                                                                        "fields": [
                                                                            {
                                                                                "kind": {
                                                                                    "variant": "Ident",
                                                                                    "fields": [
                                                                                        "s",
                                                                                        false
                                                                                    ]
                                                                                },
                                                                                "span": {
                                                                                    "lo": 67,
                                                                                    "hi": 68
                                                                                }
                                                                            }
                                                                        ]
                                                                    },
                                                                    "Joint"
                                                                ],
                                                                [
                                                                    {
                                                                        "variant": "Token",
                                                                        "fields": [
                                                                            {
                                                                                "kind": "Colon",
                                                                                "span": {
                                                                                    "lo": 68,
                                                                                    "hi": 69
                                                                                }
                                                                            }
                                                                        ]
                                                                    },
                                                                    "Alone"
                                                                ],
                                                                [
                                                                    {
                                                                        "variant": "Token",
                                                                        "fields": [
                                                                            {
                                                                                "kind": {
                                                                                    "variant": "Ident",
                                                                                    "fields": [
                                                                                        "ident",
                                                                                        false
                                                                                    ]
                                                                                },
                                                                                "span": {
                                                                                    "lo": 69,
                                                                                    "hi": 74
                                                                                }
                                                                            }
                                                                        ]
                                                                    },
                                                                    "Alone"
                                                                ]
                                                            ]
                                                        }
                                                    ]
                                                },
                                                "Alone"
                                            ],
                                            [
                                                {
                                                    "variant": "Token",
                                                    "fields": [
                                                        {
                                                            "kind": "FatArrow",
                                                            "span": {
                                                                "lo": 76,
                                                                "hi": 78
                                                            }
                                                        }
                                                    ]
                                                },
                                                "Alone"
                                            ],
                                            [
                                                {
                                                    "variant": "Delimited",
                                                    "fields": [
                                                        {
                                                            "open": {
                                                                "lo": 79,
                                                                "hi": 80
                                                            },
                                                            "close": {
                                                                "lo": 114,
                                                                "hi": 115
                                                            }
                                                        },
                                                        "Brace",
                                                        {
                                                            "0": [
                                                                [
                                                                    {
                                                                        "variant": "Token",
                                                                        "fields": [
                                                                            {
                                                                                "kind": {
                                                                                    "variant": "Ident",
                                                                                    "fields": [
                                                                                        "impl",
                                                                                        false
                                                                                    ]
                                                                                },
                                                                                "span": {
                                                                                    "lo": 89,
                                                                                    "hi": 93
                                                                                }
                                                                            }
                                                                        ]
                                                                    },
                                                                    "Alone"
                                                                ],
                                                                [
                                                                    {
                                                                        "variant": "Token",
                                                                        "fields": [
                                                                            {
                                                                                "kind": {
                                                                                    "variant": "Ident",
                                                                                    "fields": [
                                                                                        "Trait",
                                                                                        false
                                                                                    ]
                                                                                },
                                                                                "span": {
                                                                                    "lo": 94,
                                                                                    "hi": 99
                                                                                }
                                                                            }
                                                                        ]
                                                                    },
                                                                    "Alone"
                                                                ],
                                                                [
                                                                    {
                                                                        "variant": "Token",
                                                                        "fields": [
                                                                            {
                                                                                "kind": {
                                                                                    "variant": "Ident",
                                                                                    "fields": [
                                                                                        "for",
                                                                                        false
                                                                                    ]
                                                                                },
                                                                                "span": {
                                                                                    "lo": 100,
                                                                                    "hi": 103
                                                                                }
                                                                            }
                                                                        ]
                                                                    },
                                                                    "Alone"
                                                                ],
                                                                [
                                                                    {
                                                                        "variant": "Token",
                                                                        "fields": [
                                                                            {
                                                                                "kind": "Dollar",
                                                                                "span": {
                                                                                    "lo": 104,
                                                                                    "hi": 105
                                                                                }
                                                                            }
                                                                        ]
                                                                    },
                                                                    "Alone"
                                                                ],
                                                                [
                                                                    {
                                                                        "variant": "Token",
                                                                        "fields": [
                                                                            {
                                                                                "kind": {
                                                                                    "variant": "Ident",
                                                                                    "fields": [
                                                                                        "s",
                                                                                        false
                                                                                    ]
                                                                                },
                                                                                "span": {
                                                                                    "lo": 105,
                                                                                    "hi": 106
                                                                                }
                                                                            }
                                                                        ]
                                                                    },
                                                                    "Alone"
                                                                ],
                                                                [
                                                                    {
                                                                        "variant": "Delimited",
                                                                        "fields": [
                                                                            {
                                                                                "open": {
                                                                                    "lo": 107,
                                                                                    "hi": 108
                                                                                },
                                                                                "close": {
                                                                                    "lo": 108,
                                                                                    "hi": 109
                                                                                }
                                                                            },
                                                                            "Brace",
                                                                            {
                                                                                "0": []
                                                                            }
                                                                        ]
                                                                    },
                                                                    "Alone"
                                                                ]
                                                            ]
                                                        }
                                                    ]
                                                },
                                                "Alone"
                                            ],
                                            [
                                                {
                                                    "variant": "Token",
                                                    "fields": [
                                                        {
                                                            "kind": "Semi",
                                                            "span": {
                                                                "lo": 115,
                                                                "hi": 116
                                                            }
                                                        }
                                                    ]
                                                },
                                                "Alone"
                                            ]
                                        ]
                                    }
                                ]
                            },
                            "macro_rules": true
                        }
                    ]
                },
                "tokens": null
            },
            {
                "attrs": [],
                "id": 15,
                "span": {
                    "lo": 89,
                    "hi": 109
                },
                "vis": {
                    "kind": "Inherited",
                    "span": {
                        "lo": 89,
                        "hi": 89
                    },
                    "tokens": null
                },
                "ident": {
                    "name": "",
                    "span": {
                        "lo": 0,
                        "hi": 0
                    }
                },
                "kind": {
                    "variant": "Impl",
                    "fields": [
                        "No",
                        "Positive",
                        "Final",
                        "No",
                        {
                            "params": [],
                            "where_clause": {
                                "has_where_token": false,
                                "predicates": [],
                                "span": {
                                    "lo": 106,
                                    "hi": 106
                                }
                            },
                            "span": {
                                "lo": 93,
                                "hi": 93
                            }
                        },
                        {
                            "path": {
                                "span": {
                                    "lo": 94,
                                    "hi": 99
                                },
                                "segments": [
                                    {
                                        "ident": {
                                            "name": "Trait",
                                            "span": {
                                                "lo": 94,
                                                "hi": 99
                                            }
                                        },
                                        "id": 16,
                                        "args": null
                                    }
                                ],
                                "tokens": null
                            },
                            "ref_id": 17
                        },
                        {
                            "id": 18,
                            "kind": {
                                "variant": "Path",
                                "fields": [
                                    null,
                                    {
                                        "span": {
                                            "lo": 104,
                                            "hi": 106
                                        },
                                        "segments": [
                                            {
                                                "ident": {
                                                    "name": "Struct",
                                                    "span": {
                                                        "lo": 136,
                                                        "hi": 142
                                                    }
                                                },
                                                "id": 19,
                                                "args": null
                                            }
                                        ],
                                        "tokens": null
                                    }
                                ]
                            },
                            "span": {
                                "lo": 104,
                                "hi": 106
                            },
                            "tokens": null
                        },
                        []
                    ]
                },
                "tokens": null
            }
        ],
        "inline": true
    },
    "attrs": [],
    "span": {
        "lo": 0,
        "hi": 144
    },
    "proc_macros": []
}

@jyn514
Copy link
Member

jyn514 commented Jun 6, 2022

The ast-json flag was removed in #85993.

@jyn514 jyn514 closed this as not planned Won't fix, can't repro, duplicate, stale Jun 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

4 participants