Auto merge of #133694 - matthiaskrgr:rollup-s6xj4rf, r=matthiaskrgr

Rollup of 8 pull requests

Successful merges:

 - #128184 (std: refactor `pthread`-based synchronization)
 - #132047 (Robustify and genericize return-type-notation resolution in `resolve_bound_vars`)
 - #133515 (fix: hurd build, stat64.st_fsid was renamed to st_dev)
 - #133602 (fix: fix codeblocks in `PathBuf` example)
 - #133622 (update link to "C++ Exceptions under the hood" blog)
 - #133660 (Do not create trait object type if missing associated types)
 - #133686 (Add diagnostic item for `std::ops::ControlFlow`)
 - #133689 (Fixed typos by changing `happend` to `happened`)

r? `@ghost`
`@rustbot` modify labels: rollup
This commit is contained in:
bors 2024-12-01 07:53:58 +00:00
commit 6c76ed5503
40 changed files with 674 additions and 703 deletions

View File

@ -2060,48 +2060,37 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
};
match path.res {
Res::Def(DefKind::TyParam, _) | Res::SelfTyParam { trait_: _ } => {
// Get the generics of this type's hir owner. This is *different*
// from the generics of the parameter's definition, since we want
// to be able to resolve an RTN path on a nested body (e.g. method
// inside an impl) using the where clauses on the method.
// FIXME(return_type_notation): Think of some better way of doing this.
let Some(generics) = self.tcx.hir_owner_node(hir_id.owner).generics()
else {
return;
};
// Look for the first bound that contains an associated type that
// matches the segment that we're looking for. We ignore any subsequent
// bounds since we'll be emitting a hard error in HIR lowering, so this
// is purely speculative.
let one_bound = generics.predicates.iter().find_map(|predicate| {
let hir::WherePredicateKind::BoundPredicate(predicate) = predicate.kind
else {
return None;
};
let hir::TyKind::Path(hir::QPath::Resolved(None, bounded_path)) =
predicate.bounded_ty.kind
else {
return None;
};
if bounded_path.res != path.res {
return None;
}
predicate.bounds.iter().find_map(|bound| {
let hir::GenericBound::Trait(trait_) = bound else {
return None;
};
let mut bounds =
self.for_each_trait_bound_on_res(path.res).filter_map(|trait_def_id| {
BoundVarContext::supertrait_hrtb_vars(
self.tcx,
trait_.trait_ref.trait_def_id()?,
trait_def_id,
item_segment.ident,
ty::AssocKind::Fn,
)
})
});
let Some((bound_vars, assoc_item)) = one_bound else {
});
let Some((bound_vars, assoc_item)) = bounds.next() else {
// This will error in HIR lowering.
self.tcx
.dcx()
.span_delayed_bug(path.span, "no resolution for RTN path");
return;
};
// Don't bail if we have identical bounds, which may be collected from
// something like `T: Bound + Bound`, or via elaborating supertraits.
for (second_vars, second_assoc_item) in bounds {
if second_vars != bound_vars || second_assoc_item != assoc_item {
// This will error in HIR lowering.
self.tcx.dcx().span_delayed_bug(
path.span,
"ambiguous resolution for RTN path",
);
return;
}
}
(bound_vars, assoc_item.def_id, item_segment)
}
// If we have a self type alias (in an impl), try to resolve an
@ -2167,6 +2156,92 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
existing_bound_vars.extend(bound_vars);
self.record_late_bound_vars(item_segment.hir_id, existing_bound_vars_saved);
}
/// Walk the generics of the item for a trait bound whose self type
/// corresponds to the expected res, and return the trait def id.
fn for_each_trait_bound_on_res(
&self,
expected_res: Res,
) -> impl Iterator<Item = DefId> + use<'tcx, '_> {
std::iter::from_coroutine(
#[coroutine]
move || {
let mut scope = self.scope;
loop {
let hir_id = match *scope {
Scope::Binder { hir_id, .. } => Some(hir_id),
Scope::Root { opt_parent_item: Some(parent_def_id) } => {
Some(self.tcx.local_def_id_to_hir_id(parent_def_id))
}
Scope::Body { .. }
| Scope::ObjectLifetimeDefault { .. }
| Scope::Supertrait { .. }
| Scope::TraitRefBoundary { .. }
| Scope::LateBoundary { .. }
| Scope::Opaque { .. }
| Scope::Root { opt_parent_item: None } => None,
};
if let Some(hir_id) = hir_id {
let node = self.tcx.hir_node(hir_id);
// If this is a `Self` bound in a trait, yield the trait itself.
// Specifically, we don't need to look at any supertraits since
// we already do that in `BoundVarContext::supertrait_hrtb_vars`.
if let Res::SelfTyParam { trait_: _ } = expected_res
&& let hir::Node::Item(item) = node
&& let hir::ItemKind::Trait(..) = item.kind
{
// Yield the trait's def id. Supertraits will be
// elaborated from that.
yield item.owner_id.def_id.to_def_id();
} else if let Some(generics) = node.generics() {
for pred in generics.predicates {
let hir::WherePredicateKind::BoundPredicate(pred) = pred.kind
else {
continue;
};
let hir::TyKind::Path(hir::QPath::Resolved(None, bounded_path)) =
pred.bounded_ty.kind
else {
continue;
};
// Match the expected res.
if bounded_path.res != expected_res {
continue;
}
for pred in pred.bounds {
match pred {
hir::GenericBound::Trait(poly_trait_ref) => {
if let Some(def_id) =
poly_trait_ref.trait_ref.trait_def_id()
{
yield def_id;
}
}
hir::GenericBound::Outlives(_)
| hir::GenericBound::Use(_, _) => {}
}
}
}
}
}
match *scope {
Scope::Binder { s, .. }
| Scope::Body { s, .. }
| Scope::ObjectLifetimeDefault { s, .. }
| Scope::Supertrait { s, .. }
| Scope::TraitRefBoundary { s }
| Scope::LateBoundary { s, .. }
| Scope::Opaque { s, .. } => {
scope = s;
}
Scope::Root { .. } => break,
}
}
},
)
}
}
/// Detects late-bound lifetimes and inserts them into

View File

@ -219,11 +219,13 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
def_ids.retain(|def_id| !tcx.generics_require_sized_self(def_id));
}
self.complain_about_missing_assoc_tys(
if let Err(guar) = self.check_for_required_assoc_tys(
associated_types,
potential_assoc_types,
hir_trait_bounds,
);
) {
return Ty::new_error(tcx, guar);
}
// De-duplicate auto traits so that, e.g., `dyn Trait + Send + Send` is the same as
// `dyn Trait + Send`.

View File

@ -714,14 +714,14 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
/// reasonable suggestion on how to write it. For the case of multiple associated types in the
/// same trait bound have the same name (as they come from different supertraits), we instead
/// emit a generic note suggesting using a `where` clause to constraint instead.
pub(crate) fn complain_about_missing_assoc_tys(
pub(crate) fn check_for_required_assoc_tys(
&self,
associated_types: FxIndexMap<Span, FxIndexSet<DefId>>,
potential_assoc_types: Vec<usize>,
trait_bounds: &[hir::PolyTraitRef<'_>],
) {
) -> Result<(), ErrorGuaranteed> {
if associated_types.values().all(|v| v.is_empty()) {
return;
return Ok(());
}
let tcx = self.tcx();
@ -739,7 +739,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
// Account for things like `dyn Foo + 'a`, like in tests `issue-22434.rs` and
// `issue-22560.rs`.
let mut trait_bound_spans: Vec<Span> = vec![];
let mut dyn_compatibility_violations = false;
let mut dyn_compatibility_violations = Ok(());
for (span, items) in &associated_types {
if !items.is_empty() {
trait_bound_spans.push(*span);
@ -752,13 +752,20 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
let violations =
dyn_compatibility_violations_for_assoc_item(tcx, trait_def_id, *assoc_item);
if !violations.is_empty() {
report_dyn_incompatibility(tcx, *span, None, trait_def_id, &violations).emit();
dyn_compatibility_violations = true;
dyn_compatibility_violations = Err(report_dyn_incompatibility(
tcx,
*span,
None,
trait_def_id,
&violations,
)
.emit());
}
}
}
if dyn_compatibility_violations {
return;
if let Err(guar) = dyn_compatibility_violations {
return Err(guar);
}
// related to issue #91997, turbofishes added only when in an expr or pat
@ -965,7 +972,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
}
}
err.emit();
Err(err.emit())
}
/// On ambiguous associated type, look for an associated function whose name matches the

View File

@ -62,7 +62,9 @@ This API is completely unstable and subject to change.
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
#![doc(rust_logo)]
#![feature(assert_matches)]
#![feature(coroutines)]
#![feature(if_let_guard)]
#![feature(iter_from_coroutine)]
#![feature(iter_intersperse)]
#![feature(let_chains)]
#![feature(never_type)]

View File

@ -450,15 +450,15 @@ lint_invalid_nan_comparisons_eq_ne = incorrect NaN comparison, NaN cannot be dir
lint_invalid_nan_comparisons_lt_le_gt_ge = incorrect NaN comparison, NaN is not orderable
lint_invalid_reference_casting_assign_to_ref = assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
.label = casting happend here
.label = casting happened here
lint_invalid_reference_casting_bigger_layout = casting references to a bigger memory layout than the backing allocation is undefined behavior, even if the reference is unused
.label = casting happend here
.label = casting happened here
.alloc = backing allocation comes from here
.layout = casting from `{$from_ty}` ({$from_size} bytes) to `{$to_ty}` ({$to_size} bytes)
lint_invalid_reference_casting_borrow_as_mut = casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
.label = casting happend here
.label = casting happened here
lint_invalid_reference_casting_note_book = for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>

View File

@ -182,6 +182,7 @@ symbols! {
ConstParamTy_,
Context,
Continue,
ControlFlow,
Copy,
Cow,
Debug,

View File

@ -158,9 +158,9 @@ dependencies = [
[[package]]
name = "libc"
version = "0.2.162"
version = "0.2.167"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18d287de67fe55fd7e1581fe933d965a5a9477b38e949cfa9f8574ef01506398"
checksum = "09d6582e104315a817dff97f75133544b2e094ee22447d2acf4a74e189ba06fc"
dependencies = [
"rustc-std-workspace-core",
]

View File

@ -79,6 +79,7 @@ use crate::{convert, ops};
/// [`Break`]: ControlFlow::Break
/// [`Continue`]: ControlFlow::Continue
#[stable(feature = "control_flow_enum_type", since = "1.55.0")]
#[cfg_attr(not(test), rustc_diagnostic_item = "ControlFlow")]
// ControlFlow should not implement PartialOrd or Ord, per RFC 3058:
// https://rust-lang.github.io/rfcs/3058-try-trait-v2.html#traits-for-controlflow
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]

View File

@ -5,7 +5,7 @@
//! documents linked from it.
//! These are also good reads:
//! * <https://itanium-cxx-abi.github.io/cxx-abi/abi-eh.html>
//! * <https://monoinfinito.wordpress.com/series/exception-handling-in-c/>
//! * <https://nicolasbrailo.github.io/blog/projects_texts/13exceptionsunderthehood.html>
//! * <https://www.airs.com/blog/index.php?s=exception+frames>
//!
//! ## A brief summary

View File

@ -34,7 +34,7 @@ miniz_oxide = { version = "0.7.0", optional = true, default-features = false }
addr2line = { version = "0.22.0", optional = true, default-features = false }
[target.'cfg(not(all(windows, target_env = "msvc")))'.dependencies]
libc = { version = "0.2.162", default-features = false, features = [
libc = { version = "0.2.167", default-features = false, features = [
'rustc-dep-of-std',
], public = true }

View File

@ -298,7 +298,7 @@ pub trait MetadataExt {
#[stable(feature = "metadata_ext", since = "1.1.0")]
impl MetadataExt for Metadata {
fn st_dev(&self) -> u64 {
self.as_inner().as_inner().st_fsid as u64
self.as_inner().as_inner().st_dev as u64
}
fn st_ino(&self) -> u64 {
self.as_inner().as_inner().st_ino as u64

View File

@ -1158,6 +1158,7 @@ impl FusedIterator for Ancestors<'_> {}
/// Note that `PathBuf` does not always sanitize arguments, for example
/// [`push`] allows paths built from strings which include separators:
///
/// ```
/// use std::path::PathBuf;
///
/// let mut path = PathBuf::new();
@ -1166,6 +1167,7 @@ impl FusedIterator for Ancestors<'_> {}
/// path.push("windows");
/// path.push(r"..\otherdir");
/// path.push("system32");
/// ```
///
/// The behavior of `PathBuf` may be changed to a panic on such inputs
/// in the future. [`Extend::extend`] should be used to add multi-part paths.

View File

@ -27,6 +27,14 @@ pub mod thread;
#[path = "../unix/time.rs"]
pub mod time;
#[path = "../unix/sync"]
pub mod sync {
mod condvar;
mod mutex;
pub use condvar::Condvar;
pub use mutex::Mutex;
}
use crate::io::ErrorKind;
pub fn abort_internal() -> ! {

View File

@ -27,6 +27,7 @@ pub mod pipe;
pub mod process;
pub mod stack_overflow;
pub mod stdio;
pub mod sync;
pub mod thread;
pub mod thread_parking;
pub mod time;

View File

@ -427,11 +427,13 @@ pub fn current_exe() -> io::Result<PathBuf> {
pub fn current_exe() -> io::Result<PathBuf> {
unsafe {
let mut sz: u32 = 0;
#[expect(deprecated)]
libc::_NSGetExecutablePath(ptr::null_mut(), &mut sz);
if sz == 0 {
return Err(io::Error::last_os_error());
}
let mut v: Vec<u8> = Vec::with_capacity(sz as usize);
#[expect(deprecated)]
let err = libc::_NSGetExecutablePath(v.as_mut_ptr() as *mut i8, &mut sz);
if err != 0 {
return Err(io::Error::last_os_error());

View File

@ -0,0 +1,172 @@
use super::Mutex;
use crate::cell::UnsafeCell;
use crate::pin::Pin;
#[cfg(not(target_os = "nto"))]
use crate::sys::pal::time::TIMESPEC_MAX;
#[cfg(target_os = "nto")]
use crate::sys::pal::time::TIMESPEC_MAX_CAPPED;
use crate::sys::pal::time::Timespec;
use crate::time::Duration;
pub struct Condvar {
inner: UnsafeCell<libc::pthread_cond_t>,
}
impl Condvar {
pub fn new() -> Condvar {
Condvar { inner: UnsafeCell::new(libc::PTHREAD_COND_INITIALIZER) }
}
#[inline]
fn raw(&self) -> *mut libc::pthread_cond_t {
self.inner.get()
}
/// # Safety
/// `init` must have been called on this instance.
#[inline]
pub unsafe fn notify_one(self: Pin<&Self>) {
let r = unsafe { libc::pthread_cond_signal(self.raw()) };
debug_assert_eq!(r, 0);
}
/// # Safety
/// `init` must have been called on this instance.
#[inline]
pub unsafe fn notify_all(self: Pin<&Self>) {
let r = unsafe { libc::pthread_cond_broadcast(self.raw()) };
debug_assert_eq!(r, 0);
}
/// # Safety
/// * `init` must have been called on this instance.
/// * `mutex` must be locked by the current thread.
/// * This condition variable may only be used with the same mutex.
#[inline]
pub unsafe fn wait(self: Pin<&Self>, mutex: Pin<&Mutex>) {
let r = unsafe { libc::pthread_cond_wait(self.raw(), mutex.raw()) };
debug_assert_eq!(r, 0);
}
/// # Safety
/// * `init` must have been called on this instance.
/// * `mutex` must be locked by the current thread.
/// * This condition variable may only be used with the same mutex.
pub unsafe fn wait_timeout(&self, mutex: Pin<&Mutex>, dur: Duration) -> bool {
let mutex = mutex.raw();
// OSX implementation of `pthread_cond_timedwait` is buggy
// with super long durations. When duration is greater than
// 0x100_0000_0000_0000 seconds, `pthread_cond_timedwait`
// in macOS Sierra returns error 316.
//
// This program demonstrates the issue:
// https://gist.github.com/stepancheg/198db4623a20aad2ad7cddb8fda4a63c
//
// To work around this issue, the timeout is clamped to 1000 years.
#[cfg(target_vendor = "apple")]
let dur = Duration::min(dur, Duration::from_secs(1000 * 365 * 86400));
let timeout = Timespec::now(Self::CLOCK).checked_add_duration(&dur);
#[cfg(not(target_os = "nto"))]
let timeout = timeout.and_then(|t| t.to_timespec()).unwrap_or(TIMESPEC_MAX);
#[cfg(target_os = "nto")]
let timeout = timeout.and_then(|t| t.to_timespec_capped()).unwrap_or(TIMESPEC_MAX_CAPPED);
let r = unsafe { libc::pthread_cond_timedwait(self.raw(), mutex, &timeout) };
assert!(r == libc::ETIMEDOUT || r == 0);
r == 0
}
}
#[cfg(not(any(
target_os = "android",
target_vendor = "apple",
target_os = "espidf",
target_os = "horizon",
target_os = "l4re",
target_os = "redox",
target_os = "teeos",
)))]
impl Condvar {
pub const PRECISE_TIMEOUT: bool = true;
const CLOCK: libc::clockid_t = libc::CLOCK_MONOTONIC;
/// # Safety
/// May only be called once per instance of `Self`.
pub unsafe fn init(self: Pin<&mut Self>) {
use crate::mem::MaybeUninit;
struct AttrGuard<'a>(pub &'a mut MaybeUninit<libc::pthread_condattr_t>);
impl Drop for AttrGuard<'_> {
fn drop(&mut self) {
unsafe {
let result = libc::pthread_condattr_destroy(self.0.as_mut_ptr());
assert_eq!(result, 0);
}
}
}
unsafe {
let mut attr = MaybeUninit::<libc::pthread_condattr_t>::uninit();
let r = libc::pthread_condattr_init(attr.as_mut_ptr());
assert_eq!(r, 0);
let attr = AttrGuard(&mut attr);
let r = libc::pthread_condattr_setclock(attr.0.as_mut_ptr(), Self::CLOCK);
assert_eq!(r, 0);
let r = libc::pthread_cond_init(self.raw(), attr.0.as_ptr());
assert_eq!(r, 0);
}
}
}
// `pthread_condattr_setclock` is unfortunately not supported on these platforms.
#[cfg(any(
target_os = "android",
target_vendor = "apple",
target_os = "espidf",
target_os = "horizon",
target_os = "l4re",
target_os = "redox",
target_os = "teeos",
))]
impl Condvar {
pub const PRECISE_TIMEOUT: bool = false;
const CLOCK: libc::clockid_t = libc::CLOCK_REALTIME;
/// # Safety
/// May only be called once per instance of `Self`.
pub unsafe fn init(self: Pin<&mut Self>) {
if cfg!(any(target_os = "espidf", target_os = "horizon", target_os = "teeos")) {
// NOTE: ESP-IDF's PTHREAD_COND_INITIALIZER support is not released yet
// So on that platform, init() should always be called.
//
// Similar story for the 3DS (horizon) and for TEEOS.
let r = unsafe { libc::pthread_cond_init(self.raw(), crate::ptr::null()) };
assert_eq!(r, 0);
}
}
}
impl !Unpin for Condvar {}
unsafe impl Sync for Condvar {}
unsafe impl Send for Condvar {}
impl Drop for Condvar {
#[inline]
fn drop(&mut self) {
let r = unsafe { libc::pthread_cond_destroy(self.raw()) };
if cfg!(target_os = "dragonfly") {
// On DragonFly pthread_cond_destroy() returns EINVAL if called on
// a condvar that was just initialized with
// libc::PTHREAD_COND_INITIALIZER. Once it is used or
// pthread_cond_init() is called, this behaviour no longer occurs.
debug_assert!(r == 0 || r == libc::EINVAL);
} else {
debug_assert_eq!(r, 0);
}
}
}

View File

@ -0,0 +1,16 @@
#![cfg(not(any(
target_os = "linux",
target_os = "android",
all(target_os = "emscripten", target_feature = "atomics"),
target_os = "freebsd",
target_os = "openbsd",
target_os = "dragonfly",
target_os = "fuchsia",
)))]
#![forbid(unsafe_op_in_unsafe_fn)]
mod condvar;
mod mutex;
pub use condvar::Condvar;
pub use mutex::Mutex;

View File

@ -0,0 +1,135 @@
use super::super::cvt_nz;
use crate::cell::UnsafeCell;
use crate::io::Error;
use crate::mem::MaybeUninit;
use crate::pin::Pin;
pub struct Mutex {
inner: UnsafeCell<libc::pthread_mutex_t>,
}
impl Mutex {
pub fn new() -> Mutex {
Mutex { inner: UnsafeCell::new(libc::PTHREAD_MUTEX_INITIALIZER) }
}
pub(super) fn raw(&self) -> *mut libc::pthread_mutex_t {
self.inner.get()
}
/// # Safety
/// May only be called once per instance of `Self`.
pub unsafe fn init(self: Pin<&mut Self>) {
// Issue #33770
//
// A pthread mutex initialized with PTHREAD_MUTEX_INITIALIZER will have
// a type of PTHREAD_MUTEX_DEFAULT, which has undefined behavior if you
// try to re-lock it from the same thread when you already hold a lock
// (https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_init.html).
// This is the case even if PTHREAD_MUTEX_DEFAULT == PTHREAD_MUTEX_NORMAL
// (https://github.com/rust-lang/rust/issues/33770#issuecomment-220847521) -- in that
// case, `pthread_mutexattr_settype(PTHREAD_MUTEX_DEFAULT)` will of course be the same
// as setting it to `PTHREAD_MUTEX_NORMAL`, but not setting any mode will result in
// a Mutex where re-locking is UB.
//
// In practice, glibc takes advantage of this undefined behavior to
// implement hardware lock elision, which uses hardware transactional
// memory to avoid acquiring the lock. While a transaction is in
// progress, the lock appears to be unlocked. This isn't a problem for
// other threads since the transactional memory will abort if a conflict
// is detected, however no abort is generated when re-locking from the
// same thread.
//
// Since locking the same mutex twice will result in two aliasing &mut
// references, we instead create the mutex with type
// PTHREAD_MUTEX_NORMAL which is guaranteed to deadlock if we try to
// re-lock it from the same thread, thus avoiding undefined behavior.
unsafe {
let mut attr = MaybeUninit::<libc::pthread_mutexattr_t>::uninit();
cvt_nz(libc::pthread_mutexattr_init(attr.as_mut_ptr())).unwrap();
let attr = AttrGuard(&mut attr);
cvt_nz(libc::pthread_mutexattr_settype(
attr.0.as_mut_ptr(),
libc::PTHREAD_MUTEX_NORMAL,
))
.unwrap();
cvt_nz(libc::pthread_mutex_init(self.raw(), attr.0.as_ptr())).unwrap();
}
}
/// # Safety
/// * If `init` was not called on this instance, reentrant locking causes
/// undefined behaviour.
/// * Destroying a locked mutex causes undefined behaviour.
pub unsafe fn lock(self: Pin<&Self>) {
#[cold]
#[inline(never)]
fn fail(r: i32) -> ! {
let error = Error::from_raw_os_error(r);
panic!("failed to lock mutex: {error}");
}
let r = unsafe { libc::pthread_mutex_lock(self.raw()) };
// As we set the mutex type to `PTHREAD_MUTEX_NORMAL` above, we expect
// the lock call to never fail. Unfortunately however, some platforms
// (Solaris) do not conform to the standard, and instead always provide
// deadlock detection. How kind of them! Unfortunately that means that
// we need to check the error code here. To save us from UB on other
// less well-behaved platforms in the future, we do it even on "good"
// platforms like macOS. See #120147 for more context.
if r != 0 {
fail(r)
}
}
/// # Safety
/// * If `init` was not called on this instance, reentrant locking causes
/// undefined behaviour.
/// * Destroying a locked mutex causes undefined behaviour.
pub unsafe fn try_lock(self: Pin<&Self>) -> bool {
unsafe { libc::pthread_mutex_trylock(self.raw()) == 0 }
}
/// # Safety
/// The mutex must be locked by the current thread.
pub unsafe fn unlock(self: Pin<&Self>) {
let r = unsafe { libc::pthread_mutex_unlock(self.raw()) };
debug_assert_eq!(r, 0);
}
}
impl !Unpin for Mutex {}
unsafe impl Send for Mutex {}
unsafe impl Sync for Mutex {}
impl Drop for Mutex {
fn drop(&mut self) {
// SAFETY:
// If `lock` or `init` was called, the mutex must have been pinned, so
// it is still at the same location. Otherwise, `inner` must contain
// `PTHREAD_MUTEX_INITIALIZER`, which is valid at all locations. Thus,
// this call always destroys a valid mutex.
let r = unsafe { libc::pthread_mutex_destroy(self.raw()) };
if cfg!(target_os = "dragonfly") {
// On DragonFly pthread_mutex_destroy() returns EINVAL if called on a
// mutex that was just initialized with libc::PTHREAD_MUTEX_INITIALIZER.
// Once it is used (locked/unlocked) or pthread_mutex_init() is called,
// this behaviour no longer occurs.
debug_assert!(r == 0 || r == libc::EINVAL);
} else {
debug_assert_eq!(r, 0);
}
}
}
struct AttrGuard<'a>(pub &'a mut MaybeUninit<libc::pthread_mutexattr_t>);
impl Drop for AttrGuard<'_> {
fn drop(&mut self) {
unsafe {
let result = libc::pthread_mutexattr_destroy(self.0.as_mut_ptr());
assert_eq!(result, 0);
}
}
}

View File

@ -5,7 +5,7 @@
//! documents linked from it.
//! These are also good reads:
//! * <https://itanium-cxx-abi.github.io/cxx-abi/abi-eh.html>
//! * <https://monoinfinito.wordpress.com/series/exception-handling-in-c/>
//! * <https://nicolasbrailo.github.io/blog/projects_texts/13exceptionsunderthehood.html>
//! * <https://www.airs.com/blog/index.php?s=exception+frames>
//!
//! ## A brief summary

View File

@ -1,196 +1,88 @@
use crate::cell::UnsafeCell;
use crate::ptr;
use crate::sync::atomic::AtomicPtr;
use crate::sync::atomic::Ordering::Relaxed;
use crate::sys::sync::{Mutex, OnceBox};
#[cfg(not(target_os = "nto"))]
use crate::sys::time::TIMESPEC_MAX;
#[cfg(target_os = "nto")]
use crate::sys::time::TIMESPEC_MAX_CAPPED;
use crate::time::Duration;
#![forbid(unsafe_op_in_unsafe_fn)]
struct AllocatedCondvar(UnsafeCell<libc::pthread_cond_t>);
use crate::pin::Pin;
use crate::ptr;
use crate::sync::atomic::AtomicUsize;
use crate::sync::atomic::Ordering::Relaxed;
use crate::sys::pal::sync as pal;
use crate::sys::sync::{Mutex, OnceBox};
use crate::time::{Duration, Instant};
pub struct Condvar {
inner: OnceBox<AllocatedCondvar>,
mutex: AtomicPtr<libc::pthread_mutex_t>,
}
unsafe impl Send for AllocatedCondvar {}
unsafe impl Sync for AllocatedCondvar {}
impl AllocatedCondvar {
fn new() -> Box<Self> {
let condvar = Box::new(AllocatedCondvar(UnsafeCell::new(libc::PTHREAD_COND_INITIALIZER)));
cfg_if::cfg_if! {
if #[cfg(any(
target_os = "l4re",
target_os = "android",
target_os = "redox",
target_vendor = "apple",
))] {
// `pthread_condattr_setclock` is unfortunately not supported on these platforms.
} else if #[cfg(any(target_os = "espidf", target_os = "horizon", target_os = "teeos"))] {
// NOTE: ESP-IDF's PTHREAD_COND_INITIALIZER support is not released yet
// So on that platform, init() should always be called
// Moreover, that platform does not have pthread_condattr_setclock support,
// hence that initialization should be skipped as well
//
// Similar story for the 3DS (horizon).
let r = unsafe { libc::pthread_cond_init(condvar.0.get(), crate::ptr::null()) };
assert_eq!(r, 0);
} else {
use crate::mem::MaybeUninit;
let mut attr = MaybeUninit::<libc::pthread_condattr_t>::uninit();
let r = unsafe { libc::pthread_condattr_init(attr.as_mut_ptr()) };
assert_eq!(r, 0);
let r = unsafe { libc::pthread_condattr_setclock(attr.as_mut_ptr(), libc::CLOCK_MONOTONIC) };
assert_eq!(r, 0);
let r = unsafe { libc::pthread_cond_init(condvar.0.get(), attr.as_ptr()) };
assert_eq!(r, 0);
let r = unsafe { libc::pthread_condattr_destroy(attr.as_mut_ptr()) };
assert_eq!(r, 0);
}
}
condvar
}
}
impl Drop for AllocatedCondvar {
#[inline]
fn drop(&mut self) {
let r = unsafe { libc::pthread_cond_destroy(self.0.get()) };
if cfg!(target_os = "dragonfly") {
// On DragonFly pthread_cond_destroy() returns EINVAL if called on
// a condvar that was just initialized with
// libc::PTHREAD_COND_INITIALIZER. Once it is used or
// pthread_cond_init() is called, this behavior no longer occurs.
debug_assert!(r == 0 || r == libc::EINVAL);
} else {
debug_assert_eq!(r, 0);
}
}
cvar: OnceBox<pal::Condvar>,
mutex: AtomicUsize,
}
impl Condvar {
pub const fn new() -> Condvar {
Condvar { inner: OnceBox::new(), mutex: AtomicPtr::new(ptr::null_mut()) }
}
fn get(&self) -> *mut libc::pthread_cond_t {
self.inner.get_or_init(AllocatedCondvar::new).0.get()
Condvar { cvar: OnceBox::new(), mutex: AtomicUsize::new(0) }
}
#[inline]
fn verify(&self, mutex: *mut libc::pthread_mutex_t) {
// Relaxed is okay here because we never read through `self.addr`, and only use it to
fn get(&self) -> Pin<&pal::Condvar> {
self.cvar.get_or_init(|| {
let mut cvar = Box::pin(pal::Condvar::new());
// SAFETY: we only call `init` once per `pal::Condvar`, namely here.
unsafe { cvar.as_mut().init() };
cvar
})
}
#[inline]
fn verify(&self, mutex: Pin<&pal::Mutex>) {
let addr = ptr::from_ref::<pal::Mutex>(&mutex).addr();
// Relaxed is okay here because we never read through `self.mutex`, and only use it to
// compare addresses.
match self.mutex.compare_exchange(ptr::null_mut(), mutex, Relaxed, Relaxed) {
Ok(_) => {} // Stored the address
Err(n) if n == mutex => {} // Lost a race to store the same address
match self.mutex.compare_exchange(0, addr, Relaxed, Relaxed) {
Ok(_) => {} // Stored the address
Err(n) if n == addr => {} // Lost a race to store the same address
_ => panic!("attempted to use a condition variable with two mutexes"),
}
}
#[inline]
pub fn notify_one(&self) {
let r = unsafe { libc::pthread_cond_signal(self.get()) };
debug_assert_eq!(r, 0);
// SAFETY: we called `init` above.
unsafe { self.get().notify_one() }
}
#[inline]
pub fn notify_all(&self) {
let r = unsafe { libc::pthread_cond_broadcast(self.get()) };
debug_assert_eq!(r, 0);
// SAFETY: we called `init` above.
unsafe { self.get().notify_all() }
}
#[inline]
pub unsafe fn wait(&self, mutex: &Mutex) {
let mutex = mutex.get_assert_locked();
// SAFETY: the caller guarantees that the lock is owned, thus the mutex
// must have been initialized already.
let mutex = unsafe { mutex.pal.get_unchecked() };
self.verify(mutex);
let r = libc::pthread_cond_wait(self.get(), mutex);
debug_assert_eq!(r, 0);
// SAFETY: we called `init` above, we verified that this condition
// variable is only used with `mutex` and the caller guarantees that
// `mutex` is locked by the current thread.
unsafe { self.get().wait(mutex) }
}
// This implementation is used on systems that support pthread_condattr_setclock
// where we configure condition variable to use monotonic clock (instead of
// default system clock). This approach avoids all problems that result
// from changes made to the system time.
#[cfg(not(any(
target_os = "android",
target_os = "espidf",
target_os = "horizon",
target_vendor = "apple",
)))]
pub unsafe fn wait_timeout(&self, mutex: &Mutex, dur: Duration) -> bool {
use crate::sys::time::Timespec;
let mutex = mutex.get_assert_locked();
// SAFETY: the caller guarantees that the lock is owned, thus the mutex
// must have been initialized already.
let mutex = unsafe { mutex.pal.get_unchecked() };
self.verify(mutex);
#[cfg(not(target_os = "nto"))]
let timeout = Timespec::now(libc::CLOCK_MONOTONIC)
.checked_add_duration(&dur)
.and_then(|t| t.to_timespec())
.unwrap_or(TIMESPEC_MAX);
#[cfg(target_os = "nto")]
let timeout = Timespec::now(libc::CLOCK_MONOTONIC)
.checked_add_duration(&dur)
.and_then(|t| t.to_timespec_capped())
.unwrap_or(TIMESPEC_MAX_CAPPED);
let r = libc::pthread_cond_timedwait(self.get(), mutex, &timeout);
assert!(r == libc::ETIMEDOUT || r == 0);
r == 0
}
// This implementation is modeled after libcxx's condition_variable
// https://github.com/llvm-mirror/libcxx/blob/release_35/src/condition_variable.cpp#L46
// https://github.com/llvm-mirror/libcxx/blob/release_35/include/__mutex_base#L367
#[cfg(any(
target_os = "android",
target_os = "espidf",
target_os = "horizon",
target_vendor = "apple",
))]
pub unsafe fn wait_timeout(&self, mutex: &Mutex, dur: Duration) -> bool {
use crate::sys::time::SystemTime;
use crate::time::Instant;
let mutex = mutex.get_assert_locked();
self.verify(mutex);
// OSX implementation of `pthread_cond_timedwait` is buggy
// with super long durations. When duration is greater than
// 0x100_0000_0000_0000 seconds, `pthread_cond_timedwait`
// in macOS Sierra returns error 316.
//
// This program demonstrates the issue:
// https://gist.github.com/stepancheg/198db4623a20aad2ad7cddb8fda4a63c
//
// To work around this issue, and possible bugs of other OSes, timeout
// is clamped to 1000 years, which is allowable per the API of `wait_timeout`
// because of spurious wakeups.
let dur = Duration::min(dur, Duration::from_secs(1000 * 365 * 86400));
// pthread_cond_timedwait uses system time, but we want to report timeout
// based on stable time.
let now = Instant::now();
let timeout = SystemTime::now()
.t
.checked_add_duration(&dur)
.and_then(|t| t.to_timespec())
.unwrap_or(TIMESPEC_MAX);
let r = libc::pthread_cond_timedwait(self.get(), mutex, &timeout);
debug_assert!(r == libc::ETIMEDOUT || r == 0);
// ETIMEDOUT is not a totally reliable method of determining timeout due
// to clock shifts, so do the check ourselves
now.elapsed() < dur
if pal::Condvar::PRECISE_TIMEOUT {
// SAFETY: we called `init` above, we verified that this condition
// variable is only used with `mutex` and the caller guarantees that
// `mutex` is locked by the current thread.
unsafe { self.get().wait_timeout(mutex, dur) }
} else {
// Timeout reports are not reliable, so do the check ourselves.
let now = Instant::now();
// SAFETY: we called `init` above, we verified that this condition
// variable is only used with `mutex` and the caller guarantees that
// `mutex` is locked by the current thread.
let woken = unsafe { self.get().wait_timeout(mutex, dur) };
woken || now.elapsed() < dur
}
}
}

View File

@ -13,17 +13,19 @@ impl Condvar {
}
fn get(&self) -> &SpinMutex<WaitVariable<()>> {
self.inner.get_or_init(|| Box::new(SpinMutex::new(WaitVariable::new(()))))
self.inner.get_or_init(|| Box::pin(SpinMutex::new(WaitVariable::new(())))).get_ref()
}
#[inline]
pub fn notify_one(&self) {
let _ = WaitQueue::notify_one(self.get().lock());
let guard = self.get().lock();
let _ = WaitQueue::notify_one(guard);
}
#[inline]
pub fn notify_all(&self) {
let _ = WaitQueue::notify_all(self.get().lock());
let guard = self.get().lock();
let _ = WaitQueue::notify_all(guard);
}
pub unsafe fn wait(&self, mutex: &Mutex) {

View File

@ -1,163 +1,66 @@
use crate::cell::UnsafeCell;
use crate::io::Error;
use crate::mem::{MaybeUninit, forget};
use crate::sys::cvt_nz;
#![forbid(unsafe_op_in_unsafe_fn)]
use crate::mem::forget;
use crate::pin::Pin;
use crate::sys::pal::sync as pal;
use crate::sys::sync::OnceBox;
struct AllocatedMutex(UnsafeCell<libc::pthread_mutex_t>);
pub struct Mutex {
inner: OnceBox<AllocatedMutex>,
}
unsafe impl Send for AllocatedMutex {}
unsafe impl Sync for AllocatedMutex {}
impl AllocatedMutex {
fn new() -> Box<Self> {
let mutex = Box::new(AllocatedMutex(UnsafeCell::new(libc::PTHREAD_MUTEX_INITIALIZER)));
// Issue #33770
//
// A pthread mutex initialized with PTHREAD_MUTEX_INITIALIZER will have
// a type of PTHREAD_MUTEX_DEFAULT, which has undefined behavior if you
// try to re-lock it from the same thread when you already hold a lock
// (https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_init.html).
// This is the case even if PTHREAD_MUTEX_DEFAULT == PTHREAD_MUTEX_NORMAL
// (https://github.com/rust-lang/rust/issues/33770#issuecomment-220847521) -- in that
// case, `pthread_mutexattr_settype(PTHREAD_MUTEX_DEFAULT)` will of course be the same
// as setting it to `PTHREAD_MUTEX_NORMAL`, but not setting any mode will result in
// a Mutex where re-locking is UB.
//
// In practice, glibc takes advantage of this undefined behavior to
// implement hardware lock elision, which uses hardware transactional
// memory to avoid acquiring the lock. While a transaction is in
// progress, the lock appears to be unlocked. This isn't a problem for
// other threads since the transactional memory will abort if a conflict
// is detected, however no abort is generated when re-locking from the
// same thread.
//
// Since locking the same mutex twice will result in two aliasing &mut
// references, we instead create the mutex with type
// PTHREAD_MUTEX_NORMAL which is guaranteed to deadlock if we try to
// re-lock it from the same thread, thus avoiding undefined behavior.
unsafe {
let mut attr = MaybeUninit::<libc::pthread_mutexattr_t>::uninit();
cvt_nz(libc::pthread_mutexattr_init(attr.as_mut_ptr())).unwrap();
let attr = PthreadMutexAttr(&mut attr);
cvt_nz(libc::pthread_mutexattr_settype(
attr.0.as_mut_ptr(),
libc::PTHREAD_MUTEX_NORMAL,
))
.unwrap();
cvt_nz(libc::pthread_mutex_init(mutex.0.get(), attr.0.as_ptr())).unwrap();
}
mutex
}
}
impl Drop for AllocatedMutex {
#[inline]
fn drop(&mut self) {
let r = unsafe { libc::pthread_mutex_destroy(self.0.get()) };
if cfg!(target_os = "dragonfly") {
// On DragonFly pthread_mutex_destroy() returns EINVAL if called on a
// mutex that was just initialized with libc::PTHREAD_MUTEX_INITIALIZER.
// Once it is used (locked/unlocked) or pthread_mutex_init() is called,
// this behavior no longer occurs.
debug_assert!(r == 0 || r == libc::EINVAL);
} else {
debug_assert_eq!(r, 0);
}
}
pub pal: OnceBox<pal::Mutex>,
}
impl Mutex {
#[inline]
pub const fn new() -> Mutex {
Mutex { inner: OnceBox::new() }
}
/// Gets access to the pthread mutex under the assumption that the mutex is
/// locked.
///
/// This allows skipping the initialization check, as the mutex can only be
/// locked if it is already initialized, and allows relaxing the ordering
/// on the pointer load, since the allocation cannot have been modified
/// since the `lock` and the lock must have occurred on the current thread.
///
/// # Safety
/// Causes undefined behavior if the mutex is not locked.
#[inline]
pub(crate) unsafe fn get_assert_locked(&self) -> *mut libc::pthread_mutex_t {
unsafe { self.inner.get_unchecked().0.get() }
Mutex { pal: OnceBox::new() }
}
#[inline]
fn get(&self) -> *mut libc::pthread_mutex_t {
// If initialization fails, the mutex is destroyed. This is always sound,
// however, as the mutex cannot have been locked yet.
self.inner.get_or_init(AllocatedMutex::new).0.get()
fn get(&self) -> Pin<&pal::Mutex> {
// If the initialization race is lost, the new mutex is destroyed.
// This is sound however, as it cannot have been locked.
self.pal.get_or_init(|| {
let mut pal = Box::pin(pal::Mutex::new());
// SAFETY: we only call `init` once per `pal::Mutex`, namely here.
unsafe { pal.as_mut().init() };
pal
})
}
#[inline]
pub fn lock(&self) {
#[cold]
#[inline(never)]
fn fail(r: i32) -> ! {
let error = Error::from_raw_os_error(r);
panic!("failed to lock mutex: {error}");
}
let r = unsafe { libc::pthread_mutex_lock(self.get()) };
// As we set the mutex type to `PTHREAD_MUTEX_NORMAL` above, we expect
// the lock call to never fail. Unfortunately however, some platforms
// (Solaris) do not conform to the standard, and instead always provide
// deadlock detection. How kind of them! Unfortunately that means that
// we need to check the error code here. To save us from UB on other
// less well-behaved platforms in the future, we do it even on "good"
// platforms like macOS. See #120147 for more context.
if r != 0 {
fail(r)
}
// SAFETY: we call `init` above, therefore reentrant locking is safe.
// In `drop` we ensure that the mutex is not destroyed while locked.
unsafe { self.get().lock() }
}
#[inline]
pub unsafe fn unlock(&self) {
let r = libc::pthread_mutex_unlock(self.get_assert_locked());
debug_assert_eq!(r, 0);
// SAFETY: the mutex can only be locked if it is already initialized
// and we observed this initialization since we observed the locking.
unsafe { self.pal.get_unchecked().unlock() }
}
#[inline]
pub fn try_lock(&self) -> bool {
unsafe { libc::pthread_mutex_trylock(self.get()) == 0 }
// SAFETY: we call `init` above, therefore reentrant locking is safe.
// In `drop` we ensure that the mutex is not destroyed while locked.
unsafe { self.get().try_lock() }
}
}
impl Drop for Mutex {
fn drop(&mut self) {
let Some(mutex) = self.inner.take() else { return };
let Some(pal) = self.pal.take() else { return };
// We're not allowed to pthread_mutex_destroy a locked mutex,
// so check first if it's unlocked.
if unsafe { libc::pthread_mutex_trylock(mutex.0.get()) == 0 } {
unsafe { libc::pthread_mutex_unlock(mutex.0.get()) };
drop(mutex);
if unsafe { pal.as_ref().try_lock() } {
unsafe { pal.as_ref().unlock() };
drop(pal)
} else {
// The mutex is locked. This happens if a MutexGuard is leaked.
// In this case, we just leak the Mutex too.
forget(mutex);
}
}
}
pub(super) struct PthreadMutexAttr<'a>(pub &'a mut MaybeUninit<libc::pthread_mutexattr_t>);
impl Drop for PthreadMutexAttr<'_> {
fn drop(&mut self) {
unsafe {
let result = libc::pthread_mutexattr_destroy(self.0.as_mut_ptr());
debug_assert_eq!(result, 0);
forget(pal)
}
}
}

View File

@ -13,7 +13,7 @@ impl Mutex {
}
fn get(&self) -> &SpinMutex<WaitVariable<bool>> {
self.inner.get_or_init(|| Box::new(SpinMutex::new(WaitVariable::new(false))))
self.inner.get_or_init(|| Box::pin(SpinMutex::new(WaitVariable::new(false)))).get_ref()
}
#[inline]
@ -33,7 +33,7 @@ impl Mutex {
pub unsafe fn unlock(&self) {
// SAFETY: the mutex was locked by the current thread, so it has been
// initialized already.
let guard = unsafe { self.inner.get_unchecked().lock() };
let guard = unsafe { self.inner.get_unchecked().get_ref().lock() };
if let Err(mut guard) = WaitQueue::notify_one(guard) {
// No other waiters, unlock
*guard.lock_var_mut() = false;

View File

@ -6,6 +6,7 @@
#![allow(dead_code)] // Only used on some platforms.
use crate::mem::replace;
use crate::pin::Pin;
use crate::ptr::null_mut;
use crate::sync::atomic::AtomicPtr;
use crate::sync::atomic::Ordering::{Acquire, Relaxed, Release};
@ -27,46 +28,46 @@ impl<T> OnceBox<T> {
/// pointer load in this function can be performed with relaxed ordering,
/// potentially allowing the optimizer to turn code like this:
/// ```rust, ignore
/// once_box.get_or_init(|| Box::new(42));
/// once_box.get_or_init(|| Box::pin(42));
/// unsafe { once_box.get_unchecked() }
/// ```
/// into
/// ```rust, ignore
/// once_box.get_or_init(|| Box::new(42))
/// once_box.get_or_init(|| Box::pin(42))
/// ```
///
/// # Safety
/// This causes undefined behavior if the assumption above is violated.
#[inline]
pub unsafe fn get_unchecked(&self) -> &T {
unsafe { &*self.ptr.load(Relaxed) }
pub unsafe fn get_unchecked(&self) -> Pin<&T> {
unsafe { Pin::new_unchecked(&*self.ptr.load(Relaxed)) }
}
#[inline]
pub fn get_or_init(&self, f: impl FnOnce() -> Box<T>) -> &T {
pub fn get_or_init(&self, f: impl FnOnce() -> Pin<Box<T>>) -> Pin<&T> {
let ptr = self.ptr.load(Acquire);
match unsafe { ptr.as_ref() } {
Some(val) => val,
Some(val) => unsafe { Pin::new_unchecked(val) },
None => self.initialize(f),
}
}
#[inline]
pub fn take(&mut self) -> Option<Box<T>> {
pub fn take(&mut self) -> Option<Pin<Box<T>>> {
let ptr = replace(self.ptr.get_mut(), null_mut());
if !ptr.is_null() { Some(unsafe { Box::from_raw(ptr) }) } else { None }
if !ptr.is_null() { Some(unsafe { Pin::new_unchecked(Box::from_raw(ptr)) }) } else { None }
}
#[cold]
fn initialize(&self, f: impl FnOnce() -> Box<T>) -> &T {
let new_ptr = Box::into_raw(f());
fn initialize(&self, f: impl FnOnce() -> Pin<Box<T>>) -> Pin<&T> {
let new_ptr = Box::into_raw(unsafe { Pin::into_inner_unchecked(f()) });
match self.ptr.compare_exchange(null_mut(), new_ptr, Release, Acquire) {
Ok(_) => unsafe { &*new_ptr },
Ok(_) => unsafe { Pin::new_unchecked(&*new_ptr) },
Err(ptr) => {
// Lost the race to another thread.
// Drop the value we created, and use the one from the other thread instead.
drop(unsafe { Box::from_raw(new_ptr) });
unsafe { &*ptr }
unsafe { Pin::new_unchecked(&*ptr) }
}
}
}

View File

@ -1,93 +1,19 @@
//! Thread parking without `futex` using the `pthread` synchronization primitives.
use crate::cell::UnsafeCell;
use crate::marker::PhantomPinned;
use crate::pin::Pin;
use crate::sync::atomic::AtomicUsize;
use crate::sync::atomic::Ordering::{Acquire, Relaxed, Release};
#[cfg(not(target_os = "nto"))]
use crate::sys::time::TIMESPEC_MAX;
#[cfg(target_os = "nto")]
use crate::sys::time::TIMESPEC_MAX_CAPPED;
use crate::sys::pal::sync::{Condvar, Mutex};
use crate::time::Duration;
const EMPTY: usize = 0;
const PARKED: usize = 1;
const NOTIFIED: usize = 2;
unsafe fn lock(lock: *mut libc::pthread_mutex_t) {
let r = libc::pthread_mutex_lock(lock);
debug_assert_eq!(r, 0);
}
unsafe fn unlock(lock: *mut libc::pthread_mutex_t) {
let r = libc::pthread_mutex_unlock(lock);
debug_assert_eq!(r, 0);
}
unsafe fn notify_one(cond: *mut libc::pthread_cond_t) {
let r = libc::pthread_cond_signal(cond);
debug_assert_eq!(r, 0);
}
unsafe fn wait(cond: *mut libc::pthread_cond_t, lock: *mut libc::pthread_mutex_t) {
let r = libc::pthread_cond_wait(cond, lock);
debug_assert_eq!(r, 0);
}
unsafe fn wait_timeout(
cond: *mut libc::pthread_cond_t,
lock: *mut libc::pthread_mutex_t,
dur: Duration,
) {
// Use the system clock on systems that do not support pthread_condattr_setclock.
// This unfortunately results in problems when the system time changes.
#[cfg(any(target_os = "espidf", target_os = "horizon", target_vendor = "apple"))]
let (now, dur) = {
use crate::cmp::min;
use crate::sys::time::SystemTime;
// OSX implementation of `pthread_cond_timedwait` is buggy
// with super long durations. When duration is greater than
// 0x100_0000_0000_0000 seconds, `pthread_cond_timedwait`
// in macOS Sierra return error 316.
//
// This program demonstrates the issue:
// https://gist.github.com/stepancheg/198db4623a20aad2ad7cddb8fda4a63c
//
// To work around this issue, and possible bugs of other OSes, timeout
// is clamped to 1000 years, which is allowable per the API of `park_timeout`
// because of spurious wakeups.
let dur = min(dur, Duration::from_secs(1000 * 365 * 86400));
let now = SystemTime::now().t;
(now, dur)
};
// Use the monotonic clock on other systems.
#[cfg(not(any(target_os = "espidf", target_os = "horizon", target_vendor = "apple")))]
let (now, dur) = {
use crate::sys::time::Timespec;
(Timespec::now(libc::CLOCK_MONOTONIC), dur)
};
#[cfg(not(target_os = "nto"))]
let timeout =
now.checked_add_duration(&dur).and_then(|t| t.to_timespec()).unwrap_or(TIMESPEC_MAX);
#[cfg(target_os = "nto")]
let timeout = now
.checked_add_duration(&dur)
.and_then(|t| t.to_timespec_capped())
.unwrap_or(TIMESPEC_MAX_CAPPED);
let r = libc::pthread_cond_timedwait(cond, lock, &timeout);
debug_assert!(r == libc::ETIMEDOUT || r == 0);
}
pub struct Parker {
state: AtomicUsize,
lock: UnsafeCell<libc::pthread_mutex_t>,
cvar: UnsafeCell<libc::pthread_cond_t>,
// The `pthread` primitives require a stable address, so make this struct `!Unpin`.
_pinned: PhantomPinned,
lock: Mutex,
cvar: Condvar,
}
impl Parker {
@ -96,38 +22,21 @@ impl Parker {
/// # Safety
/// The constructed parker must never be moved.
pub unsafe fn new_in_place(parker: *mut Parker) {
// Use the default mutex implementation to allow for simpler initialization.
// This could lead to undefined behavior when deadlocking. This is avoided
// by not deadlocking. Note in particular the unlocking operation before any
// panic, as code after the panic could try to park again.
(&raw mut (*parker).state).write(AtomicUsize::new(EMPTY));
(&raw mut (*parker).lock).write(UnsafeCell::new(libc::PTHREAD_MUTEX_INITIALIZER));
parker.write(Parker {
state: AtomicUsize::new(EMPTY),
lock: Mutex::new(),
cvar: Condvar::new(),
});
cfg_if::cfg_if! {
if #[cfg(any(
target_os = "l4re",
target_os = "android",
target_os = "redox",
target_os = "vita",
target_vendor = "apple",
))] {
(&raw mut (*parker).cvar).write(UnsafeCell::new(libc::PTHREAD_COND_INITIALIZER));
} else if #[cfg(any(target_os = "espidf", target_os = "horizon"))] {
let r = libc::pthread_cond_init((&raw mut (*parker).cvar).cast(), crate::ptr::null());
assert_eq!(r, 0);
} else {
use crate::mem::MaybeUninit;
let mut attr = MaybeUninit::<libc::pthread_condattr_t>::uninit();
let r = libc::pthread_condattr_init(attr.as_mut_ptr());
assert_eq!(r, 0);
let r = libc::pthread_condattr_setclock(attr.as_mut_ptr(), libc::CLOCK_MONOTONIC);
assert_eq!(r, 0);
let r = libc::pthread_cond_init((&raw mut (*parker).cvar).cast(), attr.as_ptr());
assert_eq!(r, 0);
let r = libc::pthread_condattr_destroy(attr.as_mut_ptr());
assert_eq!(r, 0);
}
}
Pin::new_unchecked(&mut (*parker).cvar).init();
}
fn lock(self: Pin<&Self>) -> Pin<&Mutex> {
unsafe { self.map_unchecked(|p| &p.lock) }
}
fn cvar(self: Pin<&Self>) -> Pin<&Condvar> {
unsafe { self.map_unchecked(|p| &p.cvar) }
}
// This implementation doesn't require `unsafe`, but other implementations
@ -142,7 +51,7 @@ impl Parker {
}
// Otherwise we need to coordinate going to sleep
lock(self.lock.get());
self.lock().lock();
match self.state.compare_exchange(EMPTY, PARKED, Relaxed, Relaxed) {
Ok(_) => {}
Err(NOTIFIED) => {
@ -154,20 +63,20 @@ impl Parker {
// read from the write it made to `state`.
let old = self.state.swap(EMPTY, Acquire);
unlock(self.lock.get());
self.lock().unlock();
assert_eq!(old, NOTIFIED, "park state changed unexpectedly");
return;
} // should consume this notification, so prohibit spurious wakeups in next park.
Err(_) => {
unlock(self.lock.get());
self.lock().unlock();
panic!("inconsistent park state")
}
}
loop {
wait(self.cvar.get(), self.lock.get());
self.cvar().wait(self.lock());
match self.state.compare_exchange(NOTIFIED, EMPTY, Acquire, Relaxed) {
Ok(_) => break, // got a notification
@ -175,7 +84,7 @@ impl Parker {
}
}
unlock(self.lock.get());
self.lock().unlock();
}
// This implementation doesn't require `unsafe`, but other implementations
@ -189,19 +98,19 @@ impl Parker {
return;
}
lock(self.lock.get());
self.lock().lock();
match self.state.compare_exchange(EMPTY, PARKED, Relaxed, Relaxed) {
Ok(_) => {}
Err(NOTIFIED) => {
// We must read again here, see `park`.
let old = self.state.swap(EMPTY, Acquire);
unlock(self.lock.get());
self.lock().unlock();
assert_eq!(old, NOTIFIED, "park state changed unexpectedly");
return;
} // should consume this notification, so prohibit spurious wakeups in next park.
Err(_) => {
unlock(self.lock.get());
self.lock().unlock();
panic!("inconsistent park_timeout state")
}
}
@ -210,13 +119,13 @@ impl Parker {
// from a notification we just want to unconditionally set the state back to
// empty, either consuming a notification or un-flagging ourselves as
// parked.
wait_timeout(self.cvar.get(), self.lock.get(), dur);
self.cvar().wait_timeout(self.lock(), dur);
match self.state.swap(EMPTY, Acquire) {
NOTIFIED => unlock(self.lock.get()), // got a notification, hurray!
PARKED => unlock(self.lock.get()), // no notification, alas
NOTIFIED => self.lock().unlock(), // got a notification, hurray!
PARKED => self.lock().unlock(), // no notification, alas
n => {
unlock(self.lock.get());
self.lock().unlock();
panic!("inconsistent park_timeout state: {n}")
}
}
@ -248,21 +157,9 @@ impl Parker {
// parked thread wakes it doesn't get woken only to have to wait for us
// to release `lock`.
unsafe {
lock(self.lock.get());
unlock(self.lock.get());
notify_one(self.cvar.get());
self.lock().lock();
self.lock().unlock();
self.cvar().notify_one();
}
}
}
impl Drop for Parker {
fn drop(&mut self) {
unsafe {
libc::pthread_cond_destroy(self.cvar.get_mut());
libc::pthread_mutex_destroy(self.lock.get_mut());
}
}
}
unsafe impl Sync for Parker {}
unsafe impl Send for Parker {}

View File

@ -1,12 +0,0 @@
//@ known-bug: #131668
#![feature(generic_associated_types_extended)]
trait B {
type Y<const N: i16>;
}
struct Erase<T: B>(T);
fn make_static() {
Erase::<dyn for<'c> B<&'c ()>>(());
}

View File

@ -0,0 +1,31 @@
//@ check-pass
#![feature(return_type_notation)]
trait Trait {
fn method(&self) -> impl Sized;
}
impl Trait for () {
fn method(&self) -> impl Sized {}
}
struct Struct<T>(T);
// This test used to fail a debug assertion since we weren't resolving the item
// for `T::method(..)` correctly, leading to two bound vars being given the
// index 0. The solution is to look at both generics of `test` and its parent impl.
impl<T> Struct<T>
where
T: Trait,
{
fn test()
where
T::method(..): Send
{}
}
fn main() {
Struct::<()>::test();
}

View File

@ -5,7 +5,6 @@ trait Item {
pub struct Flatten<I> {
inner: <IntoIterator<Item: IntoIterator<Item: >>::IntoIterator as Item>::Core,
//~^ ERROR E0191
//~| ERROR E0223
}
fn main() {}

View File

@ -4,18 +4,6 @@ error[E0191]: the value of the associated types `Item` and `IntoIter` in `IntoIt
LL | inner: <IntoIterator<Item: IntoIterator<Item: >>::IntoIterator as Item>::Core,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: specify the associated types: `IntoIterator<Item: IntoIterator<Item: >, Item = Type, IntoIter = Type>`
error[E0223]: ambiguous associated type
--> $DIR/overlaping-bound-suggestion.rs:6:13
|
LL | inner: <IntoIterator<Item: IntoIterator<Item: >>::IntoIterator as Item>::Core,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: if there were a trait named `Example` with associated type `IntoIterator` implemented for `(dyn IntoIterator + 'static)`, you could use the fully-qualified path
|
LL | inner: <<(dyn IntoIterator + 'static) as Example>::IntoIterator as Item>::Core,
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to 1 previous error
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0191, E0223.
For more information about an error, try `rustc --explain E0191`.
For more information about this error, try `rustc --explain E0191`.

View File

@ -3,9 +3,6 @@
#![feature(async_closure)]
fn foo(x: &dyn async Fn()) {}
//~^ ERROR the trait `AsyncFn` cannot be made into an object
//~| ERROR the trait `AsyncFnMut` cannot be made into an object
//~| ERROR the trait `AsyncFnMut` cannot be made into an object
//~| ERROR the trait `AsyncFnMut` cannot be made into an object
//~^ ERROR the trait `AsyncFnMut` cannot be made into an object
fn main() {}

View File

@ -13,52 +13,6 @@ note: for a trait to be "dyn-compatible" it needs to allow building a vtable to
&mut F
std::boxed::Box<F, A>
error[E0038]: the trait `AsyncFnMut` cannot be made into an object
--> $DIR/dyn-pos.rs:5:16
|
LL | fn foo(x: &dyn async Fn()) {}
| ^^^^^^^^^^ `AsyncFnMut` cannot be made into an object
|
note: for a trait to be "dyn-compatible" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
--> $SRC_DIR/core/src/ops/async_function.rs:LL:COL
|
= note: the trait cannot be made into an object because it contains the generic associated type `CallRefFuture`
= help: the following types implement the trait, consider defining an enum where each variant holds one of these types, implementing `AsyncFnMut` for this new enum and using it instead:
&F
&mut F
std::boxed::Box<F, A>
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
error[E0038]: the trait `AsyncFnMut` cannot be made into an object
--> $DIR/dyn-pos.rs:5:16
|
LL | fn foo(x: &dyn async Fn()) {}
| ^^^^^^^^^^ `AsyncFnMut` cannot be made into an object
|
note: for a trait to be "dyn-compatible" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
--> $SRC_DIR/core/src/ops/async_function.rs:LL:COL
|
= note: the trait cannot be made into an object because it contains the generic associated type `CallRefFuture`
= help: the following types implement the trait, consider defining an enum where each variant holds one of these types, implementing `AsyncFnMut` for this new enum and using it instead:
&F
&mut F
std::boxed::Box<F, A>
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
error[E0038]: the trait `AsyncFn` cannot be made into an object
--> $DIR/dyn-pos.rs:5:12
|
LL | fn foo(x: &dyn async Fn()) {}
| ^^^^^^^^^^^^^^ `AsyncFn` cannot be made into an object
|
note: for a trait to be "dyn-compatible" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
--> $SRC_DIR/core/src/ops/async_function.rs:LL:COL
|
= note: the trait cannot be made into an object because it contains the generic associated type `CallRefFuture`
= help: the following types implement the trait, consider defining an enum where each variant holds one of these types, implementing `AsyncFn` for this new enum and using it instead:
&F
std::boxed::Box<F, A>
error: aborting due to 4 previous errors
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0038`.

View File

@ -3,8 +3,5 @@ trait Foo {
}
fn bar(x: &dyn Foo) {} //~ ERROR the trait `Foo` cannot be made into an object
//~^ ERROR the trait `Foo` cannot be made into an object
//~| ERROR the trait `Foo` cannot be made into an object
//~| ERROR the trait `Foo` cannot be made into an object
fn main() {}

View File

@ -13,53 +13,6 @@ LL | type Bar<T>;
| ^^^ ...because it contains the generic associated type `Bar`
= help: consider moving `Bar` to another trait
error[E0038]: the trait `Foo` cannot be made into an object
--> $DIR/missing-assoc-type.rs:5:16
|
LL | fn bar(x: &dyn Foo) {}
| ^^^ `Foo` cannot be made into an object
|
note: for a trait to be "dyn-compatible" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
--> $DIR/missing-assoc-type.rs:2:10
|
LL | trait Foo {
| --- this trait cannot be made into an object...
LL | type Bar<T>;
| ^^^ ...because it contains the generic associated type `Bar`
= help: consider moving `Bar` to another trait
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
error[E0038]: the trait `Foo` cannot be made into an object
--> $DIR/missing-assoc-type.rs:5:16
|
LL | fn bar(x: &dyn Foo) {}
| ^^^ `Foo` cannot be made into an object
|
note: for a trait to be "dyn-compatible" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
--> $DIR/missing-assoc-type.rs:2:10
|
LL | trait Foo {
| --- this trait cannot be made into an object...
LL | type Bar<T>;
| ^^^ ...because it contains the generic associated type `Bar`
= help: consider moving `Bar` to another trait
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
error[E0038]: the trait `Foo` cannot be made into an object
--> $DIR/missing-assoc-type.rs:5:12
|
LL | fn bar(x: &dyn Foo) {}
| ^^^^^^^ `Foo` cannot be made into an object
|
note: for a trait to be "dyn-compatible" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
--> $DIR/missing-assoc-type.rs:2:10
|
LL | trait Foo {
| --- this trait cannot be made into an object...
LL | type Bar<T>;
| ^^^ ...because it contains the generic associated type `Bar`
= help: consider moving `Bar` to another trait
error: aborting due to 4 previous errors
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0038`.

View File

@ -8,6 +8,5 @@ impl Add for i32 {
fn main() {
let x = &10 as &dyn Add;
//~^ ERROR E0393
//~| ERROR E0191
//~^ ERROR E0191
}

View File

@ -7,22 +7,6 @@ LL | type Output;
LL | let x = &10 as &dyn Add;
| ^^^ help: specify the associated type: `Add<Output = Type>`
error[E0393]: the type parameter `Rhs` must be explicitly specified
--> $DIR/issue-21950.rs:10:25
|
LL | trait Add<Rhs=Self> {
| ------------------- type parameter `Rhs` must be specified for this
...
LL | let x = &10 as &dyn Add;
| ^^^
|
= note: because of the default `Self` reference, type parameters must be specified on object types
help: set the type parameter to the desired type
|
LL | let x = &10 as &dyn Add<Rhs>;
| +++++
error: aborting due to 1 previous error
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0191, E0393.
For more information about an error, try `rustc --explain E0191`.
For more information about this error, try `rustc --explain E0191`.

View File

@ -3,13 +3,11 @@ use std::ops::BitXor;
fn main() {
let x: u8 = BitXor::bitor(0 as u8, 0 as u8);
//~^ ERROR must be specified
//~| no function or associated item named
//~| WARN trait objects without an explicit `dyn` are deprecated
//~| WARN this is accepted in the current edition
let g = BitXor::bitor;
//~^ ERROR must be specified
//~| no function or associated item named
//~| WARN trait objects without an explicit `dyn` are deprecated
//~| WARN this is accepted in the current edition
}

View File

@ -18,17 +18,8 @@ error[E0191]: the value of the associated type `Output` in `BitXor` must be spec
LL | let x: u8 = BitXor::bitor(0 as u8, 0 as u8);
| ^^^^^^ help: specify the associated type: `BitXor::<Output = Type>`
error[E0599]: no function or associated item named `bitor` found for trait object `dyn BitXor<_>` in the current scope
--> $DIR/issue-28344.rs:4:25
|
LL | let x: u8 = BitXor::bitor(0 as u8, 0 as u8);
| ^^^^^ function or associated item not found in `dyn BitXor<_>`
|
help: there is a method `bitxor` with a similar name, but with different arguments
--> $SRC_DIR/core/src/ops/bit.rs:LL:COL
warning: trait objects without an explicit `dyn` are deprecated
--> $DIR/issue-28344.rs:10:13
--> $DIR/issue-28344.rs:9:13
|
LL | let g = BitXor::bitor;
| ^^^^^^
@ -41,21 +32,11 @@ LL | let g = <dyn BitXor>::bitor;
| ++++ +
error[E0191]: the value of the associated type `Output` in `BitXor` must be specified
--> $DIR/issue-28344.rs:10:13
--> $DIR/issue-28344.rs:9:13
|
LL | let g = BitXor::bitor;
| ^^^^^^ help: specify the associated type: `BitXor::<Output = Type>`
error[E0599]: no function or associated item named `bitor` found for trait object `dyn BitXor<_>` in the current scope
--> $DIR/issue-28344.rs:10:21
|
LL | let g = BitXor::bitor;
| ^^^^^ function or associated item not found in `dyn BitXor<_>`
|
help: there is a method `bitxor` with a similar name
--> $SRC_DIR/core/src/ops/bit.rs:LL:COL
error: aborting due to 2 previous errors; 2 warnings emitted
error: aborting due to 4 previous errors; 2 warnings emitted
Some errors have detailed explanations: E0191, E0599.
For more information about an error, try `rustc --explain E0191`.
For more information about this error, try `rustc --explain E0191`.

View File

@ -103,7 +103,7 @@ error: casting `&T` to `&mut T` is undefined behavior, even if the reference is
--> $DIR/reference_casting.rs:45:16
|
LL | let deferred = num as *const i32 as *mut i32;
| ----------------------------- casting happend here
| ----------------------------- casting happened here
LL | let _num = &mut *deferred;
| ^^^^^^^^^^^^^^
|
@ -113,7 +113,7 @@ error: casting `&T` to `&mut T` is undefined behavior, even if the reference is
--> $DIR/reference_casting.rs:48:16
|
LL | let deferred = (std::ptr::from_ref(num) as *const i32 as *const i32).cast_mut() as *mut i32;
| ---------------------------------------------------------------------------- casting happend here
| ---------------------------------------------------------------------------- casting happened here
LL | let _num = &mut *deferred;
| ^^^^^^^^^^^^^^
|
@ -123,7 +123,7 @@ error: casting `&T` to `&mut T` is undefined behavior, even if the reference is
--> $DIR/reference_casting.rs:51:16
|
LL | let deferred = (std::ptr::from_ref(num) as *const i32 as *const i32).cast_mut() as *mut i32;
| ---------------------------------------------------------------------------- casting happend here
| ---------------------------------------------------------------------------- casting happened here
...
LL | let _num = &mut *deferred_rebind;
| ^^^^^^^^^^^^^^^^^^^^^
@ -150,7 +150,7 @@ error: casting `&T` to `&mut T` is undefined behavior, even if the reference is
--> $DIR/reference_casting.rs:62:16
|
LL | let num = NUM as *const i32 as *mut i32;
| ----------------------------- casting happend here
| ----------------------------- casting happened here
...
LL | let _num = &mut *num;
| ^^^^^^^^^
@ -279,7 +279,7 @@ error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
--> $DIR/reference_casting.rs:115:5
|
LL | let value = num as *const i32 as *mut i32;
| ----------------------------- casting happend here
| ----------------------------- casting happened here
LL | *value = 1;
| ^^^^^^^^^^
|
@ -289,7 +289,7 @@ error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
--> $DIR/reference_casting.rs:119:5
|
LL | let value = value as *mut i32;
| ----------------- casting happend here
| ----------------- casting happened here
LL | *value = 1;
| ^^^^^^^^^^
|
@ -299,7 +299,7 @@ error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
--> $DIR/reference_casting.rs:122:5
|
LL | let value = num as *const i32 as *mut i32;
| ----------------------------- casting happend here
| ----------------------------- casting happened here
LL | *value = 1;
| ^^^^^^^^^^
|
@ -309,7 +309,7 @@ error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
--> $DIR/reference_casting.rs:125:5
|
LL | let value = num as *const i32 as *mut i32;
| ----------------------------- casting happend here
| ----------------------------- casting happened here
...
LL | *value_rebind = 1;
| ^^^^^^^^^^^^^^^^^
@ -336,7 +336,7 @@ error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
--> $DIR/reference_casting.rs:131:5
|
LL | let value = num as *const i32 as *mut i32;
| ----------------------------- casting happend here
| ----------------------------- casting happened here
...
LL | std::ptr::write(value, 2);
| ^^^^^^^^^^^^^^^^^^^^^^^^^
@ -347,7 +347,7 @@ error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
--> $DIR/reference_casting.rs:133:5
|
LL | let value = num as *const i32 as *mut i32;
| ----------------------------- casting happend here
| ----------------------------- casting happened here
...
LL | std::ptr::write_unaligned(value, 2);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -358,7 +358,7 @@ error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
--> $DIR/reference_casting.rs:135:5
|
LL | let value = num as *const i32 as *mut i32;
| ----------------------------- casting happend here
| ----------------------------- casting happened here
...
LL | std::ptr::write_volatile(value, 2);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -496,7 +496,7 @@ LL | let w: *mut [u16; 2] = &mut l as *mut [u8; 2] as *mut _;
| --------------------------------
| | |
| | backing allocation comes from here
| casting happend here
| casting happened here
LL | let w: *mut [u16] = unsafe {&mut *w};
| ^^^^^^^
|

View File

@ -1,8 +1,6 @@
// #107983 - testing that `__iterator_get_unchecked` isn't suggested
// HELP included so that compiletest errors on the bad suggestion
pub fn i_can_has_iterator() -> impl Iterator<Item = u32> {
//~^ ERROR expected `Box<dyn Iterator>`
//~| HELP consider constraining the associated type
Box::new(1..=10) as Box<dyn Iterator>
//~^ ERROR the value of the associated type `Item`
//~| HELP specify the associated type

View File

@ -1,24 +1,9 @@
error[E0191]: the value of the associated type `Item` in `Iterator` must be specified
--> $DIR/trait-hidden-method.rs:6:33
--> $DIR/trait-hidden-method.rs:4:33
|
LL | Box::new(1..=10) as Box<dyn Iterator>
| ^^^^^^^^ help: specify the associated type: `Iterator<Item = Type>`
error[E0271]: expected `Box<dyn Iterator>` to be an iterator that yields `u32`, but it yields `<dyn Iterator as Iterator>::Item`
--> $DIR/trait-hidden-method.rs:3:32
|
LL | pub fn i_can_has_iterator() -> impl Iterator<Item = u32> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^ expected `u32`, found associated type
...
LL | Box::new(1..=10) as Box<dyn Iterator>
| ------------------------------------- return type was inferred to be `Box<dyn Iterator>` here
|
= note: expected type `u32`
found associated type `<dyn Iterator as Iterator>::Item`
= help: consider constraining the associated type `<dyn Iterator as Iterator>::Item` to `u32`
= note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
error: aborting due to 1 previous error
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0191, E0271.
For more information about an error, try `rustc --explain E0191`.
For more information about this error, try `rustc --explain E0191`.