Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 5 pull requests #87351

Merged
merged 12 commits into from
Jul 21, 2021
Merged
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ impl LintStore {
match level {
Level::Allow => "-A",
Level::Warn => "-W",
Level::ForceWarn => "--force-warns",
Level::ForceWarn => "--force-warn",
Level::Deny => "-D",
Level::Forbid => "-F",
},
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint_defs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl Level {
match self {
Level::Allow => "allow",
Level::Warn => "warn",
Level::ForceWarn => "force-warns",
Level::ForceWarn => "force-warn",
Level::Deny => "deny",
Level::Forbid => "forbid",
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ pub fn struct_lint_level<'s, 'd>(
Level::Deny => "-D",
Level::Forbid => "-F",
Level::Allow => "-A",
Level::ForceWarn => "--force-warns",
Level::ForceWarn => "--force-warn",
};
let hyphen_case_lint_name = name.replace("_", "-");
if lint_flag_val.as_str() == name {
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,7 @@ pub fn rustc_short_optgroups() -> Vec<RustcOptGroup> {
),
opt::multi_s(
"",
"force-warns",
"force-warn",
"Specifiy lints that should warn even if \
they are allowed somewhere else",
"LINT",
Expand Down Expand Up @@ -1175,11 +1175,11 @@ pub fn get_cmd_lint_options(
let mut lint_opts_with_position = vec![];
let mut describe_lints = false;

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

Expand Down
67 changes: 56 additions & 11 deletions library/core/src/alloc/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,69 @@ use crate::ptr;
///
/// # Example
///
/// ```no_run
/// use std::alloc::{GlobalAlloc, Layout, alloc};
/// ```
/// use std::alloc::{GlobalAlloc, Layout};
/// use std::cell::UnsafeCell;
/// use std::ptr::null_mut;
/// use std::sync::atomic::{
/// AtomicUsize,
/// Ordering::{Acquire, SeqCst},
/// };
///
/// struct MyAllocator;
///
/// unsafe impl GlobalAlloc for MyAllocator {
/// unsafe fn alloc(&self, _layout: Layout) -> *mut u8 { null_mut() }
/// unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {}
/// const ARENA_SIZE: usize = 128 * 1024;
/// const MAX_SUPPORTED_ALIGN: usize = 4096;
/// #[repr(C, align(4096))] // 4096 == MAX_SUPPORTED_ALIGN
/// struct SimpleAllocator {
/// arena: UnsafeCell<[u8; ARENA_SIZE]>,
/// remaining: AtomicUsize, // we allocate from the top, counting down
/// }
///
/// #[global_allocator]
/// static A: MyAllocator = MyAllocator;
/// static ALLOCATOR: SimpleAllocator = SimpleAllocator {
/// arena: UnsafeCell::new([0x55; ARENA_SIZE]),
/// remaining: AtomicUsize::new(ARENA_SIZE),
/// };
///
/// fn main() {
/// unsafe {
/// assert!(alloc(Layout::new::<u32>()).is_null())
/// unsafe impl Sync for SimpleAllocator {}
///
/// unsafe impl GlobalAlloc for SimpleAllocator {
/// unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
/// let size = layout.size();
/// let align = layout.align();
///
/// // `Layout` contract forbids making a `Layout` with align=0, or align not power of 2.
/// // So we can safely use a mask to ensure alignment without worrying about UB.
/// let align_mask_to_round_down = !(align - 1);
///
/// if align > MAX_SUPPORTED_ALIGN {
/// return null_mut();
/// }
///
/// let mut allocated = 0;
/// if self
/// .remaining
/// .fetch_update(SeqCst, SeqCst, |mut remaining| {
/// if size > remaining {
/// return None;
/// }
/// remaining -= size;
/// remaining &= align_mask_to_round_down;
/// allocated = remaining;
/// Some(remaining)
/// })
/// .is_err()
/// {
/// return null_mut();
/// };
/// (self.arena.get() as *mut u8).add(allocated)
/// }
/// unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {}
/// }
///
/// fn main() {
/// let _s = format!("allocating a string!");
/// let currently = ALLOCATOR.remaining.load(Acquire);
/// println!("allocated so far: {}", ARENA_SIZE - currently);
/// }
/// ```
///
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# `force-warns`
# `force-warn`

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

------------------------

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)]`.
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)]`.

## Example

Expand All @@ -18,4 +18,4 @@ fn dead_function() {}
fn main() {}
```

We can force a warning to be produced by providing `--force-warns dead_code` to rustc.
We can force a warning to be produced by providing `--force-warn dead_code` to rustc.
46 changes: 34 additions & 12 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use clean::AttributesExt;

use std::cmp::Ordering;
use std::fmt;

use rustc_data_structures::fx::FxHashMap;
use rustc_hir as hir;
Expand Down Expand Up @@ -155,7 +156,7 @@ fn should_hide_fields(n_fields: usize) -> bool {
n_fields > 12
}

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

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

// If there are too many associated types, hide _everything_
if should_hide_fields(types.len()) {
if should_hide_fields(count_types) {
toggle = true;
toggle_open(w, "associated items");
toggle_open(
w,
format_args!("{} associated items", count_types + count_consts + count_methods),
);
}
for t in &types {
render_assoc_item(w, t, AssocItemLink::Anchor(None), ItemType::Trait, cx);
Expand All @@ -523,9 +530,18 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
// We also do this if the types + consts is large because otherwise we could
// render a bunch of types and _then_ a bunch of consts just because both were
// _just_ under the limit
if !toggle && should_hide_fields(types.len() + consts.len()) {
if !toggle && should_hide_fields(count_types + count_consts) {
toggle = true;
toggle_open(w, "associated constants and methods");
toggle_open(
w,
format_args!(
"{} associated constant{} and {} method{}",
count_consts,
pluralize(count_consts),
count_methods,
pluralize(count_methods),
),
);
}
if !types.is_empty() && !consts.is_empty() {
w.write_str("\n");
Expand All @@ -534,9 +550,9 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
render_assoc_item(w, t, AssocItemLink::Anchor(None), ItemType::Trait, cx);
w.write_str(";\n");
}
if !toggle && should_hide_fields(required.len() + provided.len()) {
if !toggle && should_hide_fields(count_methods) {
toggle = true;
toggle_open(w, "methods");
toggle_open(w, format_args!("{} methods", count_methods));
}
if !consts.is_empty() && !required.is_empty() {
w.write_str("\n");
Expand Down Expand Up @@ -933,9 +949,10 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum
w.write_str(" {}");
} else {
w.write_str(" {\n");
let toggle = should_hide_fields(e.variants.len());
let count_variants = e.variants.len();
let toggle = should_hide_fields(count_variants);
if toggle {
toggle_open(w, "variants");
toggle_open(w, format_args!("{} variants", count_variants));
}
for v in &e.variants {
w.write_str(" ");
Expand Down Expand Up @@ -1012,7 +1029,8 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum

use crate::clean::Variant;
if let clean::VariantItem(Variant::Struct(ref s)) = *variant.kind {
toggle_open(w, "fields");
let count_fields = s.fields.len();
toggle_open(w, format_args!("{} field{}", count_fields, pluralize(count_fields)));
let variant_id = cx.derive_id(format!(
"{}.{}.fields",
ItemType::Variant,
Expand Down Expand Up @@ -1385,7 +1403,7 @@ fn render_union(
fields.iter().filter(|f| matches!(*f.kind, clean::StructFieldItem(..))).count();
let toggle = should_hide_fields(count_fields);
if toggle {
toggle_open(w, "fields");
toggle_open(w, format_args!("{} fields", count_fields));
}

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

writeln!(w, "</div>");
}

fn pluralize(count: usize) -> &'static str {
if count > 1 { "s" } else { "" }
}
4 changes: 2 additions & 2 deletions src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,10 +511,10 @@ fn opts() -> Vec<RustcOptGroup> {
"LEVEL",
)
}),
unstable("force-warns", |o| {
unstable("force-warn", |o| {
o.optopt(
"",
"force-warns",
"force-warn",
"Lints that will warn even if allowed somewhere else",
"LINTS",
)
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-make/unstable-flag-required/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

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

45 changes: 38 additions & 7 deletions src/test/rustdoc/toggle-item-contents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct PubStruct {

// @has 'toggle_item_contents/struct.BigPubStruct.html'
// @count - '//details[@class="rustdoc-toggle type-contents-toggle"]' 1
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show fields'
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show 13 fields'
pub struct BigPubStruct {
pub a: usize,
pub b: usize,
Expand All @@ -28,7 +28,7 @@ pub struct BigPubStruct {

// @has 'toggle_item_contents/union.BigUnion.html'
// @count - '//details[@class="rustdoc-toggle type-contents-toggle"]' 1
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show fields'
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show 13 fields'
pub union BigUnion {
pub a: usize,
pub b: usize,
Expand Down Expand Up @@ -63,7 +63,7 @@ pub struct PrivStruct {

// @has 'toggle_item_contents/enum.Enum.html'
// @count - '//details[@class="rustdoc-toggle type-contents-toggle"]' 1
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show fields'
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show 2 fields'
pub enum Enum {
A, B, C,
D {
Expand All @@ -72,9 +72,19 @@ pub enum Enum {
}
}

// @has 'toggle_item_contents/enum.EnumStructVariant.html'
// @count - '//details[@class="rustdoc-toggle type-contents-toggle"]' 1
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show 1 field'
pub enum EnumStructVariant {
A, B, C,
D {
a: u8,
}
}

// @has 'toggle_item_contents/enum.LargeEnum.html'
// @count - '//details[@class="rustdoc-toggle type-contents-toggle"]' 1
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show variants'
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show 13 variants'
pub enum LargeEnum {
A, B, C, D, E, F(u8), G, H, I, J, K, L, M
}
Expand All @@ -90,7 +100,7 @@ pub trait Trait {

// @has 'toggle_item_contents/trait.GinormousTrait.html'
// @count - '//details[@class="rustdoc-toggle type-contents-toggle"]' 1
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show associated items'
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show 16 associated items'
pub trait GinormousTrait {
type A;
type B;
Expand All @@ -113,7 +123,7 @@ pub trait GinormousTrait {

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

// @has 'toggle_item_contents/trait.GiganticTrait.html'
// @count - '//details[@class="rustdoc-toggle type-contents-toggle"]' 1
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show 1 associated constant and 1 method'
pub trait GiganticTrait {
type A;
type B;
type C;
type D;
type E;
type F;
type G;
type H;
type I;
type J;
type K;
type L;
const M: usize = 1;
#[must_use]
fn foo();
}

// @has 'toggle_item_contents/trait.BigTrait.html'
// @count - '//details[@class="rustdoc-toggle type-contents-toggle"]' 1
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show methods'
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show 14 methods'
pub trait BigTrait {
type A;
#[must_use]
Expand Down
Loading