Skip to content

Commit 91175bd

Browse files
Rollup merge of #137744 - skius:master, r=Nadrieril
Re-add `Clone`-derive on `Thir` This PR adds back `Clone` for `Thir`. If a tool wants to access a `thir_body` query result in the `Callbacks::after_analysis` hook, it can't do so (I think) without a `Clone` impl on `Thir`, because `check_unsafety` steals the value. With `Clone`, the `thir_body` query provider can be overriden to cache a clone of the `Thir`, circumventing that issue. Specifically, we need it for https://github.com/rust-corpus/qrates, [here](https://github.com/skius/qrates/blob/ca7a2301968a43862f2c04daffed71a9de8c333c/extractor/src/lib.rs#L205). Please let me know if there are issues with this PR/if there's another way to solve the problem at hand
2 parents e05a977 + b5f0c82 commit 91175bd

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

compiler/rustc_middle/src/thir.rs

+25-22
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_hir as hir;
1919
use rustc_hir::def_id::DefId;
2020
use rustc_hir::{BindingMode, ByRef, HirId, MatchSource, RangeEnd};
2121
use rustc_index::{IndexVec, newtype_index};
22-
use rustc_macros::{HashStable, TypeVisitable};
22+
use rustc_macros::{HashStable, TyDecodable, TyEncodable, TypeVisitable};
2323
use rustc_span::def_id::LocalDefId;
2424
use rustc_span::{ErrorGuaranteed, Span, Symbol};
2525
use rustc_target::asm::InlineAsmRegOrRegClass;
@@ -49,10 +49,13 @@ macro_rules! thir_with_elements {
4949
}
5050
)*
5151

52+
// Note: Making `Thir` implement `Clone` is useful for external tools that need access to
53+
// THIR bodies even after the `Steal` query result has been stolen.
54+
// One such tool is https://github.com/rust-corpus/qrates/.
5255
/// A container for a THIR body.
5356
///
5457
/// This can be indexed directly by any THIR index (e.g. [`ExprId`]).
55-
#[derive(Debug, HashStable)]
58+
#[derive(Debug, HashStable, Clone)]
5659
pub struct Thir<'tcx> {
5760
pub body_type: BodyTy<'tcx>,
5861
$(
@@ -90,15 +93,15 @@ thir_with_elements! {
9093
params: ParamId => Param<'tcx> => "p{}",
9194
}
9295

93-
#[derive(Debug, HashStable)]
96+
#[derive(Debug, HashStable, Clone)]
9497
pub enum BodyTy<'tcx> {
9598
Const(Ty<'tcx>),
9699
Fn(FnSig<'tcx>),
97100
GlobalAsm(Ty<'tcx>),
98101
}
99102

100103
/// Description of a type-checked function parameter.
101-
#[derive(Debug, HashStable)]
104+
#[derive(Clone, Debug, HashStable)]
102105
pub struct Param<'tcx> {
103106
/// The pattern that appears in the parameter list, or None for implicit parameters.
104107
pub pat: Option<Box<Pat<'tcx>>>,
@@ -118,7 +121,7 @@ pub enum LintLevel {
118121
Explicit(HirId),
119122
}
120123

121-
#[derive(Debug, HashStable)]
124+
#[derive(Clone, Debug, HashStable)]
122125
pub struct Block {
123126
/// Whether the block itself has a label. Used by `label: {}`
124127
/// and `try` blocks.
@@ -138,7 +141,7 @@ pub struct Block {
138141

139142
type UserTy<'tcx> = Option<Box<CanonicalUserType<'tcx>>>;
140143

141-
#[derive(Debug, HashStable)]
144+
#[derive(Clone, Debug, HashStable)]
142145
pub struct AdtExpr<'tcx> {
143146
/// The ADT we're constructing.
144147
pub adt_def: AdtDef<'tcx>,
@@ -155,7 +158,7 @@ pub struct AdtExpr<'tcx> {
155158
pub base: AdtExprBase<'tcx>,
156159
}
157160

158-
#[derive(Debug, HashStable)]
161+
#[derive(Clone, Debug, HashStable)]
159162
pub enum AdtExprBase<'tcx> {
160163
/// A struct expression where all the fields are explicitly enumerated: `Foo { a, b }`.
161164
None,
@@ -168,7 +171,7 @@ pub enum AdtExprBase<'tcx> {
168171
DefaultFields(Box<[Ty<'tcx>]>),
169172
}
170173

171-
#[derive(Debug, HashStable)]
174+
#[derive(Clone, Debug, HashStable)]
172175
pub struct ClosureExpr<'tcx> {
173176
pub closure_id: LocalDefId,
174177
pub args: UpvarArgs<'tcx>,
@@ -177,7 +180,7 @@ pub struct ClosureExpr<'tcx> {
177180
pub fake_reads: Vec<(ExprId, FakeReadCause, HirId)>,
178181
}
179182

180-
#[derive(Debug, HashStable)]
183+
#[derive(Clone, Debug, HashStable)]
181184
pub struct InlineAsmExpr<'tcx> {
182185
pub asm_macro: AsmMacro,
183186
pub template: &'tcx [InlineAsmTemplatePiece],
@@ -195,12 +198,12 @@ pub enum BlockSafety {
195198
ExplicitUnsafe(HirId),
196199
}
197200

198-
#[derive(Debug, HashStable)]
201+
#[derive(Clone, Debug, HashStable)]
199202
pub struct Stmt<'tcx> {
200203
pub kind: StmtKind<'tcx>,
201204
}
202205

203-
#[derive(Debug, HashStable)]
206+
#[derive(Clone, Debug, HashStable)]
204207
pub enum StmtKind<'tcx> {
205208
/// An expression with a trailing semicolon.
206209
Expr {
@@ -240,11 +243,11 @@ pub enum StmtKind<'tcx> {
240243
},
241244
}
242245

243-
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, HashStable)]
246+
#[derive(Clone, Debug, Copy, PartialEq, Eq, Hash, HashStable, TyEncodable, TyDecodable)]
244247
pub struct LocalVarId(pub HirId);
245248

