Skip to content

Commit

Permalink
Rollup merge of rust-lang#137744 - skius:master, r=Nadrieril
Browse files Browse the repository at this point in the history
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
  • Loading branch information
compiler-errors authored Mar 6, 2025
2 parents ea934e4 + b5f0c82 commit 2479067
Showing 1 changed file with 25 additions and 22 deletions.
47 changes: 25 additions & 22 deletions compiler/rustc_middle/src/thir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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>,
$(
Expand Down Expand Up @@ -90,15 +93,15 @@ 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>),
GlobalAsm(Ty<'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>>>,
Expand All @@ -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.
Expand All @@ -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>,
Expand All @@ -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,
Expand All @@ -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>,
Expand All @@ -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],
Expand All @@ -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 {
Expand Down Expand Up @@ -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>,
Expand All @@ -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.
Expand Down Expand Up @@ -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>,
Expand All @@ -579,7 +582,7 @@ pub enum LogicalOp {
Or,
}

#[derive(Debug, HashStable)]
#[derive(Clone, Debug, HashStable)]
pub enum InlineAsmOperand<'tcx> {
In {
reg: InlineAsmRegOrRegClass,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down

0 comments on commit 2479067

Please sign in to comment.