Skip to content

Commit 06b13d5

Browse files
committed
Auto merge of #156870 - JonathanBrouwer:rollup-PbM4nVA, r=JonathanBrouwer
Rollup of 6 pull requests Successful merges: - #156769 (Use print instead of po in debuginfo path test) - #156784 (Fix reborrow_info early return skipping field validation) - #156827 (float_literal_f32_fallback: Don't suggest invalid code) - #156828 (Add unstable Share trait) - #156830 (Add `#[doc(alias = "phi")]` for float `GOLDEN_RATIO` constants) - #156860 (Fix Pieter-Louis Schoeman mailmap entry)
2 parents e1ff77d + 2843641 commit 06b13d5

35 files changed

Lines changed: 542 additions & 14 deletions

.mailmap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@ Philipp Matthias Schäfer <philipp.matthias.schaefer@posteo.de>
569569
phosphorus <steepout@qq.com>
570570
Pierre Krieger <pierre.krieger1708@gmail.com>
571571
pierwill <pierwill@users.noreply.github.com> <19642016+pierwill@users.noreply.github.com>
572+
Pieter-Louis Schoeman <pl.schoeman44@gmail.com> <127837395+P8L1@users.noreply.github.com>
572573
Pietro Albini <pietro@pietroalbini.org> <pietro@pietroalbini.io>
573574
Pietro Albini <pietro@pietroalbini.org> <pietro.albini@ferrous-systems.com>
574575
Pradyumna Rahul <prkinformed@gmail.com>

compiler/rustc_hir_analysis/src/coherence/builtin.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,8 +563,8 @@ pub(crate) fn reborrow_info<'tcx>(
563563
)
564564
.is_ok()
565565
{
566-
// Field implements Reborrow.
567-
return Ok(());
566+
// Field implements Reborrow, check remaining fields.
567+
continue;
568568
}
569569

570570
// Field does not implement Reborrow: it must be Copy.

