Skip to content

Commit 9b563d7

Browse files
committed
Moved over methods to rustc_type_ir and added helper functions to
interner.
1 parent 77d1fb1 commit 9b563d7

File tree

11 files changed

+1062
-435
lines changed

11 files changed

+1062
-435
lines changed

compiler/rustc_middle/src/mir/consts.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,6 @@ impl ConstValue {
9494
self.try_to_scalar_int()?.try_into().ok()
9595
}
9696

97-
pub fn try_to_target_usize(&self, tcx: TyCtxt<'_>) -> Option<u64> {
98-
Some(self.try_to_scalar_int()?.to_target_usize(tcx))
99-
}
100-
10197
pub fn try_to_bits_for_ty<'tcx>(
10298
&self,
10399
tcx: TyCtxt<'tcx>,

compiler/rustc_middle/src/ty/adt.rs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,22 @@ impl<'tcx> rustc_type_ir::inherent::AdtDef<TyCtxt<'tcx>> for AdtDef<'tcx> {
211211
self.is_struct()
212212
}
213213

214+
fn is_box(self) -> bool {
215+
self.is_box()
216+
}
217+
218+
fn is_pin(self) -> bool {
219+
self.is_pin()
220+
}
221+
222+
fn is_enum(self) -> bool {
223+
self.is_enum()
224+
}
225+
226+
fn is_union(self) -> bool {
227+
self.is_union()
228+
}
229+
214230
fn struct_tail_ty(self, interner: TyCtxt<'tcx>) -> Option<ty::EarlyBinder<'tcx, Ty<'tcx>>> {
215231
Some(interner.type_of(self.non_enum_variant().tail_opt()?.did))
216232
}
@@ -254,6 +270,24 @@ impl<'tcx> rustc_type_ir::inherent::AdtDef<TyCtxt<'tcx>> for AdtDef<'tcx> {
254270
hir::Constness::NotConst => AdtDestructorKind::NotConst,
255271
})
256272
}
273+
274+
/// Asserts this is a struct or union and returns its unique variant.
275+
fn non_enum_variant(self) -> &'tcx VariantDef {
276+
assert!(self.is_struct() || self.is_union());
277+
self.variant(FIRST_VARIANT)
278+
}
279+
280+
fn repr_is_simd(self) -> bool {
281+
self.0.0.repr.simd()
282+
}
283+
284+
fn scalable_element_cnt(self) -> Option<u16> {
285+
if Some(ScalableElt::ElementCount(element_count)) = self.repr().scalable {
286+
Some(element_count)
287+
} else {
288+
None
289+
}
290+
}
257291
}
258292

259293
#[derive(Copy, Clone, Debug, Eq, PartialEq, HashStable, TyEncodable, TyDecodable)]
@@ -458,12 +492,6 @@ impl<'tcx> AdtDef<'tcx> {
458492
self.destructor(tcx).is_some()
459493
}
460494

