Replace mk_const
with Const::new_x
methods
This commit is contained in:
parent
cd68ead9ec
commit
ddbc774e74
32 changed files with 219 additions and 134 deletions
|
@ -386,11 +386,10 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
|
|||
.type_of(param.def_id)
|
||||
.no_bound_vars()
|
||||
.expect("ct params cannot have early bound vars");
|
||||
tcx.mk_const(
|
||||
ty::ConstKind::Bound(
|
||||
ty::INNERMOST,
|
||||
ty::BoundVar::from_usize(num_bound_vars),
|
||||
),
|
||||
ty::Const::new_bound(
|
||||
tcx,
|
||||
ty::INNERMOST,
|
||||
ty::BoundVar::from_usize(num_bound_vars),
|
||||
ty,
|
||||
)
|
||||
.into()
|
||||
|
|
|
@ -1970,7 +1970,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
|
|||
assert!(!ct.ty().has_escaping_bound_vars());
|
||||
|
||||
match ct.kind() {
|
||||
ty::ConstKind::Bound(_, bv) => self.tcx.mk_const(
|
||||
ty::ConstKind::Bound(_, bv) => ty::Const::new_placeholder(
|
||||
self.tcx,
|
||||
ty::PlaceholderConst { universe: self.universe, bound: bv },
|
||||
ct.ty(),
|
||||
),
|
||||
|
|
|
@ -2047,11 +2047,10 @@ pub(super) fn check_type_bounds<'tcx>(
|
|||
GenericParamDefKind::Const { .. } => {
|
||||
let bound_var = ty::BoundVariableKind::Const;
|
||||
bound_vars.push(bound_var);
|
||||
tcx.mk_const(
|
||||
ty::ConstKind::Bound(
|
||||
ty::INNERMOST,
|
||||
ty::BoundVar::from_usize(bound_vars.len() - 1),
|
||||
),
|
||||
ty::Const::new_bound(
|
||||
tcx,
|
||||
ty::INNERMOST,
|
||||
ty::BoundVar::from_usize(bound_vars.len() - 1),
|
||||
tcx.type_of(param.def_id)
|
||||
.no_bound_vars()
|
||||
.expect("const parameter types cannot be generic"),
|
||||
|
|
|
@ -243,9 +243,12 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
|
|||
let name = param.name.ident().name;
|
||||
let param_const = ty::ParamConst::new(index, name);
|
||||
|
||||
let ct_ty = tcx.type_of(param.def_id.to_def_id()).subst_identity();
|
||||
let ct_ty = tcx
|
||||
.type_of(param.def_id.to_def_id())
|
||||
.no_bound_vars()
|
||||
.expect("const parameters cannot be generic");
|
||||
|
||||
let ct = tcx.mk_const(param_const, ct_ty);
|
||||
let ct = ty::Const::new_param(tcx, param_const, ct_ty);
|
||||
|
||||
predicates.insert((
|
||||
ty::ClauseKind::ConstArgHasType(ct, ct_ty).to_predicate(tcx),
|
||||
|
|
|
@ -497,7 +497,7 @@ impl<'cx, 'tcx> TypeFolder<TyCtxt<'tcx>> for Canonicalizer<'cx, 'tcx> {
|
|||
// any equated inference vars correctly!
|
||||
let root_vid = self.infcx.root_const_var(vid);
|
||||
if root_vid != vid {
|
||||
ct = self.infcx.tcx.mk_const(ty::InferConst::Var(root_vid), ct.ty());
|
||||
ct = ty::Const::new_var(self.infcx.tcx, root_vid, ct.ty());
|
||||
vid = root_vid;
|
||||
}
|
||||
|
||||
|
@ -804,10 +804,7 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
|
|||
self.fold_const(bound_to)
|
||||
} else {
|
||||
let var = self.canonical_var(info, const_var.into());
|
||||
self.interner().mk_const(
|
||||
ty::ConstKind::Bound(self.binder_index, var),
|
||||
self.fold_ty(const_var.ty()),
|
||||
)
|
||||
ty::Const::new_bound(self.tcx, self.binder_index, var, self.fold_ty(const_var.ty()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -155,7 +155,7 @@ impl<'tcx> InferCtxt<'tcx> {
|
|||
CanonicalVarKind::PlaceholderConst(ty::PlaceholderConst { universe, bound }, ty) => {
|
||||
let universe_mapped = universe_map(universe);
|
||||
let placeholder_mapped = ty::PlaceholderConst { universe: universe_mapped, bound };
|
||||
self.tcx.mk_const(placeholder_mapped, ty).into()
|
||||
ty::Const::new_placeholder(self.tcx, placeholder_mapped, ty).into()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -95,7 +95,7 @@ impl<'a, 'tcx> TypeFreshener<'a, 'tcx> {
|
|||
Entry::Vacant(entry) => {
|
||||
let index = self.const_freshen_count;
|
||||
self.const_freshen_count += 1;
|
||||
let ct = self.infcx.tcx.mk_const(freshener(index), ty);
|
||||
let ct = ty::Const::new_infer(self.infcx.tcx, freshener(index), ty);
|
||||
entry.insert(ct);
|
||||
ct
|
||||
}
|
||||
|
|
|
@ -398,7 +398,7 @@ where
|
|||
origin: var_value.origin,
|
||||
val: ConstVariableValue::Unknown { universe: self.for_universe },
|
||||
});
|
||||
Ok(self.tcx().mk_const(new_var_id, c.ty()))
|
||||
Ok(ty::Const::new_var(self.tcx(), new_var_id, c.ty()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -412,7 +412,11 @@ where
|
|||
substs,
|
||||
substs,
|
||||
)?;
|
||||
Ok(self.tcx().mk_const(ty::UnevaluatedConst { def, substs }, c.ty()))
|
||||
Ok(ty::Const::new_unevaluated(
|
||||
self.tcx(),
|
||||
ty::UnevaluatedConst { def, substs },
|
||||
c.ty(),
|
||||
))
|
||||
}
|
||||
ty::ConstKind::Placeholder(placeholder) => {
|
||||
if self.for_universe.can_name(placeholder.universe) {
|
||||
|
|
|
@ -94,7 +94,8 @@ impl<'tcx> InferCtxt<'tcx> {
|
|||
})
|
||||
},
|
||||
consts: &mut |bound_var: ty::BoundVar, ty| {
|
||||
self.tcx.mk_const(
|
||||
ty::Const::new_placeholder(
|
||||
self.tcx,
|
||||
ty::PlaceholderConst { universe: next_universe, bound: bound_var },
|
||||
ty,
|
||||
)
|
||||
|
|
|
@ -999,7 +999,7 @@ impl<'tcx> InferCtxt<'tcx> {
|
|||
}
|
||||
|
||||
pub fn next_const_var(&self, ty: Ty<'tcx>, origin: ConstVariableOrigin) -> ty::Const<'tcx> {
|
||||
self.tcx.mk_const(self.next_const_var_id(origin), ty)
|
||||
ty::Const::new_var(self.tcx, self.next_const_var_id(origin), ty)
|
||||
}
|
||||
|
||||
pub fn next_const_var_in_universe(
|
||||
|
@ -1013,7 +1013,7 @@ impl<'tcx> InferCtxt<'tcx> {
|
|||
.borrow_mut()
|
||||
.const_unification_table()
|
||||
.new_key(ConstVarValue { origin, val: ConstVariableValue::Unknown { universe } });
|
||||
self.tcx.mk_const(vid, ty)
|
||||
ty::Const::new_var(self.tcx, vid, ty)
|
||||
}
|
||||
|
||||
pub fn next_const_var_id(&self, origin: ConstVariableOrigin) -> ConstVid<'tcx> {
|
||||
|
@ -1131,15 +1131,15 @@ impl<'tcx> InferCtxt<'tcx> {
|
|||
origin,
|
||||
val: ConstVariableValue::Unknown { universe: self.universe() },
|
||||
});
|
||||
self.tcx
|
||||
.mk_const(
|
||||
const_var_id,
|
||||
self.tcx
|
||||
.type_of(param.def_id)
|
||||
.no_bound_vars()
|
||||
.expect("const parameter types cannot be generic"),
|
||||
)
|
||||
.into()
|
||||
ty::Const::new_var(
|
||||
self.tcx,
|
||||
const_var_id,
|
||||
self.tcx
|
||||
.type_of(param.def_id)
|
||||
.no_bound_vars()
|
||||
.expect("const parameter types cannot be generic"),
|
||||
)
|
||||
.into()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1472,7 +1472,7 @@ impl<'tcx> InferCtxt<'tcx> {
|
|||
span: Option<Span>,
|
||||
) -> Result<ty::Const<'tcx>, ErrorHandled> {
|
||||
match self.const_eval_resolve(param_env, unevaluated, span) {
|
||||
Ok(Some(val)) => Ok(self.tcx.mk_const(val, ty)),
|
||||
Ok(Some(val)) => Ok(ty::Const::new_value(self.tcx, val, ty)),
|
||||
Ok(None) => {
|
||||
let tcx = self.tcx;
|
||||
let def_id = unevaluated.def;
|
||||
|
@ -1964,7 +1964,8 @@ fn replace_param_and_infer_substs_with_placeholder<'tcx>(
|
|||
if ty.has_non_region_param() || ty.has_non_region_infer() {
|
||||
bug!("const `{c}`'s type should not reference params or types");
|
||||
}
|
||||
self.tcx.mk_const(
|
||||
ty::Const::new_placeholder(
|
||||
self.tcx,
|
||||
ty::PlaceholderConst {
|
||||
universe: ty::UniverseIndex::ROOT,
|
||||
bound: ty::BoundVar::from_u32({
|
||||
|
|
|
@ -443,12 +443,13 @@ impl<'tcx> CanonicalVarValues<'tcx> {
|
|||
ty::Region::new_late_bound(tcx, ty::INNERMOST, br).into()
|
||||
}
|
||||
CanonicalVarKind::Const(_, ty)
|
||||
| CanonicalVarKind::PlaceholderConst(_, ty) => tcx
|
||||
.mk_const(
|
||||
ty::ConstKind::Bound(ty::INNERMOST, ty::BoundVar::from_usize(i)),
|
||||
ty,
|
||||
)
|
||||
.into(),
|
||||
| CanonicalVarKind::PlaceholderConst(_, ty) => ty::Const::new_bound(
|
||||
tcx,
|
||||
ty::INNERMOST,
|
||||
ty::BoundVar::from_usize(i),
|
||||
ty,
|
||||
)
|
||||
.into(),
|
||||
}
|
||||
},
|
||||
)),
|
||||
|
|
|
@ -2510,7 +2510,7 @@ impl<'tcx> ConstantKind<'tcx> {
|
|||
let generics = tcx.generics_of(item_def_id);
|
||||
let index = generics.param_def_id_to_index[&def_id];
|
||||
let name = tcx.item_name(def_id);
|
||||
let ty_const = tcx.mk_const(ty::ParamConst::new(index, name), ty);
|
||||
let ty_const = ty::Const::new_param(tcx, ty::ParamConst::new(index, name), ty);
|
||||
debug!(?ty_const);
|
||||
|
||||
return Self::Ty(ty_const);
|
||||
|
|
|
@ -344,7 +344,7 @@ impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> RefDecodable<'tcx, D>
|
|||
impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> Decodable<D> for ty::Const<'tcx> {
|
||||
fn decode(decoder: &mut D) -> Self {
|
||||
let consts: ty::ConstData<'tcx> = Decodable::decode(decoder);
|
||||
decoder.interner().mk_const(consts.kind, consts.ty)
|
||||
decoder.interner().mk_ct_from_kind(consts.kind, consts.ty)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -41,6 +41,69 @@ impl<'tcx> Const<'tcx> {
|
|||
self.0.kind
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn new(tcx: TyCtxt<'tcx>, kind: ty::ConstKind<'tcx>, ty: Ty<'tcx>) -> Const<'tcx> {
|
||||
tcx.mk_ct_from_kind(kind, ty)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn new_param(tcx: TyCtxt<'tcx>, param: ty::ParamConst, ty: Ty<'tcx>) -> Const<'tcx> {
|
||||
Const::new(tcx, ty::ConstKind::Param(param), ty)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn new_var(tcx: TyCtxt<'tcx>, infer: ty::ConstVid<'tcx>, ty: Ty<'tcx>) -> Const<'tcx> {
|
||||
Const::new(tcx, ty::ConstKind::Infer(ty::InferConst::Var(infer)), ty)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn new_fresh(tcx: TyCtxt<'tcx>, fresh: u32, ty: Ty<'tcx>) -> Const<'tcx> {
|
||||
Const::new(tcx, ty::ConstKind::Infer(ty::InferConst::Fresh(fresh)), ty)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn new_infer(tcx: TyCtxt<'tcx>, infer: ty::InferConst<'tcx>, ty: Ty<'tcx>) -> Const<'tcx> {
|
||||
Const::new(tcx, ty::ConstKind::Infer(infer), ty)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn new_bound(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
debruijn: ty::DebruijnIndex,
|
||||
var: ty::BoundVar,
|
||||
ty: Ty<'tcx>,
|
||||
) -> Const<'tcx> {
|
||||
Const::new(tcx, ty::ConstKind::Bound(debruijn, var), ty)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn new_placeholder(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
placeholder: ty::PlaceholderConst<'tcx>,
|
||||
ty: Ty<'tcx>,
|
||||
) -> Const<'tcx> {
|
||||
Const::new(tcx, ty::ConstKind::Placeholder(placeholder), ty)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn new_unevaluated(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
uv: ty::UnevaluatedConst<'tcx>,
|
||||
ty: Ty<'tcx>,
|
||||
) -> Const<'tcx> {
|
||||
Const::new(tcx, ty::ConstKind::Unevaluated(uv), ty)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn new_value(tcx: TyCtxt<'tcx>, val: ty::ValTree<'tcx>, ty: Ty<'tcx>) -> Const<'tcx> {
|
||||
Const::new(tcx, ty::ConstKind::Value(val), ty)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn new_expr(tcx: TyCtxt<'tcx>, expr: ty::Expr<'tcx>, ty: Ty<'tcx>) -> Const<'tcx> {
|
||||
Const::new(tcx, ty::ConstKind::Expr(expr), ty)
|
||||
}
|
||||
|
||||
/// Literals and const generic parameters are eagerly converted to a constant, everything else
|
||||
/// becomes `Unevaluated`.
|
||||
#[instrument(skip(tcx), level = "debug")]
|
||||
|
@ -60,7 +123,8 @@ impl<'tcx> Const<'tcx> {
|
|||
|
||||
match Self::try_eval_lit_or_param(tcx, ty, expr) {
|
||||
Some(v) => v,
|
||||
None => tcx.mk_const(
|
||||
None => ty::Const::new_unevaluated(
|
||||
tcx,
|
||||
ty::UnevaluatedConst {
|
||||
def: def.to_def_id(),
|
||||
substs: InternalSubsts::identity_for_item(tcx, def.to_def_id()),
|
||||
|
@ -126,12 +190,16 @@ impl<'tcx> Const<'tcx> {
|
|||
let generics = tcx.generics_of(item_def_id);
|
||||
let index = generics.param_def_id_to_index[&def_id];
|
||||
let name = tcx.item_name(def_id);
|
||||
Some(tcx.mk_const(ty::ParamConst::new(index, name), param_ty))
|
||||
Some(ty::Const::new_param(tcx, ty::ParamConst::new(index, name), param_ty))
|
||||
}
|
||||
Some(rbv::ResolvedArg::LateBound(debruijn, index, _)) => {
|
||||
Some(ty::Const::new_bound(
|
||||
tcx,
|
||||
debruijn,
|
||||
ty::BoundVar::from_u32(index),
|
||||
param_ty,
|
||||
))
|
||||
}
|
||||
Some(rbv::ResolvedArg::LateBound(debruijn, index, _)) => Some(tcx.mk_const(
|
||||
ty::ConstKind::Bound(debruijn, ty::BoundVar::from_u32(index)),
|
||||
param_ty,
|
||||
)),
|
||||
Some(rbv::ResolvedArg::Error(guar)) => Some(tcx.const_error(param_ty, guar)),
|
||||
arg => bug!("unexpected bound var resolution for {:?}: {arg:?}", expr.hir_id),
|
||||
}
|
||||
|
@ -155,7 +223,8 @@ impl<'tcx> Const<'tcx> {
|
|||
.layout_of(ty)
|
||||
.unwrap_or_else(|e| panic!("could not compute layout for {:?}: {:?}", ty, e))
|
||||
.size;
|
||||
tcx.mk_const(
|
||||
ty::Const::new_value(
|
||||
tcx,
|
||||
ty::ValTree::from_scalar_int(ScalarInt::try_from_uint(bits, size).unwrap()),
|
||||
ty.value,
|
||||
)
|
||||
|
@ -164,7 +233,7 @@ impl<'tcx> Const<'tcx> {
|
|||
#[inline]
|
||||
/// Creates an interned zst constant.
|
||||
pub fn zero_sized(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Self {
|
||||
tcx.mk_const(ty::ValTree::zst(), ty)
|
||||
ty::Const::new_value(tcx, ty::ValTree::zst(), ty)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
@ -215,7 +284,7 @@ impl<'tcx> Const<'tcx> {
|
|||
pub fn eval(self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>) -> Const<'tcx> {
|
||||
if let Some(val) = self.kind().try_eval_for_typeck(tcx, param_env) {
|
||||
match val {
|
||||
Ok(val) => tcx.mk_const(val, self.ty()),
|
||||
Ok(val) => ty::Const::new_value(tcx, val, self.ty()),
|
||||
Err(guar) => tcx.const_error(self.ty(), guar),
|
||||
}
|
||||
} else {
|
||||
|
|
|
@ -736,7 +736,7 @@ impl<'tcx> TyCtxt<'tcx> {
|
|||
/// Like [TyCtxt::ty_error] but for constants, with current `ErrorGuaranteed`
|
||||
#[track_caller]
|
||||
pub fn const_error(self, ty: Ty<'tcx>, reported: ErrorGuaranteed) -> Const<'tcx> {
|
||||
self.mk_const(ty::ConstKind::Error(reported), ty)
|
||||
self.mk_ct_from_kind(ty::ConstKind::Error(reported), ty)
|
||||
}
|
||||
|
||||
/// Like [TyCtxt::ty_error] but for constants.
|
||||
|
@ -758,7 +758,7 @@ impl<'tcx> TyCtxt<'tcx> {
|
|||
msg: &'static str,
|
||||
) -> Const<'tcx> {
|
||||
let reported = self.sess.delay_span_bug(span, msg);
|
||||
self.mk_const(ty::ConstKind::Error(reported), ty)
|
||||
self.const_error(ty, reported)
|
||||
}
|
||||
|
||||
pub fn consider_optimizing<T: Fn() -> String>(self, msg: T) -> bool {
|
||||
|
@ -1924,8 +1924,8 @@ impl<'tcx> TyCtxt<'tcx> {
|
|||
}
|
||||
|
||||
#[inline]
|
||||
pub fn mk_const(self, kind: impl Into<ty::ConstKind<'tcx>>, ty: Ty<'tcx>) -> Const<'tcx> {
|
||||
self.intern_const(ty::ConstData { kind: kind.into(), ty })
|
||||
pub fn mk_ct_from_kind(self, kind: ty::ConstKind<'tcx>, ty: Ty<'tcx>) -> Const<'tcx> {
|
||||
self.intern_const(ty::ConstData { kind, ty })
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
@ -1989,14 +1989,14 @@ impl<'tcx> TyCtxt<'tcx> {
|
|||
ty::Region::new_early_bound(self, param.to_early_bound_region_data()).into()
|
||||
}
|
||||
GenericParamDefKind::Type { .. } => self.mk_ty_param(param.index, param.name).into(),
|
||||
GenericParamDefKind::Const { .. } => self
|
||||
.mk_const(
|
||||
ParamConst { index: param.index, name: param.name },
|
||||
self.type_of(param.def_id)
|
||||
.no_bound_vars()
|
||||
.expect("const parameter types cannot be generic"),
|
||||
)
|
||||
.into(),
|
||||
GenericParamDefKind::Const { .. } => ty::Const::new_param(
|
||||
self,
|
||||
ParamConst { index: param.index, name: param.name },
|
||||
self.type_of(param.def_id)
|
||||
.no_bound_vars()
|
||||
.expect("const parameter types cannot be generic"),
|
||||
)
|
||||
.into(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -351,7 +351,7 @@ impl<'tcx> TyCtxt<'tcx> {
|
|||
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)
|
||||
ty::Const::new_bound(self, ty::INNERMOST, shift_bv(c), ty)
|
||||
},
|
||||
},
|
||||
)
|
||||
|
@ -400,7 +400,7 @@ impl<'tcx> TyCtxt<'tcx> {
|
|||
let index = entry.index();
|
||||
let var = ty::BoundVar::from_usize(index);
|
||||
let () = entry.or_insert_with(|| ty::BoundVariableKind::Const).expect_const();
|
||||
self.tcx.mk_const(ty::ConstKind::Bound(ty::INNERMOST, var), ty)
|
||||
ty::Const::new_bound(self.tcx, ty::INNERMOST, var, ty)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -475,7 +475,7 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for Shifter<'tcx> {
|
|||
&& debruijn >= self.current_index
|
||||
{
|
||||
let debruijn = debruijn.shifted_in(self.amount);
|
||||
self.tcx.mk_const(ty::ConstKind::Bound(debruijn, bound_ct), ct.ty())
|
||||
ty::Const::new_bound(self.tcx, debruijn, bound_ct, ct.ty())
|
||||
} else {
|
||||
ct.super_fold_with(self)
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ use crate::error::UnsupportedFnAbi;
|
|||
use crate::middle::codegen_fn_attrs::CodegenFnAttrFlags;
|
||||
use crate::query::TyCtxtAt;
|
||||
use crate::ty::normalize_erasing_regions::NormalizationError;
|
||||
use crate::ty::{self, ReprOptions, Ty, TyCtxt, TypeVisitableExt};
|
||||
use crate::ty::{self, ConstKind, ReprOptions, Ty, TyCtxt, TypeVisitableExt};
|
||||
use rustc_error_messages::DiagnosticMessage;
|
||||
use rustc_errors::{DiagnosticBuilder, Handler, IntoDiagnostic};
|
||||
use rustc_hir as hir;
|
||||
|
@ -480,13 +480,11 @@ fn mul_sorted_consts<'tcx>(
|
|||
b: ty::Const<'tcx>,
|
||||
) -> Option<ty::Const<'tcx>> {
|
||||
use crate::mir::BinOp::Mul;
|
||||
use ty::ConstKind::Expr;
|
||||
use ty::Expr::Binop;
|
||||
|
||||
let mut work = vec![a, b];
|
||||
let mut done = vec![];
|
||||
while let Some(n) = work.pop() {
|
||||
if let Expr(Binop(Mul, l, r)) = n.kind() {
|
||||
if let ConstKind::Expr(ty::Expr::Binop(Mul, l, r)) = n.kind() {
|
||||
work.push(l);
|
||||
work.push(r)
|
||||
} else {
|
||||
|
@ -517,7 +515,7 @@ fn mul_sorted_consts<'tcx>(
|
|||
done.sort_unstable();
|
||||
|
||||
// create a single tree from the buffer
|
||||
done.into_iter().reduce(|acc, n| tcx.mk_const(Expr(Binop(Mul, n, acc)), n.ty()))
|
||||
done.into_iter().reduce(|acc, n| ty::Const::new_expr(tcx, ty::Expr::Binop(Mul, n, acc), n.ty()))
|
||||
}
|
||||
|
||||
pub trait HasTyCtxt<'tcx>: HasDataLayout {
|
||||
|
|
|
@ -1601,7 +1601,8 @@ pub trait PrettyPrinter<'tcx>:
|
|||
}
|
||||
// Aggregates, printed as array/tuple/struct/variant construction syntax.
|
||||
(ty::ValTree::Branch(_), ty::Array(..) | ty::Tuple(..) | ty::Adt(..)) => {
|
||||
let contents = self.tcx().destructure_const(self.tcx().mk_const(valtree, ty));
|
||||
let contents =
|
||||
self.tcx().destructure_const(ty::Const::new_value(self.tcx(), valtree, ty));
|
||||
let fields = contents.fields.iter().copied();
|
||||
match *ty.kind() {
|
||||
ty::Array(..) => {
|
||||
|
|
|
@ -627,7 +627,11 @@ pub fn structurally_relate_consts<'tcx, R: TypeRelation<'tcx>>(
|
|||
au.substs,
|
||||
bu.substs,
|
||||
)?;
|
||||
return Ok(tcx.mk_const(ty::UnevaluatedConst { def: au.def, substs }, a.ty()));
|
||||
return Ok(ty::Const::new_unevaluated(
|
||||
tcx,
|
||||
ty::UnevaluatedConst { def: au.def, substs },
|
||||
a.ty(),
|
||||
));
|
||||
}
|
||||
// Before calling relate on exprs, it is necessary to ensure that the nested consts
|
||||
// have identical types.
|
||||
|
@ -668,8 +672,7 @@ pub fn structurally_relate_consts<'tcx, R: TypeRelation<'tcx>>(
|
|||
}
|
||||
_ => return Err(TypeError::ConstMismatch(expected_found(r, a, b))),
|
||||
};
|
||||
let kind = ty::ConstKind::Expr(expr);
|
||||
return Ok(tcx.mk_const(kind, a.ty()));
|
||||
return Ok(ty::Const::new_expr(tcx, expr, a.ty()));
|
||||
}
|
||||
_ => false,
|
||||
};
|
||||
|
|
|
@ -730,7 +730,7 @@ impl<'tcx> TypeSuperFoldable<TyCtxt<'tcx>> for ty::Const<'tcx> {
|
|||
let ty = self.ty().try_fold_with(folder)?;
|
||||
let kind = self.kind().try_fold_with(folder)?;
|
||||
if ty != self.ty() || kind != self.kind() {
|
||||
Ok(folder.interner().mk_const(kind, ty))
|
||||
Ok(folder.interner().mk_ct_from_kind(kind, ty))
|
||||
} else {
|
||||
Ok(self)
|
||||
}
|
||||
|
|
|
@ -84,7 +84,7 @@ pub fn as_constant_inner<'tcx>(
|
|||
Constant { user_ty, span, literal }
|
||||
}
|
||||
ExprKind::ConstParam { param, def_id: _ } => {
|
||||
let const_param = tcx.mk_const(ty::ConstKind::Param(param), expr.ty);
|
||||
let const_param = ty::Const::new_param(tcx, param, expr.ty);
|
||||
let literal = ConstantKind::Ty(const_param);
|
||||
|
||||
Constant { user_ty: None, span, literal }
|
||||
|
|
|
@ -79,5 +79,5 @@ pub(crate) fn lit_to_const<'tcx>(
|
|||
_ => return Err(LitToConstError::TypeError),
|
||||
};
|
||||
|
||||
Ok(tcx.mk_const(valtree, ty))
|
||||
Ok(ty::Const::new_value(tcx, valtree, ty))
|
||||
}
|
||||
|
|
|
@ -380,7 +380,9 @@ impl<'tcx> ConstToPat<'tcx> {
|
|||
ty::Ref(_, pointee_ty, ..) => match *pointee_ty.kind() {
|
||||
// `&str` is represented as a valtree, let's keep using this
|
||||
// optimization for now.
|
||||
ty::Str => PatKind::Constant { value: mir::ConstantKind::Ty(tcx.mk_const(cv, ty)) },
|
||||
ty::Str => PatKind::Constant {
|
||||
value: mir::ConstantKind::Ty(ty::Const::new_value(tcx, cv, ty)),
|
||||
},
|
||||
// Backwards compatibility hack: support references to non-structural types,
|
||||
// but hard error if we aren't behind a double reference. We could just use
|
||||
// the fallback code path below, but that would allow *more* of this fishy
|
||||
|
@ -438,9 +440,9 @@ impl<'tcx> ConstToPat<'tcx> {
|
|||
}
|
||||
}
|
||||
},
|
||||
ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::FnDef(..) => {
|
||||
PatKind::Constant { value: mir::ConstantKind::Ty(tcx.mk_const(cv, ty)) }
|
||||
}
|
||||
ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::FnDef(..) => PatKind::Constant {
|
||||
value: mir::ConstantKind::Ty(ty::Const::new_value(tcx, cv, ty)),
|
||||
},
|
||||
ty::FnPtr(..) | ty::RawPtr(..) => unreachable!(),
|
||||
_ => {
|
||||
self.saw_const_match_error.set(true);
|
||||
|
|
|
@ -525,7 +525,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
|
|||
.tcx
|
||||
.const_eval_global_id_for_typeck(param_env_reveal_all, cid, Some(span))
|
||||
.map(|val| match val {
|
||||
Some(valtree) => mir::ConstantKind::Ty(self.tcx.mk_const(valtree, ty)),
|
||||
Some(valtree) => mir::ConstantKind::Ty(ty::Const::new_value(self.tcx, valtree, ty)),
|
||||
None => mir::ConstantKind::Val(
|
||||
self.tcx
|
||||
.const_eval_global_id(param_env_reveal_all, cid, Some(span))
|
||||
|
@ -631,7 +631,13 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
|
|||
if let Ok(Some(valtree)) =
|
||||
self.tcx.const_eval_resolve_for_typeck(self.param_env, ct, Some(span))
|
||||
{
|
||||
self.const_to_pat(ConstantKind::Ty(self.tcx.mk_const(valtree, ty)), id, span, None).kind
|
||||
self.const_to_pat(
|
||||
ConstantKind::Ty(ty::Const::new_value(self.tcx, valtree, ty)),
|
||||
id,
|
||||
span,
|
||||
None,
|
||||
)
|
||||
.kind
|
||||
} else {
|
||||
// If that fails, convert it to an opaque constant pattern.
|
||||
match tcx.const_eval_resolve(self.param_env, uneval, None) {
|
||||
|
|
|
@ -651,7 +651,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> {
|
|||
.builtin_deref(true)
|
||||
.expect("tried to dereference on non-ptr type")
|
||||
.ty;
|
||||
let dereferenced_const = self.tcx.mk_const(ct.kind(), pointee_ty);
|
||||
let dereferenced_const = self.tcx.mk_ct_from_kind(ct.kind(), pointee_ty);
|
||||
self = dereferenced_const.print(self)?;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -377,7 +377,7 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for Canonicalizer<'_, 'tcx> {
|
|||
// any equated inference vars correctly!
|
||||
let root_vid = self.infcx.root_const_var(vid);
|
||||
if root_vid != vid {
|
||||
c = self.infcx.tcx.mk_const(ty::InferConst::Var(root_vid), c.ty());
|
||||
c = ty::Const::new_var(self.infcx.tcx, root_vid, c.ty());
|
||||
vid = root_vid;
|
||||
}
|
||||
|
||||
|
@ -426,6 +426,6 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for Canonicalizer<'_, 'tcx> {
|
|||
var
|
||||
}),
|
||||
);
|
||||
self.interner().mk_const(ty::ConstKind::Bound(self.binder_index, var), c.ty())
|
||||
ty::Const::new_bound(self.infcx.tcx, self.binder_index, var, c.ty())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -96,7 +96,7 @@ impl<'tcx> NormalizationFolder<'_, 'tcx> {
|
|||
let recursion_limit = tcx.recursion_limit();
|
||||
if !recursion_limit.value_within_limit(self.depth) {
|
||||
self.at.infcx.err_ctxt().report_overflow_error(
|
||||
&tcx.mk_const(uv, ty),
|
||||
&ty::Const::new_unevaluated(tcx, uv, ty),
|
||||
self.at.cause.span,
|
||||
true,
|
||||
|_| {},
|
||||
|
@ -131,7 +131,7 @@ impl<'tcx> NormalizationFolder<'_, 'tcx> {
|
|||
let ct = infcx.resolve_vars_if_possible(new_infer_ct);
|
||||
ct.try_fold_with(self)?
|
||||
} else {
|
||||
tcx.mk_const(uv, ty).try_super_fold_with(self)?
|
||||
ty::Const::new_unevaluated(tcx, uv, ty).try_super_fold_with(self)?
|
||||
};
|
||||
|
||||
self.depth -= 1;
|
||||
|
|
|
@ -794,7 +794,7 @@ impl<'tcx> AutoTraitFinder<'tcx> {
|
|||
unevaluated,
|
||||
Some(obligation.cause.span),
|
||||
) {
|
||||
Ok(Some(valtree)) => Ok(selcx.tcx().mk_const(valtree, c.ty())),
|
||||
Ok(Some(valtree)) => Ok(ty::Const::new_value(selcx.tcx(),valtree, c.ty())),
|
||||
Ok(None) => {
|
||||
let tcx = self.tcx;
|
||||
let reported =
|
||||
|
|
|
@ -1612,16 +1612,15 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
|
|||
.tcx
|
||||
.mk_projection(data.projection_ty.def_id, data.projection_ty.substs)
|
||||
.into(),
|
||||
ty::TermKind::Const(ct) => self
|
||||
.tcx
|
||||
.mk_const(
|
||||
ty::UnevaluatedConst {
|
||||
def: data.projection_ty.def_id,
|
||||
substs: data.projection_ty.substs,
|
||||
},
|
||||
ct.ty(),
|
||||
)
|
||||
.into(),
|
||||
ty::TermKind::Const(ct) => ty::Const::new_unevaluated(
|
||||
self.tcx,
|
||||
ty::UnevaluatedConst {
|
||||
def: data.projection_ty.def_id,
|
||||
substs: data.projection_ty.substs,
|
||||
},
|
||||
ct.ty(),
|
||||
)
|
||||
.into(),
|
||||
};
|
||||
let normalized_term =
|
||||
ocx.normalize(&obligation.cause, obligation.param_env, unnormalized_term);
|
||||
|
|
|
@ -924,7 +924,7 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for BoundVarReplacer<'_, 'tcx> {
|
|||
let universe = self.universe_for(debruijn);
|
||||
let p = ty::PlaceholderConst { universe, bound: bound_const };
|
||||
self.mapped_consts.insert(p, bound_const);
|
||||
self.infcx.tcx.mk_const(p, ct.ty())
|
||||
ty::Const::new_placeholder(self.infcx.tcx, p, ct.ty())
|
||||
}
|
||||
_ => ct.super_fold_with(self),
|
||||
}
|
||||
|
@ -1059,7 +1059,7 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for PlaceholderReplacer<'_, 'tcx> {
|
|||
let db = ty::DebruijnIndex::from_usize(
|
||||
self.universe_indices.len() - index + self.current_index.as_usize() - 1,
|
||||
);
|
||||
self.interner().mk_const(ty::ConstKind::Bound(db, *replace_var), ct.ty())
|
||||
ty::Const::new_bound(self.infcx.tcx, db, *replace_var, ct.ty())
|
||||
}
|
||||
None => ct,
|
||||
}
|
||||
|
@ -1500,16 +1500,16 @@ fn project<'cx, 'tcx>(
|
|||
DefKind::AssocTy | DefKind::ImplTraitPlaceholder => tcx
|
||||
.mk_projection(obligation.predicate.def_id, obligation.predicate.substs)
|
||||
.into(),
|
||||
DefKind::AssocConst => tcx
|
||||
.mk_const(
|
||||
ty::ConstKind::Unevaluated(ty::UnevaluatedConst::new(
|
||||
obligation.predicate.def_id,
|
||||
obligation.predicate.substs,
|
||||
)),
|
||||
tcx.type_of(obligation.predicate.def_id)
|
||||
.subst(tcx, obligation.predicate.substs),
|
||||
)
|
||||
.into(),
|
||||
DefKind::AssocConst => ty::Const::new_unevaluated(
|
||||
tcx,
|
||||
ty::UnevaluatedConst::new(
|
||||
obligation.predicate.def_id,
|
||||
obligation.predicate.substs,
|
||||
),
|
||||
tcx.type_of(obligation.predicate.def_id)
|
||||
.subst(tcx, obligation.predicate.substs),
|
||||
)
|
||||
.into(),
|
||||
kind => {
|
||||
bug!("unknown projection def-id: {}", kind.descr(obligation.predicate.def_id))
|
||||
}
|
||||
|
@ -2397,8 +2397,8 @@ fn confirm_impl_candidate<'cx, 'tcx>(
|
|||
let term: ty::EarlyBinder<ty::Term<'tcx>> = if is_const {
|
||||
let did = assoc_ty.item.def_id;
|
||||
let identity_substs = crate::traits::InternalSubsts::identity_for_item(tcx, did);
|
||||
let kind = ty::ConstKind::Unevaluated(ty::UnevaluatedConst::new(did, identity_substs));
|
||||
ty.map_bound(|ty| tcx.mk_const(kind, ty).into())
|
||||
let uv = ty::UnevaluatedConst::new(did, identity_substs);
|
||||
ty.map_bound(|ty| ty::Const::new_unevaluated(tcx, uv, ty).into())
|
||||
} else {
|
||||
ty.map_bound(|ty| ty.into())
|
||||
};
|
||||
|
|
|
@ -613,11 +613,10 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
|
|||
GenericParamDefKind::Const { .. } => {
|
||||
let bound_var = ty::BoundVariableKind::Const;
|
||||
bound_vars.push(bound_var);
|
||||
tcx.mk_const(
|
||||
ty::ConstKind::Bound(
|
||||
ty::INNERMOST,
|
||||
ty::BoundVar::from_usize(bound_vars.len() - 1),
|
||||
),
|
||||
ty::Const::new_bound(
|
||||
tcx,
|
||||
ty::INNERMOST,
|
||||
ty::BoundVar::from_usize(bound_vars.len() - 1),
|
||||
tcx.type_of(param.def_id)
|
||||
.no_bound_vars()
|
||||
.expect("const parameter types cannot be generic"),
|
||||
|
|
|
@ -33,8 +33,10 @@ pub(crate) fn destructure_const<'tcx>(
|
|||
let (fields, variant) = match const_.ty().kind() {
|
||||
ty::Array(inner_ty, _) | ty::Slice(inner_ty) => {
|
||||
// construct the consts for the elements of the array/slice
|
||||
let field_consts =
|
||||
branches.iter().map(|b| tcx.mk_const(*b, *inner_ty)).collect::<Vec<_>>();
|
||||
let field_consts = branches
|
||||
.iter()
|
||||
.map(|b| ty::Const::new_value(tcx, *b, *inner_ty))
|
||||
.collect::<Vec<_>>();
|
||||
debug!(?field_consts);
|
||||
|
||||
(field_consts, None)
|
||||
|
@ -52,7 +54,7 @@ pub(crate) fn destructure_const<'tcx>(
|
|||
|
||||
for (field, field_valtree) in iter::zip(fields, branches) {
|
||||
let field_ty = field.ty(tcx, substs);
|
||||
let field_const = tcx.mk_const(*field_valtree, field_ty);
|
||||
let field_const = ty::Const::new_value(tcx, *field_valtree, field_ty);
|
||||
field_consts.push(field_const);
|
||||
}
|
||||
debug!(?field_consts);
|
||||
|
@ -61,7 +63,7 @@ pub(crate) fn destructure_const<'tcx>(
|
|||
}
|
||||
ty::Tuple(elem_tys) => {
|
||||
let fields = iter::zip(*elem_tys, branches)
|
||||
.map(|(elem_ty, elem_valtree)| tcx.mk_const(*elem_valtree, elem_ty))
|
||||
.map(|(elem_ty, elem_valtree)| ty::Const::new_value(tcx, *elem_valtree, elem_ty))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
(fields, None)
|
||||
|
@ -125,17 +127,17 @@ fn recurse_build<'tcx>(
|
|||
}
|
||||
&ExprKind::NonHirLiteral { lit, user_ty: _ } => {
|
||||
let val = ty::ValTree::from_scalar_int(lit);
|
||||
tcx.mk_const(val, node.ty)
|
||||
ty::Const::new_value(tcx, val, node.ty)
|
||||
}
|
||||
&ExprKind::ZstLiteral { user_ty: _ } => {
|
||||
let val = ty::ValTree::zst();
|
||||
tcx.mk_const(val, node.ty)
|
||||
ty::Const::new_value(tcx, val, node.ty)
|
||||
}
|
||||
&ExprKind::NamedConst { def_id, substs, user_ty: _ } => {
|
||||
let uneval = ty::UnevaluatedConst::new(def_id, substs);
|
||||
tcx.mk_const(uneval, node.ty)
|
||||
ty::Const::new_unevaluated(tcx, uneval, node.ty)
|
||||
}
|
||||
ExprKind::ConstParam { param, .. } => tcx.mk_const(*param, node.ty),
|
||||
ExprKind::ConstParam { param, .. } => ty::Const::new_param(tcx, *param, node.ty),
|
||||
|
||||
ExprKind::Call { fun, args, .. } => {
|
||||
let fun = recurse_build(tcx, body, *fun, root_span)?;
|
||||
|
@ -145,16 +147,16 @@ fn recurse_build<'tcx>(
|
|||
new_args.push(recurse_build(tcx, body, id, root_span)?);
|
||||
}
|
||||
let new_args = tcx.mk_const_list(&new_args);
|
||||
tcx.mk_const(Expr::FunctionCall(fun, new_args), node.ty)
|
||||
ty::Const::new_expr(tcx, Expr::FunctionCall(fun, new_args), node.ty)
|
||||
}
|
||||
&ExprKind::Binary { op, lhs, rhs } if check_binop(op) => {
|
||||
let lhs = recurse_build(tcx, body, lhs, root_span)?;
|
||||
let rhs = recurse_build(tcx, body, rhs, root_span)?;
|
||||
tcx.mk_const(Expr::Binop(op, lhs, rhs), node.ty)
|
||||
ty::Const::new_expr(tcx, Expr::Binop(op, lhs, rhs), node.ty)
|
||||
}
|
||||
&ExprKind::Unary { op, arg } if check_unop(op) => {
|
||||
let arg = recurse_build(tcx, body, arg, root_span)?;
|
||||
tcx.mk_const(Expr::UnOp(op, arg), node.ty)
|
||||
ty::Const::new_expr(tcx, Expr::UnOp(op, arg), node.ty)
|
||||
}
|
||||
// This is necessary so that the following compiles:
|
||||
//
|
||||
|
@ -175,11 +177,11 @@ fn recurse_build<'tcx>(
|
|||
// This is important so that `N as usize as usize` doesnt unify with `N as usize`. (untested)
|
||||
&ExprKind::Use { source } => {
|
||||
let arg = recurse_build(tcx, body, source, root_span)?;
|
||||
tcx.mk_const(Expr::Cast(CastKind::Use, arg, node.ty), node.ty)
|
||||
ty::Const::new_expr(tcx, Expr::Cast(CastKind::Use, arg, node.ty), node.ty)
|
||||
}
|
||||
&ExprKind::Cast { source } => {
|
||||
let arg = recurse_build(tcx, body, source, root_span)?;
|
||||
tcx.mk_const(Expr::Cast(CastKind::As, arg, node.ty), node.ty)
|
||||
ty::Const::new_expr(tcx, Expr::Cast(CastKind::As, arg, node.ty), node.ty)
|
||||
}
|
||||
ExprKind::Borrow { arg, .. } => {
|
||||
let arg_node = &body.exprs[*arg];
|
||||
|
|
Loading…
Add table
Reference in a new issue