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 Aug 22, 2023
1 parent 2c24f50 commit 8fc2809
Show file tree
Hide file tree
Showing 21 changed files with 52 additions and 57 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 @@ -259,7 +259,7 @@ fn ident_can_begin_type(name: Symbol, span: Span, is_raw: bool) -> bool {
.contains(&name)
}

#[derive(Clone, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
#[derive(Clone, Copy, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
pub enum TokenKind {
/* Expression-operator symbols. */
Eq,
Expand Down Expand Up @@ -329,7 +329,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 @@ -461,7 +461,7 @@ impl TokenStream {
Delimiter::Invisible(InvisibleSource::FlattenToken),
TokenStream::token_alone(token::Lifetime(name), uninterpolated_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 @@ -147,7 +147,7 @@ impl<'a, 'cx, 'matcher> Tracker<'matcher> for CollectTrackerAndEmitter<'a, 'cx,
.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 @@ -181,7 +181,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 @@ -645,7 +645,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 @@ -807,7 +807,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 @@ -870,7 +870,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 @@ -946,7 +946,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 @@ -1120,7 +1120,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 @@ -257,7 +257,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 @@ -294,7 +294,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 @@ -128,7 +128,7 @@ pub(super) fn transcribe<'a>(
if repeat_idx < repeat_len {
*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 @@ -330,7 +330,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 token, &mut marker);
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
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/parser/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ impl<'a> Parser<'a> {
Err(err) => err.cancel(),
}

Err(InvalidMetaItem { span: self.token.span, token: self.token.clone() }
Err(InvalidMetaItem { span: self.token.span, token: self.token }
.into_diagnostic(&self.sess.span_diagnostic))
}
}
Expand Down
15 changes: 7 additions & 8 deletions compiler/rustc_parse/src/parser/attr_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,12 @@ 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 =
std::iter::once((FlatToken::Token(self.start_token.0.clone()), self.start_token.1))
.chain((0..self.num_calls).map(|_| {
let token = cursor_snapshot.next();
(FlatToken::Token(token.0), token.1)
}))
.take(self.num_calls);
let tokens = std::iter::once((FlatToken::Token(self.start_token.0), self.start_token.1))
.chain((0..self.num_calls).map(|_| {
let token = cursor_snapshot.next();
(FlatToken::Token(token.0), token.1)
}))
.take(self.num_calls);

if !self.replace_ranges.is_empty() {
let mut tokens: Vec<_> = tokens.collect();
Expand Down Expand Up @@ -211,7 +210,7 @@ impl<'a> Parser<'a> {
return Ok(f(self, attrs.attrs)?.0);
}

let start_token = (self.token.clone(), self.token_spacing);
let start_token = (self.token, self.token_spacing);
let cursor_snapshot = self.token_cursor.clone();
let start_pos = self.num_bump_calls;

Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,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, false)) = self.token.ident()
Expand Down Expand Up @@ -362,7 +362,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 @@ -474,7 +474,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 @@ -533,7 +533,7 @@ impl<'a> Parser<'a> {
// let y = 42;
self.sess.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 @@ -558,7 +558,7 @@ impl<'a> Parser<'a> {
let span = self.prev_token.span.shrink_to_hi();
self.sess.emit_err(ExpectedSemi {
span,
token: self.token.clone(),
token: self.token,
unexpected_token_label: Some(self.token.span),
sugg: ExpectedSemiSugg::AddSemi(span),
});
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ impl<'a> Parser<'a> {
fn error_found_expr_would_be_stmt(&self, lhs: &Expr) {
self.sess.emit_err(errors::FoundExprWouldBeStmt {
span: self.token.span,
token: self.token.clone(),
token: self.token,
suggestion: ExprParenthesesNeeded::surrounding(lhs.span),
});
}
Expand Down Expand Up @@ -685,7 +685,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 @@ -1544,7 +1544,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 @@ -1983,7 +1983,7 @@ impl<'a> Parser<'a> {
// err.downgrade_to_delayed_bug();
// return Err(err);
// }
let token = self.token.clone();
let token = self.token;
let err = |self_: &Self| {
let msg = format!("unexpected token: {}", super::token_descr(&token));
self_.struct_span_err(token.span, msg)
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 @@ -1560,8 +1560,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(err.into_diagnostic(&self.sess.span_diagnostic));
};

Expand Down Expand Up @@ -2114,7 +2113,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)?;
self.sess.emit_err(errors::NestedAdt {
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_parse/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,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, delim, ref tts) => {
let trees = tts.clone().into_trees();
Expand Down Expand Up @@ -564,7 +564,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 @@ -1378,7 +1378,7 @@ impl<'a> Parser<'a> {
token::CloseDelim(_) | token::Eof => unreachable!(),
_ => {
self.bump();
TokenTree::Token(self.prev_token.clone(), Spacing::Alone)
TokenTree::Token(self.prev_token, Spacing::Alone)
}
}
}
Expand Down
9 changes: 3 additions & 6 deletions compiler/rustc_parse/src/parser/nonterminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,8 @@ impl<'a> Parser<'a> {
self.bump();
Ok(ParseNtResult::Ident(ident, is_raw))
} else {
Err(UnexpectedNonterminal::Ident {
span: self.token.span,
token: self.token.clone(),
}
.into_diagnostic(&self.sess.span_diagnostic))
Err(UnexpectedNonterminal::Ident { span: self.token.span, token: self.token }
.into_diagnostic(&self.sess.span_diagnostic))
}
}
NonterminalKind::Path => Ok(ParseNtResult::Path(P(
Expand All @@ -193,7 +190,7 @@ impl<'a> Parser<'a> {
} else {
Err(UnexpectedNonterminal::Lifetime {
span: self.token.span,
token: self.token.clone(),
token: self.token,
}
.into_diagnostic(&self.sess.span_diagnostic))
}
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 @@ -315,7 +315,7 @@ impl<'a> Parser<'a> {
self.sess.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).then_some(()),
});
self.bump();
Expand Down Expand Up @@ -1000,8 +1000,8 @@ impl<'a> Parser<'a> {
etc = true;
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
.sess
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ impl<'a> Parser<'a> {
return Ok((AttrVec::new(), block));
}

let maybe_ident = self.prev_token.clone();
let maybe_ident = self.prev_token;
self.maybe_recover_unexpected_block_label();
if !self.eat(&token::OpenDelim(Delimiter::Brace)) {
return self.error_block_no_opening_brace();
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_parse/src/parser/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ impl<'a> Parser<'a> {

// Recovery
mutbl = Mutability::Mut;
let (dyn_tok, dyn_tok_sp) = (self.token.clone(), self.token_spacing);
let (dyn_tok, dyn_tok_sp) = (self.token, self.token_spacing);
self.bump();
self.bump_with(5, (dyn_tok, dyn_tok_sp));
}
Expand Down Expand Up @@ -724,7 +724,7 @@ impl<'a> Parser<'a> {
/// ```
fn parse_generic_bound(&mut self) -> PResult<'a, GenericBound> {
let lo = self.token.span;
let leading_token = self.prev_token.clone();
let leading_token = self.prev_token;
let has_parens = self.eat(&token::OpenDelim(Delimiter::Parenthesis));
let inner_lo = self.token.span;

Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2490,7 +2490,7 @@ fn filter_tokens_from_list(
}
token if should_retain(token) => {
skip_next_comma = false;
tokens.push(token.clone());
tokens.push(token);
}
_ => {
skip_next_comma = true;
Expand Down
2 changes: 1 addition & 1 deletion src/tools/rust-analyzer/crates/ide/src/expand_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<
// struct Bar;
// ```

let derive = sema.descend_into_macros(tok.clone()).into_iter().find_map(|descended| {
let derive = sema.descend_into_macros(tok).into_iter().find_map(|descended| {
let hir_file = sema.hir_file_for(&descended.parent()?);
if !hir_file.is_derive_attr_pseudo_expansion(db) {
return None;
Expand Down
Loading

0 comments on commit 8fc2809

Please sign in to comment.