Skip to content

Commit 9709a43

Browse files
committed
Auto merge of #106228 - matthiaskrgr:rollup-jsznhww, r=matthiaskrgr
Rollup of 8 pull requests Successful merges: - #104402 (Move `ReentrantMutex` to `std::sync`) - #104493 (available_parallelism: Gracefully handle zero value cfs_period_us) - #105359 (Make sentinel value configurable in `library/std/src/sys_common/thread_local_key.rs`) - #105497 (Clarify `catch_unwind` docs about panic hooks) - #105570 (Properly calculate best failure in macro matching) - #105702 (Format only modified files) - #105998 (adjust message on non-unwinding panic) - #106161 (Iterator::find: link to Iterator::position in docs for discoverability) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 270c94e + 89ccd70 commit 9709a43

File tree

19 files changed

+213
-31
lines changed

19 files changed

+213
-31
lines changed

compiler/rustc_expand/src/mbe/diagnostics.rs

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub(super) fn failed_to_match_macro<'cx>(
4343
return result;
4444
}
4545

46-
let Some((token, label, remaining_matcher)) = tracker.best_failure else {
46+
let Some(BestFailure { token, msg: label, remaining_matcher, .. }) = tracker.best_failure else {
4747
return DummyResult::any(sp);
4848
};
4949

@@ -95,11 +95,24 @@ struct CollectTrackerAndEmitter<'a, 'cx, 'matcher> {
9595
cx: &'a mut ExtCtxt<'cx>,
9696
remaining_matcher: Option<&'matcher MatcherLoc>,
9797
/// Which arm's failure should we report? (the one furthest along)
98-
best_failure: Option<(Token, &'static str, MatcherLoc)>,
98+
best_failure: Option<BestFailure>,
9999
root_span: Span,
100100
result: Option<Box<dyn MacResult + 'cx>>,
101101
}
102102

103+
struct BestFailure {
104+
token: Token,
105+
position_in_tokenstream: usize,
106+
msg: &'static str,
107+
remaining_matcher: MatcherLoc,
108+
}
109+
110+
impl BestFailure {
111+
fn is_better_position(&self, position: usize) -> bool {
112+
position > self.position_in_tokenstream
113+
}
114+
}
115+
103116
impl<'a, 'cx, 'matcher> Tracker<'matcher> for CollectTrackerAndEmitter<'a, 'cx, 'matcher> {
104117
fn before_match_loc(&mut self, parser: &TtParser, matcher: &'matcher MatcherLoc) {
105118
if self.remaining_matcher.is_none()
@@ -119,18 +132,25 @@ impl<'a, 'cx, 'matcher> Tracker<'matcher> for CollectTrackerAndEmitter<'a, 'cx,
119132
"should not collect detailed info for successful macro match",
120133
);
121134
}
122-
Failure(token, msg) => match self.best_failure {
123-
Some((ref best_token, _, _)) if best_token.span.lo() >= token.span.lo() => {}
124-
_ => {
125-
self.best_failure = Some((
126-
token.clone(),
135+
Failure(token, approx_position, msg) => {
136+
debug!(?token, ?msg, "a new failure of an arm");
137+
138+
if self
139+
.best_failure
140+
.as_ref()
141+
.map_or(true, |failure| failure.is_better_position(*approx_position))
142+
{
143+
self.best_failure = Some(BestFailure {
144+
token: token.clone(),
145+
position_in_tokenstream: *approx_position,
127146
msg,
128-
self.remaining_matcher
147+
remaining_matcher: self
148+
.remaining_matcher
129149
.expect("must have collected matcher already")
130150
.clone(),
131-
))
151+
})
132152
}
133-
},
153+
}
134154
Error(err_sp, msg) => {
135155
let span = err_sp.substitute_dummy(self.root_span);
136156
self.cx.struct_span_err(span, msg).emit();

compiler/rustc_expand/src/mbe/macro_parser.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,8 @@ pub(crate) enum ParseResult<T> {
310310
Success(T),
311311
/// Arm failed to match. If the second parameter is `token::Eof`, it indicates an unexpected
312312
/// end of macro invocation. Otherwise, it indicates that no rules expected the given token.
313-
Failure(Token, &'static str),
313+
/// The usize is the approximate position of the token in the input token stream.
314+
Failure(Token, usize, &'static str),
314315
/// Fatal error (malformed macro?). Abort compilation.
315316
Error(rustc_span::Span, String),
316317
ErrorReported(ErrorGuaranteed),
@@ -455,6 +456,7 @@ impl TtParser {
455456
&mut self,
456457
matcher: &'matcher [MatcherLoc],
457458
token: &Token,
459+
approx_position: usize,
458460
track: &mut T,
459461
) -> Option<NamedParseResult> {
460462
// Matcher positions that would be valid if the macro invocation was over now. Only
@@ -598,6 +600,7 @@ impl TtParser {
598600
token::Eof,
599601
if token.span.is_dummy() { token.span } else { token.span.shrink_to_hi() },
600602
),
603+
approx_position,
601604
"missing tokens in macro arguments",
602605
),
603606
})
@@ -627,7 +630,12 @@ impl TtParser {
627630

628631
// Process `cur_mps` until either we have finished the input or we need to get some
629632
// parsing from the black-box parser done.
630-
let res = self.parse_tt_inner(matcher, &parser.token, track);
633+
let res = self.parse_tt_inner(
634+
matcher,
635+
&parser.token,
636+
parser.approx_token_stream_pos(),
637+
track,
638+
);
631639
if let Some(res) = res {
632640
return res;
633641
}
@@ -642,6 +650,7 @@ impl TtParser {
642650
// parser: syntax error.
643651
return Failure(
644652
parser.token.clone(),
653+
parser.approx_token_stream_pos(),
645654
"no rules expected this token in macro call",
646655
);
647656
}

compiler/rustc_expand/src/mbe/macro_rules.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,8 @@ pub(super) fn try_match_macro<'matcher, T: Tracker<'matcher>>(
326326

327327
return Ok((i, named_matches));
328328
}
329-
Failure(_, _) => {
330-
trace!("Failed to match arm, trying the next one");
329+
Failure(_, reached_position, _) => {
330+
trace!(%reached_position, "Failed to match arm, trying the next one");
331331
// Try the next arm.
332332
}
333333
Error(_, _) => {
@@ -432,7 +432,7 @@ pub fn compile_declarative_macro(
432432
let argument_map =
433433
match tt_parser.parse_tt(&mut Cow::Owned(parser), &argument_gram, &mut NoopTracker) {
434434
Success(m) => m,
435-
Failure(token, msg) => {
435+
Failure(token, _, msg) => {
436436
let s = parse_failure_msg(&token);
437437
let sp = token.span.substitute_dummy(def.span);
438438
let mut err = sess.parse_sess.span_diagnostic.struct_span_err(sp, &s);

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,6 +1499,10 @@ impl<'a> Parser<'a> {
14991499
pub fn clear_expected_tokens(&mut self) {
15001500
self.expected_tokens.clear();
15011501
}
1502+
1503+
pub fn approx_token_stream_pos(&self) -> usize {
1504+
self.token_cursor.num_next_calls
1505+
}
15021506
}
15031507

15041508
pub(crate) fn make_unclosed_delims_error(

library/core/src/iter/traits/iterator.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2654,7 +2654,10 @@ pub trait Iterator {
26542654
/// argument is a double reference. You can see this effect in the
26552655
/// examples below, with `&&x`.
26562656
///
2657+
/// If you need the index of the element, see [`position()`].
2658+
///
26572659
/// [`Some(element)`]: Some
2660+
/// [`position()`]: Iterator::position
26582661
///
26592662
/// # Examples
26602663
///

library/std/src/io/stdio.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ use crate::fmt;
1010
use crate::fs::File;
1111
use crate::io::{self, BufReader, IoSlice, IoSliceMut, LineWriter, Lines};
1212
use crate::sync::atomic::{AtomicBool, Ordering};
13-
use crate::sync::{Arc, Mutex, MutexGuard, OnceLock};
13+
use crate::sync::{Arc, Mutex, MutexGuard, OnceLock, ReentrantMutex, ReentrantMutexGuard};
1414
use crate::sys::stdio;
15-
use crate::sys_common::remutex::{ReentrantMutex, ReentrantMutexGuard};
1615

1716
type LocalStream = Arc<Mutex<Vec<u8>>>;
1817

library/std/src/panic.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ where
114114
/// aborting the process as well. This function *only* catches unwinding panics,
115115
/// not those that abort the process.
116116
///
117+
/// Note that if a custom panic hook has been set, it will be invoked before
118+
/// the panic is caught, before unwinding.
119+
///
117120
/// Also note that unwinding into Rust code with a foreign exception (e.g.
118121
/// an exception thrown from C++ code) is undefined behavior.
119122
///

library/std/src/panicking.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,11 @@ fn rust_panic_with_hook(
699699
// have limited options. Currently our preference is to
700700
// just abort. In the future we may consider resuming
701701
// unwinding or otherwise exiting the thread cleanly.
702-
rtprintpanic!("thread panicked while panicking. aborting.\n");
702+
if !can_unwind {
703+
rtprintpanic!("thread caused non-unwinding panic. aborting.\n");
704+
} else {
705+
rtprintpanic!("thread panicked while panicking. aborting.\n");
706+
}
703707
crate::sys::abort_internal();
704708
}
705709

library/std/src/sync/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ pub use self::lazy_lock::LazyLock;
177177
#[unstable(feature = "once_cell", issue = "74465")]
178178
pub use self::once_lock::OnceLock;
179179

180+
pub(crate) use self::remutex::{ReentrantMutex, ReentrantMutexGuard};
181+
180182
pub mod mpsc;
181183

182184
mod barrier;
@@ -187,4 +189,5 @@ mod mutex;
187189
mod once;
188190
mod once_lock;
189191
mod poison;
192+
mod remutex;
190193
mod rwlock;

0 commit comments

Comments
 (0)