From 540fcc617a7d0ab408e81e5668cfa3971233353d Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Mon, 16 Sep 2024 20:17:34 +1000 Subject: [PATCH] Move some supertraits outward. Specifically, put them where they are genuinely required, i.e. the outermost place they can be. --- compiler/rustc_codegen_ssa/src/mir/operand.rs | 2 +- compiler/rustc_codegen_ssa/src/traits/mod.rs | 9 ++++++++- .../rustc_codegen_ssa/src/traits/type_.rs | 19 ++++++++++--------- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/mir/operand.rs b/compiler/rustc_codegen_ssa/src/mir/operand.rs index 1891de8c0eb..9cf600286b2 100644 --- a/compiler/rustc_codegen_ssa/src/mir/operand.rs +++ b/compiler/rustc_codegen_ssa/src/mir/operand.rs @@ -280,7 +280,7 @@ pub fn immediate(self) -> V { /// /// If you don't need the type, see [`OperandValue::pointer_parts`] /// or [`OperandValue::deref`]. - pub fn deref>(self, cx: &Cx) -> PlaceRef<'tcx, V> { + pub fn deref>(self, cx: &Cx) -> PlaceRef<'tcx, V> { if self.layout.ty.is_box() { // Derefer should have removed all Box derefs bug!("dereferencing {:?} in codegen", self.layout.ty); diff --git a/compiler/rustc_codegen_ssa/src/traits/mod.rs b/compiler/rustc_codegen_ssa/src/traits/mod.rs index f662a766bb8..d6acf7e3cb8 100644 --- a/compiler/rustc_codegen_ssa/src/traits/mod.rs +++ b/compiler/rustc_codegen_ssa/src/traits/mod.rs @@ -27,6 +27,11 @@ use std::fmt; +use rustc_middle::ty::layout::FnAbiOf; +use rustc_middle::ty::Ty; +use rustc_target::abi::call::FnAbi; +use rustc_middle::ty::layout::{LayoutOf, TyAndLayout}; + pub use self::abi::AbiBuilderMethods; pub use self::asm::{AsmBuilderMethods, AsmMethods, GlobalAsmOperandRef, InlineAsmOperandRef}; pub use self::backend::{BackendTypes, CodegenBackend, ExtraBackendMethods}; @@ -46,7 +51,9 @@ pub trait CodegenObject = Copy + PartialEq + fmt::Debug; -pub trait CodegenMethods<'tcx> = TypeMethods<'tcx> +pub trait CodegenMethods<'tcx> = LayoutOf<'tcx, LayoutOfResult = TyAndLayout<'tcx>> + + FnAbiOf<'tcx, FnAbiOfResult = &'tcx FnAbi<'tcx, Ty<'tcx>>> + + TypeMethods<'tcx> + ConstMethods<'tcx> + StaticMethods + DebugInfoMethods<'tcx> diff --git a/compiler/rustc_codegen_ssa/src/traits/type_.rs b/compiler/rustc_codegen_ssa/src/traits/type_.rs index 55143733233..da2b596509c 100644 --- a/compiler/rustc_codegen_ssa/src/traits/type_.rs +++ b/compiler/rustc_codegen_ssa/src/traits/type_.rs @@ -1,5 +1,5 @@ use rustc_middle::bug; -use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt, LayoutOf, TyAndLayout}; +use rustc_middle::ty::layout::{HasTyCtxt, TyAndLayout}; use rustc_middle::ty::{self, Ty}; use rustc_target::abi::call::{ArgAbi, CastTarget, FnAbi, Reg}; use rustc_target::abi::{AddressSpace, Float, Integer}; @@ -9,7 +9,7 @@ use crate::common::TypeKind; use crate::mir::place::PlaceRef; -pub trait BaseTypeMethods<'tcx>: BackendTypes + HasTyCtxt<'tcx> { +pub trait BaseTypeMethods<'tcx>: BackendTypes { fn type_i8(&self) -> Self::Type; fn type_i16(&self) -> Self::Type; fn type_i32(&self) -> Self::Type; @@ -40,7 +40,9 @@ pub trait BaseTypeMethods<'tcx>: BackendTypes + HasTyCtxt<'tcx> { fn val_ty(&self, v: Self::Value) -> Self::Type; } -pub trait DerivedTypeMethods<'tcx>: BaseTypeMethods<'tcx> + MiscMethods<'tcx> { +pub trait DerivedTypeMethods<'tcx>: + BaseTypeMethods<'tcx> + MiscMethods<'tcx> + HasTyCtxt<'tcx> +{ fn type_int(&self) -> Self::Type { match &self.sess().target.c_int_width[..] { "16" => self.type_i16(), @@ -98,13 +100,12 @@ fn type_has_metadata(&self, ty: Ty<'tcx>) -> bool { } } -impl<'tcx, T> DerivedTypeMethods<'tcx> for T where Self: BaseTypeMethods<'tcx> + MiscMethods<'tcx> {} - -pub trait LayoutTypeMethods<'tcx>: - BackendTypes - + LayoutOf<'tcx, LayoutOfResult = TyAndLayout<'tcx>> - + FnAbiOf<'tcx, FnAbiOfResult = &'tcx FnAbi<'tcx, Ty<'tcx>>> +impl<'tcx, T> DerivedTypeMethods<'tcx> for T where + Self: BaseTypeMethods<'tcx> + MiscMethods<'tcx> + HasTyCtxt<'tcx> { +} + +pub trait LayoutTypeMethods<'tcx>: BackendTypes { /// The backend type used for a rust type when it's in memory, /// such as when it's stack-allocated or when it's being loaded or stored. fn backend_type(&self, layout: TyAndLayout<'tcx>) -> Self::Type;