Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions compiler/rustc_hir_typeck/src/writeback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1003,8 +1003,10 @@ impl<'cx, 'tcx> TypeFolder<TyCtxt<'tcx>> for Resolver<'cx, 'tcx> {
}

fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
debug_assert!(!r.is_bound(), "Should not be resolving bound region.");
self.fcx.tcx.lifetimes.re_erased
match r.kind() {
ty::ReBound(..) => r,
_ => self.fcx.tcx.lifetimes.re_erased,
}
}

fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3177,8 +3177,7 @@ define_print! {
write!(p, "` can be evaluated")?;
}
ty::ClauseKind::UnstableFeature(symbol) => {
write!(p, "unstable feature: ")?;
write!(p, "`{symbol}`")?;
write!(p, "feature({symbol}) is enabled")?;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1305,17 +1305,20 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
{
if ty.is_structural_eq_shallow(self.tcx) {
diag.span_suggestion(
span,
"add `#[derive(ConstParamTy)]` to the struct",
span.shrink_to_lo(),
format!("add `#[derive(ConstParamTy)]` to the {}", def.descr()),
"#[derive(ConstParamTy)]\n",
Applicability::MachineApplicable,
);
} else {
// FIXME(adt_const_params): We should check there's not already an
// overlapping `Eq`/`PartialEq` impl.
diag.span_suggestion(
span,
"add `#[derive(ConstParamTy, PartialEq, Eq)]` to the struct",
span.shrink_to_lo(),
format!(
"add `#[derive(ConstParamTy, PartialEq, Eq)]` to the {}",
def.descr()
),
"#[derive(ConstParamTy, PartialEq, Eq)]\n",
Applicability::MachineApplicable,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ where
} else if let Err(guar) = infcx.tcx.check_potentially_region_dependent_goals(root_def_id) {
Err(guar)
} else {
Err(infcx
.dcx()
.delayed_bug(format!("errors selecting obligation during MIR typeck: {errors:?}")))
Err(infcx.dcx().delayed_bug(format!(
"errors selecting obligation during MIR typeck: {name} {root_def_id:?} {errors:?}"
)))
}
})?;

Expand Down
7 changes: 2 additions & 5 deletions library/alloc/src/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -970,17 +970,14 @@ impl Default for Rc<CStr> {
/// This may or may not share an allocation with other Rcs on the same thread.
#[inline]
fn default() -> Self {
let rc = Rc::<[u8]>::from(*b"\0");
// `[u8]` has the same layout as `CStr`, and it is `NUL` terminated.
unsafe { Rc::from_raw(Rc::into_raw(rc) as *const CStr) }
Rc::from(c"")
}
}

#[stable(feature = "default_box_extra", since = "1.17.0")]
impl Default for Box<CStr> {
fn default() -> Box<CStr> {
let boxed: Box<[u8]> = Box::from([0]);
unsafe { Box::from_raw(Box::into_raw(boxed) as *mut CStr) }
Box::from(c"")
}
}

Expand Down
8 changes: 7 additions & 1 deletion library/core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,19 @@ use crate::fmt::{self, Debug, Display, Formatter};
/// assert_eq!(err.to_string(), "invalid digit found in string");
/// ```
///
/// # Error source
///
/// Errors may provide cause information. [`Error::source()`] is generally
/// used when errors cross "abstraction boundaries". If one module must report
/// an error that is caused by an error from a lower-level module, it can allow
/// accessing that error via [`Error::source()`]. This makes it possible for the
/// accessing that error via `Error::source()`. This makes it possible for the
/// high-level module to provide its own errors while also revealing some of the
/// implementation for debugging.
///
/// In error types that wrap an underlying error, the underlying error
/// should be either returned by the outer error's `Error::source()`, or rendered
/// by the outer error's `Display` implementation, but not both.
///
/// # Example
///
/// Implementing the `Error` trait only requires that `Debug` and `Display` are implemented too.
Expand Down
4 changes: 1 addition & 3 deletions library/core/src/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,7 @@ impl fmt::Debug for CStr {
impl Default for &CStr {
#[inline]
fn default() -> Self {
const SLICE: &[c_char] = &[0];
// SAFETY: `SLICE` is indeed pointing to a valid nul-terminated string.
unsafe { CStr::from_ptr(SLICE.as_ptr()) }
c""
}
}

Expand Down
12 changes: 6 additions & 6 deletions library/std/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2107,31 +2107,31 @@ impl PartialEq for PathBuf {
impl cmp::PartialEq<str> for PathBuf {
#[inline]
fn eq(&self, other: &str) -> bool {
Path::eq(self, other)
self.as_path() == other
}
}

#[stable(feature = "eq_str_for_path", since = "CURRENT_RUSTC_VERSION")]
impl cmp::PartialEq<PathBuf> for str {
#[inline]
fn eq(&self, other: &PathBuf) -> bool {
other == self
self == other.as_path()
}
}

#[stable(feature = "eq_str_for_path", since = "CURRENT_RUSTC_VERSION")]
impl cmp::PartialEq<String> for PathBuf {
#[inline]
fn eq(&self, other: &String) -> bool {
**self == **other
self.as_path() == other.as_str()
}
}

#[stable(feature = "eq_str_for_path", since = "CURRENT_RUSTC_VERSION")]
impl cmp::PartialEq<PathBuf> for String {
#[inline]
fn eq(&self, other: &PathBuf) -> bool {
other == self
self.as_str() == other.as_path()
}
}

Expand Down Expand Up @@ -3426,15 +3426,15 @@ impl cmp::PartialEq<Path> for str {
impl cmp::PartialEq<String> for Path {
#[inline]
fn eq(&self, other: &String) -> bool {
self == &*other
self == other.as_str()
}
}

#[stable(feature = "eq_str_for_path", since = "CURRENT_RUSTC_VERSION")]
impl cmp::PartialEq<Path> for String {
#[inline]
fn eq(&self, other: &Path) -> bool {
other == self
self.as_str() == other
}
}

Expand Down
11 changes: 9 additions & 2 deletions library/std/src/sys/pal/hermit/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,22 @@ impl Timespec {
}

fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
fn sub_ge_to_unsigned(a: i64, b: i64) -> u64 {
debug_assert!(a >= b);
a.wrapping_sub(b).cast_unsigned()
}

if self >= other {
// Logic here is identical to Unix version of `Timestamp::sub_timespec`,
// check comments there why operations do not overflow.
Ok(if self.t.tv_nsec >= other.t.tv_nsec {
Duration::new(
(self.t.tv_sec - other.t.tv_sec) as u64,
sub_ge_to_unsigned(self.t.tv_sec, other.t.tv_sec),
(self.t.tv_nsec - other.t.tv_nsec) as u32,
)
} else {
Duration::new(
(self.t.tv_sec - 1 - other.t.tv_sec) as u64,
sub_ge_to_unsigned(self.t.tv_sec - 1, other.t.tv_sec),
(self.t.tv_nsec + NSEC_PER_SEC - other.t.tv_nsec) as u32,
)
})
Expand Down
27 changes: 12 additions & 15 deletions library/std/src/sys/pal/unix/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,28 +134,25 @@ impl Timespec {
}

pub fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
// When a >= b, the difference fits in u64.
fn sub_ge_to_unsigned(a: i64, b: i64) -> u64 {
debug_assert!(a >= b);
a.wrapping_sub(b).cast_unsigned()
}

if self >= other {
// NOTE(eddyb) two aspects of this `if`-`else` are required for LLVM
// to optimize it into a branchless form (see also #75545):
//
// 1. `self.tv_sec - other.tv_sec` shows up as a common expression
// in both branches, i.e. the `else` must have its `- 1`
// subtraction after the common one, not interleaved with it
// (it used to be `self.tv_sec - 1 - other.tv_sec`)
//
// 2. the `Duration::new` call (or any other additional complexity)
// is outside of the `if`-`else`, not duplicated in both branches
//
// Ideally this code could be rearranged such that it more
// directly expresses the lower-cost behavior we want from it.
let (secs, nsec) = if self.tv_nsec.as_inner() >= other.tv_nsec.as_inner() {
(
(self.tv_sec - other.tv_sec) as u64,
sub_ge_to_unsigned(self.tv_sec, other.tv_sec),
self.tv_nsec.as_inner() - other.tv_nsec.as_inner(),
)
} else {
// Following sequence of assertions explain why `self.tv_sec - 1` does not underflow.
debug_assert!(self.tv_nsec < other.tv_nsec);
debug_assert!(self.tv_sec > other.tv_sec);
debug_assert!(self.tv_sec > i64::MIN);
(
(self.tv_sec - other.tv_sec - 1) as u64,
sub_ge_to_unsigned(self.tv_sec - 1, other.tv_sec),
self.tv_nsec.as_inner() + (NSEC_PER_SEC as u32) - other.tv_nsec.as_inner(),
)
};
Expand Down
16 changes: 13 additions & 3 deletions library/std/tests/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2528,7 +2528,17 @@ fn normalize_lexically() {
}

#[test]
/// See issue#146183
fn compare_path_to_str() {
assert!(&PathBuf::from("x") == "x");
/// See issue#146183 and issue#146940
fn compare_path_like_to_str_like() {
let path_buf = PathBuf::from("x");
let path = Path::new("x");
let s = String::from("x");
assert!(path == "x");
assert!("x" == path);
assert!(path == &s);
assert!(&s == path);
assert!(&path_buf == "x");
assert!("x" == &path_buf);
assert!(path_buf == s);
assert!(s == path_buf);
}
16 changes: 16 additions & 0 deletions library/std/tests/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,19 @@ fn big_math() {
check(instant.checked_add(Duration::from_secs(100)), Instant::checked_sub);
check(instant.checked_add(Duration::from_secs(i64::MAX as _)), Instant::checked_sub);
}

#[test]
#[cfg(unix)]
fn system_time_duration_since_max_range_on_unix() {
// Repro regression https://github.com/rust-lang/rust/issues/146228

// Min and max values of `SystemTime` on Unix.
let min = SystemTime::UNIX_EPOCH - (Duration::new(i64::MAX as u64 + 1, 0));
let max = SystemTime::UNIX_EPOCH + (Duration::new(i64::MAX as u64, 999_999_999));

let delta_a = max.duration_since(min).expect("duration_since overflow");
let delta_b = min.duration_since(max).expect_err("duration_since overflow").duration();

assert_eq!(Duration::MAX, delta_a);
assert_eq!(Duration::MAX, delta_b);
}
27 changes: 0 additions & 27 deletions tests/crashes/117808.rs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL |
LL | struct A([u8]);
| ---- this field does not implement `ConstParamTy_`
|
note: the `ConstParamTy_` impl for `[u8]` requires that `unstable feature: `unsized_const_params``
note: the `ConstParamTy_` impl for `[u8]` requires that `feature(unsized_const_params) is enabled`
--> $DIR/unsized_field-1.rs:10:10
|
LL | struct A([u8]);
Expand All @@ -22,7 +22,7 @@ LL |
LL | struct B(&'static [u8]);
| ------------- this field does not implement `ConstParamTy_`
|
note: the `ConstParamTy_` impl for `&'static [u8]` requires that `unstable feature: `unsized_const_params``
note: the `ConstParamTy_` impl for `&'static [u8]` requires that `feature(unsized_const_params) is enabled`
--> $DIR/unsized_field-1.rs:14:10
|
LL | struct B(&'static [u8]);
Expand All @@ -37,7 +37,7 @@ LL |
LL | struct D(unsized_const_param::GenericNotUnsizedParam<&'static [u8]>);
| ---------------------------------------------------------- this field does not implement `ConstParamTy_`
|
note: the `ConstParamTy_` impl for `GenericNotUnsizedParam<&'static [u8]>` requires that `unstable feature: `unsized_const_params``
note: the `ConstParamTy_` impl for `GenericNotUnsizedParam<&'static [u8]>` requires that `feature(unsized_const_params) is enabled`
--> $DIR/unsized_field-1.rs:21:10
|
LL | struct D(unsized_const_param::GenericNotUnsizedParam<&'static [u8]>);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ LL | struct D<const X: C>;
|
help: add `#[derive(ConstParamTy, PartialEq, Eq)]` to the struct
|
LL - struct C;
LL + #[derive(ConstParamTy, PartialEq, Eq)]
LL | struct C;
|

error: aborting due to 1 previous error
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/const-generics/issue-80471.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ error[E0741]: `Nat` must implement `ConstParamTy` to be used as the type of a co
LL | fn foo<const N: Nat>() {}
| ^^^
|
help: add `#[derive(ConstParamTy)]` to the struct
help: add `#[derive(ConstParamTy)]` to the enum
|
LL - enum Nat {
LL + #[derive(ConstParamTy)]
LL | enum Nat {
|

error: aborting due to 1 previous error
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/const-generics/issues/issue-97278.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ error[E0741]: `Bar` must implement `ConstParamTy` to be used as the type of a co
LL | fn test<const BAR: Bar>() {}
| ^^^
|
help: add `#[derive(ConstParamTy)]` to the struct
help: add `#[derive(ConstParamTy)]` to the enum
|
LL - enum Bar {
LL + #[derive(ConstParamTy)]
LL | enum Bar {
|

error: aborting due to 1 previous error
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/consts/refs_check_const_eq-issue-88384.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ LL | struct Foo<const T: CompileTimeSettings>;
|
help: add `#[derive(ConstParamTy)]` to the struct
|
LL - struct CompileTimeSettings {
LL + #[derive(ConstParamTy)]
LL | struct CompileTimeSettings {
|

error[E0741]: `CompileTimeSettings` must implement `ConstParamTy` to be used as the type of a const generic parameter
Expand All @@ -27,8 +27,8 @@ LL | impl<const T: CompileTimeSettings> Foo<T> {
|
help: add `#[derive(ConstParamTy)]` to the struct
|
LL - struct CompileTimeSettings {
LL + #[derive(ConstParamTy)]
LL | struct CompileTimeSettings {
|

error: aborting due to 2 previous errors; 1 warning emitted
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/traits/next-solver/writeback-predicate-bound-region.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//@ edition: 2024
//@ check-pass
//@ compile-flags: -Znext-solver

// This previously ICE'd during writeback when resolving
// the stalled coroutine predicate due to its bound lifetime.

trait Trait<'a> {}
impl<'a, T: Send> Trait<'a> for T {}

fn is_trait<T: for<'a> Trait<'a>>(_: T) {}
fn main() {
is_trait(async {})
}
Loading
Loading