Skip to content

Commit 76d770a

Browse files
committed
Auto merge of rust-lang#95600 - Dylan-DPC:rollup-580y2ra, r=Dylan-DPC
Rollup of 4 pull requests Successful merges: - rust-lang#95587 (Remove need for associated_type_bounds in std.) - rust-lang#95589 (Include a header in .rlink files) - rust-lang#95593 (diagnostics: add test case for bogus T:Sized suggestion) - rust-lang#95597 (Refer to u8 by absolute path in expansion of thread_local) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 8f96ef4 + 0e528f0 commit 76d770a

File tree

10 files changed

+113
-11
lines changed

10 files changed

+113
-11
lines changed

compiler/rustc_codegen_ssa/src/lib.rs

+51
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use rustc_hir::LangItem;
2929
use rustc_middle::dep_graph::WorkProduct;
3030
use rustc_middle::middle::dependency_format::Dependencies;
3131
use rustc_middle::ty::query::{ExternProviders, Providers};
32+
use rustc_serialize::{opaque, Decodable, Decoder, Encoder};
3233
use rustc_session::config::{CrateType, OutputFilenames, OutputType, RUST_CGU_EXT};
3334
use rustc_session::cstore::{self, CrateSource};
3435
use rustc_session::utils::NativeLibKind;
@@ -190,3 +191,53 @@ pub fn looks_like_rust_object_file(filename: &str) -> bool {
190191
// Check if the "inner" extension
191192
ext2 == Some(RUST_CGU_EXT)
192193
}
194+
195+
const RLINK_VERSION: u32 = 1;
196+
const RLINK_MAGIC: &[u8] = b"rustlink";
197+
198+
const RUSTC_VERSION: Option<&str> = option_env!("CFG_VERSION");
199+
200+
impl CodegenResults {
201+
pub fn serialize_rlink(codegen_results: &CodegenResults) -> Vec<u8> {
202+
let mut encoder = opaque::Encoder::new(vec![]);
203+
encoder.emit_raw_bytes(RLINK_MAGIC).unwrap();
204+
// `emit_raw_bytes` is used to make sure that the version representation does not depend on
205+
// Encoder's inner representation of `u32`.
206+
encoder.emit_raw_bytes(&RLINK_VERSION.to_be_bytes()).unwrap();
207+
encoder.emit_str(RUSTC_VERSION.unwrap()).unwrap();
208+
209+
let mut encoder = rustc_serialize::opaque::Encoder::new(encoder.into_inner());
210+
rustc_serialize::Encodable::encode(codegen_results, &mut encoder).unwrap();
211+
encoder.into_inner()
212+
}
213+
214+
pub fn deserialize_rlink(data: Vec<u8>) -> Result<Self, String> {
215+
// The Decodable machinery is not used here because it panics if the input data is invalid
216+
// and because its internal representation may change.
217+
if !data.starts_with(RLINK_MAGIC) {
218+
return Err("The input does not look like a .rlink file".to_string());
219+
}
220+
let data = &data[RLINK_MAGIC.len()..];
221+
if data.len() < 4 {
222+
return Err("The input does not contain version number".to_string());
223+
}
224+
225+
let mut version_array: [u8; 4] = Default::default();
226+
version_array.copy_from_slice(&data[..4]);
227+
if u32::from_be_bytes(version_array) != RLINK_VERSION {
228+
return Err(".rlink file was produced with encoding version {version_array}, but the current version is {RLINK_VERSION}".to_string());
229+
}
230+
231+
let mut decoder = opaque::Decoder::new(&data[4..], 0);
232+
let rustc_version = decoder.read_str();
233+
let current_version = RUSTC_VERSION.unwrap();
234+
if rustc_version != current_version {
235+
return Err(format!(
236+
".rlink file was produced by rustc version {rustc_version}, but the current version is {current_version}."
237+
));
238+
}
239+
240+
let codegen_results = CodegenResults::decode(&mut decoder);
241+
Ok(codegen_results)
242+
}
243+
}