246249
/// A THIR expression.
247-
#[derive(Debug, HashStable)]
250+
#[derive(Clone, Debug, HashStable)]
248251
pub struct Expr<'tcx> {
249252
/// kind of expression
250253
pub kind: ExprKind<'tcx>,
@@ -271,7 +274,7 @@ pub struct TempLifetime {
271274
pub backwards_incompatible: Option<region::Scope>,
272275
}
273276

274-
#[derive(Debug, HashStable)]
277+
#[derive(Clone, Debug, HashStable)]
275278
pub enum ExprKind<'tcx> {
276279
/// `Scope`s are used to explicitly mark destruction scopes,
277280
/// and to track the `HirId` of the expressions within the scope.
@@ -548,20 +551,20 @@ pub enum ExprKind<'tcx> {
548551
/// Represents the association of a field identifier and an expression.
549552
///
550553
/// This is used in struct constructors.
551-
#[derive(Debug, HashStable)]
554+
#[derive(Clone, Debug, HashStable)]
552555
pub struct FieldExpr {
553556
pub name: FieldIdx,
554557
pub expr: ExprId,
555558
}
556559

557-
#[derive(Debug, HashStable)]
560+
#[derive(Clone, Debug, HashStable)]
558561
pub struct FruInfo<'tcx> {
559562
pub base: ExprId,
560563
pub field_types: Box<[Ty<'tcx>]>,
561564
}
562565

563566
/// A `match` arm.
564-
#[derive(Debug, HashStable)]
567+
#[derive(Clone, Debug, HashStable)]
565568
pub struct Arm<'tcx> {
566569
pub pattern: Box<Pat<'tcx>>,
567570
pub guard: Option<ExprId>,
@@ -579,7 +582,7 @@ pub enum LogicalOp {
579582
Or,
580583
}
581584

582-
#[derive(Debug, HashStable)]
585+
#[derive(Clone, Debug, HashStable)]
583586
pub enum InlineAsmOperand<'tcx> {
584587
In {
585588
reg: InlineAsmRegOrRegClass,
@@ -616,13 +619,13 @@ pub enum InlineAsmOperand<'tcx> {
616619
},
617620
}
618621

619-
#[derive(Debug, HashStable, TypeVisitable)]
622+
#[derive(Clone, Debug, HashStable, TypeVisitable)]
620623
pub struct FieldPat<'tcx> {
621624
pub field: FieldIdx,
622625
pub pattern: Pat<'tcx>,
623626
}
624627

625-
#[derive(Debug, HashStable, TypeVisitable)]
628+
#[derive(Clone, Debug, HashStable, TypeVisitable)]
626629
pub struct Pat<'tcx> {
627630
pub ty: Ty<'tcx>,
628631
pub span: Span,
@@ -729,7 +732,7 @@ impl<'tcx> Pat<'tcx> {
729732
}
730733
}
731734

732-
#[derive(Debug, HashStable, TypeVisitable)]
735+
#[derive(Clone, Debug, HashStable, TypeVisitable)]
733736
pub struct Ascription<'tcx> {
734737
pub annotation: CanonicalUserTypeAnnotation<'tcx>,
735738
/// Variance to use when relating the `user_ty` to the **type of the value being
@@ -753,7 +756,7 @@ pub struct Ascription<'tcx> {
753756
pub variance: ty::Variance,
754757
}
755758

756-
#[derive(Debug, HashStable, TypeVisitable)]
759+
#[derive(Clone, Debug, HashStable, TypeVisitable)]
757760
pub enum PatKind<'tcx> {
758761
/// A wildcard pattern: `_`.
759762
Wild,

0 commit comments

Comments
 (0)