Skip to content

Commit d261b53

Browse files
committed
Auto merge of #3481 - RalfJung:rustup, r=RalfJung
Rustup
2 parents 9776f64 + 7c3c271 commit d261b53

File tree

62 files changed

+471
-286
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+471
-286
lines changed

compiler/rustc_codegen_ssa/src/back/archive.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use object::read::macho::FatArch;
1313
use tempfile::Builder as TempFileBuilder;
1414

1515
use std::error::Error;
16-
use std::fs::File;
16+
use std::fs::{self, File};
1717
use std::io::{self, Write};
1818
use std::path::{Path, PathBuf};
1919

@@ -280,22 +280,33 @@ impl<'a> ArArchiveBuilder<'a> {
280280
// This prevents programs (including rustc) from attempting to read a partial archive.
281281
// It also enables writing an archive with the same filename as a dependency on Windows as
282282
// required by a test.
283-
let mut archive_tmpfile = TempFileBuilder::new()
283+
// The tempfile crate currently uses 0o600 as mode for the temporary files and directories
284+
// it creates. We need it to be the default mode for back compat reasons however. (See
285+
// #107495) To handle this we are telling tempfile to create a temporary directory instead
286+
// and then inside this directory create a file using File::create.
287+
let archive_tmpdir = TempFileBuilder::new()
284288
.suffix(".temp-archive")
285-
.tempfile_in(output.parent().unwrap_or_else(|| Path::new("")))
286-
.map_err(|err| io_error_context("couldn't create a temp file", err))?;
289+
.tempdir_in(output.parent().unwrap_or_else(|| Path::new("")))
290+
.map_err(|err| {
291+
io_error_context("couldn't create a directory for the temp file", err)
292+
})?;
293+
let archive_tmpfile_path = archive_tmpdir.path().join("tmp.a");
294+
let mut archive_tmpfile = File::create_new(&archive_tmpfile_path)
295+
.map_err(|err| io_error_context("couldn't create the temp file", err))?;
287296

288-
write_archive_to_stream(archive_tmpfile.as_file_mut(), &entries, archive_kind, false)?;
297+
write_archive_to_stream(&mut archive_tmpfile, &entries, archive_kind, false)?;
289298

290299
let any_entries = !entries.is_empty();
291300
drop(entries);
292301
// Drop src_archives to unmap all input archives, which is necessary if we want to write the
293302
// output archive to the same location as an input archive on Windows.
294303
drop(self.src_archives);
295304

296-
archive_tmpfile
297-
.persist(output)
298-
.map_err(|err| io_error_context("failed to rename archive file", err.error))?;
305+
fs::rename(archive_tmpfile_path, output)
306+
.map_err(|err| io_error_context("failed to rename archive file", err))?;
307+
archive_tmpdir
308+
.close()
309+
.map_err(|err| io_error_context("failed to remove temporary directory", err))?;
299310

300311
Ok(any_entries)
301312
}

compiler/rustc_const_eval/src/interpret/machine.rs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -288,28 +288,19 @@ pub trait Machine<'mir, 'tcx: 'mir>: Sized {
288288
}
289289

290290
/// Return the `AllocId` for the given thread-local static in the current thread.
291-
fn thread_local_static_base_pointer(
291+
fn thread_local_static_pointer(
292292
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
293293
def_id: DefId,
294294
) -> InterpResult<'tcx, Pointer<Self::Provenance>> {
295295
throw_unsup!(ThreadLocalStatic(def_id))
296296
}
297297

298-
/// Return the root pointer for the given `extern static`.
299-
fn extern_static_base_pointer(
298+
/// Return the `AllocId` for the given `extern static`.
299+
fn extern_static_pointer(
300300
ecx: &InterpCx<'mir, 'tcx, Self>,
301301
def_id: DefId,
302302
) -> InterpResult<'tcx, Pointer<Self::Provenance>>;
303303

304-
/// Return a "base" pointer for the given allocation: the one that is used for direct
305-
/// accesses to this static/const/fn allocation, or the one returned from the heap allocator.
306-
///
307-
/// Not called on `extern` or thread-local statics (those use the methods above).
308-
fn adjust_alloc_base_pointer(
309-
ecx: &InterpCx<'mir, 'tcx, Self>,
310-
ptr: Pointer,
311-
) -> InterpResult<'tcx, Pointer<Self::Provenance>>;
312-
313304
/// "Int-to-pointer cast"
314305
fn ptr_from_addr_cast(
315306
ecx: &InterpCx<'mir, 'tcx, Self>,
@@ -336,6 +327,8 @@ pub trait Machine<'mir, 'tcx: 'mir>: Sized {
336327

337328
/// Called to adjust allocations to the Provenance and AllocExtra of this machine.
338329
///
330+
/// If `alloc` contains pointers, then they are all pointing to globals.
331+
///
339332
/// The way we construct allocations is to always first construct it without extra and then add
340333
/// the extra. This keeps uniform code paths for handling both allocations created by CTFE for
341334
/// globals, and allocations created by Miri during evaluation.
@@ -354,6 +347,19 @@ pub trait Machine<'mir, 'tcx: 'mir>: Sized {
354347
kind: Option<MemoryKind<Self::MemoryKind>>,
355348
) -> InterpResult<'tcx, Cow<'b, Allocation<Self::Provenance, Self::AllocExtra, Self::Bytes>>>;
356349

350+
/// Return a "root" pointer for the given allocation: the one that is used for direct
351+
/// accesses to this static/const/fn allocation, or the one returned from the heap allocator.
352+
///
353+
/// Not called on `extern` or thread-local statics (those use the methods above).
354+
///
355+
/// `kind` is the kind of the allocation the pointer points to; it can be `None` when
356+
/// it's a global and `GLOBAL_KIND` is `None`.
357+
fn adjust_alloc_root_pointer(
358+
ecx: &InterpCx<'mir, 'tcx, Self>,
359+
ptr: Pointer,
360+
kind: Option<MemoryKind<Self::MemoryKind>>,
361+
) -> InterpResult<'tcx, Pointer<Self::Provenance>>;
362+
357363
/// Evaluate the inline assembly.
358364
///
359365
/// This should take care of jumping to the next block (one of `targets`) when asm goto
@@ -592,7 +598,7 @@ pub macro compile_time_machine(<$mir: lifetime, $tcx: lifetime>) {
592598
Ok(alloc)
593599
}
594600

595-
fn extern_static_base_pointer(
601+
fn extern_static_pointer(
596602
ecx: &InterpCx<$mir, $tcx, Self>,
597603
def_id: DefId,
598604
) -> InterpResult<$tcx, Pointer> {
@@ -601,9 +607,10 @@ pub macro compile_time_machine(<$mir: lifetime, $tcx: lifetime>) {
601607
}
602608

603609
#[inline(always)]
604-
fn adjust_alloc_base_pointer(
610+
fn adjust_alloc_root_pointer(
605611
_ecx: &InterpCx<$mir, $tcx, Self>,
606612
ptr: Pointer<CtfeProvenance>,
613+
_kind: Option<MemoryKind<Self::MemoryKind>>,
607614
) -> InterpResult<$tcx, Pointer<CtfeProvenance>> {
608615
Ok(ptr)
609616
}

compiler/rustc_const_eval/src/interpret/memory.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
165165
///
166166
/// This function can fail only if `ptr` points to an `extern static`.
167167
#[inline]
168-
pub fn global_base_pointer(
168+
pub fn global_root_pointer(
169169
&self,
170170
ptr: Pointer<CtfeProvenance>,
171171
) -> InterpResult<'tcx, Pointer<M::Provenance>> {
@@ -178,12 +178,18 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
178178
bug!("global memory cannot point to thread-local static")
179179
}
180180
Some(GlobalAlloc::Static(def_id)) if self.tcx.is_foreign_item(def_id) => {
181-
return M::extern_static_base_pointer(self, def_id);
181+
return M::extern_static_pointer(self, def_id);
182+
}
183+
None => {
184+
assert!(
185+
self.memory.extra_fn_ptr_map.contains_key(&alloc_id),
186+
"{alloc_id:?} is neither global nor a function pointer"
187+
);
182188
}
183189
_ => {}
184190
}
185191
// And we need to get the provenance.
186-
M::adjust_alloc_base_pointer(self, ptr)
192+
M::adjust_alloc_root_pointer(self, ptr, M::GLOBAL_KIND.map(MemoryKind::Machine))
187193
}
188194

189195
pub fn fn_ptr(&mut self, fn_val: FnVal<'tcx, M::ExtraFnVal>) -> Pointer<M::Provenance> {
@@ -197,9 +203,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
197203
id
198204
}
199205
};
200-
// Functions are global allocations, so make sure we get the right base pointer.
206+
// Functions are global allocations, so make sure we get the right root pointer.
201207
// We know this is not an `extern static` so this cannot fail.
202-
self.global_base_pointer(Pointer::from(id)).unwrap()
208+
self.global_root_pointer(Pointer::from(id)).unwrap()
203209
}
204210

205211
pub fn allocate_ptr(
@@ -240,7 +246,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
240246
);
241247
let alloc = M::adjust_allocation(self, id, Cow::Owned(alloc), Some(kind))?;
242248
self.memory.alloc_map.insert(id, (kind, alloc.into_owned()));
243-
M::adjust_alloc_base_pointer(self, Pointer::from(id))
249+
M::adjust_alloc_root_pointer(self, Pointer::from(id), Some(kind))
244250
}
245251

