Skip to content

Commit 0e5bca5

Browse files
authored
Rollup merge of #82255 - nhwn:nonzero-err-as-bug, r=davidtwco
Make `treat_err_as_bug` Option<NonZeroUsize> `rustc -Z treat-err-as-bug=N` already requires `N` to be nonzero when the argument is parsed, so changing the type from `Option<usize>` to `Option<NonZeroUsize>` is a low-hanging fruit in terms of layout optimization.
2 parents 8541435 + 8ddd846 commit 0e5bca5

File tree

4 files changed

+16
-11
lines changed

4 files changed

+16
-11
lines changed

compiler/rustc_errors/src/lib.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use rustc_span::{Loc, MultiSpan, Span};
3030

3131
use std::borrow::Cow;
3232
use std::hash::{Hash, Hasher};
33+
use std::num::NonZeroUsize;
3334
use std::panic;
3435
use std::path::Path;
3536
use std::{error, fmt};
@@ -359,7 +360,7 @@ pub struct HandlerFlags {
359360
pub can_emit_warnings: bool,
360361
/// If true, error-level diagnostics are upgraded to bug-level.
361362
/// (rustc: see `-Z treat-err-as-bug`)
362-
pub treat_err_as_bug: Option<usize>,
363+
pub treat_err_as_bug: Option<NonZeroUsize>,
363364
/// If true, immediately emit diagnostics that would otherwise be buffered.
364365
/// (rustc: see `-Z dont-buffer-diagnostics` and `-Z treat-err-as-bug`)
365366
pub dont_buffer_diagnostics: bool,
@@ -396,7 +397,7 @@ impl Handler {
396397
pub fn with_tty_emitter(
397398
color_config: ColorConfig,
398399
can_emit_warnings: bool,
399-
treat_err_as_bug: Option<usize>,
400+
treat_err_as_bug: Option<NonZeroUsize>,
400401
sm: Option<Lrc<SourceMap>>,
401402
) -> Self {
402403
Self::with_tty_emitter_and_flags(
@@ -424,7 +425,7 @@ impl Handler {
424425

425426
pub fn with_emitter(
426427
can_emit_warnings: bool,
427-
treat_err_as_bug: Option<usize>,
428+
treat_err_as_bug: Option<NonZeroUsize>,
428429
emitter: Box<dyn Emitter + sync::Send>,
429430
) -> Self {
430431
Handler::with_emitter_and_flags(
@@ -841,7 +842,7 @@ impl HandlerInner {
841842
}
842843

843844
fn treat_err_as_bug(&self) -> bool {
844-
self.flags.treat_err_as_bug.map_or(false, |c| self.err_count() >= c)
845+
self.flags.treat_err_as_bug.map_or(false, |c| self.err_count() >= c.get())
845846
}
846847

847848
fn print_error_count(&mut self, registry: &Registry) {
@@ -950,7 +951,7 @@ impl HandlerInner {
950951
// This is technically `self.treat_err_as_bug()` but `delay_span_bug` is called before
951952
// incrementing `err_count` by one, so we need to +1 the comparing.
952953
// FIXME: Would be nice to increment err_count in a more coherent way.
953-
if self.flags.treat_err_as_bug.map_or(false, |c| self.err_count() + 1 >= c) {
954+
if self.flags.treat_err_as_bug.map_or(false, |c| self.err_count() + 1 >= c.get()) {
954955
// FIXME: don't abort here if report_delayed_bugs is off
955956
self.span_bug(sp, msg);
956957
}
@@ -1023,7 +1024,7 @@ impl HandlerInner {
10231024

10241025
fn panic_if_treat_err_as_bug(&self) {
10251026
if self.treat_err_as_bug() {
1026-
match (self.err_count(), self.flags.treat_err_as_bug.unwrap_or(0)) {
1027+
match (self.err_count(), self.flags.treat_err_as_bug.map(|c| c.get()).unwrap_or(0)) {
10271028
(1, 1) => panic!("aborting due to `-Z treat-err-as-bug=1`"),
10281029
(0, _) | (1, _) => {}
10291030
(count, as_bug) => panic!(

compiler/rustc_interface/src/tests.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use rustc_target::spec::{CodeModel, LinkerFlavor, MergeFunctions, PanicStrategy}
2020
use rustc_target::spec::{RelocModel, RelroLevel, SplitDebuginfo, TlsModel};
2121
use std::collections::{BTreeMap, BTreeSet};
2222
use std::iter::FromIterator;
23+
use std::num::NonZeroUsize;
2324
use std::path::{Path, PathBuf};
2425

2526
type CfgSpecs = FxHashSet<(String, Option<String>)>;
@@ -595,7 +596,7 @@ fn test_debugging_options_tracking_hash() {
595596
tracked!(tune_cpu, Some(String::from("abc")));
596597
tracked!(tls_model, Some(TlsModel::GeneralDynamic));
597598
tracked!(trap_unreachable, Some(false));
598-
tracked!(treat_err_as_bug, Some(1));
599+
tracked!(treat_err_as_bug, NonZeroUsize::new(1));
599600
tracked!(unleash_the_miri_inside_of_you, true);
600601
tracked!(use_ctors_section, Some(true));
601602
tracked!(verify_llvm_ir, true);

compiler/rustc_session/src/config.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2313,6 +2313,7 @@ crate mod dep_tracking {
23132313
use std::collections::hash_map::DefaultHasher;
23142314
use std::collections::BTreeMap;
23152315
use std::hash::Hash;
2316+
use std::num::NonZeroUsize;
23162317
use std::path::PathBuf;
23172318

23182319
pub trait DepTrackingHash {
@@ -2353,6 +2354,7 @@ crate mod dep_tracking {
23532354
impl_dep_tracking_hash_via_hash!(lint::Level);
23542355
impl_dep_tracking_hash_via_hash!(Option<bool>);
23552356
impl_dep_tracking_hash_via_hash!(Option<usize>);
2357+
impl_dep_tracking_hash_via_hash!(Option<NonZeroUsize>);
23562358
impl_dep_tracking_hash_via_hash!(Option<String>);
23572359
impl_dep_tracking_hash_via_hash!(Option<(String, u64)>);
23582360
impl_dep_tracking_hash_via_hash!(Option<Vec<String>>);

compiler/rustc_session/src/options.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use std::collections::BTreeMap;
1616

1717
use std::collections::hash_map::DefaultHasher;
1818
use std::hash::Hasher;
19+
use std::num::NonZeroUsize;
1920
use std::path::PathBuf;
2021
use std::str;
2122

@@ -591,10 +592,10 @@ macro_rules! options {
591592
true
592593
}
593594

594-
fn parse_treat_err_as_bug(slot: &mut Option<usize>, v: Option<&str>) -> bool {
595+
fn parse_treat_err_as_bug(slot: &mut Option<NonZeroUsize>, v: Option<&str>) -> bool {
595596
match v {
596-
Some(s) => { *slot = s.parse().ok().filter(|&x| x != 0); slot.unwrap_or(0) != 0 }
597-
None => { *slot = Some(1); true }
597+
Some(s) => { *slot = s.parse().ok(); slot.is_some() }
598+
None => { *slot = NonZeroUsize::new(1); true }
598599
}
599600
}
600601

@@ -1141,7 +1142,7 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
11411142
"for every macro invocation, print its name and arguments (default: no)"),
11421143
trap_unreachable: Option<bool> = (None, parse_opt_bool, [TRACKED],
11431144
"generate trap instructions for unreachable intrinsics (default: use target setting, usually yes)"),
1144-
treat_err_as_bug: Option<usize> = (None, parse_treat_err_as_bug, [TRACKED],
1145+
treat_err_as_bug: Option<NonZeroUsize> = (None, parse_treat_err_as_bug, [TRACKED],
11451146
"treat error number `val` that occurs as bug"),
11461147
trim_diagnostic_paths: bool = (true, parse_bool, [UNTRACKED],
11471148
"in diagnostics, use heuristics to shorten paths referring to items"),

0 commit comments

Comments
 (0)