Skip to content

Badly behaved Encode and Decode impls for FileMap #42374

Closed
@dtolnay

Description

@dtolnay

The impls added in #22235 violate some assumptions in libserialize by trying to serialize multiple values for the "lines" field. When decoding, it ends up hitting this unwrap.

#![feature(rustc_private)]

extern crate syntax_pos;
extern crate serialize;

use std::cell::RefCell;
use syntax_pos::{FileMap, BytePos};
use serialize::json;

fn main() {
    let file_map = FileMap {
        name: "rustc".to_owned(),
        name_was_remapped: false,
        crate_of_origin: 0,
        src: None,
        start_pos: BytePos(0),
        end_pos: BytePos(0),
        lines: RefCell::new(vec![BytePos(1), BytePos(2), BytePos(3)]),
        multibyte_chars: RefCell::new(vec![]),
    };

    let j = json::as_pretty_json(&file_map).to_string();
    println!("{}", j);

    // panic in libserialize
    let _ = json::decode::<FileMap>(&j);
}
{
  "name": "rustc",
  "name_was_remapped": false,
  "start_pos": 0,
  "end_pos": 0,
  "lines": 31111,
  "multibyte_chars": []
}
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', /checkout/src/libcore/option.rs:335

This may not be important if it has worked for this long, but there may be other variations of this bug lurking elsewhere.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions