Skip to content

Rollup of 8 pull requests #138151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
93ef808
Revert vita's c_char back to i8
pheki Feb 7, 2025
67cc82a
Inline VecDeque<u8> and BorrowedCursor methods
thaliaarchi Feb 15, 2025
41bdd2b
Override default Write methods for cursor-like types
thaliaarchi Feb 16, 2025
a8d78fe
Specialize OsString::push for strings
thaliaarchi Feb 28, 2025
83407b8
Specialize constructing OsString from strings
thaliaarchi Feb 28, 2025
b119671
Tweak BufReader::peek() doctest to expose bug in Buffer::read_more()
wgwoods Mar 1, 2025
6d07144
Fix logic error in Buffer::read_more()
wgwoods Mar 1, 2025
1b21952
Also add a MIR pre-codegen test for the derived `PartialOrd::le`
scottmcm Mar 2, 2025
eae5ed6
Make `is_le` and friends work like clang's
scottmcm Mar 2, 2025
ac40ea7
Suggest typo fix for static lifetime
compiler-errors Mar 6, 2025
98dc15f
stabilize const_char_classify
RalfJung Mar 6, 2025
8f8c7fc
stabilize const_sockaddr_setters
RalfJung Mar 6, 2025
2458ccd
Simplify printf and shell format suggestions
thaliaarchi Mar 1, 2025
0b151c6
Rollup merge of #136667 - vita-rust:revert-vita-c-char, r=cuviper
matthiaskrgr Mar 7, 2025
458095a
Rollup merge of #137107 - thaliaarchi:io-optional-methods/cursors, r=…
matthiaskrgr Mar 7, 2025
d986027
Rollup merge of #137777 - thaliaarchi:os_string-push-str, r=joboet
matthiaskrgr Mar 7, 2025
a98df54
Rollup merge of #137832 - wgwoods:fix-bufreader-peek, r=joboet
matthiaskrgr Mar 7, 2025
9e16082
Rollup merge of #137904 - scottmcm:ordering-is, r=workingjubilee
matthiaskrgr Mar 7, 2025
79a8c6d
Rollup merge of #138115 - compiler-errors:static-typo, r=BoxyUwU
matthiaskrgr Mar 7, 2025
a928c15
Rollup merge of #138125 - thaliaarchi:defer-alloc-printf-suggestion, …
matthiaskrgr Mar 7, 2025
c33e9d6
Rollup merge of #138129 - RalfJung:stabilize-const-things, r=tgross35
matthiaskrgr Mar 7, 2025
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
4 changes: 1 addition & 3 deletions compiler/rustc_builtin_macros/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,11 +711,9 @@ fn report_missing_placeholders(
};