461-
/// Asserts this is a struct or union and returns its unique variant.
462-
pub fn non_enum_variant(self) -> &'tcx VariantDef {
463-
assert!(self.is_struct() || self.is_union());
464-
self.variant(FIRST_VARIANT)
465-
}
466-
467495
#[inline]
468496
pub fn predicates(self, tcx: TyCtxt<'tcx>) -> GenericPredicates<'tcx> {
469497
tcx.predicates_of(self.did())

compiler/rustc_middle/src/ty/consts.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ impl<'tcx> rustc_type_ir::inherent::Const<TyCtxt<'tcx>> for Const<'tcx> {
213213
fn new_error(interner: TyCtxt<'tcx>, guar: ErrorGuaranteed) -> Self {
214214
Const::new_error(interner, guar)
215215
}
216+
217+
fn try_to_target_usize(&self, tcx: TyCtxt<'tcx>) -> Option<u64> {
218+
Some(self.try_to_scalar_int()?.to_target_usize(tcx))
219+
}
216220
}
217221

218222
impl<'tcx> Const<'tcx> {

compiler/rustc_middle/src/ty/context.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,10 +1100,6 @@ impl<'tcx> TyCtxt<'tcx> {
11001100
self.coroutine_kind(def_id).is_some()
11011101
}
11021102

1103-
pub fn is_async_drop_in_place_coroutine(self, def_id: DefId) -> bool {
1104-
self.is_lang_item(self.parent(def_id), LangItem::AsyncDropInPlace)
1105-
}
1106-
11071103
pub fn type_const_span(self, def_id: DefId) -> Option<Span> {
11081104
if !self.is_type_const(def_id) {
11091105
return None;

compiler/rustc_middle/src/ty/context/impl_interner.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
//! Implementation of [`rustc_type_ir::Interner`] for [`TyCtxt`].
22
33
use std::fmt;
4+
use std::ops::Range;
45

5-
use rustc_abi::ExternAbi;
6+
use rustc_abi::{ExternAbi, VariantIdx};
67
use rustc_data_structures::debug_assert_matches;
78
use rustc_data_structures::intern::Interned;
89
use rustc_errors::ErrorGuaranteed;
@@ -22,8 +23,8 @@ use crate::traits::solve::{
2223
self, CanonicalInput, ExternalConstraints, ExternalConstraintsData, QueryResult, inspect,
2324
};
2425
use crate::ty::{
25-
self, Clause, Const, List, ParamTy, Pattern, PolyExistentialPredicate, Predicate, Region, Ty,
26-
TyCtxt,
26+
self, AdtDef, Clause, Const, List, ParamTy, Pattern, PolyExistentialPredicate, Predicate,
27+
Region, Ty, TyCtxt, VariantDef,
2728
};
2829

2930
#[allow(rustc::usage_of_ty_tykind)]
@@ -41,11 +42,14 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
4142
type CoroutineClosureId = DefId;
4243
type CoroutineId = DefId;
4344
type AdtId = DefId;
45+
type AdtDef = AdtDef;
46+
type VariantDef = VariantDef;
4447
type ImplId = DefId;
4548
type UnevaluatedConstId = DefId;
4649
type Span = Span;
4750
type Interned<T: Copy + Clone + std::fmt::Debug + std::hash::Hash + Eq + PartialEq> =
4851
Interned<'tcx, T>;
52+
type VariantIdx = VariantIdx;
4953

5054
type GenericArgs = ty::GenericArgsRef<'tcx>;
5155

@@ -738,6 +742,23 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
738742
bug!("item_name: no name for {:?}", self.def_path(id));
739743
})
740744
}
745+
746+
fn u8_type(self) -> Ty<'tcx> {
747+
self.types.u8
748+
}
749+
750+
fn is_async_drop_in_place_coroutine(self, def_id: DefId) -> bool {
751+
self.is_lang_item(self.parent(def_id), LangItem::AsyncDropInPlace)
752+
}
753+
754+
#[inline]
755+
fn coroutine_variant_range(
756+
self,
757+
def_id: DefId,
758+
coroutine_args: ty::CoroutineArgs<Self>,
759+
) -> Range<VariantIdx> {
760+
self.coroutine_variant_range(def_id, coroutine_args)
761+
}
741762
}
742763

743764
/// Defines trivial conversion functions between the main [`LangItem`] enum,

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,6 +1238,16 @@ impl VariantDef {
12381238
}
12391239
}
12401240

1241+
impl<'tcx> rustc_type_ir::inherent::VariantDef for VariantDef {
1242+
fn field_zero_ty(self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
1243+
self.fields[FieldIdx::ZERO].ty(tcx, args)
1244+
}
1245+
1246+
fn fields_len(self) -> usize {
1247+
self.fields.len()
1248+
}
1249+
}
1250+
12411251
impl PartialEq for VariantDef {
12421252
#[inline]
12431253
fn eq(&self, other: &Self) -> bool {
@@ -2148,6 +2158,18 @@ impl<'tcx> TyCtxt<'tcx> {
21482158

21492159
!self.associated_types_for_impl_traits_in_associated_fn(trait_item_def_id).is_empty()
21502160
}
2161+
2162+
/// The valid variant indices of this coroutine.
2163+
#[inline]
2164+
pub fn coroutine_variant_range(
2165+
self,
2166+
def_id: DefId,
2167+
coroutine_args: ty::CoroutineArgs<Self>,
2168+
) -> Range<VariantIdx> {
2169+
// FIXME requires optimized MIR
2170+
rustc_abi::FIRST_VARIANT
2171+
..self.coroutine_layout(def_id, coroutine_args).unwrap().variant_fields.next_index()
2172+
}
21512173
}
21522174

21532175
pub fn provide(providers: &mut Providers) {

compiler/rustc_middle/src/ty/print/pretty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
11571157

11581158
write!(p, ")")?;
11591159
if let Some(ty) = return_ty.skip_binder().as_type() {
1160-
if !matches!(ty.kind(), ty::Tuple(tys) if tys.is_empty()) {
1160+
if !matches!(ty.is_unit()) {
11611161
write!(p, " -> ")?;
11621162
return_ty.print(p)?;
11631163
}

0 commit comments

Comments
 (0)