Skip to content

Commit

Permalink
Encode/decode extern statics in metadata and incremental cache
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Mar 20, 2018
1 parent 6bfa7d0 commit 13bfbe1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
14 changes: 14 additions & 0 deletions src/librustc/mir/interpret/mod.rs
Expand Up @@ -156,6 +156,8 @@ impl ::rustc_serialize::UseSpecializedDecodable for AllocId {}

pub const ALLOC_DISCRIMINANT: usize = 0;
pub const FN_DISCRIMINANT: usize = 1;
pub const EXTERN_STATIC_DISCRIMINANT: usize = 2;
pub const SHORTHAND_START: usize = 3;

pub fn specialized_encode_alloc_id<
'a, 'tcx,
Expand All @@ -173,13 +175,18 @@ pub fn specialized_encode_alloc_id<
trace!("encoding {:?} with {:#?}", alloc_id, alloc);
ALLOC_DISCRIMINANT.encode(encoder)?;
alloc.encode(encoder)?;
// encode whether this allocation is the root allocation of a static
tcx.interpret_interner
.get_corresponding_static_def_id(alloc_id)
.encode(encoder)?;
} else if let Some(fn_instance) = tcx.interpret_interner.get_fn(alloc_id) {
trace!("encoding {:?} with {:#?}", alloc_id, fn_instance);
FN_DISCRIMINANT.encode(encoder)?;
fn_instance.encode(encoder)?;
} else if let Some(did) = tcx.interpret_interner.get_corresponding_static_def_id(alloc_id) {
// extern "C" statics don't have allocations, just encode its def_id
EXTERN_STATIC_DISCRIMINANT.encode(encoder)?;
did.encode(encoder)?;
} else {
bug!("alloc id without corresponding allocation: {}", alloc_id);
}
Expand Down Expand Up @@ -225,6 +232,13 @@ pub fn specialized_decode_alloc_id<
cache(decoder, pos, id);
Ok(id)
},
EXTERN_STATIC_DISCRIMINANT => {
trace!("creating extern static alloc id at {}", pos);
let did = DefId::decode(decoder)?;
let alloc_id = tcx.interpret_interner.reserve();
tcx.interpret_interner.cache(did, alloc_id);
Ok(alloc_id)
},
shorthand => {
trace!("loading shorthand {}", shorthand);
short(decoder, shorthand)
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/maps/on_disk_cache.rs
Expand Up @@ -819,7 +819,7 @@ impl<'enc, 'a, 'tcx, E> SpecializedEncoder<interpret::AllocId> for CacheEncoder<
// of the metadata file, because that would end up making our indices
// not special. It is essentially impossible for that to happen,
// but let's make sure
assert!(pos != interpret::ALLOC_DISCRIMINANT && pos != interpret::FN_DISCRIMINANT);
assert!(pos >= interpret::SHORTHAND_START);
entry.insert(pos);
None
},
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_metadata/encoder.rs
Expand Up @@ -203,9 +203,9 @@ impl<'a, 'tcx> SpecializedEncoder<interpret::AllocId> for EncodeContext<'a, 'tcx
Entry::Occupied(entry) => Some(entry.get().clone()),
Entry::Vacant(entry) => {
// ensure that we don't place any AllocIds at the very beginning
// of the metadata file, because that would end up making our 0 and 1 indices
// of the metadata file, because that would end up making our indices
// not special. This is essentially impossible, but let's make sure
assert!(pos != 0 && pos != 1);
assert!(pos >= interpret::SHORTHAND_START);
entry.insert(pos);
None
},
Expand Down
21 changes: 21 additions & 0 deletions src/test/incremental/extern_static/issue-49153.rs
@@ -0,0 +1,21 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// https://github.com/rust-lang/rust/issues/49153

// revisions:rpass1 rpass2

extern "C" {
pub static __ImageBase: u8;
}

pub static FOO: &'static u8 = unsafe { &__ImageBase };

fn main() {}

0 comments on commit 13bfbe1

Please sign in to comment.