let pos = sub.position();
let sub = String::from(sub.as_str());
if explained.contains(&sub) {
if !explained.insert(sub.to_string()) {
continue;
}
explained.insert(sub);

if !found_foreign {
found_foreign = true;
Expand Down
16 changes: 10 additions & 6 deletions compiler/rustc_builtin_macros/src/format_foreign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ pub(crate) mod printf {
Escape((usize, usize)),
}

impl<'a> Substitution<'a> {
pub(crate) fn as_str(&self) -> &str {
impl ToString for Substitution<'_> {
fn to_string(&self) -> String {
match self {
Substitution::Format(fmt) => fmt.span,
Substitution::Escape(_) => "%%",
Substitution::Format(fmt) => fmt.span.into(),
Substitution::Escape(_) => "%%".into(),
}
}
}

impl Substitution<'_> {
pub(crate) fn position(&self) -> InnerSpan {
match self {
Substitution::Format(fmt) => fmt.position,
Expand Down Expand Up @@ -627,15 +629,17 @@ pub(crate) mod shell {
Escape((usize, usize)),
}

impl Substitution<'_> {
pub(crate) fn as_str(&self) -> String {
impl ToString for Substitution<'_> {
fn to_string(&self) -> String {
match self {
Substitution::Ordinal(n, _) => format!("${n}"),
Substitution::Name(n, _) => format!("${n}"),
Substitution::Escape(_) => "$$".into(),
}
}
}

impl Substitution<'_> {
pub(crate) fn position(&self) -> InnerSpan {
let (Self::Ordinal(_, pos) | Self::Name(_, pos) | Self::Escape(pos)) = self;
InnerSpan::new(pos.0, pos.1)
Expand Down
48 changes: 30 additions & 18 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use rustc_hir::def_id::{CRATE_DEF_ID, DefId};
use rustc_hir::{MissingLifetimeKind, PrimTy};
use rustc_middle::ty;
use rustc_session::{Session, lint};
use rustc_span::edit_distance::find_best_match_for_name;
use rustc_span::edit_distance::{edit_distance, find_best_match_for_name};
use rustc_span::edition::Edition;
use rustc_span::hygiene::MacroKind;
use rustc_span::{DUMMY_SP, Ident, Span, Symbol, kw, sym};
Expand Down Expand Up @@ -2919,23 +2919,35 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
)
.with_span_label(lifetime_ref.ident.span, "undeclared lifetime")
};
self.suggest_introducing_lifetime(
&mut err,
Some(lifetime_ref.ident.name.as_str()),
|err, _, span, message, suggestion, span_suggs| {
err.multipart_suggestion_with_style(
message,
std::iter::once((span, suggestion)).chain(span_suggs.clone()).collect(),
Applicability::MaybeIncorrect,
if span_suggs.is_empty() {
SuggestionStyle::ShowCode
} else {
SuggestionStyle::ShowAlways
},
);
true
},
);

// Check if this is a typo of `'static`.
if edit_distance(lifetime_ref.ident.name.as_str(), "'static", 2).is_some() {
err.span_suggestion_verbose(
lifetime_ref.ident.span,
"you may have misspelled the `'static` lifetime",
"'static",
Applicability::MachineApplicable,
);
} else {
self.suggest_introducing_lifetime(
&mut err,
Some(lifetime_ref.ident.name.as_str()),
|err, _, span, message, suggestion, span_suggs| {
err.multipart_suggestion_with_style(
message,
std::iter::once((span, suggestion)).chain(span_suggs.clone()).collect(),
Applicability::MaybeIncorrect,
if span_suggs.is_empty() {
SuggestionStyle::ShowCode
} else {
SuggestionStyle::ShowAlways
},
);
true
},
);
}

err.emit();
}

Expand Down
4 changes: 2 additions & 2 deletions library/core/src/char/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ impl char {
/// '1'.is_digit(1);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_char_classify", issue = "132241")]
#[rustc_const_stable(feature = "const_char_classify", since = "CURRENT_RUSTC_VERSION")]
#[inline]
pub const fn is_digit(self, radix: u32) -> bool {
self.to_digit(radix).is_some()
Expand Down Expand Up @@ -886,7 +886,7 @@ impl char {
/// ```
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_char_classify", issue = "132241")]
#[rustc_const_stable(feature = "const_char_classify", since = "CURRENT_RUSTC_VERSION")]
#[inline]
pub const fn is_whitespace(self) -> bool {
match self {
Expand Down
22 changes: 16 additions & 6 deletions library/core/src/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,12 @@ pub enum Ordering {
}

impl Ordering {
#[inline]
const fn as_raw(self) -> i8 {
// FIXME(const-hack): just use `PartialOrd` against `Equal` once that's const
crate::intrinsics::discriminant_value(&self)
}

/// Returns `true` if the ordering is the `Equal` variant.
///
/// # Examples
Expand All @@ -413,7 +419,11 @@ impl Ordering {
#[rustc_const_stable(feature = "ordering_helpers", since = "1.53.0")]
#[stable(feature = "ordering_helpers", since = "1.53.0")]
pub const fn is_eq(self) -> bool {
matches!(self, Equal)
// All the `is_*` methods are implemented as comparisons against zero
// to follow how clang's libcxx implements their equivalents in
// <https://github.com/llvm/llvm-project/blob/60486292b79885b7800b082754153202bef5b1f0/libcxx/include/__compare/is_eq.h#L23-L28>

self.as_raw() == 0
}

/// Returns `true` if the ordering is not the `Equal` variant.
Expand All @@ -432,7 +442,7 @@ impl Ordering {
#[rustc_const_stable(feature = "ordering_helpers", since = "1.53.0")]
#[stable(feature = "ordering_helpers", since = "1.53.0")]
pub const fn is_ne(self) -> bool {
!matches!(self, Equal)
self.as_raw() != 0
}

/// Returns `true` if the ordering is the `Less` variant.
Expand All @@ -451,7 +461,7 @@ impl Ordering {
#[rustc_const_stable(feature = "ordering_helpers", since = "1.53.0")]
#[stable(feature = "ordering_helpers", since = "1.53.0")]
pub const fn is_lt(self) -> bool {
matches!(self, Less)
self.as_raw() < 0
}

/// Returns `true` if the ordering is the `Greater` variant.
Expand All @@ -470,7 +480,7 @@ impl Ordering {
#[rustc_const_stable(feature = "ordering_helpers", since = "1.53.0")]
#[stable(feature = "ordering_helpers", since = "1.53.0")]
pub const fn is_gt(self) -> bool {
matches!(self, Greater)
self.as_raw() > 0
}

/// Returns `true` if the ordering is either the `Less` or `Equal` variant.
Expand All @@ -489,7 +499,7 @@ impl Ordering {
#[rustc_const_stable(feature = "ordering_helpers", since = "1.53.0")]
#[stable(feature = "ordering_helpers", since = "1.53.0")]
pub const fn is_le(self) -> bool {
!matches!(self, Greater)
self.as_raw() <= 0
}

/// Returns `true` if the ordering is either the `Greater` or `Equal` variant.
Expand All @@ -508,7 +518,7 @@ impl Ordering {
#[rustc_const_stable(feature = "ordering_helpers", since = "1.53.0")]
#[stable(feature = "ordering_helpers", since = "1.53.0")]
pub const fn is_ge(self) -> bool {
!matches!(self, Less)
self.as_raw() >= 0
}

/// Reverses the `Ordering`.
Expand Down
8 changes: 6 additions & 2 deletions library/core/src/ffi/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ mod c_char_definition {
// These are the targets on which c_char is unsigned. Usually the
// signedness is the same for all target_os values on a given architecture
// but there are some exceptions (see isSignedCharDefault() in clang).
//
// aarch64:
// Section 10 "Arm C and C++ language mappings" in Procedure Call Standard for the Arm®
// 64-bit Architecture (AArch64) says C/C++ char is unsigned byte.
Expand Down Expand Up @@ -97,14 +96,19 @@ mod c_char_definition {
// are promoted to int as if from type signed char by default, unless the /J compilation
// option is used."
// https://learn.microsoft.com/en-us/cpp/cpp/fundamental-types-cpp?view=msvc-170#character-types
// Vita:
// Chars are signed by default on the Vita, and VITASDK follows that convention.
// https://github.com/vitasdk/buildscripts/blob/09c533b771591ecde88864b6acad28ffb688dbd4/patches/gcc/0001-gcc-10.patch#L33-L34
//
// L4Re:
// The kernel builds with -funsigned-char on all targets (but useserspace follows the
// The kernel builds with -funsigned-char on all targets (but userspace follows the
// architecture defaults). As we only have a target for userspace apps so there are no
// special cases for L4Re below.
// https://github.com/rust-lang/rust/pull/132975#issuecomment-2484645240
if #[cfg(all(
not(windows),
not(target_vendor = "apple"),
not(target_os = "vita"),
any(
target_arch = "aarch64",
target_arch = "arm",
Expand Down
16 changes: 8 additions & 8 deletions library/core/src/net/socket_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ impl SocketAddr {
/// ```
#[inline]
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
#[rustc_const_unstable(feature = "const_sockaddr_setters", issue = "131714")]
#[rustc_const_stable(feature = "const_sockaddr_setters", since = "CURRENT_RUSTC_VERSION")]
pub const fn set_ip(&mut self, new_ip: IpAddr) {
// `match (*self, new_ip)` would have us mutate a copy of self only to throw it away.
match (self, new_ip) {
Expand Down Expand Up @@ -244,7 +244,7 @@ impl SocketAddr {
/// ```
#[inline]
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
#[rustc_const_unstable(feature = "const_sockaddr_setters", issue = "131714")]
#[rustc_const_stable(feature = "const_sockaddr_setters", since = "CURRENT_RUSTC_VERSION")]
pub const fn set_port(&mut self, new_port: u16) {
match *self {
SocketAddr::V4(ref mut a) => a.set_port(new_port),
Expand Down Expand Up @@ -350,7 +350,7 @@ impl SocketAddrV4 {
/// ```
#[inline]
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
#[rustc_const_unstable(feature = "const_sockaddr_setters", issue = "131714")]
#[rustc_const_stable(feature = "const_sockaddr_setters", since = "CURRENT_RUSTC_VERSION")]
pub const fn set_ip(&mut self, new_ip: Ipv4Addr) {
self.ip = new_ip;
}
Expand Down Expand Up @@ -386,7 +386,7 @@ impl SocketAddrV4 {
/// ```
#[inline]
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
#[rustc_const_unstable(feature = "const_sockaddr_setters", issue = "131714")]
#[rustc_const_stable(feature = "const_sockaddr_setters", since = "CURRENT_RUSTC_VERSION")]
pub const fn set_port(&mut self, new_port: u16) {
self.port = new_port;
}
Expand Down Expand Up @@ -448,7 +448,7 @@ impl SocketAddrV6 {
/// ```
#[inline]
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
#[rustc_const_unstable(feature = "const_sockaddr_setters", issue = "131714")]
#[rustc_const_stable(feature = "const_sockaddr_setters", since = "CURRENT_RUSTC_VERSION")]
pub const fn set_ip(&mut self, new_ip: Ipv6Addr) {
self.ip = new_ip;
}
Expand Down Expand Up @@ -484,7 +484,7 @@ impl SocketAddrV6 {
/// ```
#[inline]
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
#[rustc_const_unstable(feature = "const_sockaddr_setters", issue = "131714")]
#[rustc_const_stable(feature = "const_sockaddr_setters", since = "CURRENT_RUSTC_VERSION")]
pub const fn set_port(&mut self, new_port: u16) {
self.port = new_port;
}
Expand Down Expand Up @@ -532,7 +532,7 @@ impl SocketAddrV6 {
/// ```
#[inline]
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
#[rustc_const_unstable(feature = "const_sockaddr_setters", issue = "131714")]
#[rustc_const_stable(feature = "const_sockaddr_setters", since = "CURRENT_RUSTC_VERSION")]
pub const fn set_flowinfo(&mut self, new_flowinfo: u32) {
self.flowinfo = new_flowinfo;
}
Expand Down Expand Up @@ -575,7 +575,7 @@ impl SocketAddrV6 {
/// ```
#[inline]
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
#[rustc_const_unstable(feature = "const_sockaddr_setters", issue = "131714")]
#[rustc_const_stable(feature = "const_sockaddr_setters", since = "CURRENT_RUSTC_VERSION")]
pub const fn set_scope_id(&mut self, new_scope_id: u32) {
self.scope_id = new_scope_id;
}
Expand Down
50 changes: 48 additions & 2 deletions library/std/src/ffi/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,30 @@ impl OsString {
#[inline]
#[rustc_confusables("append", "put")]
pub fn push<T: AsRef<OsStr>>(&mut self, s: T) {
self.inner.push_slice(&s.as_ref().inner)
trait SpecPushTo {
fn spec_push_to(&self, buf: &mut OsString);
}

impl<T: AsRef<OsStr>> SpecPushTo for T {
#[inline]
default fn spec_push_to(&self, buf: &mut OsString) {
buf.inner.push_slice(&self.as_ref().inner);
}
}

// Use a more efficient implementation when the string is UTF-8.
macro spec_str($T:ty) {
impl SpecPushTo for $T {
#[inline]
fn spec_push_to(&self, buf: &mut OsString) {
buf.inner.push_str(self);
}
}
}
spec_str!(str);
spec_str!(String);

s.spec_push_to(self)
}

/// Creates a new `OsString` with at least the given capacity.
Expand Down Expand Up @@ -587,7 +610,30 @@ impl<T: ?Sized + AsRef<OsStr>> From<&T> for OsString {
/// Copies any value implementing <code>[AsRef]&lt;[OsStr]&gt;</code>
/// into a newly allocated [`OsString`].
fn from(s: &T) -> OsString {
s.as_ref().to_os_string()
trait SpecToOsString {
fn spec_to_os_string(&self) -> OsString;
}

impl<T: AsRef<OsStr>> SpecToOsString for T {
#[inline]
default fn spec_to_os_string(&self) -> OsString {
self.as_ref().to_os_string()
}
}

// Preserve the known-UTF-8 property for strings.
macro spec_str($T:ty) {
impl SpecToOsString for $T {
#[inline]
fn spec_to_os_string(&self) -> OsString {
OsString::from(String::from(self))
}
}
}
spec_str!(str);
spec_str!(String);

s.spec_to_os_string()
}
}

Expand Down
6 changes: 3 additions & 3 deletions library/std/src/io/buffered/bufreader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,16 @@ impl<R: Read + ?Sized> BufReader<R> {
/// #![feature(bufreader_peek)]
/// use std::io::{Read, BufReader};
///
/// let mut bytes = &b"oh, hello"[..];
/// let mut bytes = &b"oh, hello there"[..];
/// let mut rdr = BufReader::with_capacity(6, &mut bytes);
/// assert_eq!(rdr.peek(2).unwrap(), b"oh");
/// let mut buf = [0; 4];
/// rdr.read(&mut buf[..]).unwrap();
/// assert_eq!(&buf, b"oh, ");
/// assert_eq!(rdr.peek(2).unwrap(), b"he");
/// assert_eq!(rdr.peek(5).unwrap(), b"hello");
/// let mut s = String::new();
/// rdr.read_to_string(&mut s).unwrap();
/// assert_eq!(&s, "hello");
/// assert_eq!(&s, "hello there");
/// assert_eq!(rdr.peek(1).unwrap().len(), 0);
/// ```
#[unstable(feature = "bufreader_peek", issue = "128405")]
Expand Down
Loading
Loading