The trait `TyEncoder` is not implemented for `FileEncoder`

After reading Serialization in Rustc - Rust Compiler Development Guide, I attempted to write code for serializing/deserializing the MIR body by referring to https://github.com/rust-lang/rust/blob/master/compiler/rustc_serialize/tests/opaque.rs.

use crate::MirPass;
use rustc_middle::mir::*;
use rustc_middle::ty::TyCtxt;
use rustc_serialize::opaque::{FileEncoder, MemDecoder};
use rustc_serialize::{Decodable, Encodable};
use std::fs;

pub struct MirSerDe;

impl<'tcx> MirPass<'tcx> for MirSerDe {
    fn run_pass(&self, _tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
        let tmpfile = tempfile::NamedTempFile::new().unwrap();
        let tmpfile = tmpfile.path();

        // Serialize
        let mut encoder = FileEncoder::new(&tmpfile).unwrap();
        Encodable::encode(body, &mut encoder);
        encoder.finish().unwrap();

        // Deserialize
        let data = fs::read(&tmpfile).unwrap();
        let mut decoder = MemDecoder::new(&data[..], 0);
        let decoded = Decodable::decode(&mut decoder);

        assert_eq!(body, decoded);
    }
}

Result:

$ ./x.py build --stage 1 --keep-stage 1 library
Building bootstrap
    Finished dev [unoptimized] target(s) in 0.02s
Building stage0 library artifacts (x86_64-unknown-linux-gnu)
    Finished release [optimized] target(s) in 0.06s
Building compiler artifacts (stage0 -> stage1, x86_64-unknown-linux-gnu)
   Compiling rustc_mir_transform v0.0.0 (/home/user/rust/compiler/rustc_mir_transform)
error[E0277]: the trait bound `FileEncoder: TyEncoder` is not satisfied
  --> compiler/rustc_mir_transform/src/mir_serde.rs:17:33
   |
17 |         Encodable::encode(body, &mut encoder);
   |         -----------------       ^^^^^^^^^^^^ the trait `TyEncoder` is not implemented for `FileEncoder`
   |         |
   |         required by a bound introduced by this call
   |
   = help: the trait `TyEncoder` is implemented for `CacheEncoder<'a, 'tcx>`
   = note: required for `rustc_middle::mir::Body<'tcx>` to implement `Encodable<FileEncoder>`

error[E0369]: binary operation `==` cannot be applied to type `&mut rustc_middle::mir::Body<'tcx>`
  --> /home/user/rust/library/core/src/macros/mod.rs:40:32
   |
36 | macro_rules! assert_eq {
   | ---------------------- in this expansion of `assert_eq!`
...
40 |                 if !(*left_val == *right_val) {
   |                      --------- ^^ ---------- _
   |                      |
   |                      &mut rustc_middle::mir::Body<'tcx>
   |
  ::: compiler/rustc_mir_transform/src/mir_serde.rs:25:9
   |
25 |         assert_eq!(body, decoded);
   |         ------------------------- in this macro invocation

Some errors have detailed explanations: E0277, E0369.
For more information about an error, try `rustc --explain E0277`.
error: could not compile `rustc_mir_transform` (lib) due to 2 previous errors
Build completed unsuccessfully in 0:00:01

Can you help me with how I should modify it?

Or is there another way to implement the logic described above?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.