Skip to content

Commit 658fad6

Browse files
committed
Auto merge of rust-lang#107642 - Dylan-DPC:rollup-edcqhm5, r=Dylan-DPC
Rollup of 6 pull requests Successful merges: - rust-lang#107082 (Autotrait bounds on dyn-safe trait methods) - rust-lang#107427 (Add candidates for DiscriminantKind builtin) - rust-lang#107539 (Emit warnings on unused parens in index expressions) - rust-lang#107544 (Improve `TokenCursor`.) - rust-lang#107585 (Don't cause a cycle when formatting query description that references a FnDef) - rust-lang#107633 (Fix suggestion for coercing Option<&String> to Option<&str>) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 9545094 + c927027 commit 658fad6

33 files changed

+684
-202
lines changed

compiler/rustc_ast/src/tokenstream.rs

+27-15
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ use std::{fmt, iter};
4141
/// Nothing special happens to misnamed or misplaced `SubstNt`s.
4242
#[derive(Debug, Clone, PartialEq, Encodable, Decodable, HashStable_Generic)]
4343
pub enum TokenTree {
44-
/// A single token.
44+
/// A single token. Should never be `OpenDelim` or `CloseDelim`, because
45+
/// delimiters are implicitly represented by `Delimited`.
4546
Token(Token, Spacing),
4647
/// A delimited sequence of token trees.
4748
Delimited(DelimSpan, Delimiter, TokenStream),
@@ -388,12 +389,12 @@ impl TokenStream {
388389
self.0.len()
389390
}
390391

391-
pub fn trees(&self) -> CursorRef<'_> {
392-
CursorRef::new(self)
392+
pub fn trees(&self) -> RefTokenTreeCursor<'_> {
393+
RefTokenTreeCursor::new(self)
393394
}
394395

395-
pub fn into_trees(self) -> Cursor {
396-
Cursor::new(self)
396+
pub fn into_trees(self) -> TokenTreeCursor {
397+
TokenTreeCursor::new(self)
397398
}
398399

399400
/// Compares two `TokenStream`s, checking equality without regarding span information.
@@ -551,24 +552,25 @@ impl TokenStream {
551552
}
552553
}
553554

554-
/// By-reference iterator over a [`TokenStream`].
555+
/// By-reference iterator over a [`TokenStream`], that produces `&TokenTree`
556+
/// items.
555557
#[derive(Clone)]
556-
pub struct CursorRef<'t> {
558+
pub struct RefTokenTreeCursor<'t> {
557559
stream: &'t TokenStream,
558560
index: usize,
559561
}
560562

561-
impl<'t> CursorRef<'t> {
563+
impl<'t> RefTokenTreeCursor<'t> {
562564
fn new(stream: &'t TokenStream) -> Self {
563-
CursorRef { stream, index: 0 }
565+
RefTokenTreeCursor { stream, index: 0 }
564566
}
565567

566568
pub fn look_ahead(&self, n: usize) -> Option<&TokenTree> {
567569
self.stream.0.get(self.index + n)
568570
}
569571
}
570572

571-
impl<'t> Iterator for CursorRef<'t> {
573+
impl<'t> Iterator for RefTokenTreeCursor<'t> {
572574
type Item = &'t TokenTree;
573575

574576
fn next(&mut self) -> Option<&'t TokenTree> {
@@ -579,15 +581,16 @@ impl<'t> Iterator for CursorRef<'t> {
579581
}
580582
}
581583

582-
/// Owning by-value iterator over a [`TokenStream`].
584+
/// Owning by-value iterator over a [`TokenStream`], that produces `TokenTree`
585+
/// items.
583586
// FIXME: Many uses of this can be replaced with by-reference iterator to avoid clones.
584587
#[derive(Clone)]
585-
pub struct Cursor {
588+
pub struct TokenTreeCursor {
586589
pub stream: TokenStream,
587590
index: usize,
588591
}
589592

590-
impl Iterator for Cursor {
593+
impl Iterator for TokenTreeCursor {
591594
type Item = TokenTree;
592595

593596
fn next(&mut self) -> Option<TokenTree> {
@@ -598,9 +601,9 @@ impl Iterator for Cursor {
598601
}
599602
}
600603

601-
impl Cursor {
604+
impl TokenTreeCursor {
602605
fn new(stream: TokenStream) -> Self {
603-
Cursor { stream, index: 0 }
606+
TokenTreeCursor { stream, index: 0 }
604607
}
605608

606609
#[inline]
@@ -614,6 +617,15 @@ impl Cursor {
614617
pub fn look_ahead(&self, n: usize) -> Option<&TokenTree> {
615618
self.stream.0.get(self.index + n)
616619
}
620+
621+
// Replace the previously obtained token tree with `tts`, and rewind to
622+
// just before them.
623+
pub fn replace_prev_and_rewind(&mut self, tts: Vec<TokenTree>) {
624+
assert!(self.index > 0);
625+
self.index -= 1;
626+
let stream = Lrc::make_mut(&mut self.stream.0);
627+
stream.splice(self.index..self.index + 1, tts);
628+
}
617629
}
618630

619631
#[derive(Debug, Copy, Clone, PartialEq, Encodable, Decodable, HashStable_Generic)]

compiler/rustc_data_structures/src/sync.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ cfg_if! {
3131
pub auto trait Send {}
3232
pub auto trait Sync {}
3333

34-
impl<T: ?Sized> Send for T {}
35-
impl<T: ?Sized> Sync for T {}
34+
impl<T> Send for T {}
35+
impl<T> Sync for T {}
3636

3737
#[macro_export]
3838
macro_rules! rustc_erase_owner {

compiler/rustc_error_messages/locales/en-US/hir_typeck.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,5 @@ hir_typeck_lang_start_incorrect_ret_ty = the return type of the `start` lang ite
6161
hir_typeck_help_set_edition_cargo = set `edition = "{$edition}"` in `Cargo.toml`
6262
hir_typeck_help_set_edition_standalone = pass `--edition {$edition}` to `rustc`
6363
hir_typeck_note_edition_guide = for more on editions, read https://doc.rust-lang.org/edition-guide
64+
65+
hir_typeck_convert_to_str = try converting the passed type into a `&str`

compiler/rustc_expand/src/mbe/metavar_expr.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustc_ast::token::{self, Delimiter};
2-
use rustc_ast::tokenstream::{CursorRef, TokenStream, TokenTree};
2+
use rustc_ast::tokenstream::{RefTokenTreeCursor, TokenStream, TokenTree};
33
use rustc_ast::{LitIntType, LitKind};
44
use rustc_ast_pretty::pprust;
55
use rustc_errors::{Applicability, PResult};
@@ -72,7 +72,7 @@ impl MetaVarExpr {
7272

7373
// Checks if there are any remaining tokens. For example, `${ignore(ident ... a b c ...)}`
7474
fn check_trailing_token<'sess>(
75-
iter: &mut CursorRef<'_>,
75+
iter: &mut RefTokenTreeCursor<'_>,
7676
sess: &'sess ParseSess,
7777
) -> PResult<'sess, ()> {
7878
if let Some(tt) = iter.next() {
@@ -88,7 +88,7 @@ fn check_trailing_token<'sess>(
8888

8989
/// Parse a meta-variable `count` expression: `count(ident[, depth])`
9090
fn parse_count<'sess>(
91-
iter: &mut CursorRef<'_>,
91+
iter: &mut RefTokenTreeCursor<'_>,
9292
sess: &'sess ParseSess,
9393
span: Span,
9494
) -> PResult<'sess, MetaVarExpr> {
@@ -99,7 +99,7 @@ fn parse_count<'sess>(
9999

100100
/// Parses the depth used by index(depth) and length(depth).
101101
fn parse_depth<'sess>(
102-
iter: &mut CursorRef<'_>,
102+
iter: &mut RefTokenTreeCursor<'_>,
103103
sess: &'sess ParseSess,
104104
span: Span,
105105
) -> PResult<'sess, usize> {
@@ -126,7 +126,7 @@ fn parse_depth<'sess>(
126126

127127
/// Parses an generic ident
128128
fn parse_ident<'sess>(
129-
iter: &mut CursorRef<'_>,
129+
iter: &mut RefTokenTreeCursor<'_>,
130130
sess: &'sess ParseSess,
131131
span: Span,
132132
) -> PResult<'sess, Ident> {
@@ -152,7 +152,7 @@ fn parse_ident<'sess>(
152152

153153
/// Tries to move the iterator forward returning `true` if there is a comma. If not, then the
154154
/// iterator is not modified and the result is `false`.
155-
fn try_eat_comma(iter: &mut CursorRef<'_>) -> bool {
155+
fn try_eat_comma(iter: &mut RefTokenTreeCursor<'_>) -> bool {
156156
if let Some(TokenTree::Token(token::Token { kind: token::Comma, .. }, _)) = iter.look_ahead(0) {
157157
let _ = iter.next();
158158
return true;

0 commit comments

Comments
 (0)