Skip to content

Commit 59a9b9e

Browse files
committed
Auto merge of rust-lang#138151 - matthiaskrgr:rollup-j0p6ed1, r=matthiaskrgr
Rollup of 8 pull requests Successful merges: - rust-lang#136667 (Revert vita's c_char back to i8) - rust-lang#137107 (Override default `Write` methods for cursor-like types) - rust-lang#137777 (Specialize `OsString::push` and `OsString as From` for UTF-8) - rust-lang#137832 (Fix crash in BufReader::peek()) - rust-lang#137904 (Improve the generic MIR in the default `PartialOrd::le` and friends) - rust-lang#138115 (Suggest typo fix for static lifetime) - rust-lang#138125 (Simplify `printf` and shell format suggestions) - rust-lang#138129 (Stabilize const_char_classify, const_sockaddr_setters) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 91a0e16 + c33e9d6 commit 59a9b9e

File tree

19 files changed

+431
-69
lines changed

19 files changed

+431
-69
lines changed

compiler/rustc_builtin_macros/src/format.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -711,11 +711,9 @@ fn report_missing_placeholders(
711711
};
712712

713713
let pos = sub.position();
714-
let sub = String::from(sub.as_str());
715-
if explained.contains(&sub) {
714+
if !explained.insert(sub.to_string()) {
716715
continue;
717716
}
718-
explained.insert(sub);
719717

720718
if !found_foreign {
721719
found_foreign = true;

compiler/rustc_builtin_macros/src/format_foreign.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ pub(crate) mod printf {
1212
Escape((usize, usize)),
1313
}
1414

15-
impl<'a> Substitution<'a> {
16-
pub(crate) fn as_str(&self) -> &str {
15+
impl ToString for Substitution<'_> {
16+
fn to_string(&self) -> String {
1717
match self {
18-
Substitution::Format(fmt) => fmt.span,
19-
Substitution::Escape(_) => "%%",
18+
Substitution::Format(fmt) => fmt.span.into(),
19+
Substitution::Escape(_) => "%%".into(),
2020
}
2121
}
22+
}
2223

24+
impl Substitution<'_> {
2325
pub(crate) fn position(&self) -> InnerSpan {
2426
match self {
2527
Substitution::Format(fmt) => fmt.position,
@@ -627,15 +629,17 @@ pub(crate) mod shell {
627629
Escape((usize, usize)),
628630
}
629631

630-
impl Substitution<'_> {
631-
pub(crate) fn as_str(&self) -> String {
632+
impl ToString for Substitution<'_> {
633+
fn to_string(&self) -> String {
632634
match self {
633635
Substitution::Ordinal(n, _) => format!("${n}"),
634636
Substitution::Name(n, _) => format!("${n}"),
635637
Substitution::Escape(_) => "$$".into(),
636638
}
637639
}
640+
}
638641

642+
impl Substitution<'_> {
639643
pub(crate) fn position(&self) -> InnerSpan {
640644
let (Self::Ordinal(_, pos) | Self::Name(_, pos) | Self::Escape(pos)) = self;
641645
InnerSpan::new(pos.0, pos.1)

compiler/rustc_resolve/src/late/diagnostics.rs

+30-18
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use rustc_hir::def_id::{CRATE_DEF_ID, DefId};
2424
use rustc_hir::{MissingLifetimeKind, PrimTy};
2525
use rustc_middle::ty;
2626
use rustc_session::{Session, lint};
27-
use rustc_span::edit_distance::find_best_match_for_name;
27+
use rustc_span::edit_distance::{edit_distance, find_best_match_for_name};
2828
use rustc_span::edition::Edition;
2929
use rustc_span::hygiene::MacroKind;
3030
use rustc_span::{DUMMY_SP, Ident, Span, Symbol, kw, sym};
@@ -2919,23 +2919,35 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
29192919
)
29202920
.with_span_label(lifetime_ref.ident.span, "undeclared lifetime")
29212921
};
2922-
self.suggest_introducing_lifetime(
2923-
&mut err,
2924-
Some(lifetime_ref.ident.name.as_str()),
2925-
|err, _, span, message, suggestion, span_suggs| {
2926-
err.multipart_suggestion_with_style(
2927-
message,
2928-
std::iter::once((span, suggestion)).chain(span_suggs.clone()).collect(),
2929-
Applicability::MaybeIncorrect,
2930-
if span_suggs.is_empty() {
2931-
SuggestionStyle::ShowCode
2932-
} else {
2933-
SuggestionStyle::ShowAlways
2934-
},
2935-
);
2936-
true
2937-
},
2938-
);
2922+
2923+
// Check if this is a typo of `'static`.
2924+
if edit_distance(lifetime_ref.ident.name.as_str(), "'static", 2).is_some() {
2925+
err.span_suggestion_verbose(
2926+
lifetime_ref.ident.span,
2927+
"you may have misspelled the `'static` lifetime",
2928+
"'static",
2929+
Applicability::MachineApplicable,
2930+
);
2931+
} else {
2932+
self.suggest_introducing_lifetime(
2933+
&mut err,
2934+
Some(lifetime_ref.ident.name.as_str()),
2935+
|err, _, span, message, suggestion, span_suggs| {
2936+
err.multipart_suggestion_with_style(
2937+
message,
2938+
std::iter::once((span, suggestion)).chain(span_suggs.clone()).collect(),
2939+
Applicability::MaybeIncorrect,
2940+
if span_suggs.is_empty() {
2941+
SuggestionStyle::ShowCode
2942+
} else {
2943+
SuggestionStyle::ShowAlways
2944+
},
2945+
);
2946+
true
2947+
},
2948+
);
2949+
}
2950+
29392951
err.emit();
29402952
}
29412953

library/core/src/char/methods.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ impl char {
337337
/// '1'.is_digit(1);
338338
/// ```
339339
#[stable(feature = "rust1", since = "1.0.0")]
340-
#[rustc_const_unstable(feature = "const_char_classify", issue = "132241")]
340+
#[rustc_const_stable(feature = "const_char_classify", since = "CURRENT_RUSTC_VERSION")]
341341
#[inline]
342342
pub const fn is_digit(self, radix: u32) -> bool {
343343
self.to_digit(radix).is_some()
@@ -886,7 +886,7 @@ impl char {
886886
/// ```
887887
#[must_use]
888888
#[stable(feature = "rust1", since = "1.0.0")]
889-
#[rustc_const_unstable(feature = "const_char_classify", issue = "132241")]
889+
#[rustc_const_stable(feature = "const_char_classify", since = "CURRENT_RUSTC_VERSION")]
890890
#[inline]
891891
pub const fn is_whitespace(self) -> bool {
892892
match self {

library/core/src/cmp.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,12 @@ pub enum Ordering {
397397
}
398398

399399
impl Ordering {
400+
#[inline]
401+
const fn as_raw(self) -> i8 {
402+
// FIXME(const-hack): just use `PartialOrd` against `Equal` once that's const
403+
crate::intrinsics::discriminant_value(&self)
404+
}
405+
400406
/// Returns `true` if the ordering is the `Equal` variant.
401407
///
402408
/// # Examples
@@ -413,7 +419,11 @@ impl Ordering {
413419
#[rustc_const_stable(feature = "ordering_helpers", since = "1.53.0")]
414420
#[stable(feature = "ordering_helpers", since = "1.53.0")]
415421
pub const fn is_eq(self) -> bool {
416-
matches!(self, Equal)
422+
// All the `is_*` methods are implemented as comparisons against zero
423+
// to follow how clang's libcxx implements their equivalents in
424+
// <https://github.com/llvm/llvm-project/blob/60486292b79885b7800b082754153202bef5b1f0/libcxx/include/__compare/is_eq.h#L23-L28>
425+
426+
self.as_raw() == 0
417427
}
418428

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

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

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

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

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

514524
/// Reverses the `Ordering`.

library/core/src/ffi/primitives.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ mod c_char_definition {
3939
// These are the targets on which c_char is unsigned. Usually the
4040
// signedness is the same for all target_os values on a given architecture
4141
// but there are some exceptions (see isSignedCharDefault() in clang).
42-
//
4342
// aarch64:
4443
// Section 10 "Arm C and C++ language mappings" in Procedure Call Standard for the Arm®
4544
// 64-bit Architecture (AArch64) says C/C++ char is unsigned byte.
@@ -97,14 +96,19 @@ mod c_char_definition {
9796
// are promoted to int as if from type signed char by default, unless the /J compilation
9897
// option is used."
9998
// https://learn.microsoft.com/en-us/cpp/cpp/fundamental-types-cpp?view=msvc-170#character-types
99+
// Vita:
100+
// Chars are signed by default on the Vita, and VITASDK follows that convention.
101+
// https://github.com/vitasdk/buildscripts/blob/09c533b771591ecde88864b6acad28ffb688dbd4/patches/gcc/0001-gcc-10.patch#L33-L34
102+
//
100103
// L4Re:
101-
// The kernel builds with -funsigned-char on all targets (but useserspace follows the
104+
// The kernel builds with -funsigned-char on all targets (but userspace follows the
102105
// architecture defaults). As we only have a target for userspace apps so there are no
103106
// special cases for L4Re below.
104107
// https://github.com/rust-lang/rust/pull/132975#issuecomment-2484645240
105108
if #[cfg(all(
106109
not(windows),
107110
not(target_vendor = "apple"),
111+
not(target_os = "vita"),
108112
any(
109113
target_arch = "aarch64",
110114
target_arch = "arm",

library/core/src/net/socket_addr.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ impl SocketAddr {
200200
/// ```
201201
#[inline]
202202
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
203-
#[rustc_const_unstable(feature = "const_sockaddr_setters", issue = "131714")]
203+
#[rustc_const_stable(feature = "const_sockaddr_setters", since = "CURRENT_RUSTC_VERSION")]
204204
pub const fn set_ip(&mut self, new_ip: IpAddr) {
205205
// `match (*self, new_ip)` would have us mutate a copy of self only to throw it away.
206206
match (self, new_ip) {
@@ -244,7 +244,7 @@ impl SocketAddr {
244244
/// ```
245245
#[inline]
246246
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
247-
#[rustc_const_unstable(feature = "const_sockaddr_setters", issue = "131714")]
247+
#[rustc_const_stable(feature = "const_sockaddr_setters", since = "CURRENT_RUSTC_VERSION")]
248248
pub const fn set_port(&mut self, new_port: u16) {
249249
match *self {
250250
SocketAddr::V4(ref mut a) => a.set_port(new_port),
@@ -350,7 +350,7 @@ impl SocketAddrV4 {
350350
/// ```
351351
#[inline]
352352
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
353-
#[rustc_const_unstable(feature = "const_sockaddr_setters", issue = "131714")]
353+
#[rustc_const_stable(feature = "const_sockaddr_setters", since = "CURRENT_RUSTC_VERSION")]
354354
pub const fn set_ip(&mut self, new_ip: Ipv4Addr) {
355355
self.ip = new_ip;
356356
}
@@ -386,7 +386,7 @@ impl SocketAddrV4 {
386386
/// ```
387387
#[inline]
388388
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
389-
#[rustc_const_unstable(feature = "const_sockaddr_setters", issue = "131714")]
389+
#[rustc_const_stable(feature = "const_sockaddr_setters", since = "CURRENT_RUSTC_VERSION")]
390390
pub const fn set_port(&mut self, new_port: u16) {
391391
self.port = new_port;
392392
}
@@ -448,7 +448,7 @@ impl SocketAddrV6 {
448448
/// ```
449449
#[inline]
450450
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
451-
#[rustc_const_unstable(feature = "const_sockaddr_setters", issue = "131714")]
451+
#[rustc_const_stable(feature = "const_sockaddr_setters", since = "CURRENT_RUSTC_VERSION")]
452452
pub const fn set_ip(&mut self, new_ip: Ipv6Addr) {
453453
self.ip = new_ip;
454454
}
@@ -484,7 +484,7 @@ impl SocketAddrV6 {
484484
/// ```
485485
#[inline]
486486
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
487-
#[rustc_const_unstable(feature = "const_sockaddr_setters", issue = "131714")]
487+
#[rustc_const_stable(feature = "const_sockaddr_setters", since = "CURRENT_RUSTC_VERSION")]
488488
pub const fn set_port(&mut self, new_port: u16) {
489489
self.port = new_port;
490490
}
@@ -532,7 +532,7 @@ impl SocketAddrV6 {
532532
/// ```
533533
#[inline]
534534
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
535-
#[rustc_const_unstable(feature = "const_sockaddr_setters", issue = "131714")]
535+
#[rustc_const_stable(feature = "const_sockaddr_setters", since = "CURRENT_RUSTC_VERSION")]
536536
pub const fn set_flowinfo(&mut self, new_flowinfo: u32) {
537537
self.flowinfo = new_flowinfo;
538538
}
@@ -575,7 +575,7 @@ impl SocketAddrV6 {
575575
/// ```
576576
#[inline]
577577
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
578-
#[rustc_const_unstable(feature = "const_sockaddr_setters", issue = "131714")]
578+
#[rustc_const_stable(feature = "const_sockaddr_setters", since = "CURRENT_RUSTC_VERSION")]
579579
pub const fn set_scope_id(&mut self, new_scope_id: u32) {
580580
self.scope_id = new_scope_id;
581581
}

library/std/src/ffi/os_str.rs

+48-2
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,30 @@ impl OsString {
257257
#[inline]
258258
#[rustc_confusables("append", "put")]
259259
pub fn push<T: AsRef<OsStr>>(&mut self, s: T) {
260-
self.inner.push_slice(&s.as_ref().inner)
260+
trait SpecPushTo {
261+
fn spec_push_to(&self, buf: &mut OsString);
262+
}
263+
264+
impl<T: AsRef<OsStr>> SpecPushTo for T {
265+
#[inline]
266+
default fn spec_push_to(&self, buf: &mut OsString) {
267+
buf.inner.push_slice(&self.as_ref().inner);
268+
}
269+
}
270+
271+
// Use a more efficient implementation when the string is UTF-8.
272+
macro spec_str($T:ty) {
273+
impl SpecPushTo for $T {
274+
#[inline]
275+
fn spec_push_to(&self, buf: &mut OsString) {
276+
buf.inner.push_str(self);
277+
}
278+
}
279+
}
280+
spec_str!(str);
281+
spec_str!(String);
282+
283+
s.spec_push_to(self)
261284
}
262285

263286
/// Creates a new `OsString` with at least the given capacity.
@@ -587,7 +610,30 @@ impl<T: ?Sized + AsRef<OsStr>> From<&T> for OsString {
587610
/// Copies any value implementing <code>[AsRef]&lt;[OsStr]&gt;</code>
588611
/// into a newly allocated [`OsString`].
589612
fn from(s: &T) -> OsString {
590-
s.as_ref().to_os_string()
613+
trait SpecToOsString {
614+
fn spec_to_os_string(&self) -> OsString;
615+
}
616+
617+
impl<T: AsRef<OsStr>> SpecToOsString for T {
618+
#[inline]
619+
default fn spec_to_os_string(&self) -> OsString {
620+
self.as_ref().to_os_string()
621+
}
622+
}
623+
624+
// Preserve the known-UTF-8 property for strings.
625+
macro spec_str($T:ty) {
626+
impl SpecToOsString for $T {
627+
#[inline]
628+
fn spec_to_os_string(&self) -> OsString {
629+
OsString::from(String::from(self))
630+
}
631+
}
632+
}
633+
spec_str!(str);
634+
spec_str!(String);
635+
636+
s.spec_to_os_string()
591637
}
592638
}
593639

library/std/src/io/buffered/bufreader.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,16 @@ impl<R: Read + ?Sized> BufReader<R> {
118118
/// #![feature(bufreader_peek)]
119119
/// use std::io::{Read, BufReader};
120120
///
121-
/// let mut bytes = &b"oh, hello"[..];
121+
/// let mut bytes = &b"oh, hello there"[..];
122122
/// let mut rdr = BufReader::with_capacity(6, &mut bytes);
123123
/// assert_eq!(rdr.peek(2).unwrap(), b"oh");
124124
/// let mut buf = [0; 4];
125125
/// rdr.read(&mut buf[..]).unwrap();
126126
/// assert_eq!(&buf, b"oh, ");
127-
/// assert_eq!(rdr.peek(2).unwrap(), b"he");
127+
/// assert_eq!(rdr.peek(5).unwrap(), b"hello");
128128
/// let mut s = String::new();
129129
/// rdr.read_to_string(&mut s).unwrap();
130-
/// assert_eq!(&s, "hello");
130+
/// assert_eq!(&s, "hello there");
131131
/// assert_eq!(rdr.peek(1).unwrap().len(), 0);
132132
/// ```
133133
#[unstable(feature = "bufreader_peek", issue = "128405")]

0 commit comments

Comments
 (0)