Reduce direct mk_ty usage.

We use more specific `mk_*` functions in most places, might as well use
them as much as possible.
This commit is contained in:
Nicholas Nethercote 2023-02-08 12:28:03 +11:00
parent 6248bbbf26
commit 7a72560154
25 changed files with 92 additions and 82 deletions

View File

@ -385,10 +385,9 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
calculate_debuginfo_offset(bx, local, &var, base);
// Create a variable which will be a pointer to the actual value
let ptr_ty = bx.tcx().mk_ty(ty::RawPtr(ty::TypeAndMut {
mutbl: mir::Mutability::Mut,
ty: place.layout.ty,
}));
let ptr_ty = bx
.tcx()
.mk_ptr(ty::TypeAndMut { mutbl: mir::Mutability::Mut, ty: place.layout.ty });
let ptr_layout = bx.layout_of(ptr_ty);
let alloca = PlaceRef::alloca(bx, ptr_layout);
bx.set_var_name(alloca.llval, &(var.name.to_string() + ".dbg.spill"));

View File

@ -193,7 +193,7 @@ fn get_info_on_unsized_field<'tcx>(
// Have to adjust type for ty::Str
let unsized_inner_ty = match unsized_inner_ty.kind() {
ty::Str => tcx.mk_ty(ty::Uint(ty::UintTy::U8)),
ty::Str => tcx.types.u8,
_ => unsized_inner_ty,
};
@ -216,7 +216,7 @@ fn create_pointee_place<'tcx>(
let (unsized_inner_ty, num_elems) = get_info_on_unsized_field(ty, valtree, tcx);
let unsized_inner_ty = match unsized_inner_ty.kind() {
ty::Str => tcx.mk_ty(ty::Uint(ty::UintTy::U8)),
ty::Str => tcx.types.u8,
_ => unsized_inner_ty,
};
let unsized_inner_ty_size =

View File

@ -217,10 +217,10 @@ impl Qualif for CustomEq {
fn in_adt_inherently<'tcx>(
cx: &ConstCx<'_, 'tcx>,
adt: AdtDef<'tcx>,
def: AdtDef<'tcx>,
substs: SubstsRef<'tcx>,
) -> bool {
let ty = cx.tcx.mk_ty(ty::Adt(adt, substs));
let ty = cx.tcx.mk_adt(def, substs);
!ty.is_structural_eq_shallow(cx.tcx)
}
}

View File

@ -1250,7 +1250,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
//
// Calling `skip_binder` is okay, because `add_bounds` expects the `param_ty`
// parameter to have a skipped binder.
let param_ty = tcx.mk_ty(ty::Alias(ty::Projection, projection_ty.skip_binder()));
let param_ty = tcx.mk_alias(ty::Projection, projection_ty.skip_binder());
self.add_bounds(param_ty, ast_bounds.iter(), bounds, candidate.bound_vars());
}
}
@ -2930,7 +2930,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
}
};
tcx.mk_ty(ty::Array(self.ast_ty_to_ty(ty), length))
tcx.mk_array_with_const_len(self.ast_ty_to_ty(ty), length)
}
hir::TyKind::Typeof(e) => {
let ty_erased = tcx.type_of(e.def_id);

View File

@ -1927,10 +1927,10 @@ pub(super) fn check_type_bounds<'tcx>(
let kind = ty::BoundTyKind::Param(param.def_id, param.name);
let bound_var = ty::BoundVariableKind::Ty(kind);
bound_vars.push(bound_var);
tcx.mk_ty(ty::Bound(
tcx.mk_bound(
ty::INNERMOST,
ty::BoundTy { var: ty::BoundVar::from_usize(bound_vars.len() - 1), kind },
))
)
.into()
}
GenericParamDefKind::Lifetime => {

View File

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

View File

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

View File

@ -1429,7 +1429,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.check_repeat_element_needs_copy_bound(element, count, element_ty);
tcx.mk_ty(ty::Array(t, count))
tcx.mk_array_with_const_len(t, count)
}
fn check_repeat_element_needs_copy_bound(

View File

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

View File

@ -752,7 +752,7 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
self.fold_ty(bound_to)
} else {
let var = self.canonical_var(info, ty_var.into());
self.tcx().mk_ty(ty::Bound(self.binder_index, var.into()))
self.tcx().mk_bound(self.binder_index, var.into())
}
}