compiler/rustc_driver/src/lib.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -588,8 +588,12 @@ pub fn try_process_rlink(sess: &Session, compiler: &interface::Compiler) -> Comp
588588
let rlink_data = fs::read(file).unwrap_or_else(|err| {
589589
sess.fatal(&format!("failed to read rlink file: {}", err));
590590
});
591-
let mut decoder = rustc_serialize::opaque::Decoder::new(&rlink_data, 0);
592-
let codegen_results: CodegenResults = rustc_serialize::Decodable::decode(&mut decoder);
591+
let codegen_results = match CodegenResults::deserialize_rlink(rlink_data) {
592+
Ok(codegen) => codegen,
593+
Err(error) => {
594+
sess.fatal(&format!("Could not deserialize .rlink file: {error}"));
595+
}
596+
};
593597
let result = compiler.codegen_backend().link(sess, codegen_results, &outputs);
594598
abort_on_err(result, sess);
595599
} else {

compiler/rustc_interface/src/queries.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::passes::{self, BoxedResolver, QueryContext};
33

44
use rustc_ast as ast;
55
use rustc_codegen_ssa::traits::CodegenBackend;
6+
use rustc_codegen_ssa::CodegenResults;
67
use rustc_data_structures::svh::Svh;
78
use rustc_data_structures::sync::{Lrc, OnceCell, WorkerLocal};
89
use rustc_hir::def_id::LOCAL_CRATE;
@@ -360,10 +361,9 @@ impl Linker {
360361
}
361362

362363
if sess.opts.debugging_opts.no_link {
363-
let mut encoder = rustc_serialize::opaque::Encoder::new(Vec::new());
364-
rustc_serialize::Encodable::encode(&codegen_results, &mut encoder).unwrap();
364+
let encoded = CodegenResults::serialize_rlink(&codegen_results);
365365
let rlink_file = self.prepare_outputs.with_extension(config::RLINK_EXT);
366-
std::fs::write(&rlink_file, encoder.into_inner()).map_err(|err| {
366+
std::fs::write(&rlink_file, encoded).map_err(|err| {
367367
sess.fatal(&format!("failed to write file {}: {}", rlink_file.display(), err));
368368
})?;
369369
return Ok(());

library/std/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,6 @@
224224
#![feature(allocator_internals)]
225225
#![feature(allow_internal_unsafe)]
226226
#![feature(allow_internal_unstable)]
227-
#![feature(associated_type_bounds)]
228227
#![feature(box_syntax)]
229228
#![feature(c_unwind)]
230229
#![feature(cfg_target_thread_local)]

library/std/src/sys/sgx/abi/usercalls/alloc.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,8 @@ impl<T: CoerceUnsized<U>, U> CoerceUnsized<UserRef<U>> for UserRef<T> {}
571571
impl<T, I> Index<I> for UserRef<[T]>
572572
where
573573
[T]: UserSafe,
574-
I: SliceIndex<[T], Output: UserSafe>,
574+
I: SliceIndex<[T]>,
575+
I::Output: UserSafe,
575576
{
576577
type Output = UserRef<I::Output>;
577578

@@ -591,7 +592,8 @@ where
591592
impl<T, I> IndexMut<I> for UserRef<[T]>
592593
where
593594
[T]: UserSafe,
594-
I: SliceIndex<[T], Output: UserSafe>,
595+
I: SliceIndex<[T]>,
596+
I::Output: UserSafe,
595597
{
596598
#[inline]
597599
fn index_mut(&mut self, index: I) -> &mut UserRef<I::Output> {

library/std/src/thread/local.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,9 @@ macro_rules! __thread_local_inner {
217217
// 1 == dtor registered, dtor not run
218218
// 2 == dtor registered and is running or has run
219219
#[thread_local]
220-
static mut STATE: u8 = 0;
220+
static mut STATE: $crate::primitive::u8 = 0;
221221

222-
unsafe extern "C" fn destroy(ptr: *mut u8) {
222+
unsafe extern "C" fn destroy(ptr: *mut $crate::primitive::u8) {
223223
let ptr = ptr as *mut $t;
224224

225225
unsafe {
@@ -235,7 +235,7 @@ macro_rules! __thread_local_inner {
235235
// so now.
236236
0 => {
237237
$crate::thread::__FastLocalKeyInner::<$t>::register_dtor(
238-
$crate::ptr::addr_of_mut!(VAL) as *mut u8,
238+
$crate::ptr::addr_of_mut!(VAL) as *mut $crate::primitive::u8,
239239
destroy,
240240
);
241241
STATE = 1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-include ../tools.mk
2+
3+
all:
4+
echo 'fn main(){}' > $(TMPDIR)/main.rs
5+
# Make sure that this fails
6+
! $(RUSTC) -Z link-only $(TMPDIR)/main.rs 2> $(TMPDIR)/stderr.txt
7+
$(CGREP) "The input does not look like a .rlink file" < $(TMPDIR)/stderr.txt
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// https://github.com/rust-lang/rust/issues/69228
2+
// Used to give bogus suggestion about T not being Sized.
3+
4+
use std::mem::size_of;
5+
6+
fn foo<T>() {
7+
let _arr: [u8; size_of::<T>()];
8+
//~^ ERROR generic parameters may not be used in const operations
9+
//~| NOTE cannot perform const operation
10+
//~| NOTE type parameters may not be used in const expressions
11+
}
12+
13+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: generic parameters may not be used in const operations
2+
--> $DIR/size-of-t.rs:7:30
3+
|
4+
LL | let _arr: [u8; size_of::<T>()];
5+
| ^ cannot perform const operation using `T`
6+
|
7+
= note: type parameters may not be used in const expressions
8+
= help: use `#![feature(generic_const_exprs)]` to allow generic const expressions
9+
10+
error: aborting due to previous error
11+
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// check-pass
2+
3+
#[allow(non_camel_case_types)]
4+
struct u8;
5+
6+
std::thread_local! {
7+
pub static A: i32 = f();
8+
pub static B: i32 = const { 0 };
9+
}
10+
11+
fn f() -> i32 {
12+
0
13+
}
14+
15+
fn main() {}

0 commit comments

Comments
 (0)