Skip to content

Commit 7a72560

Browse files
committed
Reduce direct mk_ty usage.
We use more specific `mk_*` functions in most places, might as well use them as much as possible.
1 parent 6248bbb commit 7a72560

File tree

25 files changed

+92
-82
lines changed

25 files changed

+92
-82
lines changed

compiler/rustc_codegen_ssa/src/mir/debuginfo.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -385,10 +385,9 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
385385
calculate_debuginfo_offset(bx, local, &var, base);
386386

387387
// Create a variable which will be a pointer to the actual value
388-
let ptr_ty = bx.tcx().mk_ty(ty::RawPtr(ty::TypeAndMut {
389-
mutbl: mir::Mutability::Mut,
390-
ty: place.layout.ty,
391-
}));
388+
let ptr_ty = bx
389+
.tcx()
390+
.mk_ptr(ty::TypeAndMut { mutbl: mir::Mutability::Mut, ty: place.layout.ty });
392391
let ptr_layout = bx.layout_of(ptr_ty);
393392
let alloca = PlaceRef::alloca(bx, ptr_layout);
394393
bx.set_var_name(alloca.llval, &(var.name.to_string() + ".dbg.spill"));

compiler/rustc_const_eval/src/const_eval/valtrees.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ fn get_info_on_unsized_field<'tcx>(
193193

194194
// Have to adjust type for ty::Str
195195
let unsized_inner_ty = match unsized_inner_ty.kind() {
196-
ty::Str => tcx.mk_ty(ty::Uint(ty::UintTy::U8)),
196+
ty::Str => tcx.types.u8,
197197
_ => unsized_inner_ty,
198198
};
199199

@@ -216,7 +216,7 @@ fn create_pointee_place<'tcx>(
216216

217217
let (unsized_inner_ty, num_elems) = get_info_on_unsized_field(ty, valtree, tcx);
218218
let unsized_inner_ty = match unsized_inner_ty.kind() {
219-
ty::Str => tcx.mk_ty(ty::Uint(ty::UintTy::U8)),
219+
ty::Str => tcx.types.u8,
220220
_ => unsized_inner_ty,
221221
};
222222
let unsized_inner_ty_size =

compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,10 @@ impl Qualif for CustomEq {
217217

218218
fn in_adt_inherently<'tcx>(
219219
cx: &ConstCx<'_, 'tcx>,
220-
adt: AdtDef<'tcx>,
220+
def: AdtDef<'tcx>,
221221
substs: SubstsRef<'tcx>,
222222
) -> bool {
223-
let ty = cx.tcx.mk_ty(ty::Adt(adt, substs));
223+
let ty = cx.tcx.mk_adt(def, substs);
224224
!ty.is_structural_eq_shallow(cx.tcx)
225225
}
226226
}

compiler/rustc_hir_analysis/src/astconv/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1250,7 +1250,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
12501250
//
12511251
// Calling `skip_binder` is okay, because `add_bounds` expects the `param_ty`
12521252
// parameter to have a skipped binder.
1253-
let param_ty = tcx.mk_ty(ty::Alias(ty::Projection, projection_ty.skip_binder()));
1253+
let param_ty = tcx.mk_alias(ty::Projection, projection_ty.skip_binder());
12541254
self.add_bounds(param_ty, ast_bounds.iter(), bounds, candidate.bound_vars());
12551255
}
12561256
}
@@ -2930,7 +2930,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
29302930
}
29312931
};
29322932

