Skip to content

Commit 81cc29a

Browse files
authored
Rollup merge of #145354 - cache-proc-derive-macros, r=petrochenkov
Cache derive proc macro expansion with incremental query This is a revival of #129102, originally implemented by @futile. Since it looks like they are not active currently, I'd like to push this work forward. The first commit is squashed and rebased work from the original PR, with author attribution to futile. The rest of the commits are some additional comments that I created mostly for myself to understand what happens here. I also did some cleanups based on Vadim's review comments on the original PR, plus I refactored the TLS access a bit using `scoped_tls`. The biggest issue, as usually, are tests... I tried using `#[rustc_clean(..., loaded_from_disk = "derive_macro_expansion")]`, but the problem is that since this query cannot recover the original key from its hash, and thus its fingerprintstyle is `FingerprintStyle::Opaque`, [this](https://github.com/rust-lang/rust/blob/2fef0a30ae6b2687dfb286cb544d2a542f7e2335/compiler/rustc_incremental/src/persist/dirty_clean.rs#L388) crashes when I try to use `loaded_from_disk`. Any suggestions from someone who actually understands the query system would be welcome 😅 TODO: document the new unstable flag On a no-op change re-check of `octocrab 0.49` (which has a ton of `serde` derive proc macro invocations), this saves ~0.6s out of ~6s (so a ~10% win) on my PC. r? @petrochenkov
2 parents d2015e2 + 8fa2f69 commit 81cc29a

File tree

21 files changed

+284
-72
lines changed

21 files changed

