diff options
| author | bors <bors@rust-lang.org> | 2023-07-28 12:30:27 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2023-07-28 12:30:27 +0000 |
| commit | b95fd857fe464e7dcb87b159e1f96c30709c236e (patch) | |
| tree | 2cd93826bcc2ec2be31194d0d84ee6f451d10708 | |
| parent | e4c98caffe505630f8531fbb73949b40db918a48 (diff) | |
| parent | ac747a848124c1797140aa6c10b69d00e7ffc226 (diff) | |
Auto merge of #114119 - nnethercote:opt-TokenKind-clone, r=petrochenkov
Optimize `TokenKind::clone`. `TokenKind` would impl `Copy` if it weren't for `TokenKind::Interpolated`. This commit makes `clone` reflect that. r? `@ghost`
| -rw-r--r-- | compiler/rustc_ast/src/token.rs | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/compiler/rustc_ast/src/token.rs b/compiler/rustc_ast/src/token.rs index 303bdd3a307..4c920e84f86 100644 --- a/compiler/rustc_ast/src/token.rs +++ b/compiler/rustc_ast/src/token.rs @@ -226,7 +226,9 @@ fn ident_can_begin_type(name: Symbol, span: Span, is_raw: bool) -> bool { .contains(&name) } -#[derive(Clone, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)] +// SAFETY: due to the `Clone` impl below, all fields of all variants other than +// `Interpolated` must impl `Copy`. +#[derive(PartialEq, Encodable, Decodable, Debug, HashStable_Generic)] pub enum TokenKind { /* Expression-operator symbols. */ Eq, @@ -299,6 +301,19 @@ pub enum TokenKind { Eof, } +impl Clone for TokenKind { + fn clone(&self) -> Self { + // `TokenKind` would impl `Copy` if it weren't for `Interpolated`. So + // for all other variants, this implementation of `clone` is just like + // a copy. This is faster than the `derive(Clone)` version which has a + // separate path for every variant. + match self { + Interpolated(nt) => Interpolated(nt.clone()), + _ => unsafe { std::ptr::read(self) }, + } + } +} + #[derive(Clone, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)] pub struct Token { pub kind: TokenKind, |
