Skip to content

Commit 32c9b7b

Browse files
committed
Auto merge of #87351 - ehuss:rollup-pga85az, r=ehuss
Rollup of 5 pull requests Successful merges: - #81864 (docs: GlobalAlloc: completely replace example with one that works) - #87024 (rustdoc: show count of item contents when hidden) - #87278 (:arrow_up: rust-analyzer) - #87326 (Update cargo) - #87346 (Rename force-warns to force-warn) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 05f2326 + 43e2575 commit 32c9b7b

39 files changed

+173
-75
lines changed

compiler/rustc_lint/src/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ impl LintStore {
370370
match level {
371371
Level::Allow => "-A",
372372
Level::Warn => "-W",
373-
Level::ForceWarn => "--force-warns",
373+
Level::ForceWarn => "--force-warn",
374374
Level::Deny => "-D",
375375
Level::Forbid => "-F",
376376
},

compiler/rustc_lint_defs/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl Level {
6464
match self {
6565
Level::Allow => "allow",
6666
Level::Warn => "warn",
67-
Level::ForceWarn => "force-warns",
67+
Level::ForceWarn => "force-warn",
6868
Level::Deny => "deny",
6969
Level::Forbid => "forbid",
7070
}

compiler/rustc_middle/src/lint.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ pub fn struct_lint_level<'s, 'd>(
288288
Level::Deny => "-D",
289289
Level::Forbid => "-F",
290290
Level::Allow => "-A",
291-
Level::ForceWarn => "--force-warns",
291+
Level::ForceWarn => "--force-warn",
292292
};
293293
let hyphen_case_lint_name = name.replace("_", "-");
294294
if lint_flag_val.as_str() == name {

compiler/rustc_session/src/config.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,7 @@ pub fn rustc_short_optgroups() -> Vec<RustcOptGroup> {
11011101
),
11021102
opt::multi_s(
11031103
"",
1104-
"force-warns",
1104+
"force-warn",
11051105
"Specifiy lints that should warn even if \
11061106
they are allowed somewhere else",
11071107
"LINT",
@@ -1175,11 +1175,11 @@ pub fn get_cmd_lint_options(
11751175
let mut lint_opts_with_position = vec![];
11761176
let mut describe_lints = false;
11771177

1178-
if !debugging_opts.unstable_options && matches.opt_present("force-warns") {
1178+
if !debugging_opts.unstable_options && matches.opt_present("force-warn") {
11791179
early_error(
11801180
error_format,
11811181
"the `-Z unstable-options` flag must also be passed to enable \
1182-
the flag `--force-warns=lints`",
1182+
the flag `--force-warn=lints`",
11831183
);
11841184
}
11851185

library/core/src/alloc/global.rs

+56-11
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,69 @@ use crate::ptr;
2020
///
2121
/// # Example
2222
///
23-
/// ```no_run
24-
/// use std::alloc::{GlobalAlloc, Layout, alloc};
23+
/// ```
24+
/// use std::alloc::{GlobalAlloc, Layout};
25+
/// use std::cell::UnsafeCell;
2526
/// use std::ptr::null_mut;
27+
/// use std::sync::atomic::{
28+
/// AtomicUsize,
29+
/// Ordering::{Acquire, SeqCst},
30+
/// };
2631
///
27-
/// struct MyAllocator;
28-
///
29-
/// unsafe impl GlobalAlloc for MyAllocator {
30-
/// unsafe fn alloc(&self, _layout: Layout) -> *mut u8 { null_mut() }
31-
/// unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {}
32+
/// const ARENA_SIZE: usize = 128 * 1024;
33+
/// const MAX_SUPPORTED_ALIGN: usize = 4096;
34+
/// #[repr(C, align(4096))] // 4096 == MAX_SUPPORTED_ALIGN
35+
/// struct SimpleAllocator {
36+
/// arena: UnsafeCell<[u8; ARENA_SIZE]>,
37+
/// remaining: AtomicUsize, // we allocate from the top, counting down
3238
/// }
3339
///
3440
/// #[global_allocator]
35-
/// static A: MyAllocator = MyAllocator;
41+
/// static ALLOCATOR: SimpleAllocator = SimpleAllocator {
42+
/// arena: UnsafeCell::new([0x55; ARENA_SIZE]),
43+
/// remaining: AtomicUsize::new(ARENA_SIZE),
44+
/// };
3645
///
37-
/// fn main() {
38-
/// unsafe {
39-
/// assert!(alloc(Layout::new::<u32>()).is_null())
46+
/// unsafe impl Sync for SimpleAllocator {}
47+
///
48+
/// unsafe impl GlobalAlloc for SimpleAllocator {
49+
/// unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
50+
/// let size = layout.size();
51+
/// let align = layout.align();
52+
///
53+
/// // `Layout` contract forbids making a `Layout` with align=0, or align not power of 2.
54+
/// // So we can safely use a mask to ensure alignment without worrying about UB.
55+
/// let align_mask_to_round_down = !(align - 1);
56+
///
57+
/// if align > MAX_SUPPORTED_ALIGN {
58+
/// return null_mut();
59+
/// }
60+
///
61+
/// let mut allocated = 0;
62+
/// if self
63+
/// .remaining
64+
/// .fetch_update(SeqCst, SeqCst, |mut remaining| {
65+
/// if size > remaining {
66+
/// return None;
67+
/// }
68+
/// remaining -= size;
69+
/// remaining &= align_mask_to_round_down;
70+
/// allocated = remaining;
71+
/// Some(remaining)
72+
/// })
73+
/// .is_err()
74+
/// {
75+
/// return null_mut();
76+
/// };
77+
/// (self.arena.get() as *mut u8).add(allocated)
4078
/// }
79+
/// unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {}
80+
/// }
81+
///
82+
/// fn main() {
83+
/// let _s = format!("allocating a string!");
84+
/// let currently = ALLOCATOR.remaining.load(Acquire);
85+
/// println!("allocated so far: {}", ARENA_SIZE - currently);
4186
/// }
4287
/// ```
4388
///

src/doc/unstable-book/src/compiler-flags/force-warns.md renamed to src/doc/unstable-book/src/compiler-flags/force-warn.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# `force-warns`
1+
# `force-warn`
22

33
The tracking issue for this feature is: [#85512](https://github.com/rust-lang/rust/issues/85512).
44

55
------------------------
66

7-
This feature allows you to cause any lint to produce a warning even if the lint has a different level by default or another level is set somewhere else. For instance, the `force-warns` option can be used to make a lint (e.g., `dead_code`) produce a warning even if that lint is allowed in code with `#![allow(dead_code)]`.
7+
This feature allows you to cause any lint to produce a warning even if the lint has a different level by default or another level is set somewhere else. For instance, the `force-warn` option can be used to make a lint (e.g., `dead_code`) produce a warning even if that lint is allowed in code with `#![allow(dead_code)]`.
88

99
## Example
1010

@@ -18,4 +18,4 @@ fn dead_function() {}
1818
fn main() {}
1919
```
2020

21-
We can force a warning to be produced by providing `--force-warns dead_code` to rustc.
21+
We can force a warning to be produced by providing `--force-warn dead_code` to rustc.

src/librustdoc/html/render/print_item.rs

+34-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use clean::AttributesExt;
22

33
use std::cmp::Ordering;
4+
use std::fmt;
45

56
use rustc_data_structures::fx::FxHashMap;
67
use rustc_hir as hir;
@@ -155,7 +156,7 @@ fn should_hide_fields(n_fields: usize) -> bool {
155156
n_fields > 12
156157
}
157158

158-
fn toggle_open(w: &mut Buffer, text: &str) {
159+
fn toggle_open(w: &mut Buffer, text: impl fmt::Display) {
159160
write!(
160161
w,
161162
"<details class=\"rustdoc-toggle type-contents-toggle\">\
@@ -481,6 +482,9 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
481482
let consts = t.items.iter().filter(|m| m.is_associated_const()).collect::<Vec<_>>();
482483
let required = t.items.iter().filter(|m| m.is_ty_method()).collect::<Vec<_>>();
483484
let provided = t.items.iter().filter(|m| m.is_method()).collect::<Vec<_>>();
485+
let count_types = types.len();
486+
let count_consts = consts.len();
487+
let count_methods = required.len() + provided.len();
484488

485489
// Output the trait definition
486490
wrap_into_docblock(w, |w| {
@@ -511,9 +515,12 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
511515
let mut toggle = false;
512516

513517
// If there are too many associated types, hide _everything_
514-
if should_hide_fields(types.len()) {
518+
if should_hide_fields(count_types) {
515519
toggle = true;
516-
toggle_open(w, "associated items");
520+
toggle_open(
521+
w,
522+
format_args!("{} associated items", count_types + count_consts + count_methods),
523+
);
517524
}
518525
for t in &types {
519526
render_assoc_item(w, t, AssocItemLink::Anchor(None), ItemType::Trait, cx);
@@ -523,9 +530,18 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
523530
// We also do this if the types + consts is large because otherwise we could
524531
// render a bunch of types and _then_ a bunch of consts just because both were
525532
// _just_ under the limit
526-
if !toggle && should_hide_fields(types.len() + consts.len()) {
533+
if !toggle && should_hide_fields(count_types + count_consts) {
527534
toggle = true;
528-
toggle_open(w, "associated constants and methods");
535+
toggle_open(
536+
w,
537+
format_args!(
538+
"{} associated constant{} and {} method{}",
539+
count_consts,
540+
pluralize(count_consts),
541+
count_methods,
542+
pluralize(count_methods),
543+
),
544+
);
529545
}
530546
if !types.is_empty() && !consts.is_empty() {
531547
w.write_str("\n");
@@ -534,9 +550,9 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
534550
render_assoc_item(w, t, AssocItemLink::Anchor(None), ItemType::Trait, cx);
535551
w.write_str(";\n");
536552
}
537-
if !toggle && should_hide_fields(required.len() + provided.len()) {
553+
if !toggle && should_hide_fields(count_methods) {
538554
toggle = true;
539-
toggle_open(w, "methods");
555+
toggle_open(w, format_args!("{} methods", count_methods));
540556
}
541557
if !consts.is_empty() && !required.is_empty() {
542558
w.write_str("\n");
@@ -933,9 +949,10 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum
933949
w.write_str(" {}");
934950
} else {
935951
w.write_str(" {\n");
936-
let toggle = should_hide_fields(e.variants.len());
952+
let count_variants = e.variants.len();
953+
let toggle = should_hide_fields(count_variants);
937954
if toggle {
938-
toggle_open(w, "variants");
955+
toggle_open(w, format_args!("{} variants", count_variants));
939956
}
940957
for v in &e.variants {
941958
w.write_str(" ");
@@ -1012,7 +1029,8 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum
10121029

10131030
use crate::clean::Variant;
10141031
if let clean::VariantItem(Variant::Struct(ref s)) = *variant.kind {
1015-
toggle_open(w, "fields");
1032+
let count_fields = s.fields.len();
1033+
toggle_open(w, format_args!("{} field{}", count_fields, pluralize(count_fields)));
10161034
let variant_id = cx.derive_id(format!(
10171035
"{}.{}.fields",
10181036
ItemType::Variant,
@@ -1385,7 +1403,7 @@ fn render_union(
13851403
fields.iter().filter(|f| matches!(*f.kind, clean::StructFieldItem(..))).count();
13861404
let toggle = should_hide_fields(count_fields);
13871405
if toggle {
1388-
toggle_open(w, "fields");
1406+
toggle_open(w, format_args!("{} fields", count_fields));
13891407
}
13901408

13911409
for field in fields {
@@ -1441,7 +1459,7 @@ fn render_struct(
14411459
let has_visible_fields = count_fields > 0;
14421460
let toggle = should_hide_fields(count_fields);
14431461
if toggle {
1444-
toggle_open(w, "fields");
1462+
toggle_open(w, format_args!("{} fields", count_fields));
14451463
}
14461464
for field in fields {
14471465
if let clean::StructFieldItem(ref ty) = *field.kind {
@@ -1618,3 +1636,7 @@ fn document_type_layout(w: &mut Buffer, cx: &Context<'_>, ty_def_id: DefId) {
16181636

16191637
writeln!(w, "</div>");
16201638
}
1639+
1640+
fn pluralize(count: usize) -> &'static str {
1641+
if count > 1 { "s" } else { "" }
1642+
}

src/librustdoc/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -511,10 +511,10 @@ fn opts() -> Vec<RustcOptGroup> {
511511
"LEVEL",
512512
)
513513
}),
514-
unstable("force-warns", |o| {
514+
unstable("force-warn", |o| {
515515
o.optopt(
516516
"",
517-
"force-warns",
517+
"force-warn",
518518
"Lints that will warn even if allowed somewhere else",
519519
"LINTS",
520520
)

src/test/run-make/unstable-flag-required/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
all:
44
$(RUSTDOC) --output-format=json x.html 2>&1 | diff - output-format-json.stderr
5-
$(RUSTC) --force-warns dead_code x.rs 2>&1 | diff - force-warns.stderr
5+
$(RUSTC) --force-warn dead_code x.rs 2>&1 | diff - force-warn.stderr
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
error: the `-Z unstable-options` flag must also be passed to enable the flag `--force-warns=lints`
1+
error: the `-Z unstable-options` flag must also be passed to enable the flag `--force-warn=lints`
22

src/test/rustdoc/toggle-item-contents.rs

+38-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub struct PubStruct {
99

1010
// @has 'toggle_item_contents/struct.BigPubStruct.html'
1111
// @count - '//details[@class="rustdoc-toggle type-contents-toggle"]' 1
12-
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show fields'
12+
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show 13 fields'
1313
pub struct BigPubStruct {
1414
pub a: usize,
1515
pub b: usize,
@@ -28,7 +28,7 @@ pub struct BigPubStruct {
2828

2929
// @has 'toggle_item_contents/union.BigUnion.html'
3030
// @count - '//details[@class="rustdoc-toggle type-contents-toggle"]' 1
31-
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show fields'
31+
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show 13 fields'
3232
pub union BigUnion {
3333
pub a: usize,
3434
pub b: usize,
@@ -63,7 +63,7 @@ pub struct PrivStruct {
6363

6464
// @has 'toggle_item_contents/enum.Enum.html'
6565
// @count - '//details[@class="rustdoc-toggle type-contents-toggle"]' 1
66-
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show fields'
66+
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show 2 fields'
6767
pub enum Enum {
6868
A, B, C,
6969
D {
@@ -72,9 +72,19 @@ pub enum Enum {
7272
}
7373
}
7474

75+
// @has 'toggle_item_contents/enum.EnumStructVariant.html'
76+
// @count - '//details[@class="rustdoc-toggle type-contents-toggle"]' 1
77+
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show 1 field'
78+
pub enum EnumStructVariant {
79+
A, B, C,
80+
D {
81+
a: u8,
82+
}
83+
}
84+
7585
// @has 'toggle_item_contents/enum.LargeEnum.html'
7686
// @count - '//details[@class="rustdoc-toggle type-contents-toggle"]' 1
77-
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show variants'
87+
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show 13 variants'
7888
pub enum LargeEnum {
7989
A, B, C, D, E, F(u8), G, H, I, J, K, L, M
8090
}
@@ -90,7 +100,7 @@ pub trait Trait {
90100

91101
// @has 'toggle_item_contents/trait.GinormousTrait.html'
92102
// @count - '//details[@class="rustdoc-toggle type-contents-toggle"]' 1
93-
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show associated items'
103+
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show 16 associated items'
94104
pub trait GinormousTrait {
95105
type A;
96106
type B;
@@ -113,7 +123,7 @@ pub trait GinormousTrait {
113123

114124
// @has 'toggle_item_contents/trait.HugeTrait.html'
115125
// @count - '//details[@class="rustdoc-toggle type-contents-toggle"]' 1
116-
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show associated constants and methods'
126+
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show 12 associated constants and 2 methods'
117127
pub trait HugeTrait {
118128
type A;
119129
const M: usize = 1;
@@ -133,9 +143,30 @@ pub trait HugeTrait {
133143
fn bar();
134144
}
135145

146+
// @has 'toggle_item_contents/trait.GiganticTrait.html'
147+
// @count - '//details[@class="rustdoc-toggle type-contents-toggle"]' 1
148+
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show 1 associated constant and 1 method'
149+
pub trait GiganticTrait {
150+
type A;
151+
type B;
152+
type C;
153+
type D;
154+
type E;
155+
type F;
156+
type G;
157+
type H;
158+
type I;
159+
type J;
160+
type K;
161+
type L;
162+
const M: usize = 1;
163+
#[must_use]
164+
fn foo();
165+
}
166+
136167
// @has 'toggle_item_contents/trait.BigTrait.html'
137168
// @count - '//details[@class="rustdoc-toggle type-contents-toggle"]' 1
138-
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show methods'
169+
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show 14 methods'
139170
pub trait BigTrait {
140171
type A;
141172
#[must_use]

0 commit comments

Comments
 (0)