Skip to content

Commit 6e56900

Browse files
committed
Impl Copy for Token and TokenKind.
1 parent 44a6662 commit 6e56900

File tree

19 files changed

+57
-58
lines changed

19 files changed

+57
-58
lines changed

compiler/rustc_ast/src/token.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ impl From<IdentIsRaw> for bool {
345345
}
346346
}
347347

348-
#[derive(Clone, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
348+
#[derive(Clone, Copy, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
349349
pub enum TokenKind {
350350
/* Expression-operator symbols. */
351351
/// `=`
@@ -443,7 +443,7 @@ pub enum TokenKind {
443443
Eof,
444444
}
445445

446-
#[derive(Clone, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
446+
#[derive(Clone, Copy, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
447447
pub struct Token {
448448
pub kind: TokenKind,
449449
pub span: Span,

compiler/rustc_ast/src/tokenstream.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ impl TokenStream {
465465
Delimiter::Invisible(InvisibleOrigin::FlattenToken),
466466
TokenStream::token_alone(token::Lifetime(ident.name, is_raw), ident.span),
467467
),
468-
_ => TokenTree::Token(token.clone(), spacing),
468+
_ => TokenTree::Token(*token, spacing),
469469
}
470470
}
471471

compiler/rustc_expand/src/mbe/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ impl<'dcx, 'matcher> Tracker<'matcher> for CollectTrackerAndEmitter<'dcx, 'match
161161
.map_or(true, |failure| failure.is_better_position(*approx_position))
162162
{
163163
self.best_failure = Some(BestFailure {
164-
token: token.clone(),
164+
token: *token,
165165
position_in_tokenstream: *approx_position,
166166
msg,
167167
remaining_matcher: self

compiler/rustc_expand/src/mbe/macro_parser.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ pub(super) fn compute_locs(matcher: &[TokenTree]) -> Vec<MatcherLoc> {
180180
for tt in tts {
181181
match tt {
182182
TokenTree::Token(token) => {
183-
locs.push(MatcherLoc::Token { token: token.clone() });
183+
locs.push(MatcherLoc::Token { token: *token });
184184
}
185185
TokenTree::Delimited(span, _, delimited) => {
186186
let open_token = Token::new(token::OpenDelim(delimited.delim), span.open);
@@ -649,7 +649,7 @@ impl TtParser {
649649
// There are no possible next positions AND we aren't waiting for the black-box
650650
// parser: syntax error.
651651
return Failure(T::build_failure(
652-
parser.token.clone(),
652+
parser.token,
653653
parser.approx_token_stream_pos(),
654654
"no rules expected this token in macro call",
655655
));

compiler/rustc_expand/src/mbe/macro_rules.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ impl<'tt> FirstSets<'tt> {
796796
// token could be the separator token itself.
797797

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

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

865865
assert!(first.maybe_empty);
@@ -935,7 +935,7 @@ impl<'tt> Clone for TtHandle<'tt> {
935935
// This variant *must* contain a `mbe::TokenTree::Token`, and not
936936
// any other variant of `mbe::TokenTree`.
937937
TtHandle::Token(mbe::TokenTree::Token(tok)) => {
938-
TtHandle::Token(mbe::TokenTree::Token(tok.clone()))
938+
TtHandle::Token(mbe::TokenTree::Token(*tok))
939939
}
940940

941941
_ => unreachable!(),
@@ -1111,7 +1111,7 @@ fn check_matcher_core<'tt>(
11111111
let mut new;
11121112
let my_suffix = if let Some(sep) = &seq_rep.separator {
11131113
new = suffix_first.clone();
1114-
new.add_one_maybe(TtHandle::from_token(sep.clone()));
1114+
new.add_one_maybe(TtHandle::from_token(*sep));
11151115
&new
11161116
} else {
11171117
&suffix_first

compiler/rustc_expand/src/mbe/quoted.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ fn parse_tree<'a>(
312312
}
313313

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

317317
// `tree` is the beginning of a delimited set of tokens (e.g., `(` or `{`). We need to
318318
// descend into the delimited set and further parse it.
@@ -350,7 +350,7 @@ fn parse_kleene_op<'a>(
350350
match input.next() {
351351
Some(tokenstream::TokenTree::Token(token, _)) => match kleene_op(token) {
352352
Some(op) => Ok(Ok((op, token.span))),
353-
None => Ok(Err(token.clone())),
353+
None => Ok(Err(*token)),
354354
},
355355
tree => Err(tree.map_or(span, tokenstream::TokenTree::span)),
356356
}

compiler/rustc_expand/src/mbe/transcribe.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ pub(super) fn transcribe<'a>(
163163
if repeat_idx < repeat_len {
164164
frame.idx = 0;
165165
if let Some(sep) = sep {
166-
result.push(TokenTree::Token(sep.clone(), Spacing::Alone));
166+
result.push(TokenTree::Token(*sep, Spacing::Alone));
167167
}
168168
continue;
169169
}
@@ -404,7 +404,7 @@ pub(super) fn transcribe<'a>(
404404
// Nothing much to do here. Just push the token to the result, being careful to
405405
// preserve syntax context.
406406
mbe::TokenTree::Token(token) => {
407-
let mut token = token.clone();
407+
let mut token = *token;
408408
mut_visit::visit_token(&mut marker, &mut token);
409409
let tt = TokenTree::Token(token, Spacing::Alone);
410410
result.push(tt);

compiler/rustc_parse/src/lexer/unicode_chars.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ pub(super) fn check_for_substitution(
377377
ascii_name,
378378
})
379379
};
380-
(token.clone(), sugg)
380+
(*token, sugg)
381381
}
382382

383383
/// Extract string if found at current position with given delimiters

compiler/rustc_parse/src/parser/attr_wrapper.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ impl ToAttrTokenStream for LazyAttrTokenStreamImpl {
120120
// produce an empty `TokenStream` if no calls were made, and omit the
121121
// final token otherwise.
122122
let mut cursor_snapshot = self.cursor_snapshot.clone();
123-
let tokens = iter::once(FlatToken::Token(self.start_token.clone()))
123+
let tokens = iter::once(FlatToken::Token(self.start_token))
124124
.chain(iter::repeat_with(|| FlatToken::Token(cursor_snapshot.next())))
125125
.take(self.num_calls as usize);
126126

@@ -173,7 +173,7 @@ impl ToAttrTokenStream for LazyAttrTokenStreamImpl {
173173
impl<'a> Parser<'a> {
174174
pub(super) fn collect_pos(&self) -> CollectPos {
175175
CollectPos {
176-
start_token: (self.token.clone(), self.token_spacing),
176+
start_token: (self.token, self.token_spacing),
177177
cursor_snapshot: self.token_cursor.clone(),
178178
start_pos: self.num_bump_calls,
179179
}

compiler/rustc_parse/src/parser/diagnostics.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ impl<'a> Parser<'a> {
320320
let mut recovered_ident = None;
321321
// we take this here so that the correct original token is retained in
322322
// the diagnostic, regardless of eager recovery.
323-
let bad_token = self.token.clone();
323+
let bad_token = self.token;
324324

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

499-
if **token != parser::TokenType::Token(self.token.kind.clone()) {
499+
if **token != parser::TokenType::Token(self.token.kind) {
500500
let eq = is_ident_eq_keyword(&self.token.kind, &token);
501501
// If the suggestion is a keyword and the found token is an ident,
502502
// the content of which are equal to the suggestion's content,
@@ -560,7 +560,7 @@ impl<'a> Parser<'a> {
560560
// let y = 42;
561561
let guar = self.dcx().emit_err(ExpectedSemi {
562562
span: self.token.span,
563-
token: self.token.clone(),
563+
token: self.token,
564564
unexpected_token_label: None,
565565
sugg: ExpectedSemiSugg::ChangeToSemi(self.token.span),
566566
});
@@ -585,7 +585,7 @@ impl<'a> Parser<'a> {
585585
let span = self.prev_token.span.shrink_to_hi();
586586
let guar = self.dcx().emit_err(ExpectedSemi {
587587
span,
588-
token: self.token.clone(),
588+
token: self.token,
589589
unexpected_token_label: Some(self.token.span),
590590
sugg: ExpectedSemiSugg::AddSemi(span),
591591
});
@@ -827,7 +827,7 @@ impl<'a> Parser<'a> {
827827
let span = self.prev_token.span.shrink_to_hi();
828828
let mut err = self.dcx().create_err(ExpectedSemi {
829829
span,
830-
token: self.token.clone(),
830+
token: self.token,
831831
unexpected_token_label: Some(self.token.span),
832832
sugg: ExpectedSemiSugg::AddSemi(span),
833833
});

compiler/rustc_parse/src/parser/expr.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ impl<'a> Parser<'a> {
381381
fn error_found_expr_would_be_stmt(&self, lhs: &Expr) {
382382
self.dcx().emit_err(errors::FoundExprWouldBeStmt {
383383
span: self.token.span,
384-
token: self.token.clone(),
384+
token: self.token,
385385
suggestion: ExprParenthesesNeeded::surrounding(lhs.span),
386386
});
387387
}
@@ -456,7 +456,7 @@ impl<'a> Parser<'a> {
456456
cur_op_span: Span,
457457
) -> PResult<'a, P<Expr>> {
458458
let rhs = if self.is_at_start_of_range_notation_rhs() {
459-
let maybe_lt = self.token.clone();
459+
let maybe_lt = self.token;
460460
let attrs = self.parse_outer_attributes()?;
461461
Some(
462462
self.parse_expr_assoc_with(prec + 1, attrs)
@@ -650,7 +650,7 @@ impl<'a> Parser<'a> {
650650

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

655655
let sub_diag = if negated_token.is_numeric_lit() {
656656
errors::NotAsNegationOperatorSub::SuggestNotBitwise
@@ -1615,7 +1615,7 @@ impl<'a> Parser<'a> {
16151615
}
16161616

16171617
fn parse_expr_path_start(&mut self) -> PResult<'a, P<Expr>> {
1618-
let maybe_eq_tok = self.prev_token.clone();
1618+
let maybe_eq_tok = self.prev_token;
16191619
let (qself, path) = if self.eat_lt() {
16201620
let lt_span = self.prev_token.span;
16211621
let (qself, path) = self.parse_qpath(PathStyle::Expr).map_err(|mut err| {
@@ -2061,7 +2061,7 @@ impl<'a> Parser<'a> {
20612061
&mut self,
20622062
mk_lit_char: impl FnOnce(Symbol, Span) -> L,
20632063
) -> PResult<'a, L> {
2064-
let token = self.token.clone();
2064+
let token = self.token;
20652065
let err = |self_: &Self| {
20662066
let msg = format!("unexpected token: {}", super::token_descr(&token));
20672067
self_.dcx().struct_span_err(token.span, msg)
@@ -2369,7 +2369,7 @@ impl<'a> Parser<'a> {
23692369
fn parse_expr_closure(&mut self) -> PResult<'a, P<Expr>> {
23702370
let lo = self.token.span;
23712371

2372-
let before = self.prev_token.clone();
2372+
let before = self.prev_token;
23732373
let binder = if self.check_keyword(kw::For) {
23742374
let lo = self.token.span;
23752375
let (lifetime_defs, _) = self.parse_late_bound_lifetime_defs()?;
@@ -2400,8 +2400,8 @@ impl<'a> Parser<'a> {
24002400
FnRetTy::Default(_) => {
24012401
let restrictions =
24022402
self.restrictions - Restrictions::STMT_EXPR - Restrictions::ALLOW_LET;
2403-
let prev = self.prev_token.clone();
2404-
let token = self.token.clone();
2403+
let prev = self.prev_token;
2404+
let token = self.token;
24052405
let attrs = self.parse_outer_attributes()?;
24062406
match self.parse_expr_res(restrictions, attrs) {
24072407
Ok((expr, _)) => expr,
@@ -2600,7 +2600,7 @@ impl<'a> Parser<'a> {
26002600
}
26012601
} else {
26022602
let attrs = self.parse_outer_attributes()?; // For recovery.
2603-
let maybe_fatarrow = self.token.clone();
2603+
let maybe_fatarrow = self.token;
26042604
let block = if self.check(&token::OpenDelim(Delimiter::Brace)) {
26052605
self.parse_block()?
26062606
} else {
@@ -3807,7 +3807,7 @@ impl<'a> Parser<'a> {
38073807
return Err(this.dcx().create_err(errors::ExpectedStructField {
38083808
span: this.look_ahead(1, |t| t.span),
38093809
ident_span: this.token.span,
3810-
token: this.look_ahead(1, |t| t.clone()),
3810+
token: this.look_ahead(1, |t| *t),
38113811
}));
38123812
}
38133813
let (ident, expr) = if is_shorthand {

compiler/rustc_parse/src/parser/item.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1696,8 +1696,7 @@ impl<'a> Parser<'a> {
16961696
self.expect_semi()?;
16971697
body
16981698
} else {
1699-
let err =
1700-
errors::UnexpectedTokenAfterStructName::new(self.token.span, self.token.clone());
1699+
let err = errors::UnexpectedTokenAfterStructName::new(self.token.span, self.token);
17011700
return Err(self.dcx().create_err(err));
17021701
};
17031702

@@ -2227,7 +2226,7 @@ impl<'a> Parser<'a> {
22272226
|| self.token.is_keyword(kw::Union))
22282227
&& self.look_ahead(1, |t| t.is_ident())
22292228
{
2230-
let kw_token = self.token.clone();
2229+
let kw_token = self.token;
22312230
let kw_str = pprust::token_to_string(&kw_token);
22322231
let item = self.parse_item(ForceCollect::No)?;
22332232
let mut item = item.unwrap().span;

compiler/rustc_parse/src/parser/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -290,12 +290,12 @@ impl TokenCursor {
290290
// below can be removed.
291291
if let Some(tree) = self.tree_cursor.next_ref() {
292292
match tree {
293-
&TokenTree::Token(ref token, spacing) => {
293+
&TokenTree::Token(token, spacing) => {
294294
debug_assert!(!matches!(
295295
token.kind,
296296
token::OpenDelim(_) | token::CloseDelim(_)
297297
));
298-
return (token.clone(), spacing);
298+
return (token, spacing);
299299
}
300300
&TokenTree::Delimited(sp, spacing, delim, ref tts) => {
301301
let trees = tts.clone().into_trees();
@@ -587,7 +587,7 @@ impl<'a> Parser<'a> {
587587
fn check(&mut self, tok: &TokenKind) -> bool {
588588
let is_present = self.token == *tok;
589589
if !is_present {
590-
self.expected_tokens.push(TokenType::Token(tok.clone()));
590+
self.expected_tokens.push(TokenType::Token(*tok));
591591
}
592592
is_present
593593
}
@@ -1486,7 +1486,7 @@ impl<'a> Parser<'a> {
14861486
_ => {
14871487
let prev_spacing = self.token_spacing;
14881488
self.bump();
1489-
TokenTree::Token(self.prev_token.clone(), prev_spacing)
1489+
TokenTree::Token(self.prev_token, prev_spacing)
14901490
}
14911491
}
14921492
}
@@ -1666,7 +1666,7 @@ impl<'a> Parser<'a> {
16661666
dbg_fmt.field("prev_token", &parser.prev_token);
16671667
let mut tokens = vec![];
16681668
for i in 0..*lookahead {
1669-
let tok = parser.look_ahead(i, |tok| tok.kind.clone());
1669+
let tok = parser.look_ahead(i, |tok| tok.kind);
16701670
let is_eof = tok == TokenKind::Eof;
16711671
tokens.push(tok);
16721672
if is_eof {

compiler/rustc_parse/src/parser/nonterminal.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ impl<'a> Parser<'a> {
169169
} else {
170170
Err(self.dcx().create_err(UnexpectedNonterminal::Ident {
171171
span: self.token.span,
172-
token: self.token.clone(),
172+
token: self.token,
173173
}))
174174
}
175175
}
@@ -192,7 +192,7 @@ impl<'a> Parser<'a> {
192192
} else {
193193
Err(self.dcx().create_err(UnexpectedNonterminal::Lifetime {
194194
span: self.token.span,
195-
token: self.token.clone(),
195+
token: self.token,
196196
}))
197197
}
198198
}

compiler/rustc_parse/src/parser/pat.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ impl<'a> Parser<'a> {
333333
self.dcx().emit_err(TrailingVertNotAllowed {
334334
span: self.token.span,
335335
start: lo,
336-
token: self.token.clone(),
336+
token: self.token,
337337
note_double_vert: matches!(self.token.kind, token::OrOr),
338338
});
339339
self.bump();
@@ -1256,8 +1256,8 @@ impl<'a> Parser<'a> {
12561256
etc = PatFieldsRest::Rest;
12571257
let mut etc_sp = self.token.span;
12581258
if first_etc_and_maybe_comma_span.is_none() {
1259-
if let Some(comma_tok) = self
1260-
.look_ahead(1, |t| if *t == token::Comma { Some(t.clone()) } else { None })
1259+
if let Some(comma_tok) =
1260+
self.look_ahead(1, |&t| if t == token::Comma { Some(t) } else { None })
12611261
{
12621262
let nw_span = self
12631263
.psess

compiler/rustc_parse/src/parser/path.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,8 @@ impl<'a> Parser<'a> {
376376
} else {
377377
// `(T, U) -> R`
378378

379-
let prev_token_before_parsing = self.prev_token.clone();
380-
let token_before_parsing = self.token.clone();
379+
let prev_token_before_parsing = self.prev_token;
380+
let token_before_parsing = self.token;
381381
let mut snapshot = None;
382382
if self.may_recover()
383383
&& prev_token_before_parsing == token::PathSep

0 commit comments

Comments
 (0)