246252
pub fn reallocate_ptr(

compiler/rustc_const_eval/src/interpret/operand.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -764,15 +764,15 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
764764
// Other cases need layout.
765765
let adjust_scalar = |scalar| -> InterpResult<'tcx, _> {
766766
Ok(match scalar {
767-
Scalar::Ptr(ptr, size) => Scalar::Ptr(self.global_base_pointer(ptr)?, size),
767+
Scalar::Ptr(ptr, size) => Scalar::Ptr(self.global_root_pointer(ptr)?, size),
768768
Scalar::Int(int) => Scalar::Int(int),
769769
})
770770
};
771771
let layout = from_known_layout(self.tcx, self.param_env, layout, || self.layout_of(ty))?;
772772
let imm = match val_val {
773773
mir::ConstValue::Indirect { alloc_id, offset } => {
774774
// This is const data, no mutation allowed.
775-
let ptr = self.global_base_pointer(Pointer::new(
775+
let ptr = self.global_root_pointer(Pointer::new(
776776
CtfeProvenance::from(alloc_id).as_immutable(),
777777
offset,
778778
))?;
@@ -784,7 +784,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
784784
// This is const data, no mutation allowed.
785785
let alloc_id = self.tcx.reserve_and_set_memory_alloc(data);
786786
let ptr = Pointer::new(CtfeProvenance::from(alloc_id).as_immutable(), Size::ZERO);
787-
Immediate::new_slice(self.global_base_pointer(ptr)?.into(), meta, self)
787+
Immediate::new_slice(self.global_root_pointer(ptr)?.into(), meta, self)
788788
}
789789
};
790790
Ok(OpTy { op: Operand::Immediate(imm), layout })

compiler/rustc_const_eval/src/interpret/place.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,7 @@ where
10101010
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::Provenance>> {
10111011
// This must be an allocation in `tcx`
10121012
let _ = self.tcx.global_alloc(raw.alloc_id);
1013-
let ptr = self.global_base_pointer(Pointer::from(raw.alloc_id))?;
1013+
let ptr = self.global_root_pointer(Pointer::from(raw.alloc_id))?;
10141014
let layout = self.layout_of(raw.ty)?;
10151015
Ok(self.ptr_to_mplace(ptr.into(), layout))
10161016
}

compiler/rustc_const_eval/src/interpret/step.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
144144
use rustc_middle::mir::Rvalue::*;
145145
match *rvalue {
146146
ThreadLocalRef(did) => {
147-
let ptr = M::thread_local_static_base_pointer(self, did)?;
147+
let ptr = M::thread_local_static_pointer(self, did)?;
148148
self.write_pointer(ptr, &dest)?;
149149
}
150150

compiler/rustc_const_eval/src/interpret/traits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
2828
ensure_monomorphic_enough(*self.tcx, poly_trait_ref)?;
2929

3030
let vtable_symbolic_allocation = self.tcx.reserve_and_set_vtable_alloc(ty, poly_trait_ref);
31-
let vtable_ptr = self.global_base_pointer(Pointer::from(vtable_symbolic_allocation))?;
31+
let vtable_ptr = self.global_root_pointer(Pointer::from(vtable_symbolic_allocation))?;
3232
Ok(vtable_ptr.into())
3333
}
3434

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -660,8 +660,14 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
660660
}
661661
DefKind::Impl { of_trait } => {
662662
if of_trait && let Some(impl_trait_header) = tcx.impl_trait_header(def_id) {
663-
check_impl_items_against_trait(tcx, def_id, impl_trait_header);
664-
check_on_unimplemented(tcx, def_id);
663+
if tcx
664+
.ensure()
665+
.coherent_trait(impl_trait_header.trait_ref.instantiate_identity().def_id)
666+
.is_ok()
667+
{
668+
check_impl_items_against_trait(tcx, def_id, impl_trait_header);
669+
check_on_unimplemented(tcx, def_id);
670+
}
665671
}
666672
}
667673
DefKind::Trait => {

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2011,12 +2011,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
20112011
for (span, code) in errors_causecode {
20122012
self.dcx().try_steal_modify_and_emit_err(span, StashKey::MaybeForgetReturn, |err| {
20132013
if let Some(fn_sig) = self.body_fn_sig()
2014-
&& let ExprBindingObligation(_, _, hir_id, ..) = code
2014+
&& let ExprBindingObligation(_, _, binding_hir_id, ..) = code
20152015
&& !fn_sig.output().is_unit()
20162016
{
20172017
let mut block_num = 0;
20182018
let mut found_semi = false;
2019-
for (_, node) in self.tcx.hir().parent_iter(hir_id) {
2019+
for (hir_id, node) in self.tcx.hir().parent_iter(binding_hir_id) {
2020+
// Don't proceed into parent bodies
2021+
if hir_id.owner != binding_hir_id.owner {
2022+
break;
2023+
}
20202024
match node {
20212025
hir::Node::Stmt(stmt) => {
20222026
if let hir::StmtKind::Semi(expr) = stmt.kind {

compiler/rustc_infer/src/infer/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,7 @@ impl<'tcx> InferCtxt<'tcx> {
952952
// a test for it.
953953
(_, ty::Infer(ty::TyVar(_))) => {}
954954
(ty::Infer(ty::TyVar(_)), _) => {}
955-
_ if (r_a, r_b).has_opaque_types() => {
955+
_ if r_a != r_b && (r_a, r_b).has_opaque_types() => {
956956
span_bug!(
957957
cause.span(),
958958
"opaque types got hidden types registered from within subtype predicate: {r_a:?} vs {r_b:?}"

0 commit comments

Comments
 (0)