From 13bfbe1394732163c7130dd57e6b0a86e147c7e8 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Tue, 20 Mar 2018 10:36:45 +0100 Subject: [PATCH] Encode/decode extern statics in metadata and incremental cache --- src/librustc/mir/interpret/mod.rs | 14 +++++++++++++ src/librustc/ty/maps/on_disk_cache.rs | 2 +- src/librustc_metadata/encoder.rs | 4 ++-- .../incremental/extern_static/issue-49153.rs | 21 +++++++++++++++++++ 4 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 src/test/incremental/extern_static/issue-49153.rs diff --git a/src/librustc/mir/interpret/mod.rs b/src/librustc/mir/interpret/mod.rs index 04ffb9af29ed5..e242ec4985ab4 100644 --- a/src/librustc/mir/interpret/mod.rs +++ b/src/librustc/mir/interpret/mod.rs @@ -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, @@ -173,6 +175,7 @@ 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)?; @@ -180,6 +183,10 @@ pub fn specialized_encode_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); } @@ -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) diff --git a/src/librustc/ty/maps/on_disk_cache.rs b/src/librustc/ty/maps/on_disk_cache.rs index e56d8f8e818dd..d636246b57f1b 100644 --- a/src/librustc/ty/maps/on_disk_cache.rs +++ b/src/librustc/ty/maps/on_disk_cache.rs @@ -819,7 +819,7 @@ impl<'enc, 'a, 'tcx, E> SpecializedEncoder 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 }, diff --git a/src/librustc_metadata/encoder.rs b/src/librustc_metadata/encoder.rs index 0da23c2caf4ae..6b3453f2c99e8 100644 --- a/src/librustc_metadata/encoder.rs +++ b/src/librustc_metadata/encoder.rs @@ -203,9 +203,9 @@ impl<'a, 'tcx> SpecializedEncoder 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 }, diff --git a/src/test/incremental/extern_static/issue-49153.rs b/src/test/incremental/extern_static/issue-49153.rs new file mode 100644 index 0000000000000..e0538e09c6470 --- /dev/null +++ b/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 or the MIT license +// , 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() {}