From 05c8888d277f49d0a1534c651e1a05ef4fb34743 Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Sun, 13 Apr 2025 19:02:19 +0800 Subject: [PATCH] Actually ensure that TokenKind's fields are safely copyable .. by making it generic so that we can use an `impl Copy` to ensure it on the type level. --- compiler/rustc_ast/src/token.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_ast/src/token.rs b/compiler/rustc_ast/src/token.rs index d57a369eebf2e..f09b7d60e782a 100644 --- a/compiler/rustc_ast/src/token.rs +++ b/compiler/rustc_ast/src/token.rs @@ -337,10 +337,8 @@ impl From for bool { } } -// 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 { +pub enum TokenKind> { /* Expression-operator symbols. */ /// `=` Eq, @@ -481,7 +479,7 @@ pub enum TokenKind { /// The span in the surrounding `Token` is that of the metavariable in the /// macro's RHS. The span within the Nonterminal is that of the fragment /// passed to the macro at the call site. - Interpolated(Arc), + Interpolated(Nt), /// A doc comment token. /// `Symbol` is the doc comment's data excluding its "quotes" (`///`, `/**`, etc) @@ -492,6 +490,15 @@ pub enum TokenKind { Eof, } +// make sure that everything else in `TokenKind` is actually `Copy`-able, for +// our unsafe use below. +impl Clone for TokenKind<()> { + fn clone(&self) -> Self { + *self + } +} +impl Copy for TokenKind<()> {} + impl Clone for TokenKind { fn clone(&self) -> Self { // `TokenKind` would impl `Copy` if it weren't for `Interpolated`. So