Skip to content

Commit c9f1501

Browse files
committed
Auto merge of #32883 - sanxiyn:nameless-defkey, r=arielb1
Do not encode name when encoding DefKey Since name is encoded anyway, name in DefKey is redundant. cc #32719.
2 parents 374d262 + 2fd1f3e commit c9f1501

File tree

5 files changed

+132
-22
lines changed

5 files changed

+132
-22
lines changed

src/librustc_metadata/astencode.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ use std::fmt::Debug;
4949
use rbml::reader;
5050
use rbml::writer::Encoder;
5151
use rbml;
52-
use serialize;
53-
use serialize::{Decodable, Decoder, DecoderHelpers, Encodable};
54-
use serialize::EncoderHelpers;
52+
use rustc_serialize as serialize;
53+
use rustc_serialize::{Decodable, Decoder, DecoderHelpers};
54+
use rustc_serialize::{Encodable, EncoderHelpers};
5555

5656
#[cfg(test)] use std::io::Cursor;
5757
#[cfg(test)] use syntax::parse;
@@ -445,7 +445,7 @@ fn encode_method_callee<'a, 'tcx>(ecx: &e::EncodeContext<'a, 'tcx>,
445445
rbml_w: &mut Encoder,
446446
autoderef: u32,
447447
method: &ty::MethodCallee<'tcx>) {
448-
use serialize::Encoder;
448+
use rustc_serialize::Encoder;
449449

450450
rbml_w.emit_struct("MethodCallee", 4, |rbml_w| {
451451
rbml_w.emit_struct_field("autoderef", 0, |rbml_w| {
@@ -561,7 +561,7 @@ impl<'a, 'tcx> rbml_writer_helpers<'tcx> for Encoder<'a> {
561561
}
562562

563563
fn emit_upvar_capture(&mut self, ecx: &e::EncodeContext, capture: &ty::UpvarCapture) {
564-
use serialize::Encoder;
564+
use rustc_serialize::Encoder;
565565

566566
self.emit_enum("UpvarCapture", |this| {
567567
match *capture {
@@ -589,7 +589,7 @@ impl<'a, 'tcx> rbml_writer_helpers<'tcx> for Encoder<'a> {
589589

590590
fn emit_auto_adjustment<'b>(&mut self, ecx: &e::EncodeContext<'b, 'tcx>,
591591
adj: &adjustment::AutoAdjustment<'tcx>) {
592-
use serialize::Encoder;
592+
use rustc_serialize::Encoder;
593593

594594
self.emit_enum("AutoAdjustment", |this| {
595595
match *adj {
@@ -621,7 +621,7 @@ impl<'a, 'tcx> rbml_writer_helpers<'tcx> for Encoder<'a> {
621621

622622
fn emit_autoref<'b>(&mut self, ecx: &e::EncodeContext<'b, 'tcx>,
623623
autoref: &adjustment::AutoRef<'tcx>) {
624-
use serialize::Encoder;
624+
use rustc_serialize::Encoder;
625625

626626
self.emit_enum("AutoRef", |this| {
627627
match autoref {
@@ -643,7 +643,7 @@ impl<'a, 'tcx> rbml_writer_helpers<'tcx> for Encoder<'a> {
643643

644644
fn emit_auto_deref_ref<'b>(&mut self, ecx: &e::EncodeContext<'b, 'tcx>,
645645
auto_deref_ref: &adjustment::AutoDerefRef<'tcx>) {
646-
use serialize::Encoder;
646+
use rustc_serialize::Encoder;
647647

648648
self.emit_struct("AutoDerefRef", 2, |this| {
649649
this.emit_struct_field("autoderefs", 0, |this| auto_deref_ref.autoderefs.encode(this));

src/librustc_metadata/decoder.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use self::Family::*;
1717
use astencode::decode_inlined_item;
1818
use cstore::{self, crate_metadata};
1919
use common::*;
20+
use def_key;
2021
use encoder::def_to_u64;
2122
use index;
2223
use tls_context;
@@ -49,7 +50,7 @@ use std::str;
4950

5051
use rbml::reader;
5152
use rbml;
52-
use serialize::Decodable;
53+
use rustc_serialize::Decodable;
5354
use syntax::attr;
5455
use syntax::parse::token::{self, IdentInterner};
5556
use syntax::ast;
@@ -1739,7 +1740,11 @@ pub fn def_key(cdata: Cmd, id: DefIndex) -> hir_map::DefKey {
17391740
match reader::maybe_get_doc(item_doc, tag_def_key) {
17401741
Some(def_key_doc) => {
17411742
let mut decoder = reader::Decoder::new(def_key_doc);
1742-
hir_map::DefKey::decode(&mut decoder).unwrap()
1743+
let simple_key = def_key::DefKey::decode(&mut decoder).unwrap();
1744+
let name = reader::maybe_get_doc(item_doc, tag_paths_data_name).map(|name| {
1745+
token::intern(name.as_str_slice())
1746+
});
1747+
def_key::recover_def_key(simple_key, name)
17431748
}
17441749
None => {
17451750
bug!("failed to find block with tag {:?} for item with family {:?}",

src/librustc_metadata/def_key.rs

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use rustc::hir::def_id::DefIndex;
12+
use rustc::hir::map as hir_map;
13+
use syntax::ast::Name;
14+
15+
#[derive(RustcEncodable, RustcDecodable)]
16+
pub struct DefKey {
17+
pub parent: Option<DefIndex>,
18+
pub disambiguated_data: DisambiguatedDefPathData,
19+
}
20+
21+
#[derive(RustcEncodable, RustcDecodable)]
22+
pub struct DisambiguatedDefPathData {
23+
pub data: DefPathData,
24+
pub disambiguator: u32,
25+
}
26+
27+
#[derive(RustcEncodable, RustcDecodable)]
28+
pub enum DefPathData {
29+
CrateRoot,
30+
Misc,
31+
Impl,
32+
TypeNs,
33+
ValueNs,
34+
MacroDef,
35+
ClosureExpr,
36+
TypeParam,
37+
LifetimeDef,
38+
EnumVariant,
39+
Field,
40+
StructCtor,
41+
Initializer,
42+
Binding,
43+
}
44+
45+
pub fn simplify_def_key(key: hir_map::DefKey) -> DefKey {
46+
let data = DisambiguatedDefPathData {
47+
data: simplify_def_path_data(key.disambiguated_data.data),
48+
disambiguator: key.disambiguated_data.disambiguator,
49+
};
50+
DefKey {
51+
parent: key.parent,
52+
disambiguated_data: data,
53+
}
54+
}
55+
56+
fn simplify_def_path_data(data: hir_map::DefPathData) -> DefPathData {
57+
match data {
58+
hir_map::DefPathData::CrateRoot => DefPathData::CrateRoot,
59+
hir_map::DefPathData::InlinedRoot(_) => bug!("unexpected DefPathData"),
60+
hir_map::DefPathData::Misc => DefPathData::Misc,
61+
hir_map::DefPathData::Impl => DefPathData::Impl,
62+
hir_map::DefPathData::TypeNs(_) => DefPathData::TypeNs,
63+
hir_map::DefPathData::ValueNs(_) => DefPathData::ValueNs,
64+
hir_map::DefPathData::MacroDef(_) => DefPathData::MacroDef,
65+
hir_map::DefPathData::ClosureExpr => DefPathData::ClosureExpr,
66+
hir_map::DefPathData::TypeParam(_) => DefPathData::TypeParam,
67+
hir_map::DefPathData::LifetimeDef(_) => DefPathData::LifetimeDef,
68+
hir_map::DefPathData::EnumVariant(_) => DefPathData::EnumVariant,
69+
hir_map::DefPathData::Field(_) => DefPathData::Field,
70+
hir_map::DefPathData::StructCtor => DefPathData::StructCtor,
71+
hir_map::DefPathData::Initializer => DefPathData::Initializer,
72+
hir_map::DefPathData::Binding(_) => DefPathData::Binding,
73+
}
74+
}
75+
76+
pub fn recover_def_key(key: DefKey, name: Option<Name>) -> hir_map::DefKey {
77+
let data = hir_map::DisambiguatedDefPathData {
78+
data: recover_def_path_data(key.disambiguated_data.data, name),
79+
disambiguator: key.disambiguated_data.disambiguator,
80+
};
81+
hir_map::DefKey {
82+
parent: key.parent,
83+
disambiguated_data: data,
84+
}
85+
}
86+
87+
fn recover_def_path_data(data: DefPathData, name: Option<Name>) -> hir_map::DefPathData {
88+
match data {
89+
DefPathData::CrateRoot => hir_map::DefPathData::CrateRoot,
90+
DefPathData::Misc => hir_map::DefPathData::Misc,
91+
DefPathData::Impl => hir_map::DefPathData::Impl,
92+
DefPathData::TypeNs => hir_map::DefPathData::TypeNs(name.unwrap()),
93+
DefPathData::ValueNs => hir_map::DefPathData::ValueNs(name.unwrap()),
94+
DefPathData::MacroDef => hir_map::DefPathData::MacroDef(name.unwrap()),
95+
DefPathData::ClosureExpr => hir_map::DefPathData::ClosureExpr,
96+
DefPathData::TypeParam => hir_map::DefPathData::TypeParam(name.unwrap()),
97+
DefPathData::LifetimeDef => hir_map::DefPathData::LifetimeDef(name.unwrap()),
98+
DefPathData::EnumVariant => hir_map::DefPathData::EnumVariant(name.unwrap()),
99+
DefPathData::Field => hir_map::DefPathData::Field(name.unwrap()),
100+
DefPathData::StructCtor => hir_map::DefPathData::StructCtor,
101+
DefPathData::Initializer => hir_map::DefPathData::Initializer,
102+
DefPathData::Binding => hir_map::DefPathData::Binding(name.unwrap()),
103+
}
104+
}

src/librustc_metadata/encoder.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use astencode::encode_inlined_item;
1717
use common::*;
1818
use cstore;
1919
use decoder;
20+
use def_key;
2021
use tyencode;
2122
use index::{self, IndexData};
2223

@@ -35,7 +36,7 @@ use rustc::mir::mir_map::MirMap;
3536
use rustc::session::config;
3637
use rustc::util::nodemap::{FnvHashMap, NodeMap, NodeSet};
3738

38-
use serialize::Encodable;
39+
use rustc_serialize::Encodable;
3940
use std::cell::RefCell;
4041
use std::io::prelude::*;
4142
use std::io::{Cursor, SeekFrom};
@@ -53,6 +54,7 @@ use rbml::writer::Encoder;
5354
use rustc::hir::{self, PatKind};
5455
use rustc::hir::intravisit::Visitor;
5556
use rustc::hir::intravisit;
57+
use rustc::hir::map::DefKey;
5658

5759
pub struct EncodeContext<'a, 'tcx: 'a> {
5860
pub diag: &'a Handler,
@@ -101,24 +103,22 @@ fn encode_def_id(rbml_w: &mut Encoder, id: DefId) {
101103
rbml_w.wr_tagged_u64(tag_def_id, def_to_u64(id));
102104
}
103105

106+
fn encode_def_key(rbml_w: &mut Encoder, key: DefKey) {
107+
let simple_key = def_key::simplify_def_key(key);
108+
rbml_w.start_tag(tag_def_key);
109+
simple_key.encode(rbml_w);
110+
rbml_w.end_tag();
111+
}
112+
104113
/// For every DefId that we create a metadata item for, we include a
105114
/// serialized copy of its DefKey, which allows us to recreate a path.
106115
fn encode_def_id_and_key(ecx: &EncodeContext,
107116
rbml_w: &mut Encoder,
108117
def_id: DefId)
109118
{
110119
encode_def_id(rbml_w, def_id);
111-
encode_def_key(ecx, rbml_w, def_id);
112-
}
113-
114-
fn encode_def_key(ecx: &EncodeContext,
115-
rbml_w: &mut Encoder,
116-
def_id: DefId)
117-
{
118-
rbml_w.start_tag(tag_def_key);
119120
let def_key = ecx.tcx.map.def_key(def_id);
120-
def_key.encode(rbml_w);
121-
rbml_w.end_tag();
121+
encode_def_key(rbml_w, def_key);
122122
}
123123

124124
fn encode_trait_ref<'a, 'tcx>(rbml_w: &mut Encoder,

src/librustc_metadata/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
extern crate flate;
3333
extern crate rbml;
34-
extern crate serialize;
34+
extern crate serialize as rustc_serialize; // used by deriving
3535

3636
#[macro_use]
3737
extern crate rustc;
@@ -48,6 +48,7 @@ pub mod diagnostics;
4848

4949
pub mod astencode;
5050
pub mod common;
51+
pub mod def_key;
5152
pub mod tyencode;
5253
pub mod tydecode;
5354
pub mod encoder;

0 commit comments

Comments
 (0)