+284
-72
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3887,11 +3887,13 @@ dependencies = [
38873887
"rustc_lexer",
38883888
"rustc_lint_defs",
38893889
"rustc_macros",
3890+
"rustc_middle",
38903891
"rustc_parse",
38913892
"rustc_proc_macro",
38923893
"rustc_serialize",
38933894
"rustc_session",
38943895
"rustc_span",
3896+
"scoped-tls",
38953897
"smallvec",
38963898
"thin-vec",
38973899
"tracing",

compiler/rustc_ast/src/ast.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3348,7 +3348,8 @@ impl UseTree {
33483348
/// Distinguishes between `Attribute`s that decorate items and Attributes that
33493349
/// are contained as statements within items. These two cases need to be
33503350
/// distinguished for pretty-printing.
3351-
#[derive(Clone, PartialEq, Encodable, Decodable, Debug, Copy, HashStable_Generic, Walkable)]
3351+
#[derive(Clone, PartialEq, Eq, Hash, Debug, Copy)]
3352+
#[derive(Encodable, Decodable, HashStable_Generic, Walkable)]
33523353
pub enum AttrStyle {
33533354
Outer,
33543355
Inner,

compiler/rustc_ast/src/token.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ impl DocFragmentKind {
4040
}
4141
}
4242

43-
#[derive(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Debug, HashStable_Generic)]
43+
#[derive(Clone, Copy, PartialEq, Eq, Hash, Encodable, Decodable, Debug, HashStable_Generic)]
4444
pub enum CommentKind {
4545
Line,
4646
Block,
4747
}
4848

49-
#[derive(Copy, Clone, PartialEq, Debug, Encodable, Decodable, HashStable_Generic)]
49+
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Encodable, Decodable, HashStable_Generic)]
5050
pub enum InvisibleOrigin {
5151
// From the expansion of a metavariable in a declarative macro.
5252
MetaVar(MetaVarKind),
@@ -123,7 +123,7 @@ impl fmt::Display for MetaVarKind {
123123
/// Describes how a sequence of token trees is delimited.
124124
/// Cannot use `proc_macro::Delimiter` directly because this
125125
/// structure should implement some additional traits.
126-
#[derive(Copy, Clone, Debug, PartialEq, Encodable, Decodable, HashStable_Generic)]
126+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Encodable, Decodable, HashStable_Generic)]
127127
pub enum Delimiter {
128128
/// `( ... )`
129129
Parenthesis,
@@ -186,7 +186,7 @@ impl Delimiter {
186186
// type. This means that float literals like `1f32` are classified by this type
187187
// as `Int`. Only upon conversion to `ast::LitKind` will such a literal be
188188
// given the `Float` kind.
189-
#[derive(Clone, Copy, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
189+
#[derive(Clone, Copy, PartialEq, Eq, Hash, Encodable, Decodable, Debug, HashStable_Generic)]
190190
pub enum LitKind {
191191
Bool, // AST only, must never appear in a `Token`
192192
Byte,
@@ -203,7 +203,7 @@ pub enum LitKind {
203203
}
204204

205205
/// A literal token.
206-
#[derive(Clone, Copy, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
206+
#[derive(Clone, Copy, PartialEq, Eq, Hash, Encodable, Decodable, Debug, HashStable_Generic)]
207207
pub struct Lit {
208208
pub kind: LitKind,
209209
pub symbol: Symbol,
@@ -349,7 +349,7 @@ fn ident_can_begin_type(name: Symbol, span: Span, is_raw: IdentIsRaw) -> bool {
349349
.contains(&name)
350350
}
351351

352-
#[derive(PartialEq, Encodable, Decodable, Debug, Copy, Clone, HashStable_Generic)]
352+
#[derive(PartialEq, Eq, Encodable, Decodable, Hash, Debug, Copy, Clone, HashStable_Generic)]
353353
pub enum IdentIsRaw {
354354
No,
355355
Yes,
@@ -376,7 +376,7 @@ impl From<bool> for IdentIsRaw {
376376
}
377377
}
378378

379-
#[derive(Clone, Copy, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
379+
#[derive(Clone, Copy, PartialEq, Eq, Hash, Encodable, Decodable, Debug, HashStable_Generic)]
380380
pub enum TokenKind {
381381
/* Expression-operator symbols. */
382382
/// `=`
@@ -526,7 +526,7 @@ pub enum TokenKind {
526526
Eof,
527527
}
528528

529-
#[derive(Clone, Copy, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
529+
#[derive(Clone, Copy, PartialEq, Eq, Hash, Encodable, Decodable, Debug, HashStable_Generic)]
530530
pub struct Token {
531531
pub kind: TokenKind,
532532
pub span: Span,

compiler/rustc_ast/src/tokenstream.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//! which are themselves a single [`Token`] or a `Delimited` subsequence of tokens.
66
77
use std::borrow::Cow;
8+
use std::hash::Hash;
89
use std::ops::Range;
910
use std::sync::Arc;
1011
use std::{cmp, fmt, iter, mem};
@@ -22,7 +23,7 @@ use crate::token::{self, Delimiter, Token, TokenKind};
2223
use crate::{AttrVec, Attribute};
2324

2425
/// Part of a `TokenStream`.
25-
#[derive(Debug, Clone, PartialEq, Encodable, Decodable, HashStable_Generic)]
26+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Encodable, Decodable, HashStable_Generic)]
2627
pub enum TokenTree {
2728
/// A single token. Should never be `OpenDelim` or `CloseDelim`, because
2829
/// delimiters are implicitly represented by `Delimited`.
@@ -538,7 +539,7 @@ pub struct AttrsTarget {
538539
/// compound token. Used for conversions to `proc_macro::Spacing`. Also used to
539540
/// guide pretty-printing, which is where the `JointHidden` value (which isn't
540541
/// part of `proc_macro::Spacing`) comes in useful.
541-
#[derive(Clone, Copy, Debug, PartialEq, Encodable, Decodable, HashStable_Generic)]
542+
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Encodable, Decodable, HashStable_Generic)]
542543
pub enum Spacing {
543544
/// The token cannot join with the following token to form a compound
544545
/// token.
@@ -595,7 +596,7 @@ pub enum Spacing {
595596
}
596597

597598
/// A `TokenStream` is an abstract sequence of tokens, organized into [`TokenTree`]s.
598-
#[derive(Clone, Debug, Default, Encodable, Decodable)]
599+
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, Encodable, Decodable)]
599600
pub struct TokenStream(pub(crate) Arc<Vec<TokenTree>>);
600601

601602
impl TokenStream {
@@ -811,14 +812,6 @@ impl TokenStream {
811812
}
812813
}
813814

814-
impl PartialEq<TokenStream> for TokenStream {
815-
fn eq(&self, other: &TokenStream) -> bool {
816-
self.iter().eq(other.iter())
817-
}
818-
}
819-
820-
impl Eq for TokenStream {}
821-
822815
impl FromIterator<TokenTree> for TokenStream {
823816
fn from_iter<I: IntoIterator<Item = TokenTree>>(iter: I) -> Self {
824817
TokenStream::new(iter.into_iter().collect::<Vec<TokenTree>>())
@@ -970,7 +963,8 @@ impl TokenCursor {
970963
}
971964
}
972965

973-
#[derive(Debug, Copy, Clone, PartialEq, Encodable, Decodable, HashStable_Generic, Walkable)]
966+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
967+
#[derive(Encodable, Decodable, HashStable_Generic, Walkable)]
974968
pub struct DelimSpan {
975969
pub open: Span,
976970
pub close: Span,
@@ -994,7 +988,7 @@ impl DelimSpan {
994988
}
995989
}
996990

997-
#[derive(Copy, Clone, Debug, PartialEq, Encodable, Decodable, HashStable_Generic)]
991+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Encodable, Decodable, HashStable_Generic)]
998992
pub struct DelimSpacing {
999993
pub open: Spacing,
1000994
pub close: Spacing,

compiler/rustc_expand/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ rustc_hir = { path = "../rustc_hir" }
2121
rustc_lexer = { path = "../rustc_lexer" }
2222
rustc_lint_defs = { path = "../rustc_lint_defs" }
2323
rustc_macros = { path = "../rustc_macros" }
24+
rustc_middle = { path = "../rustc_middle" }
2425
rustc_parse = { path = "../rustc_parse" }
2526
# We must use the proc_macro version that we will compile proc-macros against,
2627
# not the one from our own sysroot.
2728
rustc_proc_macro = { path = "../rustc_proc_macro" }
2829
rustc_serialize = { path = "../rustc_serialize" }
2930
rustc_session = { path = "../rustc_session" }
3031
rustc_span = { path = "../rustc_span" }
32+
scoped-tls = "1.0"
3133
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
3234
thin-vec = "0.2.12"
3335
tracing = "0.1"

compiler/rustc_expand/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,8 @@ pub mod module;
2929
#[allow(rustc::untranslatable_diagnostic)]
3030
pub mod proc_macro;
3131

32+
pub fn provide(providers: &mut rustc_middle::query::Providers) {
33+
providers.derive_macro_expansion = proc_macro::provide_derive_macro_expansion;
34+
}
35+
3236
rustc_fluent_macro::fluent_messages! { "../messages.ftl" }

0 commit comments

Comments
 (0)