View File

@ -124,7 +124,7 @@ impl<'tcx> InferCtxt<'tcx> {
CanonicalVarKind::PlaceholderTy(ty::PlaceholderType { universe, name }) => {
let universe_mapped = universe_map(universe);
let placeholder_mapped = ty::PlaceholderType { universe: universe_mapped, name };
self.tcx.mk_ty(ty::Placeholder(placeholder_mapped)).into()
self.tcx.mk_placeholder(placeholder_mapped).into()
}
CanonicalVarKind::Region(ui) => self

View File

@ -88,10 +88,10 @@ impl<'tcx> InferCtxt<'tcx> {
}))
},
types: &mut |bound_ty: ty::BoundTy| {
self.tcx.mk_ty(ty::Placeholder(ty::PlaceholderType {
self.tcx.mk_placeholder(ty::PlaceholderType {
universe: next_universe,
name: bound_ty.kind,
}))
})
},
consts: &mut |bound_var: ty::BoundVar, ty| {
self.tcx

View File

@ -2071,14 +2071,14 @@ fn replace_param_and_infer_substs_with_placeholder<'tcx>(
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
if let ty::Infer(_) = t.kind() {
self.tcx.mk_ty(ty::Placeholder(ty::PlaceholderType {
self.tcx.mk_placeholder(ty::PlaceholderType {
universe: ty::UniverseIndex::ROOT,
name: ty::BoundTyKind::Anon({
let idx = self.idx;
self.idx += 1;
idx
}),
}))
})
} else {
t.super_fold_with(self)
}

View File

@ -345,9 +345,9 @@ impl<'tcx> CanonicalVarValues<'tcx> {
var_values: tcx.mk_substs(infos.iter().enumerate().map(
|(i, info)| -> ty::GenericArg<'tcx> {
match info.kind {
CanonicalVarKind::Ty(_) | CanonicalVarKind::PlaceholderTy(_) => tcx
.mk_ty(ty::Bound(ty::INNERMOST, ty::BoundVar::from_usize(i).into()))
.into(),
CanonicalVarKind::Ty(_) | CanonicalVarKind::PlaceholderTy(_) => {
tcx.mk_bound(ty::INNERMOST, ty::BoundVar::from_usize(i).into()).into()
}
CanonicalVarKind::Region(_) | CanonicalVarKind::PlaceholderRegion(_) => {
let br = ty::BoundRegion {
var: ty::BoundVar::from_usize(i),

View File

@ -162,7 +162,7 @@ impl<'tcx> Rvalue<'tcx> {
match *self {
Rvalue::Use(ref operand) => operand.ty(local_decls, tcx),
Rvalue::Repeat(ref operand, count) => {
tcx.mk_ty(ty::Array(operand.ty(local_decls, tcx), count))
tcx.mk_array_with_const_len(operand.ty(local_decls, tcx), count)
}
Rvalue::ThreadLocalRef(did) => {
let static_ty = tcx.type_of(did);

View File

@ -1636,6 +1636,7 @@ impl<'tcx> TyCtxt<'tcx> {
if *r == kind { r } else { self.mk_region(kind) }
}
// Avoid this in favour of more specific `mk_*` methods, where possible.
#[allow(rustc::usage_of_ty_tykind)]
#[inline]
pub fn mk_ty(self, st: TyKind<'tcx>) -> Ty<'tcx> {
@ -1787,6 +1788,11 @@ impl<'tcx> TyCtxt<'tcx> {
self.mk_ty(Array(ty, ty::Const::from_usize(self, n)))
}
#[inline]
pub fn mk_array_with_const_len(self, ty: Ty<'tcx>, ct: Const<'tcx>) -> Ty<'tcx> {
self.mk_ty(Array(ty, ct))
}
#[inline]
pub fn mk_slice(self, ty: Ty<'tcx>) -> Ty<'tcx> {
self.mk_ty(Slice(ty))
@ -1862,7 +1868,7 @@ impl<'tcx> TyCtxt<'tcx> {
item_def_id: DefId,
substs: impl IntoIterator<Item = impl Into<GenericArg<'tcx>>>,
) -> Ty<'tcx> {
self.mk_ty(Alias(ty::Projection, self.mk_alias_ty(item_def_id, substs)))
self.mk_alias(ty::Projection, self.mk_alias_ty(item_def_id, substs))
}
#[inline]
@ -1970,9 +1976,24 @@ impl<'tcx> TyCtxt<'tcx> {
}
}
#[inline]
pub fn mk_bound(self, index: ty::DebruijnIndex, bound_ty: ty::BoundTy) -> Ty<'tcx> {
self.mk_ty(Bound(index, bound_ty))
}
#[inline]
pub fn mk_placeholder(self, placeholder: ty::PlaceholderType) -> Ty<'tcx> {
self.mk_ty(Placeholder(placeholder))
}
#[inline]
pub fn mk_alias(self, kind: ty::AliasKind, alias_ty: ty::AliasTy<'tcx>) -> Ty<'tcx> {
self.mk_ty(Alias(kind, alias_ty))
}
#[inline]
pub fn mk_opaque(self, def_id: DefId, substs: SubstsRef<'tcx>) -> Ty<'tcx> {
self.mk_ty(Alias(ty::Opaque, self.mk_alias_ty(def_id, substs)))
self.mk_alias(ty::Opaque, self.mk_alias_ty(def_id, substs))
}
pub fn mk_place_field(self, place: Place<'tcx>, f: Field, ty: Ty<'tcx>) -> Place<'tcx> {

View File

@ -562,10 +562,7 @@ impl<'tcx> TyCtxt<'tcx> {
))
},
types: &mut |t: ty::BoundTy| {
self.mk_ty(ty::Bound(
ty::INNERMOST,
ty::BoundTy { var: shift_bv(t.var), kind: t.kind },
))
self.mk_bound(ty::INNERMOST, ty::BoundTy { var: shift_bv(t.var), kind: t.kind })
},
consts: &mut |c, ty: Ty<'tcx>| {
self.mk_const(ty::ConstKind::Bound(ty::INNERMOST, shift_bv(c)), ty)
@ -614,7 +611,7 @@ impl<'tcx> TyCtxt<'tcx> {
ty::BoundVariableKind::Ty(ty::BoundTyKind::Anon(index as u32))
})
.expect_ty();
self.tcx.mk_ty(ty::Bound(ty::INNERMOST, BoundTy { var, kind }))
self.tcx.mk_bound(ty::INNERMOST, BoundTy { var, kind })
}
fn replace_const(&mut self, bv: ty::BoundVar, ty: Ty<'tcx>) -> ty::Const<'tcx> {
let entry = self.map.entry(bv);
@ -684,7 +681,7 @@ impl<'tcx> TypeFolder<'tcx> for Shifter<'tcx> {
match *ty.kind() {
ty::Bound(debruijn, bound_ty) if debruijn >= self.current_index => {
let debruijn = debruijn.shifted_in(self.amount);
self.tcx.mk_ty(ty::Bound(debruijn, bound_ty))
self.tcx.mk_bound(debruijn, bound_ty)
}
_ if ty.has_vars_bound_at_or_above(self.current_index) => ty.super_fold_with(self),

View File

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

View File

@ -1171,7 +1171,7 @@ impl<'tcx> FallibleTypeFolder<'tcx> for SkipBindersAt<'tcx> {
if index == self.index {
Err(())
} else {
Ok(self.tcx().mk_ty(ty::Bound(index.shifted_out(1), bv)))
Ok(self.tcx().mk_bound(index.shifted_out(1), bv))
}
} else {
ty.try_super_fold_with(self)
@ -1260,7 +1260,7 @@ impl<'tcx> AliasTy<'tcx> {
}
pub fn to_ty(self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
tcx.mk_ty(ty::Alias(self.kind(tcx), self))
tcx.mk_alias(self.kind(tcx), self)
}
}

View File

@ -3572,7 +3572,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
{
type_diffs = vec![
Sorts(ty::error::ExpectedFound {
expected: self.tcx.mk_ty(ty::Alias(ty::Projection, where_pred.skip_binder().projection_ty)),
expected: self.tcx.mk_alias(ty::Projection, where_pred.skip_binder().projection_ty),
found,
}),
];

View File

@ -785,7 +785,7 @@ impl<'tcx> TypeFolder<'tcx> for BoundVarReplacer<'_, 'tcx> {
let universe = self.universe_for(debruijn);
let p = ty::PlaceholderType { universe, name: bound_ty.kind };
self.mapped_types.insert(p, bound_ty);
self.infcx.tcx.mk_ty(ty::Placeholder(p))
self.infcx.tcx.mk_placeholder(p)
}
_ if t.has_vars_bound_at_or_above(self.current_index) => t.super_fold_with(self),
_ => t,
@ -915,7 +915,7 @@ impl<'tcx> TypeFolder<'tcx> for PlaceholderReplacer<'_, 'tcx> {
let db = ty::DebruijnIndex::from_usize(
self.universe_indices.len() - index + self.current_index.as_usize() - 1,
);
self.tcx().mk_ty(ty::Bound(db, *replace_var))
self.tcx().mk_bound(db, *replace_var)
}
None => ty,
}

View File

@ -527,13 +527,13 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
let kind = ty::BoundTyKind::Param(param.def_id, param.name);
let bound_var = ty::BoundVariableKind::Ty(kind);
bound_vars.push(bound_var);
tcx.mk_ty(ty::Bound(
tcx.mk_bound(
ty::INNERMOST,
ty::BoundTy {
var: ty::BoundVar::from_usize(bound_vars.len() - 1),
kind,
},
))
)
.into()
}
GenericParamDefKind::Lifetime => {

View File

@ -588,10 +588,7 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
_id: chalk_ir::OpaqueTyId<RustInterner<'tcx>>,
) -> chalk_ir::Ty<RustInterner<'tcx>> {
// FIXME(chalk): actually get hidden ty
self.interner
.tcx
.mk_ty(ty::Tuple(self.interner.tcx.intern_type_list(&[])))
.lower_into(self.interner)
self.interner.tcx.types.unit.lower_into(self.interner)
}
fn closure_kind(
@ -721,13 +718,13 @@ impl<'tcx> chalk_ir::UnificationDatabase<RustInterner<'tcx>> for RustIrDatabase<
fn bound_vars_for_item(tcx: TyCtxt<'_>, def_id: DefId) -> SubstsRef<'_> {
InternalSubsts::for_item(tcx, def_id, |param, substs| match param.kind {
ty::GenericParamDefKind::Type { .. } => tcx
.mk_ty(ty::Bound(
.mk_bound(
ty::INNERMOST,
ty::BoundTy {
var: ty::BoundVar::from(param.index),
kind: ty::BoundTyKind::Param(param.def_id, param.name),
},
))
)
.into(),
ty::GenericParamDefKind::Lifetime => {
@ -790,10 +787,9 @@ impl<'tcx> ty::TypeFolder<'tcx> for ReplaceOpaqueTyFolder<'tcx> {
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
if let ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs, .. }) = *ty.kind() {
if def_id == self.opaque_ty_id.0 && substs == self.identity_substs {
return self.tcx.mk_ty(ty::Bound(
self.binder_index,
ty::BoundTy::from(ty::BoundVar::from_u32(0)),
));
return self
.tcx
.mk_bound(self.binder_index, ty::BoundTy::from(ty::BoundVar::from_u32(0)));
}
}
ty

View File

@ -677,11 +677,11 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::Binders<chalk_ir::QuantifiedWhereClauses<Ru
// shifted in by one so that they are still escaping.
let predicates = ty::fold::shift_vars(interner.tcx, self, 1);
let self_ty = interner.tcx.mk_ty(ty::Bound(
let self_ty = interner.tcx.mk_bound(
// This is going to be wrapped in a binder
ty::DebruijnIndex::from_usize(1),
ty::BoundTy { var: ty::BoundVar::from_usize(0), kind: ty::BoundTyKind::Anon(0) },
));
);
let where_clauses = predicates.into_iter().map(|predicate| {
let (predicate, binders, _named_regions) =
collect_bound_vars(interner, interner.tcx, predicate);
@ -1077,18 +1077,18 @@ impl<'tcx> TypeFolder<'tcx> for ParamsSubstitutor<'tcx> {
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
match *t.kind() {
ty::Param(param) => match self.list.iter().position(|r| r == &param) {
Some(idx) => self.tcx.mk_ty(ty::Placeholder(ty::PlaceholderType {
Some(idx) => self.tcx.mk_placeholder(ty::PlaceholderType {
universe: ty::UniverseIndex::from_usize(0),
name: ty::BoundTyKind::Anon(idx as u32),
})),
}),
None => {
self.list.push(param);
let idx = self.list.len() - 1 + self.next_ty_placeholder;
self.params.insert(idx as u32, param);
self.tcx.mk_ty(ty::Placeholder(ty::PlaceholderType {
self.tcx.mk_placeholder(ty::PlaceholderType {
universe: ty::UniverseIndex::from_usize(0),
name: ty::BoundTyKind::Anon(idx as u32),
}))
})
}
},
_ => t.super_fold_with(self),
@ -1147,7 +1147,7 @@ impl<'tcx> TypeFolder<'tcx> for ReverseParamsSubstitutor<'tcx> {
match *t.kind() {
ty::Placeholder(ty::PlaceholderType { universe: ty::UniverseIndex::ROOT, name }) => {
match self.params.get(&name.expect_anon()) {
Some(param) => self.tcx.mk_ty(ty::Param(*param)),
Some(&ty::ParamTy { index, name }) => self.tcx.mk_ty_param(index, name),
None => t,
}
}

View File

@ -516,27 +516,27 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
// FIXME: Only simple types are supported here, see if we can support
// other types such as Tuple, Array, Slice, etc.
// See https://github.com/rust-lang/rust/issues/90703#issuecomment-1004263455
Some(tcx.mk_ty(match prim {
Bool => ty::Bool,
Str => ty::Str,
Char => ty::Char,
Never => ty::Never,
I8 => ty::Int(ty::IntTy::I8),
I16 => ty::Int(ty::IntTy::I16),
I32 => ty::Int(ty::IntTy::I32),
I64 => ty::Int(ty::IntTy::I64),
I128 => ty::Int(ty::IntTy::I128),
Isize => ty::Int(ty::IntTy::Isize),
F32 => ty::Float(ty::FloatTy::F32),
F64 => ty::Float(ty::FloatTy::F64),
U8 => ty::Uint(ty::UintTy::U8),
U16 => ty::Uint(ty::UintTy::U16),
U32 => ty::Uint(ty::UintTy::U32),
U64 => ty::Uint(ty::UintTy::U64),
U128 => ty::Uint(ty::UintTy::U128),
Usize => ty::Uint(ty::UintTy::Usize),
Some(match prim {
Bool => tcx.types.bool,
Str => tcx.types.str_,
Char => tcx.types.char,
Never => tcx.types.never,
I8 => tcx.types.i8,
I16 => tcx.types.i16,
I32 => tcx.types.i32,
I64 => tcx.types.i64,
I128 => tcx.types.i128,
Isize => tcx.types.isize,
F32 => tcx.types.f32,
F64 => tcx.types.f64,
U8 => tcx.types.u8,
U16 => tcx.types.u16,
U32 => tcx.types.u32,
U64 => tcx.types.u64,
U128 => tcx.types.u128,
Usize => tcx.types.usize,
_ => return None,
}))
})
}
/// Resolve an associated item, returning its containing page's `Res`