2933-
tcx.mk_ty(ty::Array(self.ast_ty_to_ty(ty), length))
2933+
tcx.mk_array_with_const_len(self.ast_ty_to_ty(ty), length)
29342934
}
29352935
hir::TyKind::Typeof(e) => {
29362936
let ty_erased = tcx.type_of(e.def_id);

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1927,10 +1927,10 @@ pub(super) fn check_type_bounds<'tcx>(
19271927
let kind = ty::BoundTyKind::Param(param.def_id, param.name);
19281928
let bound_var = ty::BoundVariableKind::Ty(kind);
19291929
bound_vars.push(bound_var);
1930-
tcx.mk_ty(ty::Bound(
1930+
tcx.mk_bound(
19311931
ty::INNERMOST,
19321932
ty::BoundTy { var: ty::BoundVar::from_usize(bound_vars.len() - 1), kind },
1933-
))
1933+
)
19341934
.into()
19351935
}
19361936
GenericParamDefKind::Lifetime => {

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -603,8 +603,7 @@ fn gather_gat_bounds<'tcx, T: TypeFoldable<'tcx>>(
603603
// our example, the type was `Self`, which will also be
604604
// `Self` in the GAT.
605605
let ty_param = gat_generics.param_at(*ty_idx, tcx);
606-
let ty_param = tcx
607-
.mk_ty(ty::Param(ty::ParamTy { index: ty_param.index, name: ty_param.name }));
606+
let ty_param = tcx.mk_ty_param(ty_param.index, ty_param.name);
608607
// Same for the region. In our example, 'a corresponds
609608
// to the 'me parameter.
610609
let region_param = gat_generics.param_at(*region_a_idx, tcx);

compiler/rustc_hir_typeck/src/check.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,7 @@ fn check_lang_start_fn<'tcx>(
264264
// for example `start`'s generic should be a type parameter
265265
let generics = tcx.generics_of(def_id);
266266
let fn_generic = generics.param_at(0, tcx);
267-
let generic_tykind =
268-
ty::Param(ty::ParamTy { index: fn_generic.index, name: fn_generic.name });
269-
let generic_ty = tcx.mk_ty(generic_tykind);
267+
let generic_ty = tcx.mk_ty_param(fn_generic.index, fn_generic.name);
270268
let expected_fn_sig =
271269
tcx.mk_fn_sig([].iter(), &generic_ty, false, hir::Unsafety::Normal, Abi::Rust);
272270
let expected_ty = tcx.mk_fn_ptr(Binder::dummy(expected_fn_sig));

compiler/rustc_hir_typeck/src/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1429,7 +1429,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14291429

14301430
self.check_repeat_element_needs_copy_bound(element, count, element_ty);
14311431

1432-
tcx.mk_ty(ty::Array(t, count))
1432+
tcx.mk_array_with_const_len(t, count)
14331433
}
14341434

14351435
fn check_repeat_element_needs_copy_bound(

compiler/rustc_hir_typeck/src/pat.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1296,7 +1296,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12961296
)
12971297
});
12981298
let element_tys = tcx.mk_type_list(element_tys_iter);
1299-
let pat_ty = tcx.mk_ty(ty::Tuple(element_tys));
1299+
let pat_ty = tcx.intern_tup(element_tys);
13001300
if let Some(mut err) = self.demand_eqtype_pat_diag(span, expected, pat_ty, ti) {
13011301
let reported = err.emit();
13021302
// Walk subpatterns with an expected type of `err` in this case to silence

compiler/rustc_infer/src/infer/canonical/canonicalizer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
752752
self.fold_ty(bound_to)
753753
} else {
754754
let var = self.canonical_var(info, ty_var.into());
755-
self.tcx().mk_ty(ty::Bound(self.binder_index, var.into()))
755+
self.tcx().mk_bound(self.binder_index, var.into())
756756
}
757757
}
758758

