Skip to content

Commit 7bdbf8e

Browse files
committed
rustc_public: Add tests for some Serialize impls
1 parent 3716afa commit 7bdbf8e

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4440,6 +4440,7 @@ dependencies = [
44404440
"rustc_target",
44414441
"scoped-tls",
44424442
"serde",
4443+
"serde_json",
44434444
"tracing",
44444445
]
44454446

compiler/rustc_public/Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,15 @@ rustc_session = { path = "../rustc_session" }
1313
rustc_span = { path = "../rustc_span" }
1414
rustc_target = { path = "../rustc_target" }
1515
scoped-tls = "1.0"
16-
serde = { version = "1.0.125", features = [ "derive" ] }
16+
serde = { version = "1.0.125", features = ["derive"] }
1717
tracing = "0.1"
1818
# tidy-alphabetical-end
1919

20+
[dev-dependencies]
21+
# tidy-alphabetical-start
22+
serde_json = "1.0.142"
23+
# tidy-alphabetical-end
24+
2025
[features]
2126
# tidy-alphabetical-start
2227
# Provides access to APIs that expose internals of the rust compiler.

compiler/rustc_public/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ pub mod compiler_interface;
5353
pub mod error;
5454
pub mod mir;
5555
pub mod target;
56+
#[cfg(test)]
57+
mod tests;
5658
pub mod ty;
5759
pub mod visitor;
5860

compiler/rustc_public/src/tests.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use rustc_public_bridge::IndexedVal;
2+
3+
use crate::abi::Layout;
4+
use crate::mir::alloc::AllocId;
5+
use crate::mir::mono::InstanceDef;
6+
use crate::ty::{MirConstId, TyConstId, VariantIdx};
7+
use crate::{CrateNum, DefId, Span, ThreadLocalIndex, Ty};
8+
9+
#[track_caller]
10+
fn check_serialize<T: serde::Serialize>(value: T, expected_json: &str) {
11+
let got_json = serde_json::to_string(&value).unwrap();
12+
assert_eq!(got_json, expected_json, "didn't get expected json for serializing");
13+
}
14+
15+
#[test]
16+
fn serialize_cratenum() {
17+
check_serialize(CrateNum(1, ThreadLocalIndex), "1");
18+
}
19+
20+
#[test]
21+
fn serialize_defid() {
22+
check_serialize(DefId::to_val(2), "2");
23+
}
24+
25+
#[test]
26+
fn serialize_layout() {
27+
check_serialize(Layout::to_val(3), "3");
28+
}
29+
30+
#[test]
31+
fn serialize_allocid() {
32+
check_serialize(AllocId::to_val(4), "4");
33+
}
34+
35+
#[test]
36+
fn serialize_ty() {
37+
check_serialize(Ty::to_val(5), "5");
38+
}
39+
40+
#[test]
41+
fn serialize_tyconstid() {
42+
check_serialize(TyConstId::to_val(6), "6");
43+
}
44+
45+
#[test]
46+
fn serialize_mirconstid() {
47+
check_serialize(MirConstId::to_val(7), "7");
48+
}
49+
50+
#[test]
51+
fn serialize_span() {
52+
check_serialize(Span::to_val(8), "8");
53+
}
54+
55+
#[test]
56+
fn serialize_variantidx() {
57+
check_serialize(VariantIdx::to_val(9), "9");
58+
}
59+
60+
#[test]
61+
fn serialize_instancedef() {
62+
check_serialize(InstanceDef::to_val(10), "10");
63+
}

0 commit comments

Comments
 (0)