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 May 16, 2024
1 parent 13fdc3d commit 7aef5db
Show file tree
Hide file tree
Showing 19 changed files with 69 additions and 70 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 @@ -278,7 +278,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 @@ -376,7 +376,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 @@ -485,7 +485,7 @@ impl TokenStream {
Delimiter::Invisible(InvisibleOrigin::FlattenToken),
TokenStream::token_alone(token::Lifetime(ident.name), 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 @@ -153,7 +153,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 @@ -787,7 +787,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 @@ -850,7 +850,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 @@ -926,7 +926,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 @@ -1102,7 +1102,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 @@ -258,7 +258,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 @@ -296,7 +296,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 @@ -155,7 +155,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 @@ -364,7 +364,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 @@ -376,7 +376,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
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(std::iter::repeat_with(|| {
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(std::iter::repeat_with(|| {
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
16 changes: 8 additions & 8 deletions compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,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 @@ -347,7 +347,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 @@ -463,7 +463,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 @@ -527,7 +527,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 @@ -552,7 +552,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 @@ -748,7 +748,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 Expand Up @@ -2371,7 +2371,7 @@ impl<'a> Parser<'a> {
// code was interpreted. This helps the user realize when a macro argument of one type is
// later reinterpreted as a different type, like `$x:expr` being reinterpreted as `$x:pat`
// in a subsequent macro invocation (#71039).
let mut tok = self.token.clone();
let mut tok = self.token;
let mut labels = vec![];
while let TokenKind::Interpolated(nt) = &tok.kind {
let tokens = nt.tokens();
Expand All @@ -2381,7 +2381,7 @@ impl<'a> Parser<'a> {
&& let tokens = tokens.0.deref()
&& let [AttrTokenTree::Token(token, _)] = &tokens[..]
{
tok = token.clone();
tok = token;
} else {
break;
}
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 @@ -434,7 +434,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 @@ -521,7 +521,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;
Some(
self.parse_expr_assoc_with(prec + 1, LhsExpr::NotYetParsed)
.map_err(|err| self.maybe_err_dotdotlt_syntax(maybe_lt, err))?,
Expand Down Expand Up @@ -709,7 +709,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 @@ -1664,7 +1664,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 @@ -2110,7 +2110,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 @@ -2453,7 +2453,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 @@ -2484,8 +2484,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;
match self.parse_expr_res(restrictions, None) {
Ok(expr) => expr,
Err(err) => self.recover_closure_body(err, before, prev, token, lo, decl_hi)?,
Expand Down Expand Up @@ -2682,7 +2682,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 {
Expand Down Expand Up @@ -3871,7 +3871,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 @@ -1672,8 +1672,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 @@ -2207,7 +2206,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.dcx().emit_err(errors::NestedAdt {
Expand Down
19 changes: 10 additions & 9 deletions compiler/rustc_parse/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,12 +310,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 @@ -606,7 +606,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 @@ -1474,7 +1474,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 @@ -1657,13 +1657,14 @@ impl<'a> Parser<'a> {
// we don't need N spans, but we want at least one, so print all of prev_token
dbg_fmt.field("prev_token", &parser.prev_token);
// make it easier to peek farther ahead by taking TokenKinds only until EOF
let tokens = (0..*lookahead)
.map(|i| parser.look_ahead(i, |tok| tok.kind.clone()))
.scan(parser.prev_token == TokenKind::Eof, |eof, tok| {
let current = eof.then_some(tok.clone()); // include a trailing EOF token
let tokens = (0..*lookahead).map(|i| parser.look_ahead(i, |tok| tok.kind)).scan(
parser.prev_token == TokenKind::Eof,
|eof, tok| {
let current = eof.then_some(tok); // include a trailing EOF token
*eof |= &tok == &TokenKind::Eof;
current
});
},
);
dbg_fmt.field_with("tokens", |field| field.debug_list().entries(tokens).finish());
dbg_fmt.field("approx_token_stream_pos", &parser.num_bump_calls);

Expand Down
Loading

0 comments on commit 7aef5db

Please sign in to comment.