Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-add Clone-derive on Thir #137744

Merged
merged 2 commits into from
Mar 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading