Skip to content

Commit

Permalink
Impl Copy for Token and TokenKind.
Browse files Browse the repository at this point in the history
  • Loading branch information
nnethercote committed Nov 25, 2024
1 parent 0a3c9b5 commit db50c0f
Show file tree
Hide file tree
Showing 19 changed files with 57 additions and 58 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_ast/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ impl From<IdentIsRaw> for bool {
}
}

#[derive(Clone, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
#[derive(Clone, Copy, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
pub enum TokenKind {
/* Expression-operator symbols. */
/// `=`
Expand Down Expand Up @@ -443,7 +443,7 @@ pub enum TokenKind {
Eof,
}

#[derive(Clone, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
#[derive(Clone, Copy, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
pub struct Token {
pub kind: TokenKind,
pub span: Span,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast/src/tokenstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ impl TokenStream {
Delimiter::Invisible(InvisibleOrigin::FlattenToken),
TokenStream::token_alone(token::Lifetime(ident.name, is_raw), ident.span),
),
_ => TokenTree::Token(token.clone(), spacing),
_ => TokenTree::Token(*token, spacing),
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_expand/src/mbe/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ impl<'dcx, 'matcher> Tracker<'matcher> for CollectTrackerAndEmitter<'dcx, 'match
.map_or(true, |failure| failure.is_better_position(*approx_position))
{
self.best_failure = Some(BestFailure {
token: token.clone(),
token: *token,
position_in_tokenstream: *approx_position,
msg,
remaining_matcher: self
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_expand/src/mbe/macro_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ pub(super) fn compute_locs(matcher: &[TokenTree]) -> Vec<MatcherLoc> {
for tt in tts {
match tt {
TokenTree::Token(token) => {
locs.push(MatcherLoc::Token { token: token.clone() });
locs.push(MatcherLoc::Token { token: *token });
}
TokenTree::Delimited(span, _, delimited) => {
let open_token = Token::new(token::OpenDelim(delimited.delim), span.open);
Expand Down Expand Up @@ -649,7 +649,7 @@ impl TtParser {
// There are no possible next positions AND we aren't waiting for the black-box
// parser: syntax error.
return Failure(T::build_failure(
parser.token.clone(),
parser.token,
parser.approx_token_stream_pos(),
"no rules expected this token in macro call",
));
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_expand/src/mbe/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ impl<'tt> FirstSets<'tt> {
// token could be the separator token itself.

if let (Some(sep), true) = (&seq_rep.separator, subfirst.maybe_empty) {
first.add_one_maybe(TtHandle::from_token(sep.clone()));
first.add_one_maybe(TtHandle::from_token(*sep));
}

// Reverse scan: Sequence comes before `first`.
Expand Down Expand Up @@ -841,7 +841,7 @@ impl<'tt> FirstSets<'tt> {
// If the sequence contents can be empty, then the first
// token could be the separator token itself.
if let (Some(sep), true) = (&seq_rep.separator, subfirst.maybe_empty) {
first.add_one_maybe(TtHandle::from_token(sep.clone()));
first.add_one_maybe(TtHandle::from_token(*sep));
}

assert!(first.maybe_empty);
Expand Down Expand Up @@ -917,7 +917,7 @@ impl<'tt> Clone for TtHandle<'tt> {
// This variant *must* contain a `mbe::TokenTree::Token`, and not
// any other variant of `mbe::TokenTree`.
TtHandle::Token(mbe::TokenTree::Token(tok)) => {
TtHandle::Token(mbe::TokenTree::Token(tok.clone()))
TtHandle::Token(mbe::TokenTree::Token(*tok))
}

_ => unreachable!(),
Expand Down Expand Up @@ -1093,7 +1093,7 @@ fn check_matcher_core<'tt>(
let mut new;
let my_suffix = if let Some(sep) = &seq_rep.separator {
new = suffix_first.clone();
new.add_one_maybe(TtHandle::from_token(sep.clone()));
new.add_one_maybe(TtHandle::from_token(*sep));
&new
} else {
&suffix_first
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_expand/src/mbe/quoted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ fn parse_tree<'a>(
}

// `tree` is an arbitrary token. Keep it.
tokenstream::TokenTree::Token(token, _) => TokenTree::Token(token.clone()),
tokenstream::TokenTree::Token(token, _) => TokenTree::Token(*token),

// `tree` is the beginning of a delimited set of tokens (e.g., `(` or `{`). We need to
// descend into the delimited set and further parse it.
Expand Down Expand Up @@ -320,7 +320,7 @@ fn parse_kleene_op<'a>(
match input.next() {
Some(tokenstream::TokenTree::Token(token, _)) => match kleene_op(token) {
Some(op) => Ok(Ok((op, token.span))),
None => Ok(Err(token.clone())),
None => Ok(Err(*token)),
},
tree => Err(tree.map_or(span, tokenstream::TokenTree::span)),
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_expand/src/mbe/transcribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ pub(super) fn transcribe<'a>(
if repeat_idx < repeat_len {
frame.idx = 0;
if let Some(sep) = sep {
result.push(TokenTree::Token(sep.clone(), Spacing::Alone));
result.push(TokenTree::Token(*sep, Spacing::Alone));
}
continue;
}
Expand Down Expand Up @@ -418,7 +418,7 @@ pub(super) fn transcribe<'a>(
// Nothing much to do here. Just push the token to the result, being careful to
// preserve syntax context.
mbe::TokenTree::Token(token) => {
let mut token = token.clone();
let mut token = *token;
mut_visit::visit_token(&mut marker, &mut token);
let tt = TokenTree::Token(token, Spacing::Alone);
result.push(tt);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/lexer/unicode_chars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ pub(super) fn check_for_substitution(
ascii_name,
})
};
(token.clone(), sugg)
(*token, sugg)
}

/// Extract string if found at current position with given delimiters
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_parse/src/parser/attr_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl ToAttrTokenStream for LazyAttrTokenStreamImpl {
// produce an empty `TokenStream` if no calls were made, and omit the
// final token otherwise.
let mut cursor_snapshot = self.cursor_snapshot.clone();
let tokens = iter::once(FlatToken::Token(self.start_token.clone()))
let tokens = iter::once(FlatToken::Token(self.start_token))
.chain(iter::repeat_with(|| FlatToken::Token(cursor_snapshot.next())))
.take(self.num_calls as usize);

Expand Down Expand Up @@ -186,7 +186,7 @@ impl ToAttrTokenStream for LazyAttrTokenStreamImpl {
impl<'a> Parser<'a> {
pub(super) fn collect_pos(&self) -> CollectPos {
CollectPos {
start_token: (self.token.clone(), self.token_spacing),
start_token: (self.token, self.token_spacing),
cursor_snapshot: self.token_cursor.clone(),
start_pos: self.num_bump_calls,
}
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ impl<'a> Parser<'a> {
let mut recovered_ident = None;
// we take this here so that the correct original token is retained in
// the diagnostic, regardless of eager recovery.
let bad_token = self.token.clone();
let bad_token = self.token;

// suggest prepending a keyword in identifier position with `r#`
let suggest_raw = if let Some((ident, IdentIsRaw::No)) = self.token.ident()
Expand Down Expand Up @@ -380,7 +380,7 @@ impl<'a> Parser<'a> {
// if the previous token is a valid keyword
// that might use a generic, then suggest a correct
// generic placement (later on)
let maybe_keyword = self.prev_token.clone();
let maybe_keyword = self.prev_token;
if valid_prev_keywords.into_iter().any(|x| maybe_keyword.is_keyword(x)) {
// if we have a valid keyword, attempt to parse generics
// also obtain the keywords symbol
Expand Down Expand Up @@ -496,7 +496,7 @@ impl<'a> Parser<'a> {
false
}

if **token != parser::TokenType::Token(self.token.kind.clone()) {
if **token != parser::TokenType::Token(self.token.kind) {
let eq = is_ident_eq_keyword(&self.token.kind, &token);
// If the suggestion is a keyword and the found token is an ident,
// the content of which are equal to the suggestion's content,
Expand Down Expand Up @@ -560,7 +560,7 @@ impl<'a> Parser<'a> {
// let y = 42;
let guar = self.dcx().emit_err(ExpectedSemi {
span: self.token.span,
token: self.token.clone(),
token: self.token,
unexpected_token_label: None,
sugg: ExpectedSemiSugg::ChangeToSemi(self.token.span),
});
Expand All @@ -585,7 +585,7 @@ impl<'a> Parser<'a> {
let span = self.prev_token.span.shrink_to_hi();
let guar = self.dcx().emit_err(ExpectedSemi {
span,
token: self.token.clone(),
token: self.token,
unexpected_token_label: Some(self.token.span),
sugg: ExpectedSemiSugg::AddSemi(span),
});
Expand Down Expand Up @@ -830,7 +830,7 @@ impl<'a> Parser<'a> {
let span = self.prev_token.span.shrink_to_hi();
let mut err = self.dcx().create_err(ExpectedSemi {
span,
token: self.token.clone(),
token: self.token,
unexpected_token_label: Some(self.token.span),
sugg: ExpectedSemiSugg::AddSemi(span),
});
Expand Down
20 changes: 10 additions & 10 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ impl<'a> Parser<'a> {
fn error_found_expr_would_be_stmt(&self, lhs: &Expr) {
self.dcx().emit_err(errors::FoundExprWouldBeStmt {
span: self.token.span,
token: self.token.clone(),
token: self.token,
suggestion: ExprParenthesesNeeded::surrounding(lhs.span),
});
}
Expand Down Expand Up @@ -456,7 +456,7 @@ impl<'a> Parser<'a> {
cur_op_span: Span,
) -> PResult<'a, P<Expr>> {
let rhs = if self.is_at_start_of_range_notation_rhs() {
let maybe_lt = self.token.clone();
let maybe_lt = self.token;
let attrs = self.parse_outer_attributes()?;
Some(
self.parse_expr_assoc_with(prec + 1, attrs)
Expand Down Expand Up @@ -650,7 +650,7 @@ impl<'a> Parser<'a> {

/// Recover on `not expr` in favor of `!expr`.
fn recover_not_expr(&mut self, lo: Span) -> PResult<'a, (Span, ExprKind)> {
let negated_token = self.look_ahead(1, |t| t.clone());
let negated_token = self.look_ahead(1, |t| *t);

let sub_diag = if negated_token.is_numeric_lit() {
errors::NotAsNegationOperatorSub::SuggestNotBitwise
Expand Down Expand Up @@ -1610,7 +1610,7 @@ impl<'a> Parser<'a> {
}

fn parse_expr_path_start(&mut self) -> PResult<'a, P<Expr>> {
let maybe_eq_tok = self.prev_token.clone();
let maybe_eq_tok = self.prev_token;
let (qself, path) = if self.eat_lt() {
let lt_span = self.prev_token.span;
let (qself, path) = self.parse_qpath(PathStyle::Expr).map_err(|mut err| {
Expand Down Expand Up @@ -2056,7 +2056,7 @@ impl<'a> Parser<'a> {
&mut self,
mk_lit_char: impl FnOnce(Symbol, Span) -> L,
) -> PResult<'a, L> {
let token = self.token.clone();
let token = self.token;
let err = |self_: &Self| {
let msg = format!("unexpected token: {}", super::token_descr(&token));
self_.dcx().struct_span_err(token.span, msg)
Expand Down Expand Up @@ -2364,7 +2364,7 @@ impl<'a> Parser<'a> {
fn parse_expr_closure(&mut self) -> PResult<'a, P<Expr>> {
let lo = self.token.span;

let before = self.prev_token.clone();
let before = self.prev_token;
let binder = if self.check_keyword(kw::For) {
let lo = self.token.span;
let (lifetime_defs, _) = self.parse_late_bound_lifetime_defs()?;
Expand Down Expand Up @@ -2395,8 +2395,8 @@ impl<'a> Parser<'a> {
FnRetTy::Default(_) => {
let restrictions =
self.restrictions - Restrictions::STMT_EXPR - Restrictions::ALLOW_LET;
let prev = self.prev_token.clone();
let token = self.token.clone();
let prev = self.prev_token;
let token = self.token;
let attrs = self.parse_outer_attributes()?;
match self.parse_expr_res(restrictions, attrs) {
Ok((expr, _)) => expr,
Expand Down Expand Up @@ -2595,7 +2595,7 @@ impl<'a> Parser<'a> {
}
} else {
let attrs = self.parse_outer_attributes()?; // For recovery.
let maybe_fatarrow = self.token.clone();
let maybe_fatarrow = self.token;
let block = if self.check(&token::OpenDelim(Delimiter::Brace)) {
self.parse_block()?
} else if let Some(block) = recover_block_from_condition(self) {
Expand Down Expand Up @@ -3812,7 +3812,7 @@ impl<'a> Parser<'a> {
return Err(this.dcx().create_err(errors::ExpectedStructField {
span: this.look_ahead(1, |t| t.span),
ident_span: this.token.span,
token: this.look_ahead(1, |t| t.clone()),
token: this.look_ahead(1, |t| *t),
}));
}
let (ident, expr) = if is_shorthand {
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1715,8 +1715,7 @@ impl<'a> Parser<'a> {
self.expect_semi()?;
body
} else {
let err =
errors::UnexpectedTokenAfterStructName::new(self.token.span, self.token.clone());
let err = errors::UnexpectedTokenAfterStructName::new(self.token.span, self.token);
return Err(self.dcx().create_err(err));
};

Expand Down Expand Up @@ -2262,7 +2261,7 @@ impl<'a> Parser<'a> {
|| self.token.is_keyword(kw::Union))
&& self.look_ahead(1, |t| t.is_ident())
{
let kw_token = self.token.clone();
let kw_token = self.token;
let kw_str = pprust::token_to_string(&kw_token);
let item = self.parse_item(ForceCollect::No)?;
let mut item = item.unwrap().span;
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_parse/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,12 @@ impl TokenCursor {
// below can be removed.
if let Some(tree) = self.tree_cursor.next_ref() {
match tree {
&TokenTree::Token(ref token, spacing) => {
&TokenTree::Token(token, spacing) => {
debug_assert!(!matches!(
token.kind,
token::OpenDelim(_) | token::CloseDelim(_)
));
return (token.clone(), spacing);
return (token, spacing);
}
&TokenTree::Delimited(sp, spacing, delim, ref tts) => {
let trees = tts.clone().into_trees();
Expand Down Expand Up @@ -589,7 +589,7 @@ impl<'a> Parser<'a> {
fn check(&mut self, tok: &TokenKind) -> bool {
let is_present = self.token == *tok;
if !is_present {
self.expected_tokens.push(TokenType::Token(tok.clone()));
self.expected_tokens.push(TokenType::Token(*tok));
}
is_present
}
Expand Down Expand Up @@ -1486,7 +1486,7 @@ impl<'a> Parser<'a> {
_ => {
let prev_spacing = self.token_spacing;
self.bump();
TokenTree::Token(self.prev_token.clone(), prev_spacing)
TokenTree::Token(self.prev_token, prev_spacing)
}
}
}
Expand Down Expand Up @@ -1679,7 +1679,7 @@ impl<'a> Parser<'a> {
dbg_fmt.field("prev_token", &parser.prev_token);
let mut tokens = vec![];
for i in 0..*lookahead {
let tok = parser.look_ahead(i, |tok| tok.kind.clone());
let tok = parser.look_ahead(i, |tok| tok.kind);
let is_eof = tok == TokenKind::Eof;
tokens.push(tok);
if is_eof {
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_parse/src/parser/nonterminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ impl<'a> Parser<'a> {
} else {
Err(self.dcx().create_err(UnexpectedNonterminal::Ident {
span: self.token.span,
token: self.token.clone(),
token: self.token,
}))
}
}
Expand All @@ -191,7 +191,7 @@ impl<'a> Parser<'a> {
} else {
Err(self.dcx().create_err(UnexpectedNonterminal::Lifetime {
span: self.token.span,
token: self.token.clone(),
token: self.token,
}))
}
}
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_parse/src/parser/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ impl<'a> Parser<'a> {
self.dcx().emit_err(TrailingVertNotAllowed {
span: self.token.span,
start: lo,
token: self.token.clone(),
token: self.token,
note_double_vert: matches!(self.token.kind, token::OrOr),
});
self.bump();
Expand Down Expand Up @@ -1507,8 +1507,8 @@ impl<'a> Parser<'a> {
etc = PatFieldsRest::Rest;
let mut etc_sp = self.token.span;
if first_etc_and_maybe_comma_span.is_none() {
if let Some(comma_tok) = self
.look_ahead(1, |t| if *t == token::Comma { Some(t.clone()) } else { None })
if let Some(comma_tok) =
self.look_ahead(1, |&t| if t == token::Comma { Some(t) } else { None })
{
let nw_span = self
.psess
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_parse/src/parser/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,8 @@ impl<'a> Parser<'a> {
} else {
// `(T, U) -> R`

let prev_token_before_parsing = self.prev_token.clone();
let token_before_parsing = self.token.clone();
let prev_token_before_parsing = self.prev_token;
let token_before_parsing = self.token;
let mut snapshot = None;
if self.may_recover()
&& prev_token_before_parsing == token::PathSep
Expand Down
Loading

0 comments on commit db50c0f

Please sign in to comment.