Skip to content

Commit 564758c

Browse files
committed
Auto merge of #69829 - Centril:rollup-lm5lzsq, r=Centril
Rollup of 7 pull requests Successful merges: - #69631 (remove non-sysroot sources from rust-src component) - #69646 (Miri visitor: detect primitive types based on type, not layout (also, more tests)) - #69651 (Try to ensure usize marker does not get merged) - #69668 (More documentation and simplification of BTreeMap's internals) - #69771 (Cleanup E0390 explanation) - #69777 (Add missing ` in doc for File::with_options()) - #69812 (Refactorings to method/probe.rs and CrateId) Failed merges: r? @ghost
2 parents 1d5241c + 06689e2 commit 564758c

File tree

21 files changed

+553
-456
lines changed

21 files changed

+553
-456
lines changed

src/bootstrap/dist.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1002,8 +1002,6 @@ impl Step for Src {
10021002
"src/tools/rustc-std-workspace-core",
10031003
"src/tools/rustc-std-workspace-alloc",
10041004
"src/tools/rustc-std-workspace-std",
1005-
"src/librustc",
1006-
"src/librustc_ast",
10071005
];
10081006

10091007
copy_src_dirs(builder, &std_src_dirs[..], &[], &dst_src);

src/liballoc/collections/btree/node.rs

+19-14
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,15 @@ impl<K, V> InternalNode<K, V> {
153153
}
154154
}
155155

156-
/// An owned pointer to a node. This basically is either `Box<LeafNode<K, V>>` or
157-
/// `Box<InternalNode<K, V>>`. However, it contains no information as to which of the two types
158-
/// of nodes is actually behind the box, and, partially due to this lack of information, has no
159-
/// destructor.
156+
/// A managed, non-null pointer to a node. This is either an owned pointer to
157+
/// `LeafNode<K, V>`, an owned pointer to `InternalNode<K, V>`, or a (not owned)
158+
/// pointer to `NodeHeader<(), ()` (more specifically, the pointer to EMPTY_ROOT_NODE).
159+
/// All of these types have a `NodeHeader<K, V>` prefix, meaning that they have at
160+
/// least the same size as `NodeHeader<K, V>` and store the same kinds of data at the same
161+
/// offsets; and they have a pointer alignment at least as large as `NodeHeader<K, V>`'s.
162+
/// However, `BoxedNode` contains no information as to which of the three types
163+
/// of nodes it actually contains, and, partially due to this lack of information,
164+
/// has no destructor.
160165
struct BoxedNode<K, V> {
161166
ptr: Unique<LeafNode<K, V>>,
162167
}
@@ -167,9 +172,7 @@ impl<K, V> BoxedNode<K, V> {
167172
}
168173

169174
fn from_internal(node: Box<InternalNode<K, V>>) -> Self {
170-
unsafe {
171-
BoxedNode { ptr: Unique::new_unchecked(Box::into_raw(node) as *mut LeafNode<K, V>) }
172-
}
175+
BoxedNode { ptr: Box::into_unique(node).cast() }
173176
}
174177

175178
unsafe fn from_ptr(ptr: NonNull<LeafNode<K, V>>) -> Self {
@@ -181,32 +184,33 @@ impl<K, V> BoxedNode<K, V> {
181184
}
182185
}
183186

184-
/// An owned tree. Note that despite being owned, this does not have a destructor,
185-
/// and must be cleaned up manually.
187+
/// Either an owned tree or a shared, empty tree. Note that this does not have a destructor,
188+
/// and must be cleaned up manually if it is an owned tree.
186189
pub struct Root<K, V> {
187190
node: BoxedNode<K, V>,
191+
/// The number of levels below the root node.
188192
height: usize,
189193
}
190194

191195
unsafe impl<K: Sync, V: Sync> Sync for Root<K, V> {}
192196
unsafe impl<K: Send, V: Send> Send for Root<K, V> {}
193197

194198
impl<K, V> Root<K, V> {
199+
/// Whether the instance of `Root` wraps a shared, empty root node. If not,
200+
/// the entire tree is uniquely owned by the owner of the `Root` instance.
195201
pub fn is_shared_root(&self) -> bool {
196202
self.as_ref().is_shared_root()
197203
}
198204

205+
/// Returns a shared tree, wrapping a shared root node that is eternally empty.
199206
pub fn shared_empty_root() -> Self {
200207
Root {
201-
node: unsafe {
202-
BoxedNode::from_ptr(NonNull::new_unchecked(
203-
&EMPTY_ROOT_NODE as *const _ as *const LeafNode<K, V> as *mut _,
204-
))
205-
},
208+
node: unsafe { BoxedNode::from_ptr(NonNull::from(&EMPTY_ROOT_NODE).cast()) },
206209
height: 0,
207210
}
208211
}
209212

213+
/// Returns a new owned tree, with its own root node that is initially empty.
210214
pub fn new_leaf() -> Self {
211215
Root { node: BoxedNode::from_leaf(Box::new(unsafe { LeafNode::new() })), height: 0 }
212216
}
@@ -310,6 +314,7 @@ impl<K, V> Root<K, V> {
310314
/// so '&LeafNode` or `&InternalNode` pointing to the shared root is undefined behavior.
311315
/// Turning this into a `NodeHeader` reference is always safe.
312316
pub struct NodeRef<BorrowType, K, V, Type> {
317+
/// The number of levels below the node.
313318
height: usize,
314319
node: NonNull<LeafNode<K, V>>,
315320
// `root` is null unless the borrow type is `Mut`

src/libcore/fmt/mod.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,18 @@ pub struct ArgumentV1<'a> {
264264
// could have been miscompiled. In practice, we never call as_usize on non-usize
265265
// containing data (as a matter of static generation of the formatting
266266
// arguments), so this is merely an additional check.
267+
//
268+
// We primarily want to ensure that the function pointer at `USIZE_MARKER` has
269+
// an address corresponding *only* to functions that also take `&usize` as their
270+
// first argument. The read_volatile here ensures that we can safely ready out a
271+
// usize from the passed reference and that this address does not point at a
272+
// non-usize taking function.
267273
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
268-
static USIZE_MARKER: fn(&usize, &mut Formatter<'_>) -> Result = |_, _| loop {};
274+
static USIZE_MARKER: fn(&usize, &mut Formatter<'_>) -> Result = |ptr, _| {
275+
// SAFETY: ptr is a reference
276+
let _v: usize = unsafe { crate::ptr::read_volatile(ptr) };
277+
loop {}
278+
};
269279

270280
impl<'a> ArgumentV1<'a> {
271281
#[doc(hidden)]

src/librustc_error_codes/error_codes/E0390.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
You tried to implement methods for a primitive type. Erroneous code example:
1+
A method was implemented on a primitive type.
2+
3+
Erroneous code example:
24

35
```compile_fail,E0390
46
struct Foo {

0 commit comments

Comments
 (0)