compiler/rustc_hir_typeck/src/fallback.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,13 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
171171
.inspect(|vid| {
172172
let origin = self.float_var_origin(*vid);
173173
// Show the entire literal in the suggestion to make it clearer.
174-
let literal = self.tcx.sess.source_map().span_to_snippet(origin.span).ok();
174+
let mut literal = self.tcx.sess.source_map().span_to_snippet(origin.span).ok();
175+
// A `.` at the end of the literal is no longer necessary if `f32` is explicitly specified
176+
if let Some(ref mut literal) = literal
177+
&& literal.ends_with('.')
178+
{
179+
literal.pop();
180+
}
175181
self.tcx.emit_node_span_lint(
176182
FLOAT_LITERAL_F32_FALLBACK,
177183
origin.lint_id.unwrap_or(CRATE_HIR_ID),

library/alloc/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@
144144
#![feature(ptr_metadata)]
145145
#![feature(rev_into_inner)]
146146
#![feature(set_ptr_value)]
147+
#![feature(share_trait)]
147148
#![feature(sized_type_properties)]
148149
#![feature(slice_from_ptr_range)]
149150
#![feature(slice_index_methods)]

library/alloc/src/rc.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ use core::any::Any;
245245
use core::cell::{Cell, CloneFromCell};
246246
#[cfg(not(no_global_oom_handling))]
247247
use core::clone::TrivialClone;
248-
use core::clone::{CloneToUninit, UseCloned};
248+
use core::clone::{CloneToUninit, Share, UseCloned};
249249
use core::cmp::Ordering;
250250
use core::hash::{Hash, Hasher};
251251
use core::intrinsics::abort;
@@ -2525,6 +2525,9 @@ impl<T: ?Sized, A: Allocator + Clone> Clone for Rc<T, A> {
25252525
#[unstable(feature = "ergonomic_clones", issue = "132290")]
25262526
impl<T: ?Sized, A: Allocator + Clone> UseCloned for Rc<T, A> {}
25272527

2528+
#[unstable(feature = "share_trait", issue = "156756")]
2529+
impl<T: ?Sized, A: Allocator + Clone> Share for Rc<T, A> {}
2530+
25282531
#[cfg(not(no_global_oom_handling))]
25292532
#[stable(feature = "rust1", since = "1.0.0")]
25302533
impl<T: Default> Default for Rc<T> {

library/alloc/src/sync.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use core::any::Any;
1212
use core::cell::CloneFromCell;
1313
#[cfg(not(no_global_oom_handling))]
1414
use core::clone::TrivialClone;
15-
use core::clone::{CloneToUninit, UseCloned};
15+
use core::clone::{CloneToUninit, Share, UseCloned};
1616
use core::cmp::Ordering;
1717
use core::hash::{Hash, Hasher};
1818
use core::intrinsics::abort;
@@ -2436,6 +2436,9 @@ impl<T: ?Sized, A: Allocator + Clone> Clone for Arc<T, A> {
24362436
#[unstable(feature = "ergonomic_clones", issue = "132290")]
24372437
impl<T: ?Sized, A: Allocator + Clone> UseCloned for Arc<T, A> {}
24382438

2439+
#[unstable(feature = "share_trait", issue = "156756")]
2440+
impl<T: ?Sized, A: Allocator + Clone> Share for Arc<T, A> {}
2441+
24392442
#[stable(feature = "rust1", since = "1.0.0")]
24402443
impl<T: ?Sized, A: Allocator> Deref for Arc<T, A> {
24412444
type Target = T;

library/core/src/clone.rs

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,90 @@ pub macro Clone($item:item) {
290290
/* compiler built-in */
291291
}
292292

293+
/// A trait for types whose [`Clone`] operation creates another alias to the same
294+
/// logical resource or shared state.
295+
///
296+
/// `Share` marks types where cloning creates another handle, reference, or alias
297+
/// to the same logical resource or shared state, rather than an independent owned
298+
/// value. The distinction is semantic, not cost-based: implementing `Share` does
299+
/// not merely mean that cloning is cheap, constant-time, allocation-free, or
300+
/// convenient.
301+
///
302+
/// Calling [`share`](Share::share) is equivalent to calling [`clone`](Clone::clone)
303+
/// for implementors, but communicates that the resulting value aliases the same
304+
/// underlying resource.
305+
///
306+
/// Shared references, `Rc<T>`, `Arc<T>`, `Sender<T>`, and `SyncSender<T>` are
307+
/// examples of types that can be shared this way. Types such as `Vec<T>`,
308+
/// `String`, and `Box<T>` are not `Share` even though they implement `Clone`,
309+
/// because cloning them creates another owned value rather than another handle
310+
/// to the same logical resource.
311+
///
312+
/// # Examples
313+
///
314+
/// ```
315+
/// #![feature(share_trait)]
316+
///
317+
/// use std::cell::Cell;
318+
/// use std::clone::Share;
319+
/// use std::rc::Rc;
320+
/// use std::sync::{
321+
/// Arc,
322+
/// atomic::{AtomicUsize, Ordering},
323+
/// };
324+
///
325+
/// let value = 1;
326+
/// let reference = &value;
327+
/// assert!(std::ptr::eq(reference, reference.share()));
328+
///
329+
/// let rc = Rc::new(Cell::new(2));
330+
/// let shared_rc = rc.share();
331+
/// assert!(Rc::ptr_eq(&rc, &shared_rc));
332+
/// shared_rc.set(3);
333+
/// assert_eq!(rc.get(), 3);
334+
///
335+
/// let arc = Arc::new(AtomicUsize::new(4));
336+
/// let shared_arc = arc.share();
337+
/// assert!(Arc::ptr_eq(&arc, &shared_arc));
338+
/// shared_arc.store(5, Ordering::Relaxed);
339+
/// assert_eq!(arc.load(Ordering::Relaxed), 5);
340+
/// ```
341+
///
342+
/// ```
343+
/// #![feature(share_trait)]
344+
///
345+
/// use std::clone::Share;
346+
/// use std::sync::mpsc::{channel, sync_channel};
347+
///
348+
/// let (sender, receiver) = channel();
349+
/// let shared_sender = sender.share();
350+
/// sender.send(1).unwrap();
351+
/// shared_sender.send(2).unwrap();
352+
///
353+
/// let mut received = [receiver.recv().unwrap(), receiver.recv().unwrap()];
354+
/// received.sort();
355+
/// assert_eq!(received, [1, 2]);
356+
///
357+
/// let (sync_sender, sync_receiver) = sync_channel(2);
358+
/// let shared_sync_sender = sync_sender.share();
359+
/// sync_sender.send(3).unwrap();
360+
/// shared_sync_sender.send(4).unwrap();
361+
///
362+
/// let mut received = [sync_receiver.recv().unwrap(), sync_receiver.recv().unwrap()];
363+
/// received.sort();
364+
/// assert_eq!(received, [3, 4]);
365+
/// ```
366+
#[unstable(feature = "share_trait", issue = "156756")]
367+
pub trait Share: Clone {
368+
/// Creates another alias to the same underlying resource or shared state.
369+
///
370+
/// This is equivalent to calling [`Clone::clone`].
371+
#[unstable(feature = "share_trait", issue = "156756")]
372+
fn share(&self) -> Self {
373+
Clone::clone(self)
374+
}
375+
}
376+
293377
/// Trait for objects whose [`Clone`] impl is lightweight (e.g. reference-counted)
294378
///
295379
/// Cloning an object implementing this trait should in general:
@@ -601,7 +685,7 @@ unsafe impl CloneToUninit for crate::bstr::ByteStr {
601685
/// are implemented in `traits::SelectionContext::copy_clone_conditions()`
602686
/// in `rustc_trait_selection`.
603687
mod impls {
604-
use super::TrivialClone;
688+
use super::{Share, TrivialClone};
605689
use crate::marker::PointeeSized;
606690

607691
macro_rules! impl_clone {
@@ -689,6 +773,9 @@ mod impls {
689773
#[rustc_const_unstable(feature = "const_clone", issue = "142757")]
690774
unsafe impl<T: PointeeSized> const TrivialClone for &T {}
691775

776+
#[unstable(feature = "share_trait", issue = "156756")]
777+
impl<T: PointeeSized> Share for &T {}
778+
692779
/// Shared references can be cloned, but mutable references *cannot*!
693780
#[stable(feature = "rust1", since = "1.0.0")]
694781
impl<T: PointeeSized> !Clone for &mut T {}

library/core/src/num/f128.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub mod consts {
3333
pub const TAU: f128 = 6.28318530717958647692528676655900576839433879875021164194989_f128;
3434

3535
/// The golden ratio (φ)
36+
#[doc(alias = "phi")]
3637
#[unstable(feature = "f128", issue = "116909")]
3738
pub const GOLDEN_RATIO: f128 =
3839
1.61803398874989484820458683436563811772030917980576286213545_f128;

library/core/src/num/f16.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub mod consts {
3535
pub const TAU: f16 = 6.28318530717958647692528676655900577_f16;
3636

3737
/// The golden ratio (φ)
38+
#[doc(alias = "phi")]
3839
#[unstable(feature = "f16", issue = "116909")]
3940
pub const GOLDEN_RATIO: f16 = 1.618033988749894848204586834365638118_f16;
4041

library/core/src/num/f32.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ pub mod consts {
292292
pub const TAU: f32 = 6.28318530717958647692528676655900577_f32;
293293

294294
/// The golden ratio (φ)
295+
#[doc(alias = "phi")]
295296
#[stable(feature = "euler_gamma_golden_ratio", since = "1.94.0")]
296297
pub const GOLDEN_RATIO: f32 = 1.618033988749894848204586834365638118_f32;
297298

0 commit comments

Comments
 (0)