Skip to content

Commit 75ff311

Browse files
committed
Auto merge of #70617 - Centril:rollup-063ycso, r=Centril
Rollup of 9 pull requests Successful merges: - #69784 (Optimize strip_prefix and strip_suffix with str patterns) - #70548 (Add long error code for error E0226) - #70555 (resolve, `try_resolve_as_non_binding`: use `delay_span_bug` due to parser recovery) - #70561 (remove obsolete comment) - #70562 (infer array len from pattern) - #70585 (std: Fix over-aligned allocations on wasm32-wasi) - #70587 (Add `Rust` to the code snippet) - #70588 (Fix incorrect documentation for `str::{split_at, split_at_mut}`) - #70613 (more clippy fixes) Failed merges: r? @ghost
2 parents 2113659 + 976f8d5 commit 75ff311

39 files changed

+470
-196
lines changed

.github/ISSUE_TEMPLATE/ice.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ http://blog.pnkfx.org/blog/2019/11/18/rust-bug-minimization-patterns/
1414

1515
### Code
1616

17-
```
17+
```Rust
1818
<code>
1919
```
2020

src/liballoc/string.rs

+15
Original file line numberDiff line numberDiff line change
@@ -1849,6 +1849,21 @@ impl<'a, 'b> Pattern<'a> for &'b String {
18491849
fn is_prefix_of(self, haystack: &'a str) -> bool {
18501850
self[..].is_prefix_of(haystack)
18511851
}
1852+
1853+
#[inline]
1854+
fn strip_prefix_of(self, haystack: &'a str) -> Option<&'a str> {
1855+
self[..].strip_prefix_of(haystack)
1856+
}
1857+
1858+
#[inline]
1859+
fn is_suffix_of(self, haystack: &'a str) -> bool {
1860+
self[..].is_suffix_of(haystack)
1861+
}
1862+
1863+
#[inline]
1864+
fn strip_suffix_of(self, haystack: &'a str) -> Option<&'a str> {
1865+
self[..].strip_suffix_of(haystack)
1866+
}
18521867
}
18531868

18541869
#[stable(feature = "rust1", since = "1.0.0")]

src/libcore/str/mod.rs

+9-32
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#![stable(feature = "rust1", since = "1.0.0")]
1010

1111
use self::pattern::Pattern;
12-
use self::pattern::{DoubleEndedSearcher, ReverseSearcher, SearchStep, Searcher};
12+
use self::pattern::{DoubleEndedSearcher, ReverseSearcher, Searcher};
1313

1414
use crate::char;
1515
use crate::fmt::{self, Write};
@@ -2642,7 +2642,7 @@ impl str {
26422642
/// # Panics
26432643
///
26442644
/// Panics if `mid` is not on a UTF-8 code point boundary, or if it is
2645-
/// beyond the last code point of the string slice.
2645+
/// past the end of the last code point of the string slice.
26462646
///
26472647
/// # Examples
26482648
///
@@ -2683,7 +2683,7 @@ impl str {
26832683
/// # Panics
26842684
///
26852685
/// Panics if `mid` is not on a UTF-8 code point boundary, or if it is
2686-
/// beyond the last code point of the string slice.
2686+
/// past the end of the last code point of the string slice.
26872687
///
26882688
/// # Examples
26892689
///
@@ -3986,26 +3986,15 @@ impl str {
39863986
/// ```
39873987
/// #![feature(str_strip)]
39883988
///
3989-
/// assert_eq!("foobar".strip_prefix("foo"), Some("bar"));
3990-
/// assert_eq!("foobar".strip_prefix("bar"), None);
3989+
/// assert_eq!("foo:bar".strip_prefix("foo:"), Some("bar"));
3990+
/// assert_eq!("foo:bar".strip_prefix("bar"), None);
39913991
/// assert_eq!("foofoo".strip_prefix("foo"), Some("foo"));
39923992
/// ```
39933993
#[must_use = "this returns the remaining substring as a new slice, \
39943994
without modifying the original"]
39953995
#[unstable(feature = "str_strip", reason = "newly added", issue = "67302")]
39963996
pub fn strip_prefix<'a, P: Pattern<'a>>(&'a self, prefix: P) -> Option<&'a str> {
3997-
let mut matcher = prefix.into_searcher(self);
3998-
if let SearchStep::Match(start, len) = matcher.next() {
3999-
debug_assert_eq!(
4000-
start, 0,
4001-
"The first search step from Searcher \
4002-
must include the first character"
4003-
);
4004-
// SAFETY: `Searcher` is known to return valid indices.
4005-
unsafe { Some(self.get_unchecked(len..)) }
4006-
} else {
4007-
None
4008-
}
3997+
prefix.strip_prefix_of(self)
40093998
}
40103999

40114000
/// Returns a string slice with the suffix removed.
@@ -4020,8 +4009,8 @@ impl str {
40204009
///
40214010
/// ```
40224011
/// #![feature(str_strip)]
4023-
/// assert_eq!("barfoo".strip_suffix("foo"), Some("bar"));
4024-
/// assert_eq!("barfoo".strip_suffix("bar"), None);
4012+
/// assert_eq!("bar:foo".strip_suffix(":foo"), Some("bar"));
4013+
/// assert_eq!("bar:foo".strip_suffix("bar"), None);
40254014
/// assert_eq!("foofoo".strip_suffix("foo"), Some("foo"));
40264015
/// ```
40274016
#[must_use = "this returns the remaining substring as a new slice, \
@@ -4032,19 +4021,7 @@ impl str {
40324021
P: Pattern<'a>,
40334022
<P as Pattern<'a>>::Searcher: ReverseSearcher<'a>,
40344023
{
4035-
let mut matcher = suffix.into_searcher(self);
4036-
if let SearchStep::Match(start, end) = matcher.next_back() {
4037-
debug_assert_eq!(
4038-
end,
4039-
self.len(),
4040-
"The first search step from ReverseSearcher \
4041-
must include the last character"
4042-
);
4043-
// SAFETY: `Searcher` is known to return valid indices.
4044-
unsafe { Some(self.get_unchecked(..start)) }
4045-
} else {
4046-
None
4047-
}
4024+
suffix.strip_suffix_of(self)
40484025
}
40494026

40504027
/// Returns a string slice with all suffixes that match a pattern

src/libcore/str/pattern.rs

+85
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,22 @@ pub trait Pattern<'a>: Sized {
4747
matches!(self.into_searcher(haystack).next(), SearchStep::Match(0, _))
4848
}
4949

50+
/// Removes the pattern from the front of haystack, if it matches.
51+
#[inline]
52+
fn strip_prefix_of(self, haystack: &'a str) -> Option<&'a str> {
53+
if let SearchStep::Match(start, len) = self.into_searcher(haystack).next() {
54+
debug_assert_eq!(
55+
start, 0,
56+
"The first search step from Searcher \
57+
must include the first character"
58+
);
59+
// SAFETY: `Searcher` is known to return valid indices.
60+
unsafe { Some(haystack.get_unchecked(len..)) }
61+
} else {
62+
None
63+
}
64+
}
65+
5066
/// Checks whether the pattern matches at the back of the haystack
5167
#[inline]
5268
fn is_suffix_of(self, haystack: &'a str) -> bool
@@ -55,6 +71,26 @@ pub trait Pattern<'a>: Sized {
5571
{
5672
matches!(self.into_searcher(haystack).next_back(), SearchStep::Match(_, j) if haystack.len() == j)
5773
}
74+
75+
/// Removes the pattern from the back of haystack, if it matches.
76+
#[inline]
77+
fn strip_suffix_of(self, haystack: &'a str) -> Option<&'a str>
78+
where
79+
Self::Searcher: ReverseSearcher<'a>,
80+
{
81+
if let SearchStep::Match(start, end) = self.into_searcher(haystack).next_back() {
82+
debug_assert_eq!(
83+
end,
84+
haystack.len(),
85+
"The first search step from ReverseSearcher \
86+
must include the last character"
87+
);
88+
// SAFETY: `Searcher` is known to return valid indices.
89+
unsafe { Some(haystack.get_unchecked(..start)) }
90+
} else {
91+
None
92+
}
93+
}
5894
}
5995

6096
// Searcher
@@ -448,13 +484,26 @@ impl<'a> Pattern<'a> for char {
448484
self.encode_utf8(&mut [0u8; 4]).is_prefix_of(haystack)
449485
}
450486

487+
#[inline]
488+
fn strip_prefix_of(self, haystack: &'a str) -> Option<&'a str> {
489+
self.encode_utf8(&mut [0u8; 4]).strip_prefix_of(haystack)
490+
}
491+
451492
#[inline]
452493
fn is_suffix_of(self, haystack: &'a str) -> bool
453494
where
454495
Self::Searcher: ReverseSearcher<'a>,
455496
{
456497
self.encode_utf8(&mut [0u8; 4]).is_suffix_of(haystack)
457498
}
499+
500+
#[inline]
501+
fn strip_suffix_of(self, haystack: &'a str) -> Option<&'a str>
502+
where
503+
Self::Searcher: ReverseSearcher<'a>,
504+
{
505+
self.encode_utf8(&mut [0u8; 4]).strip_suffix_of(haystack)
506+
}
458507
}
459508

460509
/////////////////////////////////////////////////////////////////////////////
@@ -569,13 +618,26 @@ macro_rules! pattern_methods {
569618
($pmap)(self).is_prefix_of(haystack)
570619
}
571620

621+
#[inline]
622+
fn strip_prefix_of(self, haystack: &'a str) -> Option<&'a str> {
623+
($pmap)(self).strip_prefix_of(haystack)
624+
}
625+
572626
#[inline]
573627
fn is_suffix_of(self, haystack: &'a str) -> bool
574628
where
575629
$t: ReverseSearcher<'a>,
576630
{
577631
($pmap)(self).is_suffix_of(haystack)
578632
}
633+
634+
#[inline]
635+
fn strip_suffix_of(self, haystack: &'a str) -> Option<&'a str>
636+
where
637+
$t: ReverseSearcher<'a>,
638+
{
639+
($pmap)(self).strip_suffix_of(haystack)
640+
}
579641
};
580642
}
581643

@@ -715,11 +777,34 @@ impl<'a, 'b> Pattern<'a> for &'b str {
715777
haystack.as_bytes().starts_with(self.as_bytes())
716778
}
717779

780+
/// Removes the pattern from the front of haystack, if it matches.
781+
#[inline]
782+
fn strip_prefix_of(self, haystack: &'a str) -> Option<&'a str> {
783+
if self.is_prefix_of(haystack) {
784+
// SAFETY: prefix was just verified to exist.
785+
unsafe { Some(haystack.get_unchecked(self.as_bytes().len()..)) }
786+
} else {
787+
None
788+
}
789+
}
790+
718791
/// Checks whether the pattern matches at the back of the haystack
719792
#[inline]
720793
fn is_suffix_of(self, haystack: &'a str) -> bool {
721794
haystack.as_bytes().ends_with(self.as_bytes())
722795
}
796+
797+
/// Removes the pattern from the back of haystack, if it matches.
798+
#[inline]
799+
fn strip_suffix_of(self, haystack: &'a str) -> Option<&'a str> {
800+
if self.is_suffix_of(haystack) {
801+
let i = haystack.len() - self.as_bytes().len();
802+
// SAFETY: suffix was just verified to exist.
803+
unsafe { Some(haystack.get_unchecked(..i)) }
804+
} else {
805+
None
806+
}
807+
}
723808
}
724809

725810
/////////////////////////////////////////////////////////////////////////////

src/librustc_error_codes/error_codes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ E0222: include_str!("./error_codes/E0222.md"),
119119
E0223: include_str!("./error_codes/E0223.md"),
120120
E0224: include_str!("./error_codes/E0224.md"),
121121
E0225: include_str!("./error_codes/E0225.md"),
122+
E0226: include_str!("./error_codes/E0226.md"),
122123
E0229: include_str!("./error_codes/E0229.md"),
123124
E0230: include_str!("./error_codes/E0230.md"),
124125
E0231: include_str!("./error_codes/E0231.md"),
@@ -475,7 +476,6 @@ E0751: include_str!("./error_codes/E0751.md"),
475476
// E0217, // ambiguous associated type, defined in multiple supertraits
476477
// E0218, // no associated type defined
477478
// E0219, // associated type defined in higher-ranked supertrait
478-
E0226, // only a single explicit lifetime bound is permitted
479479
E0227, // ambiguous lifetime bound, explicit lifetime bound required
480480
E0228, // explicit lifetime bound required
481481
// E0233,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
More than one explicit lifetime bound was used on a trait object.
2+
3+
Example of erroneous code:
4+
5+
```compile_fail,E0226
6+
trait Foo {}
7+
8+
type T<'a, 'b> = dyn Foo + 'a + 'b; // error: Trait object `arg` has two
9+
// lifetime bound, 'a and 'b.
10+
```
11+
12+
Here `T` is a trait object with two explicit lifetime bounds, 'a and 'b.
13+
14+
Only a single explicit lifetime bound is permitted on trait objects.
15+
To fix this error, consider removing one of the lifetime bounds:
16+
17+
```
18+
trait Foo {}
19+
20+
type T<'a> = dyn Foo + 'a;
21+
```

src/librustc_error_codes/error_codes/E0730.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ Example of erroneous code:
77
88
fn is_123<const N: usize>(x: [u32; N]) -> bool {
99
match x {
10-
[1, 2, 3] => true, // error: cannot pattern-match on an
11-
// array without a fixed length
10+
[1, 2, ..] => true, // error: cannot pattern-match on an
11+
// array without a fixed length
1212
_ => false
1313
}
1414
}

src/librustc_interface/passes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ impl<'tcx> QueryContext<'tcx> {
711711
}
712712

713713
pub fn print_stats(&mut self) {
714-
self.enter(|tcx| ty::query::print_stats(tcx))
714+
self.enter(ty::query::print_stats)
715715
}
716716
}
717717

src/librustc_mir/interpret/terminator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
3030
trace!("SwitchInt({:?})", *discr);
3131

3232
// Branch to the `otherwise` case by default, if no match is found.
33-
assert!(targets.len() > 0);
33+
assert!(!targets.is_empty());
3434
let mut target_block = targets[targets.len() - 1];
3535

3636
for (index, &const_int) in values.iter().enumerate() {

src/librustc_parse/lexer/mod.rs

-3
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,6 @@ impl<'a> StringReader<'a> {
9393
}
9494

9595
/// Returns the next token, including trivia like whitespace or comments.
96-
///
97-
/// `Err(())` means that some errors were encountered, which can be
98-
/// retrieved using `buffer_fatal_errors`.
9996
pub fn next_token(&mut self) -> Token {
10097
let start_src_index = self.src_index(self.pos);
10198
let text: &str = &self.src[start_src_index..self.end_src_index];

src/librustc_resolve/late.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -1536,20 +1536,18 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
15361536
let is_syntactic_ambiguity = !has_sub && bm == BindingMode::ByValue(Mutability::Not);
15371537

15381538
match res {
1539-
Res::Def(DefKind::Ctor(_, CtorKind::Const), _)
1540-
| Res::Def(DefKind::Const, _)
1541-
| Res::Def(DefKind::ConstParam, _)
1542-
if is_syntactic_ambiguity =>
1543-
{
1539+
Res::SelfCtor(_) // See #70549.
1540+
| Res::Def(
1541+
DefKind::Ctor(_, CtorKind::Const) | DefKind::Const | DefKind::ConstParam,
1542+
_,
1543+
) if is_syntactic_ambiguity => {
15441544
// Disambiguate in favor of a unit struct/variant or constant pattern.
15451545
if let Some(binding) = binding {
15461546
self.r.record_use(ident, ValueNS, binding, false);
15471547
}
15481548
Some(res)
15491549
}
1550-
Res::Def(DefKind::Ctor(..), _)
1551-
| Res::Def(DefKind::Const, _)
1552-
| Res::Def(DefKind::Static, _) => {
1550+
Res::Def(DefKind::Ctor(..) | DefKind::Const | DefKind::Static, _) => {
15531551
// This is unambiguously a fresh binding, either syntactically
15541552
// (e.g., `IDENT @ PAT` or `ref IDENT`) or because `IDENT` resolves
15551553
// to something unusable as a pattern (e.g., constructor function),
@@ -1572,7 +1570,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
15721570
_ => span_bug!(
15731571
ident.span,
15741572
"unexpected resolution for an identifier in pattern: {:?}",
1575-
res
1573+
res,
15761574
),
15771575
}
15781576
}

src/librustc_resolve/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#![feature(bool_to_option)]
1111
#![feature(crate_visibility_modifier)]
1212
#![feature(nll)]
13+
#![feature(or_patterns)]
1314
#![recursion_limit = "256"]
1415

1516
pub use rustc_hir::def::{Namespace, PerNS};

src/librustc_save_analysis/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> {
591591
Some(Data::RefData(Ref {
592592
kind: RefKind::Function,
593593
span,
594-
ref_id: def_id.or(decl_id).map(id_from_def_id).unwrap_or_else(|| null_id()),
594+
ref_id: def_id.or(decl_id).map(id_from_def_id).unwrap_or_else(null_id),
595595
}))
596596
}
597597
ast::ExprKind::Path(_, ref path) => {

src/librustc_session/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1650,7 +1650,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
16501650

16511651
check_thread_count(&debugging_opts, error_format);
16521652

1653-
let incremental = cg.incremental.as_ref().map(|m| PathBuf::from(m));
1653+
let incremental = cg.incremental.as_ref().map(PathBuf::from);
16541654

16551655
if debugging_opts.profile && incremental.is_some() {
16561656
early_error(

0 commit comments

Comments
 (0)