Skip to content

Commit 102eaa5

Browse files
incr.comp.: Always require Session when decoding Spans (as to avoid silently wrong results).
1 parent a174951 commit 102eaa5

File tree

7 files changed

+71
-57
lines changed

7 files changed

+71
-57
lines changed

src/librustc/hir/lowering.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,10 @@ impl<'a> LoweringContext<'a> {
838838
return n;
839839
}
840840
assert!(!def_id.is_local());
841-
let n = self.cstore.item_generics_cloned_untracked(def_id).regions.len();
841+
let n = self.cstore
842+
.item_generics_cloned_untracked(def_id, self.sess)
843+
.regions
844+
.len();
842845
self.type_def_lifetime_params.insert(def_id, n);
843846
n
844847
});

src/librustc/middle/cstore.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ pub trait CrateStore {
273273
fn item_children_untracked(&self, did: DefId, sess: &Session) -> Vec<def::Export>;
274274
fn load_macro_untracked(&self, did: DefId, sess: &Session) -> LoadedMacro;
275275
fn extern_mod_stmt_cnum_untracked(&self, emod_id: ast::NodeId) -> Option<CrateNum>;
276-
fn item_generics_cloned_untracked(&self, def: DefId) -> ty::Generics;
276+
fn item_generics_cloned_untracked(&self, def: DefId, sess: &Session) -> ty::Generics;
277277
fn associated_item_cloned_untracked(&self, def: DefId) -> ty::AssociatedItem;
278278
fn postorder_cnums_untracked(&self) -> Vec<CrateNum>;
279279

@@ -327,7 +327,7 @@ impl CrateStore for DummyCrateStore {
327327
{ bug!("crate_data_as_rc_any") }
328328
// item info
329329
fn visibility_untracked(&self, def: DefId) -> ty::Visibility { bug!("visibility") }
330-
fn item_generics_cloned_untracked(&self, def: DefId) -> ty::Generics
330+
fn item_generics_cloned_untracked(&self, def: DefId, sess: &Session) -> ty::Generics
331331
{ bug!("item_generics_cloned") }
332332

333333
// trait/impl-item info

src/librustc/middle/resolve_lifetime.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1001,8 +1001,12 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
10011001
&map.object_lifetime_defaults[&id]
10021002
} else {
10031003
let cstore = self.cstore;
1004+
let sess = self.sess;
10041005
self.xcrate_object_lifetime_defaults.entry(def_id).or_insert_with(|| {
1005-
cstore.item_generics_cloned_untracked(def_id).types.into_iter().map(|def| {
1006+
cstore.item_generics_cloned_untracked(def_id, sess)
1007+
.types
1008+
.into_iter()
1009+
.map(|def| {
10061010
def.object_lifetime_default
10071011
}).collect()
10081012
})

src/librustc_metadata/creader.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -685,14 +685,15 @@ impl<'a> CrateLoader<'a> {
685685
let mut needs_panic_runtime = attr::contains_name(&krate.attrs,
686686
"needs_panic_runtime");
687687

688+
let sess = self.sess;
688689
self.cstore.iter_crate_data(|cnum, data| {
689690
needs_panic_runtime = needs_panic_runtime ||
690-
data.needs_panic_runtime();
691-
if data.is_panic_runtime() {
691+
data.needs_panic_runtime(sess);
692+
if data.is_panic_runtime(sess) {
692693
// Inject a dependency from all #![needs_panic_runtime] to this
693694
// #![panic_runtime] crate.
694695
self.inject_dependency_if(cnum, "a panic runtime",
695-
&|data| data.needs_panic_runtime());
696+
&|data| data.needs_panic_runtime(sess));
696697
runtime_found = runtime_found || data.dep_kind.get() == DepKind::Explicit;
697698
}
698699
});
@@ -728,7 +729,7 @@ impl<'a> CrateLoader<'a> {
728729

729730
// Sanity check the loaded crate to ensure it is indeed a panic runtime
730731
// and the panic strategy is indeed what we thought it was.
731-
if !data.is_panic_runtime() {
732+
if !data.is_panic_runtime(self.sess) {
732733
self.sess.err(&format!("the crate `{}` is not a panic runtime",
733734
name));
734735
}
@@ -740,7 +741,7 @@ impl<'a> CrateLoader<'a> {
740741

741742
self.sess.injected_panic_runtime.set(Some(cnum));
742743
self.inject_dependency_if(cnum, "a panic runtime",
743-
&|data| data.needs_panic_runtime());
744+
&|data| data.needs_panic_runtime(self.sess));
744745
}
745746

746747
fn inject_sanitizer_runtime(&mut self) {
@@ -835,7 +836,7 @@ impl<'a> CrateLoader<'a> {
835836
PathKind::Crate, dep_kind);
836837

837838
// Sanity check the loaded crate to ensure it is indeed a sanitizer runtime
838-
if !data.is_sanitizer_runtime() {
839+
if !data.is_sanitizer_runtime(self.sess) {
839840
self.sess.err(&format!("the crate `{}` is not a sanitizer runtime",
840841
name));
841842
}
@@ -856,7 +857,7 @@ impl<'a> CrateLoader<'a> {
856857
PathKind::Crate, dep_kind);
857858

858859
// Sanity check the loaded crate to ensure it is indeed a profiler runtime
859-
if !data.is_profiler_runtime() {
860+
if !data.is_profiler_runtime(self.sess) {
860861
self.sess.err(&format!("the crate `profiler_builtins` is not \
861862
a profiler runtime"));
862863
}
@@ -875,7 +876,7 @@ impl<'a> CrateLoader<'a> {
875876
let mut needs_allocator = attr::contains_name(&krate.attrs,
876877
"needs_allocator");
877878
self.cstore.iter_crate_data(|_, data| {
878-
needs_allocator = needs_allocator || data.needs_allocator();
879+
needs_allocator = needs_allocator || data.needs_allocator(self.sess);
879880
});
880881
if !needs_allocator {
881882
return
@@ -997,7 +998,7 @@ impl<'a> CrateLoader<'a> {
997998
Some(data) => {
998999
// We have an allocator. We detect separately what kind it is, to allow for some
9991000
// flexibility in misconfiguration.
1000-
let attrs = data.get_item_attrs(CRATE_DEF_INDEX);
1001+
let attrs = data.get_item_attrs(CRATE_DEF_INDEX, self.sess);
10011002
let kind_interned = attr::first_attr_value_str_by_name(&attrs, "rustc_alloc_kind")
10021003
.map(Symbol::as_str);
10031004
let kind_str = kind_interned

src/librustc_metadata/cstore.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc::hir::def_id::{CRATE_DEF_INDEX, CrateNum, DefIndex};
1717
use rustc::hir::map::definitions::DefPathTable;
1818
use rustc::hir::svh::Svh;
1919
use rustc::middle::cstore::{DepKind, ExternCrate, MetadataLoader};
20-
use rustc::session::CrateDisambiguator;
20+
use rustc::session::{Session, CrateDisambiguator};
2121
use rustc_back::PanicStrategy;
2222
use rustc_data_structures::indexed_vec::IndexVec;
2323
use rustc::util::nodemap::{FxHashMap, FxHashSet, NodeMap};
@@ -176,8 +176,8 @@ impl CrateMetadata {
176176
self.root.disambiguator
177177
}
178178

179-
pub fn needs_allocator(&self) -> bool {
180-
let attrs = self.get_item_attrs(CRATE_DEF_INDEX);
179+
pub fn needs_allocator(&self, sess: &Session) -> bool {
180+
let attrs = self.get_item_attrs(CRATE_DEF_INDEX, sess);
181181
attr::contains_name(&attrs, "needs_allocator")
182182
}
183183

@@ -189,43 +189,43 @@ impl CrateMetadata {
189189
self.root.has_default_lib_allocator.clone()
190190
}
191191

192-
pub fn is_panic_runtime(&self) -> bool {
193-
let attrs = self.get_item_attrs(CRATE_DEF_INDEX);
192+
pub fn is_panic_runtime(&self, sess: &Session) -> bool {
193+
let attrs = self.get_item_attrs(CRATE_DEF_INDEX, sess);
194194
attr::contains_name(&attrs, "panic_runtime")
195195
}
196196

197-
pub fn needs_panic_runtime(&self) -> bool {
198-
let attrs = self.get_item_attrs(CRATE_DEF_INDEX);
197+
pub fn needs_panic_runtime(&self, sess: &Session) -> bool {
198+
let attrs = self.get_item_attrs(CRATE_DEF_INDEX, sess);
199199
attr::contains_name(&attrs, "needs_panic_runtime")
200200
}
201201

202-
pub fn is_compiler_builtins(&self) -> bool {
203-
let attrs = self.get_item_attrs(CRATE_DEF_INDEX);
202+
pub fn is_compiler_builtins(&self, sess: &Session) -> bool {
203+
let attrs = self.get_item_attrs(CRATE_DEF_INDEX, sess);
204204
attr::contains_name(&attrs, "compiler_builtins")
205205
}
206206

207-
pub fn is_sanitizer_runtime(&self) -> bool {
208-
let attrs = self.get_item_attrs(CRATE_DEF_INDEX);
207+
pub fn is_sanitizer_runtime(&self, sess: &Session) -> bool {
208+
let attrs = self.get_item_attrs(CRATE_DEF_INDEX, sess);
209209
attr::contains_name(&attrs, "sanitizer_runtime")
210210
}
211211

212-
pub fn is_profiler_runtime(&self) -> bool {
213-
let attrs = self.get_item_attrs(CRATE_DEF_INDEX);
212+
pub fn is_profiler_runtime(&self, sess: &Session) -> bool {
213+
let attrs = self.get_item_attrs(CRATE_DEF_INDEX, sess);
214214
attr::contains_name(&attrs, "profiler_runtime")
215215
}
216216

217-
pub fn is_no_builtins(&self) -> bool {
218-
let attrs = self.get_item_attrs(CRATE_DEF_INDEX);
217+
pub fn is_no_builtins(&self, sess: &Session) -> bool {
218+
let attrs = self.get_item_attrs(CRATE_DEF_INDEX, sess);
219219
attr::contains_name(&attrs, "no_builtins")
220220
}
221221

222-
pub fn has_copy_closures(&self) -> bool {
223-
let attrs = self.get_item_attrs(CRATE_DEF_INDEX);
222+
pub fn has_copy_closures(&self, sess: &Session) -> bool {
223+
let attrs = self.get_item_attrs(CRATE_DEF_INDEX, sess);
224224
attr::contains_feature_attr(&attrs, "copy_closures")
225225
}
226226

227-
pub fn has_clone_closures(&self) -> bool {
228-
let attrs = self.get_item_attrs(CRATE_DEF_INDEX);
227+
pub fn has_clone_closures(&self, sess: &Session) -> bool {
228+
let attrs = self.get_item_attrs(CRATE_DEF_INDEX, sess);
229229
attr::contains_feature_attr(&attrs, "clone_closures")
230230
}
231231

src/librustc_metadata/cstore_impl.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,13 @@ impl IntoArgs for (CrateNum, DefId) {
9999

100100
provide! { <'tcx> tcx, def_id, other, cdata,
101101
type_of => { cdata.get_type(def_id.index, tcx) }
102-
generics_of => { tcx.alloc_generics(cdata.get_generics(def_id.index)) }
102+
generics_of => {
103+
tcx.alloc_generics(cdata.get_generics(def_id.index, tcx.sess))
104+
}
103105
predicates_of => { cdata.get_predicates(def_id.index, tcx) }
104106
super_predicates_of => { cdata.get_super_predicates(def_id.index, tcx) }
105107
trait_def => {
106-
tcx.alloc_trait_def(cdata.get_trait_def(def_id.index))
108+
tcx.alloc_trait_def(cdata.get_trait_def(def_id.index, tcx.sess))
107109
}
108110
adt_def => { cdata.get_adt_def(def_id.index, tcx) }
109111
adt_destructor => {
@@ -153,7 +155,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
153155
lookup_deprecation_entry => {
154156
cdata.get_deprecation(def_id.index).map(DeprecationEntry::external)
155157
}
156-
item_attrs => { cdata.get_item_attrs(def_id.index) }
158+
item_attrs => { cdata.get_item_attrs(def_id.index, tcx.sess) }
157159
// FIXME(#38501) We've skipped a `read` on the `HirBody` of
158160
// a `fn` when encoding, so the dep-tracking wouldn't work.
159161
// This is only used by rustdoc anyway, which shouldn't have
@@ -171,14 +173,14 @@ provide! { <'tcx> tcx, def_id, other, cdata,
171173
is_mir_available => { cdata.is_item_mir_available(def_id.index) }
172174

173175
dylib_dependency_formats => { Rc::new(cdata.get_dylib_dependency_formats()) }
174-
is_panic_runtime => { cdata.is_panic_runtime() }
175-
is_compiler_builtins => { cdata.is_compiler_builtins() }
176+
is_panic_runtime => { cdata.is_panic_runtime(tcx.sess) }
177+
is_compiler_builtins => { cdata.is_compiler_builtins(tcx.sess) }
176178
has_global_allocator => { cdata.has_global_allocator() }
177-
is_sanitizer_runtime => { cdata.is_sanitizer_runtime() }
178-
is_profiler_runtime => { cdata.is_profiler_runtime() }
179+
is_sanitizer_runtime => { cdata.is_sanitizer_runtime(tcx.sess) }
180+
is_profiler_runtime => { cdata.is_profiler_runtime(tcx.sess) }
179181
panic_strategy => { cdata.panic_strategy() }
180182
extern_crate => { Rc::new(cdata.extern_crate.get()) }
181-
is_no_builtins => { cdata.is_no_builtins() }
183+
is_no_builtins => { cdata.is_no_builtins(tcx.sess) }
182184
impl_defaultness => { cdata.get_impl_defaultness(def_id.index) }
183185
exported_symbol_ids => { Rc::new(cdata.get_exported_symbols()) }
184186
native_libraries => { Rc::new(cdata.get_native_libraries()) }
@@ -237,8 +239,8 @@ provide! { <'tcx> tcx, def_id, other, cdata,
237239

238240
used_crate_source => { Rc::new(cdata.source.clone()) }
239241

240-
has_copy_closures => { cdata.has_copy_closures() }
241-
has_clone_closures => { cdata.has_clone_closures() }
242+
has_copy_closures => { cdata.has_copy_closures(tcx.sess) }
243+
has_clone_closures => { cdata.has_clone_closures(tcx.sess) }
242244
}
243245

244246
pub fn provide_local<'tcx>(providers: &mut Providers<'tcx>) {
@@ -358,8 +360,8 @@ impl CrateStore for cstore::CStore {
358360
self.get_crate_data(def.krate).get_visibility(def.index)
359361
}
360362

361-
fn item_generics_cloned_untracked(&self, def: DefId) -> ty::Generics {
362-
self.get_crate_data(def.krate).get_generics(def.index)
363+
fn item_generics_cloned_untracked(&self, def: DefId, sess: &Session) -> ty::Generics {
364+
self.get_crate_data(def.krate).get_generics(def.index, sess)
363365
}
364366

365367
fn associated_item_cloned_untracked(&self, def: DefId) -> ty::AssociatedItem
@@ -454,7 +456,7 @@ impl CrateStore for cstore::CStore {
454456
let body = filemap_to_stream(&sess.parse_sess, filemap, None);
455457

456458
// Mark the attrs as used
457-
let attrs = data.get_item_attrs(id.index);
459+
let attrs = data.get_item_attrs(id.index, sess);
458460
for attr in attrs.iter() {
459461
attr::mark_used(attr);
460462
}

src/librustc_metadata/decoder.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ impl<'a, 'tcx> SpecializedDecoder<Span> for DecodeContext<'a, 'tcx> {
291291
let sess = if let Some(sess) = self.sess {
292292
sess
293293
} else {
294-
return Ok(Span::new(lo, hi, NO_EXPANSION));
294+
bug!("Cannot decode Span without Session.")
295295
};
296296

297297
let (lo, hi) = if lo > hi {
@@ -313,7 +313,8 @@ impl<'a, 'tcx> SpecializedDecoder<Span> for DecodeContext<'a, 'tcx> {
313313
// originate from the same filemap.
314314
let last_filemap = &imported_filemaps[self.last_filemap_index];
315315

316-
if lo >= last_filemap.original_start_pos && lo <= last_filemap.original_end_pos &&
316+
if lo >= last_filemap.original_start_pos &&
317+
lo <= last_filemap.original_end_pos &&
317318
hi >= last_filemap.original_start_pos &&
318319
hi <= last_filemap.original_end_pos {
319320
last_filemap
@@ -335,8 +336,8 @@ impl<'a, 'tcx> SpecializedDecoder<Span> for DecodeContext<'a, 'tcx> {
335336
}
336337
};
337338

338-
let lo = (lo - filemap.original_start_pos) + filemap.translated_filemap.start_pos;
339-
let hi = (hi - filemap.original_start_pos) + filemap.translated_filemap.start_pos;
339+
let lo = (lo + filemap.translated_filemap.start_pos) - filemap.original_start_pos;
340+
let hi = (hi + filemap.translated_filemap.start_pos) - filemap.original_start_pos;
340341

341342
Ok(Span::new(lo, hi, NO_EXPANSION))
342343
}
@@ -521,9 +522,9 @@ impl<'a, 'tcx> CrateMetadata {
521522
}
522523
}
523524

524-
pub fn get_trait_def(&self, item_id: DefIndex) -> ty::TraitDef {
525+
pub fn get_trait_def(&self, item_id: DefIndex, sess: &Session) -> ty::TraitDef {
525526
let data = match self.entry(item_id).kind {
526-
EntryKind::Trait(data) => data.decode(self),
527+
EntryKind::Trait(data) => data.decode((self, sess)),
527528
_ => bug!(),
528529
};
529530

@@ -607,8 +608,11 @@ impl<'a, 'tcx> CrateMetadata {
607608
}
608609
}
609610

610-
pub fn get_generics(&self, item_id: DefIndex) -> ty::Generics {
611-
self.entry(item_id).generics.unwrap().decode(self)
611+
pub fn get_generics(&self,
612+
item_id: DefIndex,
613+
sess: &Session)
614+
-> ty::Generics {
615+
self.entry(item_id).generics.unwrap().decode((self, sess))
612616
}
613617

614618
pub fn get_type(&self, id: DefIndex, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Ty<'tcx> {
@@ -908,7 +912,7 @@ impl<'a, 'tcx> CrateMetadata {
908912
}
909913
}
910914

911-
pub fn get_item_attrs(&self, node_id: DefIndex) -> Rc<[ast::Attribute]> {
915+
pub fn get_item_attrs(&self, node_id: DefIndex, sess: &Session) -> Rc<[ast::Attribute]> {
912916
let (node_as, node_index) =
913917
(node_id.address_space().index(), node_id.as_array_index());
914918
if self.is_proc_macro(node_id) {
@@ -928,7 +932,7 @@ impl<'a, 'tcx> CrateMetadata {
928932
if def_key.disambiguated_data.data == DefPathData::StructCtor {
929933
item = self.entry(def_key.parent.unwrap());
930934
}
931-
let result: Rc<[ast::Attribute]> = Rc::from(self.get_attributes(&item));
935+
let result: Rc<[ast::Attribute]> = Rc::from(self.get_attributes(&item, sess));
932936
let vec_ = &mut self.attribute_cache.borrow_mut()[node_as];
933937
if vec_.len() < node_index + 1 {
934938
vec_.resize(node_index + 1, None);
@@ -945,9 +949,9 @@ impl<'a, 'tcx> CrateMetadata {
945949
.collect()
946950
}
947951

948-
fn get_attributes(&self, item: &Entry<'tcx>) -> Vec<ast::Attribute> {
952+
fn get_attributes(&self, item: &Entry<'tcx>, sess: &Session) -> Vec<ast::Attribute> {
949953
item.attributes
950-
.decode(self)
954+
.decode((self, sess))
951955
.map(|mut attr| {
952956
// Need new unique IDs: old thread-local IDs won't map to new threads.
953957
attr.id = attr::mk_attr_id();

0 commit comments

Comments
 (0)