Skip to content

Commit 0b011b7

Browse files
committed
Auto merge of rust-lang#111933 - matthiaskrgr:rollup-m10k3ts, r=matthiaskrgr
Rollup of 4 pull requests Successful merges: - rust-lang#95198 (Add slice::{split_,}{first,last}_chunk{,_mut}) - rust-lang#109899 (Use apple-m1 as target CPU for aarch64-apple-darwin.) - rust-lang#111624 (Emit diagnostic for privately uninhabited uncovered witnesses.) - rust-lang#111875 (Don't leak the function that is called on drop) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 776f222 + a9743e1 commit 0b011b7

File tree

13 files changed

+313
-17
lines changed

13 files changed

+313
-17
lines changed

compiler/rustc_data_structures/src/lib.rs

+14-8
Original file line numberDiff line numberDiff line change
@@ -102,21 +102,27 @@ pub mod unord;
102102
pub use ena::undo_log;
103103
pub use ena::unify;
104104

105-
pub struct OnDrop<F: Fn()>(pub F);
105+
/// Returns a structure that calls `f` when dropped.
106+
pub fn defer<F: FnOnce()>(f: F) -> OnDrop<F> {
107+
OnDrop(Some(f))
108+
}
109+
110+
pub struct OnDrop<F: FnOnce()>(Option<F>);
106111

