Skip to content

Commit f5c1376

Browse files
committed
Moved over methods to rustc_type_ir and added helper functions to
interner.
1 parent 38fe2d6 commit f5c1376

11 files changed

Lines changed: 1065 additions & 620 deletions

File tree

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
@@ -264,6 +264,22 @@ impl<'tcx> rustc_type_ir::inherent::AdtDef<TyCtxt<'tcx>> for AdtDef<'tcx> {
264264
self.repr().packed()
265265
}
266266

267+
fn is_box(self) -> bool {
268+
self.is_box()
269+
}
270+
271+
fn is_pin(self) -> bool {
272+
self.is_pin()
273+
}
274+
275+
fn is_enum(self) -> bool {
276+
self.is_enum()
277+
}
278+
279+
fn is_union(self) -> bool {
280+
self.is_union()
281+
}
282+
267283
fn struct_tail_ty(self, interner: TyCtxt<'tcx>) -> Option<ty::EarlyBinder<'tcx, Ty<'tcx>>> {
268284
Some(interner.type_of(self.non_enum_variant().tail_opt()?.did))
269285
}
@@ -315,6 +331,24 @@ impl<'tcx> rustc_type_ir::inherent::AdtDef<TyCtxt<'tcx>> for AdtDef<'tcx> {
315331
hir::Constness::NotConst => AdtDestructorKind::NotConst,
316332
})
317333
}
334+
335+
/// Asserts this is a struct or union and returns its unique variant.
336+
fn non_enum_variant(self) -> &'tcx VariantDef {
337+
assert!(self.is_struct() || self.is_union());
338+
self.variant(FIRST_VARIANT)
339+
}
340+
341+
fn repr_is_simd(self) -> bool {
342+
self.0.0.repr.simd()
343+
}
344+
345+
fn scalable_element_cnt(self) -> Option<u16> {
346+
if Some(ScalableElt::ElementCount(element_count)) = self.repr().scalable {
347+
Some(element_count)
348+
} else {
349+
None
350+
}
351+
}
318352
}
319353

320354
#[derive(Copy, Clone, Debug, Eq, PartialEq, HashStable, TyEncodable, TyDecodable)]
@@ -526,12 +560,6 @@ impl<'tcx> AdtDef<'tcx> {
526560
self.destructor(tcx).is_some()
527561
}
528562

529-
/// Asserts this is a struct or union and returns its unique variant.
530-
pub fn non_enum_variant(self) -> &'tcx VariantDef {
531-
assert!(self.is_struct() || self.is_union());
532-
self.variant(FIRST_VARIANT)
533-
}
534-
535563
#[inline]
536564
pub fn predicates(self, tcx: TyCtxt<'tcx>) -> GenericPredicates<'tcx> {
537565
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 {
@@ -2150,6 +2160,18 @@ impl<'tcx> TyCtxt<'tcx> {
21502160

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

21552177
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
@@ -1167,7 +1167,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
11671167

11681168
write!(p, ")")?;
11691169
if let Some(ty) = return_ty.skip_binder().as_type() {
1170-
if !matches!(ty.kind(), ty::Tuple(tys) if tys.is_empty()) {
1170+
if !matches!(ty.is_unit()) {
11711171
write!(p, " -> ")?;
11721172
return_ty.print(p)?;
11731173
}

0 commit comments

Comments
 (0)