Skip to content

Commit 02046a5

Browse files
committed
Auto merge of #70371 - Centril:rollup-ywi1vo3, r=Centril
Rollup of 8 pull requests Successful merges: - #70023 (clean up E0436 explanation) - #70234 (#[track_caller] on core::ops::{Index, IndexMut}.) - #70241 (Miri: move ModifiedStatic to ConstEval errors) - #70342 (IoSlice/IoSliceMut should be Send and Sync) - #70350 (Request "-Z unstable-options" for unstable options) - #70355 (Clean up E0454) - #70359 (must_use on split_off) - #70368 (Mark hotplug_codegen_backend as ignore-stage1) Failed merges: r? @ghost
2 parents 2dcf54f + bf1ad22 commit 02046a5

File tree

29 files changed

+237
-118
lines changed

29 files changed

+237
-118
lines changed

src/liballoc/collections/vec_deque.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1876,6 +1876,7 @@ impl<T> VecDeque<T> {
18761876
/// assert_eq!(buf2, [2, 3]);
18771877
/// ```
18781878
#[inline]
1879+
#[must_use = "use `.truncate()` if you don't need the other half"]
18791880
#[stable(feature = "split_off", since = "1.4.0")]
18801881
pub fn split_off(&mut self, at: usize) -> Self {
18811882
let len = self.len();

src/liballoc/string.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1461,6 +1461,7 @@ impl String {
14611461
/// ```
14621462
#[inline]
14631463
#[stable(feature = "string_split_off", since = "1.16.0")]
1464+
#[must_use = "use `.truncate()` if you don't need the other half"]
14641465
pub fn split_off(&mut self, at: usize) -> String {
14651466
assert!(self.is_char_boundary(at));
14661467
let other = self.vec.split_off(at);

src/liballoc/tests/string.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -266,14 +266,14 @@ fn test_split_off_empty() {
266266
fn test_split_off_past_end() {
267267
let orig = "Hello, world!";
268268
let mut split = String::from(orig);
269-
split.split_off(orig.len() + 1);
269+
let _ = split.split_off(orig.len() + 1);
270270
}
271271

272272
#[test]
273273
#[should_panic]
274274
fn test_split_off_mid_char() {
275275
let mut orig = String::from("山");
276-
orig.split_off(1);
276+
let _ = orig.split_off(1);
277277
}
278278

279279
#[test]

src/libcore/ops/index.rs

+2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ pub trait Index<Idx: ?Sized> {
6565

6666
/// Performs the indexing (`container[index]`) operation.
6767
#[stable(feature = "rust1", since = "1.0.0")]
68+
#[cfg_attr(not(bootstrap), track_caller)]
6869
fn index(&self, index: Idx) -> &Self::Output;
6970
}
7071

@@ -166,5 +167,6 @@ see chapter in The Book <https://doc.rust-lang.org/book/ch08-02-strings.html#ind
166167
pub trait IndexMut<Idx: ?Sized>: Index<Idx> {
167168
/// Performs the mutable indexing (`container[index]`) operation.
168169
#[stable(feature = "rust1", since = "1.0.0")]
170+
#[cfg_attr(not(bootstrap), track_caller)]
169171
fn index_mut(&mut self, index: Idx) -> &mut Self::Output;
170172
}

src/libcore/slice/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -2306,6 +2306,7 @@ impl<T> [T] {
23062306
/// assert_eq!(&bytes, b"Hello, Wello!");
23072307
/// ```
23082308
#[stable(feature = "copy_within", since = "1.37.0")]
2309+
#[track_caller]
23092310
pub fn copy_within<R: ops::RangeBounds<usize>>(&mut self, src: R, dest: usize)
23102311
where
23112312
T: Copy,
@@ -2721,18 +2722,21 @@ where
27212722

27222723
#[inline(never)]
27232724
#[cold]
2725+
#[track_caller]
27242726
fn slice_index_len_fail(index: usize, len: usize) -> ! {
27252727
panic!("index {} out of range for slice of length {}", index, len);
27262728
}
27272729

27282730
#[inline(never)]
27292731
#[cold]
2732+
#[track_caller]
27302733
fn slice_index_order_fail(index: usize, end: usize) -> ! {
27312734
panic!("slice index starts at {} but ends at {}", index, end);
27322735
}
27332736

27342737
#[inline(never)]
27352738
#[cold]
2739+
#[track_caller]
27362740
fn slice_index_overflow_fail() -> ! {
27372741
panic!("attempted to index slice up to maximum usize");
27382742
}
@@ -2804,11 +2808,13 @@ pub trait SliceIndex<T: ?Sized>: private_slice_index::Sealed {
28042808
/// Returns a shared reference to the output at this location, panicking
28052809
/// if out of bounds.
28062810
#[unstable(feature = "slice_index_methods", issue = "none")]
2811+
#[cfg_attr(not(bootstrap), track_caller)]
28072812
fn index(self, slice: &T) -> &Self::Output;
28082813

28092814
/// Returns a mutable reference to the output at this location, panicking
28102815
/// if out of bounds.
28112816
#[unstable(feature = "slice_index_methods", issue = "none")]
2817+
#[cfg_attr(not(bootstrap), track_caller)]
28122818
fn index_mut(self, slice: &mut T) -> &mut Self::Output;
28132819
}
28142820

src/libcore/str/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1794,6 +1794,7 @@ mod traits {
17941794

17951795
#[inline(never)]
17961796
#[cold]
1797+
#[track_caller]
17971798
fn str_index_overflow_fail() -> ! {
17981799
panic!("attempted to index str up to maximum usize");
17991800
}
@@ -2185,6 +2186,7 @@ fn truncate_to_char_boundary(s: &str, mut max: usize) -> (bool, &str) {
21852186

21862187
#[inline(never)]
21872188
#[cold]
2189+
#[track_caller]
21882190
fn slice_error_fail(s: &str, begin: usize, end: usize) -> ! {
21892191
const MAX_DISPLAY_LENGTH: usize = 256;
21902192
let (truncated, s_trunc) = truncate_to_char_boundary(s, MAX_DISPLAY_LENGTH);

src/librustc/mir/interpret/error.rs

-9
Original file line numberDiff line numberDiff line change
@@ -453,9 +453,6 @@ pub enum UnsupportedOpInfo {
453453
ReadForeignStatic(DefId),
454454
/// Could not find MIR for a function.
455455
NoMirFor(DefId),
456-
/// Modified a static during const-eval.
457-
/// FIXME: move this to `ConstEvalErrKind` through a machine hook.
458-
ModifiedStatic,
459456
/// Encountered a pointer where we needed raw bytes.
460457
ReadPointerAsBytes,
461458
/// Encountered raw bytes where we needed a pointer.
@@ -471,12 +468,6 @@ impl fmt::Debug for UnsupportedOpInfo {
471468
write!(f, "tried to read from foreign (extern) static {:?}", did)
472469
}
473470
NoMirFor(did) => write!(f, "could not load MIR for {:?}", did),
474-
ModifiedStatic => write!(
475-
f,
476-
"tried to modify a static's initial value from another static's \
477-
initializer"
478-
),
479-
480471
ReadPointerAsBytes => write!(f, "unable to turn pointer into raw bytes",),
481472
ReadBytesAsPointer => write!(f, "unable to turn bytes into a pointer"),
482473
}

src/librustc/ty/context.rs

+1
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,7 @@ pub struct GlobalCtxt<'tcx> {
984984
/// Stores the value of constants (and deduplicates the actual memory)
985985
allocation_interner: ShardedHashMap<&'tcx Allocation, ()>,
986986

987+
/// Stores memory for globals (statics/consts).
987988
pub alloc_map: Lock<interpret::AllocMap<'tcx>>,
988989

989990
layout_interner: ShardedHashMap<&'tcx LayoutDetails, ()>,

src/librustc_error_codes/error_codes/E0436.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
The functional record update syntax is only allowed for structs. (Struct-like
2-
enum variants don't qualify, for example.)
1+
The functional record update syntax was used on something other than a struct.
32

43
Erroneous code example:
54

@@ -24,7 +23,9 @@ fn one_up_competitor(competitor_frequency: PublicationFrequency)
2423
}
2524
```
2625

27-
Rewrite the expression without functional record update syntax:
26+
The functional record update syntax is only allowed for structs (struct-like
27+
enum variants don't qualify, for example). To fix the previous code, rewrite the
28+
expression without functional record update syntax:
2829

2930
```
3031
enum PublicationFrequency {

src/librustc_error_codes/error_codes/E0454.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
A link name was given with an empty name. Erroneous code example:
1+
A link name was given with an empty name.
2+
3+
Erroneous code example:
24

35
```compile_fail,E0454
46
#[link(name = "")] extern {}

src/librustc_mir/const_eval/error.rs

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::interpret::{ConstEvalErr, InterpErrorInfo, Machine};
1212
pub enum ConstEvalErrKind {
1313
NeedsRfc(String),
1414
ConstAccessesStatic,
15+
ModifiedGlobal,
1516
AssertFailure(AssertKind<u64>),
1617
Panic { msg: Symbol, line: u32, col: u32, file: Symbol },
1718
}
@@ -33,6 +34,9 @@ impl fmt::Display for ConstEvalErrKind {
3334
write!(f, "\"{}\" needs an rfc before being allowed inside constants", msg)
3435
}
3536
ConstAccessesStatic => write!(f, "constant accesses static"),
37+
ModifiedGlobal => {
38+
write!(f, "modifying a static's initial value from another static's initializer")
39+
}
3640
AssertFailure(ref msg) => write!(f, "{:?}", msg),
3741
Panic { msg, line, col, file } => {
3842
write!(f, "the evaluated program panicked at '{}', {}:{}:{}", msg, file, line, col)

src/librustc_mir/const_eval/machine.rs

+16-7
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ use std::hash::Hash;
88
use rustc_data_structures::fx::FxHashMap;
99

1010
use rustc::mir::AssertMessage;
11-
use rustc_span::source_map::Span;
11+
use rustc_ast::ast::Mutability;
1212
use rustc_span::symbol::Symbol;
13+
use rustc_span::{def_id::DefId, Span};
1314

1415
use crate::interpret::{
1516
self, AllocId, Allocation, GlobalId, ImmTy, InterpCx, InterpResult, Memory, MemoryKind, OpTy,
@@ -167,7 +168,7 @@ impl interpret::MayLeak for ! {
167168
}
168169

169170
impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter {
170-
type MemoryKinds = !;
171+
type MemoryKind = !;
171172
type PointerTag = ();
172173
type ExtraFnVal = !;
173174

@@ -177,7 +178,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter {
177178

178179
type MemoryMap = FxHashMap<AllocId, (MemoryKind<!>, Allocation)>;
179180

180-
const STATIC_KIND: Option<!> = None; // no copying of statics allowed
181+
const GLOBAL_KIND: Option<!> = None; // no copying of globals allowed
181182

182183
// We do not check for alignment to avoid having to carry an `Align`
183184
// in `ConstValue::ByRef`.
@@ -317,7 +318,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter {
317318
}
318319

319320
#[inline(always)]
320-
fn tag_static_base_pointer(_memory_extra: &MemoryExtra, _id: AllocId) -> Self::PointerTag {}
321+
fn tag_global_base_pointer(_memory_extra: &MemoryExtra, _id: AllocId) -> Self::PointerTag {}
321322

322323
fn box_alloc(
323324
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
@@ -345,11 +346,19 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter {
345346
Ok(())
346347
}
347348

348-
fn before_access_static(
349+
fn before_access_global(
349350
memory_extra: &MemoryExtra,
350-
_allocation: &Allocation,
351+
alloc_id: AllocId,
352+
allocation: &Allocation,
353+
def_id: Option<DefId>,
354+
is_write: bool,
351355
) -> InterpResult<'tcx> {
352-
if memory_extra.can_access_statics {
356+
if is_write && allocation.mutability == Mutability::Not {
357+
Err(err_ub!(WriteToReadOnly(alloc_id)).into())
358+
} else if is_write {
359+
Err(ConstEvalErrKind::ModifiedGlobal.into())
360+
} else if memory_extra.can_access_statics || def_id.is_none() {
361+
// `def_id.is_none()` indicates this is not a static, but a const or so.
353362
Ok(())
354363
} else {
355364
Err(ConstEvalErrKind::ConstAccessesStatic.into())

src/librustc_mir/interpret/eval_context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,8 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
253253
/// This represents a *direct* access to that memory, as opposed to access
254254
/// through a pointer that was created by the program.
255255
#[inline(always)]
256-
pub fn tag_static_base_pointer(&self, ptr: Pointer) -> Pointer<M::PointerTag> {
257-
self.memory.tag_static_base_pointer(ptr)
256+
pub fn tag_global_base_pointer(&self, ptr: Pointer) -> Pointer<M::PointerTag> {
257+
self.memory.tag_global_base_pointer(ptr)
258258
}
259259

260260
#[inline(always)]

src/librustc_mir/interpret/intern.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use super::{AllocId, Allocation, InterpCx, MPlaceTy, Machine, MemoryKind, Scalar
1616
pub trait CompileTimeMachine<'mir, 'tcx> = Machine<
1717
'mir,
1818
'tcx,
19-
MemoryKinds = !,
19+
MemoryKind = !,
2020
PointerTag = (),
2121
ExtraFnVal = !,
2222
FrameExtra = (),
@@ -104,7 +104,7 @@ fn intern_shallow<'rt, 'mir, 'tcx, M: CompileTimeMachine<'mir, 'tcx>>(
104104
MemoryKind::Stack | MemoryKind::Vtable | MemoryKind::CallerLocation => {}
105105
}
106106
// Set allocation mutability as appropriate. This is used by LLVM to put things into
107-
// read-only memory, and also by Miri when evluating other constants/statics that
107+
// read-only memory, and also by Miri when evaluating other globals that
108108
// access this one.
109109
if mode == InternMode::Static {
110110
// When `ty` is `None`, we assume no interior mutability.

src/librustc_mir/interpret/machine.rs

+21-16
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::hash::Hash;
77

88
use rustc::mir;
99
use rustc::ty::{self, Ty};
10-
use rustc_span::Span;
10+
use rustc_span::{def_id::DefId, Span};
1111

1212
use super::{
1313
AllocId, Allocation, AllocationExtra, Frame, ImmTy, InterpCx, InterpResult, Memory, MemoryKind,
@@ -79,7 +79,7 @@ pub trait AllocMap<K: Hash + Eq, V> {
7979
/// and some use case dependent behaviour can instead be applied.
8080
pub trait Machine<'mir, 'tcx>: Sized {
8181
/// Additional memory kinds a machine wishes to distinguish from the builtin ones
82-
type MemoryKinds: ::std::fmt::Debug + MayLeak + Eq + 'static;
82+
type MemoryKind: ::std::fmt::Debug + MayLeak + Eq + 'static;
8383

8484
/// Tag tracked alongside every pointer. This is used to implement "Stacked Borrows"
8585
/// <https://www.ralfj.de/blog/2018/08/07/stacked-borrows.html>.
@@ -105,16 +105,17 @@ pub trait Machine<'mir, 'tcx>: Sized {
105105
/// Memory's allocation map
106106
type MemoryMap: AllocMap<
107107
AllocId,
108-
(MemoryKind<Self::MemoryKinds>, Allocation<Self::PointerTag, Self::AllocExtra>),
108+
(MemoryKind<Self::MemoryKind>, Allocation<Self::PointerTag, Self::AllocExtra>),
109109
> + Default
110110
+ Clone;
111111

112-
/// The memory kind to use for copied statics -- or None if statics should not be mutated
113-
/// and thus any such attempt will cause a `ModifiedStatic` error to be raised.
112+
/// The memory kind to use for copied global memory (held in `tcx`) --
113+
/// or None if such memory should not be mutated and thus any such attempt will cause
114+
/// a `ModifiedStatic` error to be raised.
114115
/// Statics are copied under two circumstances: When they are mutated, and when
115-
/// `tag_allocation` or `find_foreign_static` (see below) returns an owned allocation
116+
/// `tag_allocation` (see below) returns an owned allocation
116117
/// that is added to the memory so that the work is not done twice.
117-
const STATIC_KIND: Option<Self::MemoryKinds>;
118+
const GLOBAL_KIND: Option<Self::MemoryKind>;
118119

119120
/// Whether memory accesses should be alignment-checked.
120121
const CHECK_ALIGN: bool;
@@ -207,11 +208,15 @@ pub trait Machine<'mir, 'tcx>: Sized {
207208
Ok(())
208209
}
209210

210-
/// Called before a `Static` value is accessed.
211+
/// Called before a global allocation is accessed.
212+
/// `def_id` is `Some` if this is the "lazy" allocation of a static.
211213
#[inline]
212-
fn before_access_static(
214+
fn before_access_global(
213215
_memory_extra: &Self::MemoryExtra,
216+
_alloc_id: AllocId,
214217
_allocation: &Allocation,
218+
_def_id: Option<DefId>,
219+
_is_write: bool,
215220
) -> InterpResult<'tcx> {
216221
Ok(())
217222
}
@@ -231,10 +236,10 @@ pub trait Machine<'mir, 'tcx>: Sized {
231236
/// it contains (in relocations) tagged. The way we construct allocations is
232237
/// to always first construct it without extra and then add the extra.
233238
/// This keeps uniform code paths for handling both allocations created by CTFE
234-
/// for statics, and allocations created by Miri during evaluation.
239+
/// for globals, and allocations created by Miri during evaluation.
235240
///
236241
/// `kind` is the kind of the allocation being tagged; it can be `None` when
237-
/// it's a static and `STATIC_KIND` is `None`.
242+
/// it's a global and `GLOBAL_KIND` is `None`.
238243
///
239244
/// This should avoid copying if no work has to be done! If this returns an owned
240245
/// allocation (because a copy had to be done to add tags or metadata), machine memory will
@@ -243,20 +248,20 @@ pub trait Machine<'mir, 'tcx>: Sized {
243248
///
244249
/// Also return the "base" tag to use for this allocation: the one that is used for direct
245250
/// accesses to this allocation. If `kind == STATIC_KIND`, this tag must be consistent
246-
/// with `tag_static_base_pointer`.
251+
/// with `tag_global_base_pointer`.
247252
fn init_allocation_extra<'b>(
248253
memory_extra: &Self::MemoryExtra,
249254
id: AllocId,
250255
alloc: Cow<'b, Allocation>,
251-
kind: Option<MemoryKind<Self::MemoryKinds>>,
256+
kind: Option<MemoryKind<Self::MemoryKind>>,
252257
) -> (Cow<'b, Allocation<Self::PointerTag, Self::AllocExtra>>, Self::PointerTag);
253258

254-
/// Return the "base" tag for the given *static* allocation: the one that is used for direct
255-
/// accesses to this static/const/fn allocation. If `id` is not a static allocation,
259+
/// Return the "base" tag for the given *global* allocation: the one that is used for direct
260+
/// accesses to this static/const/fn allocation. If `id` is not a global allocation,
256261
/// this will return an unusable tag (i.e., accesses will be UB)!
257262
///
258263
/// Expects `id` to be already canonical, if needed.
259-
fn tag_static_base_pointer(memory_extra: &Self::MemoryExtra, id: AllocId) -> Self::PointerTag;
264+
fn tag_global_base_pointer(memory_extra: &Self::MemoryExtra, id: AllocId) -> Self::PointerTag;
260265

261266
/// Executes a retagging operation
262267
#[inline]

0 commit comments

Comments
 (0)