Skip to content

Commit 2a54377

Browse files
committed
Auto merge of #155350 - matthiaskrgr:rollup-TOcnxP5, r=matthiaskrgr
Rollup of 6 pull requests Successful merges: - #147811 (naked functions: respect `function-sections`) - #154599 (report the `varargs_without_pattern` lint in deps) - #154970 (rustdoc: preserve `doc(cfg)` on locally re-exported type aliases) - #155326 (Disallow ZST allocations with `TypedArena`.) - #155340 (Handle nonnull pattern types in size skeleton) - #155347 (Add push_mut and new Layout methods to release notes)
2 parents e8e4541 + f1e9c98 commit 2a54377

16 files changed

Lines changed: 417 additions & 91 deletions

File tree

RELEASES.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,17 @@ Stabilized APIs
7878
- [`<*const T>::as_ref_unchecked`](https://doc.rust-lang.org/stable/std/primitive.pointer.html#method.as_ref_unchecked)
7979
- [`<*mut T>::as_ref_unchecked`](https://doc.rust-lang.org/stable/std/primitive.pointer.html#method.as_ref_unchecked-1)
8080
- [`<*mut T>::as_mut_unchecked`](https://doc.rust-lang.org/stable/std/primitive.pointer.html#method.as_mut_unchecked)
81+
- [`Vec::push_mut`](https://doc.rust-lang.org/stable/std/vec/struct.Vec.html#method.push_mut)
82+
- [`Vec::insert_mut`](https://doc.rust-lang.org/stable/std/vec/struct.Vec.html#method.insert_mut)
83+
- [`VecDeque::push_front_mut`](https://doc.rust-lang.org/stable/std/collections/struct.VecDeque.html#method.push_front_mut)
84+
- [`VecDeque::push_back_mut`](https://doc.rust-lang.org/stable/std/collections/struct.VecDeque.html#method.push_back_mut)
85+
- [`VecDeque::insert_mut`](https://doc.rust-lang.org/stable/std/collections/struct.VecDeque.html#method.insert_mut)
86+
- [`LinkedList::push_front_mut`](https://doc.rust-lang.org/stable/std/collections/struct.LinkedList.html#method.push_front_mut)
87+
- [`LinkedList::push_back_mut`](https://doc.rust-lang.org/stable/std/collections/struct.LinkedList.html#method.push_back_mut)
88+
- [`Layout::dangling_ptr`](https://doc.rust-lang.org/stable/std/alloc/struct.Layout.html#method.dangling_ptr)
89+
- [`Layout::repeat`](https://doc.rust-lang.org/stable/std/alloc/struct.Layout.html#method.repeat)
90+
- [`Layout::repeat_packed`](https://doc.rust-lang.org/stable/std/alloc/struct.Layout.html#method.repeat_packed)
91+
- [`Layout::extend_packed`](https://doc.rust-lang.org/stable/std/alloc/struct.Layout.html#method.extend_packed)
8192

8293

8394
These previously stable APIs are now stable in const contexts:

compiler/rustc_arena/src/lib.rs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -140,25 +140,19 @@ impl<T> TypedArena<T> {
140140
/// Allocates an object in the `TypedArena`, returning a reference to it.
141141
#[inline]
142142
pub fn alloc(&self, object: T) -> &mut T {
143+
assert!(size_of::<T>() != 0);
144+
143145
if self.ptr == self.end {
144146
self.grow(1)
145147
}
146148

147149
unsafe {
148-
if size_of::<T>() == 0 {
149-
self.ptr.set(self.ptr.get().wrapping_byte_add(1));
150-
let ptr = ptr::NonNull::<T>::dangling().as_ptr();
151-
// Don't drop the object. This `write` is equivalent to `forget`.
152-
ptr::write(ptr, object);
153-
&mut *ptr
154-
} else {
155-
let ptr = self.ptr.get();
156-
// Advance the pointer.
157-
self.ptr.set(self.ptr.get().add(1));
158-
// Write into uninitialized memory.
159-
ptr::write(ptr, object);
160-
&mut *ptr
161-
}
150+
let ptr = self.ptr.get();
151+
// Advance the pointer.
152+
self.ptr.set(self.ptr.get().add(1));
153+
// Write into uninitialized memory.
154+
ptr::write(ptr, object);
155+
&mut *ptr
162156
}
163157
}
164158

@@ -302,16 +296,10 @@ impl<T> TypedArena<T> {
302296
let end = self.ptr.get().addr();
303297
// We then calculate the number of elements to be dropped in the last chunk,
304298
// which is the filled area's length.
305-
let diff = if size_of::<T>() == 0 {
306-
// `T` is ZST. It can't have a drop flag, so the value here doesn't matter. We get
307-
// the number of zero-sized values in the last and only chunk, just out of caution.
308-
// Recall that `end` was incremented for each allocated value.
309-
end - start
310-
} else {
311-
// FIXME: this should *likely* use `offset_from`, but more
312-
// investigation is needed (including running tests in miri).
313-
(end - start) / size_of::<T>()
314-
};
299+
assert_ne!(size_of::<T>(), 0);
300+
// FIXME: this should *likely* use `offset_from`, but more
301+
// investigation is needed (including running tests in miri).
302+
let diff = (end - start) / size_of::<T>();
315303
// Pass that to the `destroy` method.
316304
unsafe {
317305
last_chunk.destroy(diff);

compiler/rustc_arena/src/tests.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ impl<T> TypedArena<T> {
2222
if let Some(last_chunk) = chunks_borrow.last_mut() {
2323
self.clear_last_chunk(last_chunk);
2424
let len = chunks_borrow.len();
25-
// If `T` is ZST, code below has no effect.
2625
for mut chunk in chunks_borrow.drain(..len - 1) {
2726
chunk.destroy(chunk.entries);
2827
}
@@ -117,18 +116,6 @@ fn test_noncopy() {
117116
}
118117
}
119118

120-
#[test]
121-
fn test_typed_arena_zero_sized() {
122-
let arena = TypedArena::default();
123-
#[cfg(not(miri))]
124-
const N: usize = 100000;
125-
#[cfg(miri)]
126-
const N: usize = 1000;
127-
for _ in 0..N {
128-
arena.alloc(());
129-
}
130-
}
131-
132119
#[test]
133120
fn test_typed_arena_clear() {
134121
let mut arena = TypedArena::default();
@@ -207,7 +194,8 @@ thread_local! {
207194
static DROP_COUNTER: Cell<u32> = Cell::new(0)
208195
}
209196

210-
struct SmallDroppable;
197+
#[allow(unused)]
198+
struct SmallDroppable(u8);
211199

212200
impl Drop for SmallDroppable {
213201
fn drop(&mut self) {
@@ -222,7 +210,7 @@ fn test_typed_arena_drop_small_count() {
222210
let arena: TypedArena<SmallDroppable> = TypedArena::default();
223211
for _ in 0..100 {
224212
// Allocate something with drop glue to make sure it doesn't leak.
225-
arena.alloc(SmallDroppable);
213+
arena.alloc(SmallDroppable(0));
226214
}
227215
// dropping
228216
};

compiler/rustc_codegen_ssa/src/mir/naked_asm.rs

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_middle::ty::{Instance, Ty, TyCtxt, TypeVisitableExt};
88
use rustc_middle::{bug, ty};
99
use rustc_span::sym;
1010
use rustc_target::callconv::{ArgAbi, FnAbi, PassMode};
11-
use rustc_target::spec::{Arch, BinaryFormat};
11+
use rustc_target::spec::{Arch, BinaryFormat, Env};
1212

1313
use crate::common;
1414
use crate::mir::AsmCodegenMethods;
@@ -128,6 +128,8 @@ fn prefix_and_suffix<'tcx>(
128128

129129
let is_arm = tcx.sess.target.arch == Arch::Arm;
130130
let is_thumb = tcx.sess.unstable_target_features.contains(&sym::thumb_mode);
131+
let function_sections =
132+
tcx.sess.opts.unstable_opts.function_sections.unwrap_or(tcx.sess.target.function_sections);
131133

132134
// If we're compiling the compiler-builtins crate, e.g., the equivalent of
133135
// compiler-rt, then we want to implicitly compile everything with hidden
@@ -218,8 +220,6 @@ fn prefix_and_suffix<'tcx>(
218220
let mut end = String::new();
219221
match asm_binary_format {
220222
BinaryFormat::Elf => {
221-
let section = link_section.unwrap_or_else(|| format!(".text.{asm_name}"));
222-
223223
let progbits = match is_arm {
224224
true => "%progbits",
225225
false => "@progbits",
@@ -230,7 +230,13 @@ fn prefix_and_suffix<'tcx>(
230230
false => "@function",
231231
};
232232

233-
writeln!(begin, ".pushsection {section},\"ax\", {progbits}").unwrap();
233+
if let Some(section) = &link_section {
234+
writeln!(begin, ".pushsection {section},\"ax\", {progbits}").unwrap();
235+
} else if function_sections {
236+
writeln!(begin, ".pushsection .text.{asm_name},\"ax\", {progbits}").unwrap();
237+
} else {
238+
writeln!(begin, ".text").unwrap();
239+
}
234240
writeln!(begin, ".balign {align_bytes}").unwrap();
235241
write_linkage(&mut begin).unwrap();
236242
match visibility {
@@ -249,14 +255,20 @@ fn prefix_and_suffix<'tcx>(
249255
// pattern match on assembly generated by LLVM.
250256
writeln!(end, ".Lfunc_end_{asm_name}:").unwrap();
251257
writeln!(end, ".size {asm_name}, . - {asm_name}").unwrap();
252-
writeln!(end, ".popsection").unwrap();
258+
if link_section.is_some() || function_sections {
259+
writeln!(end, ".popsection").unwrap();
260+
}
253261
if !arch_suffix.is_empty() {
254262
writeln!(end, "{}", arch_suffix).unwrap();
255263
}
256264
}
257265
BinaryFormat::MachO => {
258-
let section = link_section.unwrap_or_else(|| "__TEXT,__text".to_string());
259-
writeln!(begin, ".pushsection {},regular,pure_instructions", section).unwrap();
266+
// NOTE: LLVM ignores `-Zfunction-sections` on macos. Instead the Mach-O symbol
267+
// subsection splitting feature is used, which can be enabled with the
268+
// `.subsections_via_symbols` global directive. LLVM already enables this directive.
269+
if let Some(section) = &link_section {
270+
writeln!(begin, ".pushsection {section},regular,pure_instructions").unwrap();
271+
}
260272
writeln!(begin, ".balign {align_bytes}").unwrap();
261273
write_linkage(&mut begin).unwrap();
262274
match visibility {
@@ -267,7 +279,9 @@ fn prefix_and_suffix<'tcx>(
267279

268280
writeln!(end).unwrap();
269281
writeln!(end, ".Lfunc_end_{asm_name}:").unwrap();
270-
writeln!(end, ".popsection").unwrap();
282+
if link_section.is_some() {
283+
writeln!(end, ".popsection").unwrap();
284+
}
271285
if !arch_suffix.is_empty() {
272286
writeln!(end, "{}", arch_suffix).unwrap();
273287
}
@@ -278,8 +292,30 @@ fn prefix_and_suffix<'tcx>(
278292
writeln!(begin, ".type 32").unwrap();
279293
writeln!(begin, ".endef").unwrap();
280294

281-
let section = link_section.unwrap_or_else(|| format!(".text.{asm_name}"));
282-
writeln!(begin, ".pushsection {},\"xr\"", section).unwrap();
295+
if let Some(section) = &link_section {
296+
writeln!(begin, ".section {section},\"xr\"").unwrap()
297+
} else if !function_sections {
298+
// Function sections are enabled by default on MSVC and windows-gnullvm,
299+
// but disabled by default on GNU.
300+
writeln!(begin, ".text").unwrap();
301+
} else {
302+
// LLVM uses an extension to the section directive to support defining multiple
303+
// sections with the same name and comdat. It adds `unique,<id>` at the end of the
304+
// `.section` directive. We have no way of generating that unique ID here, so don't
305+
// emit it.
306+
//
307+
// See https://llvm.org/docs/Extensions.html#id2.
308+
match &tcx.sess.target.options.env {
309+
Env::Gnu => {
310+
writeln!(begin, ".section .text${asm_name},\"xr\",one_only,{asm_name}")
311+
.unwrap();
312+
}
313+
Env::Msvc => {
314+
writeln!(begin, ".section .text,\"xr\",one_only,{asm_name}").unwrap();
315+
}
316+
other => bug!("invalid coff env {other:?}"),
317+
}
318+
}
283319
write_linkage(&mut begin).unwrap();
284320
writeln!(begin, ".balign {align_bytes}").unwrap();
285321
writeln!(begin, "{asm_name}:").unwrap();

compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5560,15 +5560,16 @@ declare_lint! {
55605560
///
55615561
/// ### Example
55625562
///
5563-
/// ```rust
5563+
#[cfg_attr(bootstrap, doc = "```rust")]
5564+
#[cfg_attr(not(bootstrap), doc = "```rust,compile_fail")]
55645565
/// // Using `...` in non-foreign function definitions is unstable, however stability is
55655566
/// // currently only checked after attributes are expanded, so using `#[cfg(false)]` here will
55665567
/// // allow this to compile on stable Rust.
55675568
/// #[cfg(false)]
55685569
/// fn foo(...) {
55695570
///
55705571
/// }
5571-
/// ```
5572+
#[doc = "```"]
55725573
///
55735574
/// {{produces}}
55745575
///
@@ -5592,11 +5593,11 @@ declare_lint! {
55925593
///
55935594
/// [future-incompatible]: ../index.md#future-incompatible-lints
55945595
pub VARARGS_WITHOUT_PATTERN,
5595-
Warn,
5596+
Deny,
55965597
"detects usage of `...` arguments without a pattern in non-foreign items",
55975598
@future_incompatible = FutureIncompatibleInfo {
55985599
reason: fcw!(FutureReleaseError #145544),
5599-
report_in_deps: false,
5600+
report_in_deps: true,
56005601
};
56015602
}
56025603

compiler/rustc_middle/src/ty/layout.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -508,8 +508,21 @@ impl<'tcx> SizeSkeleton<'tcx> {
508508
}
509509
}
510510

511-
// Pattern types are always the same size as their base.
512-
ty::Pat(base, _) => SizeSkeleton::compute(base, tcx, typing_env),
511+
ty::Pat(base, pat) => {
512+
// Pattern types are always the same size as their base.
513+
let base = SizeSkeleton::compute(base, tcx, typing_env);
514+
match *pat {
515+
ty::PatternKind::Range { .. } | ty::PatternKind::Or(_) => base,
516+
// But in the case of `!null` patterns we need to note that in the
517+
// raw pointer.
518+
ty::PatternKind::NotNull => match base? {
519+
SizeSkeleton::Known(..) | SizeSkeleton::Generic(_) => base,
520+
SizeSkeleton::Pointer { non_zero: _, tail } => {
521+
Ok(SizeSkeleton::Pointer { non_zero: true, tail })
522+
}
523+
},
524+
}
525+
}
513526

514527
_ => Err(err),
515528
}

src/librustdoc/clean/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,8 @@ fn generate_item_with_correct_attrs(
235235
attrs.extend(get_all_import_attributes(cx, import_id, def_id, is_inline));
236236
is_inline = is_inline || import_is_inline;
237237
}
238-
add_without_unwanted_attributes(&mut attrs, target_attrs, is_inline, None);
238+
let keep_target_cfg = is_inline || matches!(kind, ItemKind::TypeAliasItem(..));
239+
add_without_unwanted_attributes(&mut attrs, target_attrs, keep_target_cfg, None);
239240
attrs
240241
} else {
241242
// We only keep the item's attributes.

src/tools/run-make-support/src/external_deps/rustc.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,16 @@ impl Rustc {
449449
self.cmd.arg("-Zcodegen-source-order");
450450
self
451451
}
452+
453+
/// Specify `-Z function-sections={yes, no}`.
454+
pub fn function_sections(&mut self, enable: bool) -> &mut Self {
455+
let flag = match enable {
456+
true => "-Zfunction-sections=yes",
457+
false => "-Zfunction-sections=no",
458+
};
459+
self.cmd.arg(flag);
460+
self
461+
}
452462
}
453463

454464
/// Query the sysroot path corresponding `rustc --print=sysroot`.

0 commit comments

Comments
 (0)