107-
impl<F: Fn()> OnDrop<F> {
108-
/// Forgets the function which prevents it from running.
109-
/// Ensure that the function owns no memory, otherwise it will be leaked.
112+
impl<F: FnOnce()> OnDrop<F> {
113+
/// Disables on-drop call.
110114
#[inline]
111-
pub fn disable(self) {
112-
std::mem::forget(self);
115+
pub fn disable(mut self) {
116+
self.0.take();
113117
}
114118
}
115119

116-
impl<F: Fn()> Drop for OnDrop<F> {
120+
impl<F: FnOnce()> Drop for OnDrop<F> {
117121
#[inline]
118122
fn drop(&mut self) {
119-
(self.0)();
123+
if let Some(f) = self.0.take() {
124+
f();
125+
}
120126
}
121127
}
122128

compiler/rustc_data_structures/src/owned_slice/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use std::{
77
};
88

99
use crate::{
10+
defer,
1011
owned_slice::{slice_owned, try_slice_owned, OwnedSlice},
11-
OnDrop,
1212
};
1313

1414
#[test]
@@ -66,7 +66,7 @@ fn boxed() {
6666
fn drop_drops() {
6767
let flag = Arc::new(AtomicBool::new(false));
6868
let flag_prime = Arc::clone(&flag);
69-
let d = OnDrop(move || flag_prime.store(true, atomic::Ordering::Relaxed));
69+
let d = defer(move || flag_prime.store(true, atomic::Ordering::Relaxed));
7070

7171
let slice = slice_owned(d, |_| &[]);
7272

compiler/rustc_interface/src/interface.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ use crate::util;
33
use rustc_ast::token;
44
use rustc_ast::{self as ast, LitKind, MetaItemKind};
55
use rustc_codegen_ssa::traits::CodegenBackend;
6+
use rustc_data_structures::defer;
67
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
78
use rustc_data_structures::sync::Lrc;
8-
use rustc_data_structures::OnDrop;
99
use rustc_errors::registry::Registry;
1010
use rustc_errors::{ErrorGuaranteed, Handler};
1111
use rustc_lint::LintStore;
@@ -325,7 +325,7 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
325325

326326
rustc_span::set_source_map(compiler.sess.parse_sess.clone_source_map(), move || {
327327
let r = {
328-
let _sess_abort_error = OnDrop(|| {
328+
let _sess_abort_error = defer(|| {
329329
compiler.sess.finish_diagnostics(registry);
330330
});
331331

compiler/rustc_middle/src/ty/context/tls.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ where
7878
{
7979
TLV.with(|tlv| {
8080
let old = tlv.replace(erase(context));
81-
let _reset = rustc_data_structures::OnDrop(move || tlv.set(old));
81+
let _reset = rustc_data_structures::defer(move || tlv.set(old));
8282
f()
8383
})
8484
}

compiler/rustc_mir_build/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,8 @@ mir_build_uncovered = {$count ->
340340
*[other] patterns `{$witness_1}`, `{$witness_2}`, `{$witness_3}` and {$remainder} more
341341
} not covered
342342
343+
mir_build_privately_uninhabited = pattern `{$witness_1}` is currently uninhabited, but this variant contains private fields which may become inhabited in the future
344+
343345
mir_build_pattern_not_covered = refutable pattern in {$origin}
344346
.pattern_ty = the matched value is of type `{$pattern_ty}`
345347

compiler/rustc_mir_build/src/errors.rs

+2
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,8 @@ pub(crate) struct PatternNotCovered<'s, 'tcx> {
781781
pub interpreted_as_const: Option<InterpretedAsConst>,
782782
#[subdiagnostic]
783783
pub adt_defined_here: Option<AdtDefinedHere<'tcx>>,
784+
#[note(mir_build_privately_uninhabited)]
785+
pub witness_1_is_privately_uninhabited: Option<()>,
784786
#[note(mir_build_pattern_ty)]
785787
pub _p: (),
786788
pub pattern_ty: Ty<'tcx>,

compiler/rustc_mir_build/src/thir/pattern/check_match.rs

+18
Original file line numberDiff line numberDiff line change
@@ -479,12 +479,30 @@ impl<'p, 'tcx> MatchVisitor<'_, 'p, 'tcx> {
479479
AdtDefinedHere { adt_def_span, ty, variants }
480480
};
481481

482+
// Emit an extra note if the first uncovered witness is
483+
// visibly uninhabited anywhere in the current crate.
484+
let witness_1_is_privately_uninhabited =
485+
if cx.tcx.features().exhaustive_patterns
486+
&& let Some(witness_1) = witnesses.get(0)
487+
&& let ty::Adt(adt, substs) = witness_1.ty().kind()
488+
&& adt.is_enum()
489+
&& let Constructor::Variant(variant_index) = witness_1.ctor()
490+
{
491+
let variant = adt.variant(*variant_index);
492+
let inhabited = variant.inhabited_predicate(cx.tcx, *adt).subst(cx.tcx, substs);
493+
assert!(inhabited.apply(cx.tcx, cx.param_env, cx.module));
494+
!inhabited.apply_ignore_module(cx.tcx, cx.param_env)
495+
} else {
496+
false
497+
};
498+
482499
self.error = Err(self.tcx.sess.emit_err(PatternNotCovered {
483500
span: pat.span,
484501
origin,
485502
uncovered: Uncovered::new(pat.span, &cx, witnesses),
486503
inform,
487504
interpreted_as_const,
505+
witness_1_is_privately_uninhabited: witness_1_is_privately_uninhabited.then_some(()),
488506
_p: (),
489507
pattern_ty,
490508
let_suggestion,

compiler/rustc_query_system/src/query/job.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use {
2222
rustc_data_structures::fx::FxHashSet,
2323
rustc_data_structures::sync::Lock,
2424
rustc_data_structures::sync::Lrc,
25-
rustc_data_structures::{jobserver, OnDrop},
25+
rustc_data_structures::{defer, jobserver},
2626
rustc_span::DUMMY_SP,
2727
std::iter,
2828
std::process,
@@ -530,7 +530,7 @@ fn remove_cycle<D: DepKind>(
530530
/// all active queries for cycles before finally resuming all the waiters at once.
531531
#[cfg(parallel_compiler)]
532532
pub fn deadlock<D: DepKind>(query_map: QueryMap<D>, registry: &rayon_core::Registry) {
533-
let on_panic = OnDrop(|| {
533+
let on_panic = defer(|| {
534534
eprintln!("deadlock handler panicked, aborting process");
535535
process::abort();
536536
});

compiler/rustc_target/src/spec/aarch64_apple_darwin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::spec::{FramePointer, SanitizerSet, Target, TargetOptions};
44
pub fn target() -> Target {
55
let arch = Arch::Arm64;
66
let mut base = opts("macos", arch);
7-
base.cpu = "apple-a14".into();
7+
base.cpu = "apple-m1".into();
88
base.max_atomic_width = Some(128);
99

1010
// FIXME: The leak sanitizer currently fails the tests, see #88132.

0 commit comments

Comments
 (0)