Skip to content

Commit dfc9d3f

Browse files
committed
Auto merge of #114272 - workingjubilee:rollup-ll9lwon, r=workingjubilee
Rollup of 4 pull requests Successful merges: - #95965 (Stabilize const-weak-new) - #109075 (Use `LazyLock` to lazily resolve backtraces) - #113741 (Don't install default projection bound for return-position `impl Trait` in trait methods with no body) - #114268 (Fix empty_write since rust version attribute) r? `@ghost` `@rustbot` modify labels: rollup
2 parents d9feb02 + 495e657 commit dfc9d3f

File tree

11 files changed

+59
-61
lines changed

11 files changed

+59
-61
lines changed

Diff for: compiler/rustc_metadata/src/rmeta/encoder.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -1144,13 +1144,7 @@ fn should_encode_type(tcx: TyCtxt<'_>, def_id: LocalDefId, def_kind: DefKind) ->
11441144
let assoc_item = tcx.associated_item(def_id);
11451145
match assoc_item.container {
11461146
ty::AssocItemContainer::ImplContainer => true,
1147-
// Always encode RPITITs, since we need to be able to project
1148-
// from an RPITIT associated item to an opaque when installing
1149-
// the default projection predicates in default trait methods
1150-
// with RPITITs.
1151-
ty::AssocItemContainer::TraitContainer => {
1152-
assoc_item.defaultness(tcx).has_value() || assoc_item.is_impl_trait_in_trait()
1153-
}
1147+
ty::AssocItemContainer::TraitContainer => assoc_item.defaultness(tcx).has_value(),
11541148
}
11551149
}
11561150
DefKind::TyParam => {

Diff for: compiler/rustc_ty_utils/src/ty.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ fn param_env(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ParamEnv<'_> {
129129
// sure that this will succeed without errors anyway.
130130

131131
if tcx.def_kind(def_id) == DefKind::AssocFn
132-
&& tcx.associated_item(def_id).container == ty::AssocItemContainer::TraitContainer
132+
&& let assoc_item = tcx.associated_item(def_id)
133+
&& assoc_item.container == ty::AssocItemContainer::TraitContainer
134+
&& assoc_item.defaultness(tcx).has_value()
133135
{
134136
let sig = tcx.fn_sig(def_id).instantiate_identity();
135137
// We accounted for the binder of the fn sig, so skip the binder.

Diff for: library/alloc/src/rc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2719,7 +2719,7 @@ impl<T> Weak<T> {
27192719
/// ```
27202720
#[inline]
27212721
#[stable(feature = "downgraded_weak", since = "1.10.0")]
2722-
#[rustc_const_unstable(feature = "const_weak_new", issue = "95091", reason = "recently added")]
2722+
#[rustc_const_stable(feature = "const_weak_new", since = "CURRENT_RUSTC_VERSION")]
27232723
#[must_use]
27242724
pub const fn new() -> Weak<T> {
27252725
Weak {

Diff for: library/alloc/src/sync.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2503,7 +2503,7 @@ impl<T> Weak<T> {
25032503
/// ```
25042504
#[inline]
25052505
#[stable(feature = "downgraded_weak", since = "1.10.0")]
2506-
#[rustc_const_unstable(feature = "const_weak_new", issue = "95091", reason = "recently added")]
2506+
#[rustc_const_stable(feature = "const_weak_new", since = "CURRENT_RUSTC_VERSION")]
25072507
#[must_use]
25082508
pub const fn new() -> Weak<T> {
25092509
Weak {

Diff for: library/std/src/backtrace.rs

+13-45
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,11 @@ mod tests;
8989
// a backtrace or actually symbolizing it.
9090

9191
use crate::backtrace_rs::{self, BytesOrWideString};
92-
use crate::cell::UnsafeCell;
9392
use crate::env;
9493
use crate::ffi::c_void;
9594
use crate::fmt;
9695
use crate::sync::atomic::{AtomicUsize, Ordering::Relaxed};
97-
use crate::sync::Once;
96+
use crate::sync::LazyLock;
9897
use crate::sys_common::backtrace::{lock, output_filename};
9998
use crate::vec::Vec;
10099

@@ -133,12 +132,11 @@ pub enum BacktraceStatus {
133132
enum Inner {
134133
Unsupported,
135134
Disabled,
136-
Captured(LazilyResolvedCapture),
135+
Captured(LazyLock<Capture, LazyResolve>),
137136
}
138137

139138
struct Capture {
140139
actual_start: usize,
141-
resolved: bool,
142140
frames: Vec<BacktraceFrame>,
143141
}
144142

@@ -179,7 +177,7 @@ impl fmt::Debug for Backtrace {
179177
let capture = match &self.inner {
180178
Inner::Unsupported => return fmt.write_str("<unsupported>"),
181179
Inner::Disabled => return fmt.write_str("<disabled>"),
182-
Inner::Captured(c) => c.force(),
180+
Inner::Captured(c) => &**c,
183181
};
184182

185183
let frames = &capture.frames[capture.actual_start..];
@@ -347,11 +345,10 @@ impl Backtrace {
347345
let inner = if frames.is_empty() {
348346
Inner::Unsupported
349347
} else {
350-
Inner::Captured(LazilyResolvedCapture::new(Capture {
348+
Inner::Captured(LazyLock::new(lazy_resolve(Capture {
351349
actual_start: actual_start.unwrap_or(0),
352350
frames,
353-
resolved: false,
354-
}))
351+
})))
355352
};
356353

357354
Backtrace { inner }
@@ -376,7 +373,7 @@ impl<'a> Backtrace {
376373
#[must_use]
377374
#[unstable(feature = "backtrace_frames", issue = "79676")]
378375
pub fn frames(&'a self) -> &'a [BacktraceFrame] {
379-
if let Inner::Captured(c) = &self.inner { &c.force().frames } else { &[] }
376+
if let Inner::Captured(c) = &self.inner { &c.frames } else { &[] }
380377
}
381378
}
382379

@@ -386,7 +383,7 @@ impl fmt::Display for Backtrace {
386383
let capture = match &self.inner {
387384
Inner::Unsupported => return fmt.write_str("unsupported backtrace"),
388385
Inner::Disabled => return fmt.write_str("disabled backtrace"),
389-
Inner::Captured(c) => c.force(),
386+
Inner::Captured(c) => &**c,
390387
};
391388

392389
let full = fmt.alternate();
@@ -430,46 +427,15 @@ impl fmt::Display for Backtrace {
430427
}
431428
}
432429

433-
struct LazilyResolvedCapture {
434-
sync: Once,
435-
capture: UnsafeCell<Capture>,
436-
}
437-
438-
impl LazilyResolvedCapture {
439-
fn new(capture: Capture) -> Self {
440-
LazilyResolvedCapture { sync: Once::new(), capture: UnsafeCell::new(capture) }
441-
}
442-
443-
fn force(&self) -> &Capture {
444-
self.sync.call_once(|| {
445-
// SAFETY: This exclusive reference can't overlap with any others
446-
// `Once` guarantees callers will block until this closure returns
447-
// `Once` also guarantees only a single caller will enter this closure
448-
unsafe { &mut *self.capture.get() }.resolve();
449-
});
450-
451-
// SAFETY: This shared reference can't overlap with the exclusive reference above
452-
unsafe { &*self.capture.get() }
453-
}
454-
}
455-
456-
// SAFETY: Access to the inner value is synchronized using a thread-safe `Once`
457-
// So long as `Capture` is `Sync`, `LazilyResolvedCapture` is too
458-
unsafe impl Sync for LazilyResolvedCapture where Capture: Sync {}
459-
460-
impl Capture {
461-
fn resolve(&mut self) {
462-
// If we're already resolved, nothing to do!
463-
if self.resolved {
464-
return;
465-
}
466-
self.resolved = true;
430+
type LazyResolve = impl (FnOnce() -> Capture) + Send + Sync;
467431

432+
fn lazy_resolve(mut capture: Capture) -> LazyResolve {
433+
move || {
468434
// Use the global backtrace lock to synchronize this as it's a
469435
// requirement of the `backtrace` crate, and then actually resolve
470436
// everything.
471437
let _lock = lock();
472-
for frame in self.frames.iter_mut() {
438+
for frame in capture.frames.iter_mut() {
473439
let symbols = &mut frame.symbols;
474440
let frame = match &frame.frame {
475441
RawFrame::Actual(frame) => frame,
@@ -490,6 +456,8 @@ impl Capture {
490456
});
491457
}
492458
}
459+
460+
capture
493461
}
494462
}
495463

Diff for: library/std/src/backtrace/tests.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@ fn generate_fake_frames() -> Vec<BacktraceFrame> {
4343
#[test]
4444
fn test_debug() {
4545
let backtrace = Backtrace {
46-
inner: Inner::Captured(LazilyResolvedCapture::new(Capture {
46+
inner: Inner::Captured(LazyLock::preinit(Capture {
4747
actual_start: 1,
48-
resolved: true,
4948
frames: generate_fake_frames(),
5049
})),
5150
};
@@ -66,9 +65,8 @@ fn test_debug() {
6665
#[test]
6766
fn test_frames() {
6867
let backtrace = Backtrace {
69-
inner: Inner::Captured(LazilyResolvedCapture::new(Capture {
68+
inner: Inner::Captured(LazyLock::preinit(Capture {
7069
actual_start: 1,
71-
resolved: true,
7270
frames: generate_fake_frames(),
7371
})),
7472
};

Diff for: library/std/src/io/util.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl SizeHint for Empty {
100100
}
101101
}
102102

103-
#[stable(feature = "empty_write", since = "1.64.0")]
103+
#[stable(feature = "empty_write", since = "CURRENT_RUSTC_VERSION")]
104104
impl Write for Empty {
105105
#[inline]
106106
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
@@ -124,7 +124,7 @@ impl Write for Empty {
124124
}
125125
}
126126

127-
#[stable(feature = "empty_write", since = "1.64.0")]
127+
#[stable(feature = "empty_write", since = "CURRENT_RUSTC_VERSION")]
128128
impl Write for &Empty {
129129
#[inline]
130130
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {

Diff for: library/std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@
272272
#![feature(staged_api)]
273273
#![feature(thread_local)]
274274
#![feature(try_blocks)]
275+
#![feature(type_alias_impl_trait)]
275276
#![feature(utf8_chunks)]
276277
// tidy-alphabetical-end
277278
//

Diff for: library/std/src/sync/lazy_lock.rs

+9
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,15 @@ impl<T, F: FnOnce() -> T> LazyLock<T, F> {
8989
LazyLock { once: Once::new(), data: UnsafeCell::new(Data { f: ManuallyDrop::new(f) }) }
9090
}
9191

92+
/// Creates a new lazy value that is already initialized.
93+
#[inline]
94+
#[cfg(test)]
95+
pub(crate) fn preinit(value: T) -> LazyLock<T, F> {
96+
let once = Once::new();
97+
once.call_once(|| {});
98+
LazyLock { once, data: UnsafeCell::new(Data { value: ManuallyDrop::new(value) }) }
99+
}
100+
92101
/// Consumes this `LazyLock` returning the stored value.
93102
///
94103
/// Returns `Ok(value)` if `Lazy` is initialized and `Err(f)` otherwise.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(return_position_impl_trait_in_trait)]
2+
3+
struct Wrapper<G: Send>(G);
4+
5+
trait Foo {
6+
fn bar() -> Wrapper<impl Sized>;
7+
//~^ ERROR `impl Sized` cannot be sent between threads safely
8+
}
9+
10+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0277]: `impl Sized` cannot be sent between threads safely
2+
--> $DIR/check-wf-on-non-defaulted-rpitit.rs:6:17
3+
|
4+
LL | fn bar() -> Wrapper<impl Sized>;
5+
| ^^^^^^^^^^^^^^^^^^^ `impl Sized` cannot be sent between threads safely
6+
|
7+
= help: the trait `Send` is not implemented for `impl Sized`
8+
note: required by a bound in `Wrapper`
9+
--> $DIR/check-wf-on-non-defaulted-rpitit.rs:3:19
10+
|
11+
LL | struct Wrapper<G: Send>(G);
12+
| ^^^^ required by this bound in `Wrapper`
13+
14+
error: aborting due to previous error
15+
16+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)