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](ca7a230196/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
This commit is contained in:
Michael Goulet 2025-03-06 12:22:14 -05:00 committed by GitHub
commit 91175bd932
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -19,7 +19,7 @@ use rustc_hir as hir;
use rustc_hir::def_id::DefId;
use rustc_hir::{BindingMode, ByRef, HirId, MatchSource, RangeEnd};
use rustc_index::{IndexVec, newtype_index};
use rustc_macros::{HashStable, TypeVisitable};
use rustc_macros::{HashStable, TyDecodable, TyEncodable, TypeVisitable};
use rustc_span::def_id::LocalDefId;
use rustc_span::{ErrorGuaranteed, Span, Symbol};
use rustc_target::asm::InlineAsmRegOrRegClass;
@ -49,10 +49,13 @@ macro_rules! thir_with_elements {
}
)*
// Note: Making `Thir` implement `Clone` is useful for external tools that need access to
// THIR bodies even after the `Steal` query result has been stolen.
// One such tool is https://github.com/rust-corpus/qrates/.
/// A container for a THIR body.
///
/// This can be indexed directly by any THIR index (e.g. [`ExprId`]).
#[derive(Debug, HashStable)]
#[derive(Debug, HashStable, Clone)]
pub struct Thir<'tcx> {
pub body_type: BodyTy<'tcx>,
$(
@ -90,7 +93,7 @@ thir_with_elements! {
params: ParamId => Param<'tcx> => "p{}",
}
#[derive(Debug, HashStable)]
#[derive(Debug, HashStable, Clone)]
pub enum BodyTy<'tcx> {
Const(Ty<'tcx>),
Fn(FnSig<'tcx>),
@ -98,7 +101,7 @@ pub enum BodyTy<'tcx> {
}
/// Description of a type-checked function parameter.
#[derive(Debug, HashStable)]
#[derive(Clone, Debug, HashStable)]
pub struct Param<'tcx> {
/// The pattern that appears in the parameter list, or None for implicit parameters.
pub pat: Option<Box<Pat<'tcx>>>,
@ -118,7 +121,7 @@ pub enum LintLevel {
Explicit(HirId),
}
#[derive(Debug, HashStable)]
#[derive(Clone, Debug, HashStable)]
pub struct Block {
/// Whether the block itself has a label. Used by `label: {}`
/// and `try` blocks.
@ -138,7 +141,7 @@ pub struct Block {
type UserTy<'tcx> = Option<Box<CanonicalUserType<'tcx>>>;
#[derive(Debug, HashStable)]
#[derive(Clone, Debug, HashStable)]
pub struct AdtExpr<'tcx> {
/// The ADT we're constructing.
pub adt_def: AdtDef<'tcx>,
@ -155,7 +158,7 @@ pub struct AdtExpr<'tcx> {
pub base: AdtExprBase<'tcx>,
}
#[derive(Debug, HashStable)]
#[derive(Clone, Debug, HashStable)]
pub enum AdtExprBase<'tcx> {
/// A struct expression where all the fields are explicitly enumerated: `Foo { a, b }`.
None,
@ -168,7 +171,7 @@ pub enum AdtExprBase<'tcx> {
DefaultFields(Box<[Ty<'tcx>]>),
}
#[derive(Debug, HashStable)]
#[derive(Clone, Debug, HashStable)]
pub struct ClosureExpr<'tcx> {
pub closure_id: LocalDefId,
pub args: UpvarArgs<'tcx>,
@ -177,7 +180,7 @@ pub struct ClosureExpr<'tcx> {
pub fake_reads: Vec<(ExprId, FakeReadCause, HirId)>,
}
#[derive(Debug, HashStable)]
#[derive(Clone, Debug, HashStable)]
pub struct InlineAsmExpr<'tcx> {
pub asm_macro: AsmMacro,
pub template: &'tcx [InlineAsmTemplatePiece],
@ -195,12 +198,12 @@ pub enum BlockSafety {
ExplicitUnsafe(HirId),
}
#[derive(Debug, HashStable)]
#[derive(Clone, Debug, HashStable)]
pub struct Stmt<'tcx> {
pub kind: StmtKind<'tcx>,
}
#[derive(Debug, HashStable)]
#[derive(Clone, Debug, HashStable)]
pub enum StmtKind<'tcx> {
/// An expression with a trailing semicolon.
Expr {
@ -240,11 +243,11 @@ pub enum StmtKind<'tcx> {
},
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, HashStable)]
#[derive(Clone, Debug, Copy, PartialEq, Eq, Hash, HashStable, TyEncodable, TyDecodable)]
pub struct LocalVarId(pub HirId);
/// A THIR expression.
#[derive(Debug, HashStable)]
#[derive(Clone, Debug, HashStable)]
pub struct Expr<'tcx> {
/// kind of expression
pub kind: ExprKind<'tcx>,
@ -271,7 +274,7 @@ pub struct TempLifetime {
pub backwards_incompatible: Option<region::Scope>,
}
#[derive(Debug, HashStable)]
#[derive(Clone, Debug, HashStable)]
pub enum ExprKind<'tcx> {
/// `Scope`s are used to explicitly mark destruction scopes,
/// and to track the `HirId` of the expressions within the scope.
@ -548,20 +551,20 @@ pub enum ExprKind<'tcx> {
/// Represents the association of a field identifier and an expression.
///
/// This is used in struct constructors.
#[derive(Debug, HashStable)]
#[derive(Clone, Debug, HashStable)]
pub struct FieldExpr {
pub name: FieldIdx,
pub expr: ExprId,
}
#[derive(Debug, HashStable)]
#[derive(Clone, Debug, HashStable)]
pub struct FruInfo<'tcx> {
pub base: ExprId,
pub field_types: Box<[Ty<'tcx>]>,
}
/// A `match` arm.
#[derive(Debug, HashStable)]
#[derive(Clone, Debug, HashStable)]
pub struct Arm<'tcx> {
pub pattern: Box<Pat<'tcx>>,
pub guard: Option<ExprId>,
@ -579,7 +582,7 @@ pub enum LogicalOp {
Or,
}
#[derive(Debug, HashStable)]
#[derive(Clone, Debug, HashStable)]
pub enum InlineAsmOperand<'tcx> {
In {
reg: InlineAsmRegOrRegClass,
@ -616,13 +619,13 @@ pub enum InlineAsmOperand<'tcx> {
},
}
#[derive(Debug, HashStable, TypeVisitable)]
#[derive(Clone, Debug, HashStable, TypeVisitable)]
pub struct FieldPat<'tcx> {
pub field: FieldIdx,
pub pattern: Pat<'tcx>,
}
#[derive(Debug, HashStable, TypeVisitable)]
#[derive(Clone, Debug, HashStable, TypeVisitable)]
pub struct Pat<'tcx> {
pub ty: Ty<'tcx>,
pub span: Span,
@ -729,7 +732,7 @@ impl<'tcx> Pat<'tcx> {
}
}
#[derive(Debug, HashStable, TypeVisitable)]
#[derive(Clone, Debug, HashStable, TypeVisitable)]
pub struct Ascription<'tcx> {
pub annotation: CanonicalUserTypeAnnotation<'tcx>,
/// Variance to use when relating the `user_ty` to the **type of the value being
@ -753,7 +756,7 @@ pub struct Ascription<'tcx> {
pub variance: ty::Variance,
}
#[derive(Debug, HashStable, TypeVisitable)]
#[derive(Clone, Debug, HashStable, TypeVisitable)]
pub enum PatKind<'tcx> {
/// A wildcard pattern: `_`.
Wild,