Skip to content

Commit c19264f

Browse files
committed
Auto merge of #49200 - oli-obk:extern_static_metadata, r=michaelwoerister
Encode/decode extern statics in metadata and incremental cache fixes #49153 cc @abonander r? @michaelwoerister incremental ICE
2 parents 1042053 + 13bfbe1 commit c19264f

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

src/librustc/mir/interpret/mod.rs

+14
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ impl ::rustc_serialize::UseSpecializedDecodable for AllocId {}
156156

157157
pub const ALLOC_DISCRIMINANT: usize = 0;
158158
pub const FN_DISCRIMINANT: usize = 1;
159+
pub const EXTERN_STATIC_DISCRIMINANT: usize = 2;
160+
pub const SHORTHAND_START: usize = 3;
159161

160162
pub fn specialized_encode_alloc_id<
161163
'a, 'tcx,
@@ -173,13 +175,18 @@ pub fn specialized_encode_alloc_id<
173175
trace!("encoding {:?} with {:#?}", alloc_id, alloc);
174176
ALLOC_DISCRIMINANT.encode(encoder)?;
175177
alloc.encode(encoder)?;
178+
// encode whether this allocation is the root allocation of a static
176179
tcx.interpret_interner
177180
.get_corresponding_static_def_id(alloc_id)
178181
.encode(encoder)?;
179182
} else if let Some(fn_instance) = tcx.interpret_interner.get_fn(alloc_id) {
180183
trace!("encoding {:?} with {:#?}", alloc_id, fn_instance);
181184
FN_DISCRIMINANT.encode(encoder)?;
182185
fn_instance.encode(encoder)?;
186+
} else if let Some(did) = tcx.interpret_interner.get_corresponding_static_def_id(alloc_id) {
187+
// extern "C" statics don't have allocations, just encode its def_id
188+
EXTERN_STATIC_DISCRIMINANT.encode(encoder)?;
189+
did.encode(encoder)?;
183190
} else {
184191
bug!("alloc id without corresponding allocation: {}", alloc_id);
185192
}
@@ -225,6 +232,13 @@ pub fn specialized_decode_alloc_id<
225232
cache(decoder, pos, id);
226233
Ok(id)
227234
},
235+
EXTERN_STATIC_DISCRIMINANT => {
236+
trace!("creating extern static alloc id at {}", pos);
237+
let did = DefId::decode(decoder)?;
238+
let alloc_id = tcx.interpret_interner.reserve();
239+
tcx.interpret_interner.cache(did, alloc_id);
240+
Ok(alloc_id)
241+
},
228242
shorthand => {
229243
trace!("loading shorthand {}", shorthand);
230244
short(decoder, shorthand)

src/librustc/ty/maps/on_disk_cache.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ impl<'enc, 'a, 'tcx, E> SpecializedEncoder<interpret::AllocId> for CacheEncoder<
820820
// of the metadata file, because that would end up making our indices
821821
// not special. It is essentially impossible for that to happen,
822822
// but let's make sure
823-
assert!(pos != interpret::ALLOC_DISCRIMINANT && pos != interpret::FN_DISCRIMINANT);
823+
assert!(pos >= interpret::SHORTHAND_START);
824824
entry.insert(pos);
825825
None
826826
},

src/librustc_metadata/encoder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,9 @@ impl<'a, 'tcx> SpecializedEncoder<interpret::AllocId> for EncodeContext<'a, 'tcx
203203
Entry::Occupied(entry) => Some(entry.get().clone()),
204204
Entry::Vacant(entry) => {
205205
// ensure that we don't place any AllocIds at the very beginning
206-
// of the metadata file, because that would end up making our 0 and 1 indices
206+
// of the metadata file, because that would end up making our indices
207207
// not special. This is essentially impossible, but let's make sure
208-
assert!(pos != 0 && pos != 1);
208+
assert!(pos >= interpret::SHORTHAND_START);
209209
entry.insert(pos);
210210
None
211211
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2018 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+
// https://github.com/rust-lang/rust/issues/49153
12+
13+
// revisions:rpass1 rpass2
14+
15+
extern "C" {
16+
pub static __ImageBase: u8;
17+
}
18+
19+
pub static FOO: &'static u8 = unsafe { &__ImageBase };
20+
21+
fn main() {}

0 commit comments

Comments
 (0)