Skip to content

Commit c422372

Browse files
committed
Auto merge of #63853 - matthewjasper:test-ast-serialization, r=estebank
Add default serialization for `Ident`s Also add tests for `-Zast-json` and `-Zast-json-noexpand` closes #63728
2 parents ac21131 + 3d71803 commit c422372

File tree

5 files changed

+96
-4
lines changed

5 files changed

+96
-4
lines changed

src/librustc/ty/query/on_disk_cache.rs

+23-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc_data_structures::thin_vec::ThinVec;
2020
use rustc_data_structures::sync::{Lrc, Lock, HashMapExt, Once};
2121
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
2222
use std::mem;
23-
use syntax::ast::NodeId;
23+
use syntax::ast::{Ident, NodeId};
2424
use syntax::source_map::{SourceMap, StableSourceFileId};
2525
use syntax_pos::{BytePos, Span, DUMMY_SP, SourceFile};
2626
use syntax_pos::hygiene::{ExpnId, SyntaxContext};
@@ -591,7 +591,8 @@ impl<'a, 'tcx> SpecializedDecoder<Span> for CacheDecoder<'a, 'tcx> {
591591
// FIXME(mw): This method does not restore `ExpnData::parent` or
592592
// `SyntaxContextData::prev_ctxt` or `SyntaxContextData::opaque`. These things
593593
// don't seem to be used after HIR lowering, so everything should be fine
594-
// as long as incremental compilation does not kick in before that.
594+
// until we want incremental compilation to serialize Spans that we need
595+
// full hygiene information for.
595596
let location = || Span::with_root_ctxt(lo, hi);
596597
let recover_from_expn_data = |this: &Self, expn_data, transparency, pos| {
597598
let span = location().fresh_expansion_with_transparency(expn_data, transparency);
@@ -626,6 +627,13 @@ impl<'a, 'tcx> SpecializedDecoder<Span> for CacheDecoder<'a, 'tcx> {
626627
}
627628
}
628629

630+
impl<'a, 'tcx> SpecializedDecoder<Ident> for CacheDecoder<'a, 'tcx> {
631+
fn specialized_decode(&mut self) -> Result<Ident, Self::Error> {
632+
// FIXME: Handle hygiene in incremental
633+
bug!("Trying to decode Ident for incremental");
634+
}
635+
}
636+
629637
// This impl makes sure that we get a runtime error when we try decode a
630638
// DefIndex that is not contained in a DefId. Such a case would be problematic
631639
// because we would not know how to transform the DefIndex to the current
@@ -833,6 +841,19 @@ where
833841
}
834842
}
835843

844+
impl<'a, 'tcx, E> SpecializedEncoder<Ident> for CacheEncoder<'a, 'tcx, E>
845+
where
846+
E: 'a + ty_codec::TyEncoder,
847+
{
848+
fn specialized_encode(&mut self, _: &Ident) -> Result<(), Self::Error> {
849+
// We don't currently encode enough information to ensure hygiene works
850+
// with incremental, so panic rather than risk incremental bugs.
851+
852+
// FIXME: Handle hygiene in incremental
853+
bug!("Trying to encode Ident for incremental")
854+
}
855+
}
856+
836857
impl<'a, 'tcx, E> ty_codec::TyEncoder for CacheEncoder<'a, 'tcx, E>
837858
where
838859
E: 'a + ty_codec::TyEncoder,

src/libsyntax_pos/symbol.rs

+22-2
Original file line numberDiff line numberDiff line change
@@ -849,9 +849,29 @@ impl fmt::Display for Ident {
849849
}
850850
}
851851

852-
impl UseSpecializedEncodable for Ident {}
852+
impl UseSpecializedEncodable for Ident {
853+
fn default_encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
854+
s.emit_struct("Ident", 2, |s| {
855+
s.emit_struct_field("name", 0, |s| {
856+
self.name.encode(s)
857+
})?;
858+
s.emit_struct_field("span", 1, |s| {
859+
self.span.encode(s)
860+
})
861+
})
862+
}
863+
}
853864

854-
impl UseSpecializedDecodable for Ident {}
865+
impl UseSpecializedDecodable for Ident {
866+
fn default_decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> {
867+
d.read_struct("Ident", 2, |d| {
868+
Ok(Ident {
869+
name: d.read_struct_field("name", 0, Decodable::decode)?,
870+
span: d.read_struct_field("span", 1, Decodable::decode)?,
871+
})
872+
})
873+
}
874+
}
855875

856876
/// A symbol is an interned or gensymed string. A gensym is a symbol that is
857877
/// never equal to any other symbol.

src/test/ui/ast-json/ast-json-ice.rs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Test that AST json serialization doesn't ICE (#63728).
2+
3+
// revisions: expand noexpand
4+
5+
//[expand] compile-flags: -Zast-json
6+
//[noexpand] compile-flags: -Zast-json-noexpand
7+
8+
// check-pass
9+
// dont-check-compiler-stdout - don't check for any AST change.
10+
11+
#![feature(asm)]
12+
13+
enum V {
14+
A(i32),
15+
B { f: [i64; 3 + 4] }
16+
}
17+
18+
trait X {
19+
type Output;
20+
fn read(&self) -> Self::Output;
21+
fn write(&mut self, _: Self::Output);
22+
}
23+
24+
macro_rules! call_println {
25+
($y:ident) => { println!("{}", $y) }
26+
}
27+
28+
fn main() {
29+
#[cfg(any(target_arch = "x86",
30+
target_arch = "x86_64",
31+
target_arch = "arm",
32+
target_arch = "aarch64"))]
33+
unsafe { asm!(""::::); }
34+
35+
let x: (i32) = 35;
36+
let y = x as i64<> + 5;
37+
38+
call_println!(y);
39+
40+
struct A;
41+
}
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Check that AST json printing works.
2+
3+
// check-pass
4+
// compile-flags: -Zast-json-noexpand
5+
// normalize-stdout-test ":\d+" -> ":0"
6+
7+
// Only include a single item to reduce how often the test output needs
8+
// updating.
9+
extern crate core;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"module":{"inner":{"lo":0,"hi":0},"items":[{"ident":{"name":"core","span":{"lo":0,"hi":0}},"attrs":[],"id":0,"node":{"variant":"ExternCrate","fields":[null]},"vis":{"node":"Inherited","span":{"lo":0,"hi":0}},"span":{"lo":0,"hi":0},"tokens":[{"variant":"Token","fields":[{"kind":{"variant":"Ident","fields":["extern",false]},"span":{"lo":0,"hi":0}}]},{"variant":"Token","fields":[{"kind":{"variant":"Ident","fields":["crate",false]},"span":{"lo":0,"hi":0}}]},{"variant":"Token","fields":[{"kind":{"variant":"Ident","fields":["core",false]},"span":{"lo":0,"hi":0}}]},{"variant":"Token","fields":[{"kind":"Semi","span":{"lo":0,"hi":0}}]}]}],"inline":true},"attrs":[],"span":{"lo":0,"hi":0}}

0 commit comments

Comments
 (0)