compiler/rustc_infer/src/infer/canonical/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ impl<'tcx> InferCtxt<'tcx> {
124124
CanonicalVarKind::PlaceholderTy(ty::PlaceholderType { universe, name }) => {
125125
let universe_mapped = universe_map(universe);
126126
let placeholder_mapped = ty::PlaceholderType { universe: universe_mapped, name };
127-
self.tcx.mk_ty(ty::Placeholder(placeholder_mapped)).into()
127+
self.tcx.mk_placeholder(placeholder_mapped).into()
128128
}
129129

130130
CanonicalVarKind::Region(ui) => self

compiler/rustc_infer/src/infer/higher_ranked/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ impl<'tcx> InferCtxt<'tcx> {
8888
}))
8989
},
9090
types: &mut |bound_ty: ty::BoundTy| {
91-
self.tcx.mk_ty(ty::Placeholder(ty::PlaceholderType {
91+
self.tcx.mk_placeholder(ty::PlaceholderType {
9292
universe: next_universe,
9393
name: bound_ty.kind,
94-
}))
94+
})
9595
},
9696
consts: &mut |bound_var: ty::BoundVar, ty| {
9797
self.tcx

compiler/rustc_infer/src/infer/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2071,14 +2071,14 @@ fn replace_param_and_infer_substs_with_placeholder<'tcx>(
20712071

20722072
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
20732073
if let ty::Infer(_) = t.kind() {
2074-
self.tcx.mk_ty(ty::Placeholder(ty::PlaceholderType {
2074+
self.tcx.mk_placeholder(ty::PlaceholderType {
20752075
universe: ty::UniverseIndex::ROOT,
20762076
name: ty::BoundTyKind::Anon({
20772077
let idx = self.idx;
20782078
self.idx += 1;
20792079
idx
20802080
}),
2081-
}))
2081+
})
20822082
} else {
20832083
t.super_fold_with(self)
20842084
}

compiler/rustc_middle/src/infer/canonical.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,9 @@ impl<'tcx> CanonicalVarValues<'tcx> {
345345
var_values: tcx.mk_substs(infos.iter().enumerate().map(
346346
|(i, info)| -> ty::GenericArg<'tcx> {
347347
match info.kind {
348-
CanonicalVarKind::Ty(_) | CanonicalVarKind::PlaceholderTy(_) => tcx
349-
.mk_ty(ty::Bound(ty::INNERMOST, ty::BoundVar::from_usize(i).into()))
350-
.into(),
348+
CanonicalVarKind::Ty(_) | CanonicalVarKind::PlaceholderTy(_) => {
349+
tcx.mk_bound(ty::INNERMOST, ty::BoundVar::from_usize(i).into()).into()
350+
}
351351
CanonicalVarKind::Region(_) | CanonicalVarKind::PlaceholderRegion(_) => {
352352
let br = ty::BoundRegion {
353353
var: ty::BoundVar::from_usize(i),

compiler/rustc_middle/src/mir/tcx.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ impl<'tcx> Rvalue<'tcx> {
162162
match *self {
163163
Rvalue::Use(ref operand) => operand.ty(local_decls, tcx),
164164
Rvalue::Repeat(ref operand, count) => {
165-
tcx.mk_ty(ty::Array(operand.ty(local_decls, tcx), count))
165+
tcx.mk_array_with_const_len(operand.ty(local_decls, tcx), count)
166166
}
167167
Rvalue::ThreadLocalRef(did) => {
168168
let static_ty = tcx.type_of(did);

compiler/rustc_middle/src/ty/context.rs

+23-2
Original file line numberDiff line numberDiff line change
@@ -1636,6 +1636,7 @@ impl<'tcx> TyCtxt<'tcx> {
16361636
if *r == kind { r } else { self.mk_region(kind) }
16371637
}
16381638

1639+
// Avoid this in favour of more specific `mk_*` methods, where possible.
16391640
#[allow(rustc::usage_of_ty_tykind)]
16401641
#[inline]
16411642
pub fn mk_ty(self, st: TyKind<'tcx>) -> Ty<'tcx> {
@@ -1787,6 +1788,11 @@ impl<'tcx> TyCtxt<'tcx> {
17871788
self.mk_ty(Array(ty, ty::Const::from_usize(self, n)))
17881789
}
17891790

1791+
#[inline]
1792+
pub fn mk_array_with_const_len(self, ty: Ty<'tcx>, ct: Const<'tcx>) -> Ty<'tcx> {
1793+
self.mk_ty(Array(ty, ct))
1794+
}
1795+
17901796
#[inline]
17911797
pub fn mk_slice(self, ty: Ty<'tcx>) -> Ty<'tcx> {
17921798
self.mk_ty(Slice(ty))
@@ -1862,7 +1868,7 @@ impl<'tcx> TyCtxt<'tcx> {
18621868
item_def_id: DefId,
18631869
substs: impl IntoIterator<Item = impl Into<GenericArg<'tcx>>>,
18641870
) -> Ty<'tcx> {
1865-
self.mk_ty(Alias(ty::Projection, self.mk_alias_ty(item_def_id, substs)))
1871+
self.mk_alias(ty::Projection, self.mk_alias_ty(item_def_id, substs))
18661872
}
18671873

18681874
#[inline]
@@ -1970,9 +1976,24 @@ impl<'tcx> TyCtxt<'tcx> {
19701976
}
19711977
}
19721978

1979+
#[inline]
1980+
pub fn mk_bound(self, index: ty::DebruijnIndex, bound_ty: ty::BoundTy) -> Ty<'tcx> {
1981+
self.mk_ty(Bound(index, bound_ty))
1982+
}
1983+
1984+
#[inline]
1985+
pub fn mk_placeholder(self, placeholder: ty::PlaceholderType) -> Ty<'tcx> {
1986+
self.mk_ty(Placeholder(placeholder))
1987+
}
1988+
1989+
#[inline]
1990+
pub fn mk_alias(self, kind: ty::AliasKind, alias_ty: ty::AliasTy<'tcx>) -> Ty<'tcx> {
1991+
self.mk_ty(Alias(kind, alias_ty))
1992+
}
1993+
19731994
#[inline]
19741995
pub fn mk_opaque(self, def_id: DefId, substs: SubstsRef<'tcx>) -> Ty<'tcx> {
1975-
self.mk_ty(Alias(ty::Opaque, self.mk_alias_ty(def_id, substs)))
1996+
self.mk_alias(ty::Opaque, self.mk_alias_ty(def_id, substs))
19761997
}
19771998

19781999
pub fn mk_place_field(self, place: Place<'tcx>, f: Field, ty: Ty<'tcx>) -> Place<'tcx> {

compiler/rustc_middle/src/ty/fold.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -562,10 +562,7 @@ impl<'tcx> TyCtxt<'tcx> {
562562
))
563563
},
564564
types: &mut |t: ty::BoundTy| {
565-
self.mk_ty(ty::Bound(
566-
ty::INNERMOST,
567-
ty::BoundTy { var: shift_bv(t.var), kind: t.kind },
568-
))
565+
self.mk_bound(ty::INNERMOST, ty::BoundTy { var: shift_bv(t.var), kind: t.kind })
569566
},
570567
consts: &mut |c, ty: Ty<'tcx>| {
571568
self.mk_const(ty::ConstKind::Bound(ty::INNERMOST, shift_bv(c)), ty)
@@ -614,7 +611,7 @@ impl<'tcx> TyCtxt<'tcx> {
614611
ty::BoundVariableKind::Ty(ty::BoundTyKind::Anon(index as u32))
615612
})
616613
.expect_ty();
617-
self.tcx.mk_ty(ty::Bound(ty::INNERMOST, BoundTy { var, kind }))
614+
self.tcx.mk_bound(ty::INNERMOST, BoundTy { var, kind })
618615
}
619616
fn replace_const(&mut self, bv: ty::BoundVar, ty: Ty<'tcx>) -> ty::Const<'tcx> {
620617
let entry = self.map.entry(bv);
@@ -684,7 +681,7 @@ impl<'tcx> TypeFolder<'tcx> for Shifter<'tcx> {
684681
match *ty.kind() {
685682
ty::Bound(debruijn, bound_ty) if debruijn >= self.current_index => {
686683
let debruijn = debruijn.shifted_in(self.amount);
687-
self.tcx.mk_ty(ty::Bound(debruijn, bound_ty))
684+
self.tcx.mk_bound(debruijn, bound_ty)
688685
}
689686

690687
_ if ty.has_vars_bound_at_or_above(self.current_index) => ty.super_fold_with(self),

compiler/rustc_middle/src/ty/relate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ pub fn super_relate_tys<'tcx, R: TypeRelation<'tcx>>(
502502
(&ty::Array(a_t, sz_a), &ty::Array(b_t, sz_b)) => {
503503
let t = relation.relate(a_t, b_t)?;
504504
match relation.relate(sz_a, sz_b) {
505-
Ok(sz) => Ok(tcx.mk_ty(ty::Array(t, sz))),
505+
Ok(sz) => Ok(tcx.mk_array_with_const_len(t, sz)),
506506
Err(err) => {
507507
// Check whether the lengths are both concrete/known values,
508508
// but are unequal, for better diagnostics.

compiler/rustc_middle/src/ty/sty.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1171,7 +1171,7 @@ impl<'tcx> FallibleTypeFolder<'tcx> for SkipBindersAt<'tcx> {
11711171
if index == self.index {
11721172
Err(())
11731173
} else {
1174-
Ok(self.tcx().mk_ty(ty::Bound(index.shifted_out(1), bv)))
1174+
Ok(self.tcx().mk_bound(index.shifted_out(1), bv))
11751175
}
11761176
} else {
11771177
ty.try_super_fold_with(self)
@@ -1260,7 +1260,7 @@ impl<'tcx> AliasTy<'tcx> {
12601260
}
12611261

12621262
pub fn to_ty(self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
1263-
tcx.mk_ty(ty::Alias(self.kind(tcx), self))
1263+
tcx.mk_alias(self.kind(tcx), self)
12641264
}
12651265
}
12661266

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3572,7 +3572,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
35723572
{
35733573
type_diffs = vec![
35743574
Sorts(ty::error::ExpectedFound {
3575-
expected: self.tcx.mk_ty(ty::Alias(ty::Projection, where_pred.skip_binder().projection_ty)),
3575+
expected: self.tcx.mk_alias(ty::Projection, where_pred.skip_binder().projection_ty),
35763576
found,
35773577
}),
35783578
];

compiler/rustc_trait_selection/src/traits/project.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ impl<'tcx> TypeFolder<'tcx> for BoundVarReplacer<'_, 'tcx> {
785785
let universe = self.universe_for(debruijn);
786786
let p = ty::PlaceholderType { universe, name: bound_ty.kind };
787787
self.mapped_types.insert(p, bound_ty);
788-
self.infcx.tcx.mk_ty(ty::Placeholder(p))
788+
self.infcx.tcx.mk_placeholder(p)
789789
}
790790
_ if t.has_vars_bound_at_or_above(self.current_index) => t.super_fold_with(self),
791791
_ => t,
@@ -915,7 +915,7 @@ impl<'tcx> TypeFolder<'tcx> for PlaceholderReplacer<'_, 'tcx> {
915915
let db = ty::DebruijnIndex::from_usize(
916916
self.universe_indices.len() - index + self.current_index.as_usize() - 1,
917917
);
918-
self.tcx().mk_ty(ty::Bound(db, *replace_var))
918+
self.tcx().mk_bound(db, *replace_var)
919919
}
920920
None => ty,
921921
}

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -527,13 +527,13 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
527527
let kind = ty::BoundTyKind::Param(param.def_id, param.name);
528528
let bound_var = ty::BoundVariableKind::Ty(kind);
529529
bound_vars.push(bound_var);
530-
tcx.mk_ty(ty::Bound(
530+
tcx.mk_bound(
531531
ty::INNERMOST,
532532
ty::BoundTy {
533533
var: ty::BoundVar::from_usize(bound_vars.len() - 1),
534534
kind,
535535
},
536-
))
536+
)
537537
.into()
538538
}
539539
GenericParamDefKind::Lifetime => {

compiler/rustc_traits/src/chalk/db.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -588,10 +588,7 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
588588
_id: chalk_ir::OpaqueTyId<RustInterner<'tcx>>,
589589
) -> chalk_ir::Ty<RustInterner<'tcx>> {
590590
// FIXME(chalk): actually get hidden ty
591-
self.interner
592-
.tcx
593-
.mk_ty(ty::Tuple(self.interner.tcx.intern_type_list(&[])))
594-
.lower_into(self.interner)
591+
self.interner.tcx.types.unit.lower_into(self.interner)
595592
}
596593

597594
fn closure_kind(
@@ -721,13 +718,13 @@ impl<'tcx> chalk_ir::UnificationDatabase<RustInterner<'tcx>> for RustIrDatabase<
721718
fn bound_vars_for_item(tcx: TyCtxt<'_>, def_id: DefId) -> SubstsRef<'_> {
722719
InternalSubsts::for_item(tcx, def_id, |param, substs| match param.kind {
723720
ty::GenericParamDefKind::Type { .. } => tcx
724-
.mk_ty(ty::Bound(
721+
.mk_bound(
725722
ty::INNERMOST,
726723
ty::BoundTy {
727724
var: ty::BoundVar::from(param.index),
728725
kind: ty::BoundTyKind::Param(param.def_id, param.name),
729726
},
730-
))
727+
)
731728
.into(),
732729

733730
ty::GenericParamDefKind::Lifetime => {
@@ -790,10 +787,9 @@ impl<'tcx> ty::TypeFolder<'tcx> for ReplaceOpaqueTyFolder<'tcx> {
790787
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
791788
if let ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs, .. }) = *ty.kind() {
792789
if def_id == self.opaque_ty_id.0 && substs == self.identity_substs {
793-
return self.tcx.mk_ty(ty::Bound(
794-
self.binder_index,
795-
ty::BoundTy::from(ty::BoundVar::from_u32(0)),
796-
));
790+
return self
791+
.tcx
792+
.mk_bound(self.binder_index, ty::BoundTy::from(ty::BoundVar::from_u32(0)));
797793
}
798794
}
799795
ty

0 commit comments

Comments
 (0)