Auto merge of #125958 - BoxyUwU:remove_const_ty, r=lcnr
Remove the `ty` field from type system `Const`s Fixes #125556 Fixes #122908 Part of the work on `adt_const_params`/`generic_const_param_types`/`min_generic_const_exprs`/generally making the compiler nicer. cc rust-lang/project-const-generics#44 Please review commit-by-commit otherwise I wasted a lot of time not just squashing this into a giant mess (and also it'll be SO much nicer because theres a lot of fluff changes mixed in with other more careful changes if looking via File Changes --- Why do this? - The `ty` field keeps causing ICEs and weird behaviour due to it either being treated as "part of the const" or it being forgotten about leading to ICEs. - As we move forward with `adt_const_params` and a potential `min_generic_const_exprs` it's going to become more complex to actually lower the correct `Ty<'tcx>` - It muddles the idea behind how we check `Const` arguments have the correct type. By having the `ty` field it may seem like we ought to be relating it when we relate two types, or that its generally important information about the `Const`. - Brings the compiler more in line with `a-mir-formality` as that also tracks the type of type system `Const`s via `ConstArgHasType` bounds in the env instead of on the `Const` itself. - A lot of stuff is a lot nicer when you dont have to pass around the type of a const lol. Everywhere we construct `Const` is now significantly nicer 😅 See #125671's description for some more information about the `ty` field --- General summary of changes in this PR: - Add `Ty` to `ConstKind::Value` as otherwise there is no way to implement `ConstArgHasType` to ensure that const arguments are correctly typed for the parameter when we stop creating anon consts for all const args. It's also just incredibly difficult/annoying to thread the correct `Ty` around to a bunch of ctfe functions otherwise. - Fully implement `ConstArgHasType` in both the old and new solver. Since it now has no reliance on the `ty` field it serves its originally intended purpose of being able to act as a double check that trait vs impls have correctly typed const parameters. It also will now be able to be responsible for checking types of const arguments to parameters under `min_generic_const_exprs`. - Add `Ty` to `mir::Const::Ty`. I dont have a great understanding of why mir constants are setup like this to be honest. Regardless they need to be able to determine the type of the const and the easiest way to make this happen was to simply store the `Ty` along side the `ty::Const`. Maybe we can do better here in the future but I'd have to spend way more time looking at everywhere we use `mir::Const`. - rustdoc has its own `Const` which also has a `ty` field. It was relatively easy to remove this. --- r? `@lcnr` `@compiler-errors`
This commit is contained in:
@@ -765,7 +765,7 @@ impl<'tcx> AutoTraitFinder<'tcx> {
|
||||
unevaluated,
|
||||
obligation.cause.span,
|
||||
) {
|
||||
Ok(Some(valtree)) => Ok(ty::Const::new_value(selcx.tcx(),valtree, c.ty())),
|
||||
Ok(Some(valtree)) => Ok(ty::Const::new_value(selcx.tcx(),valtree, self.tcx.type_of(unevaluated.def).instantiate(self.tcx, unevaluated.args))),
|
||||
Ok(None) => {
|
||||
let tcx = self.tcx;
|
||||
let reported =
|
||||
|
||||
@@ -529,7 +529,6 @@ fn plug_infer_with_placeholders<'tcx>(
|
||||
ty::Const::new_placeholder(
|
||||
self.infcx.tcx,
|
||||
ty::Placeholder { universe: self.universe, bound: self.next_var() },
|
||||
ct.ty(),
|
||||
),
|
||||
)
|
||||
else {
|
||||
|
||||
@@ -34,7 +34,7 @@ pub fn is_const_evaluatable<'tcx>(
|
||||
ty::ConstKind::Param(_)
|
||||
| ty::ConstKind::Bound(_, _)
|
||||
| ty::ConstKind::Placeholder(_)
|
||||
| ty::ConstKind::Value(_)
|
||||
| ty::ConstKind::Value(_, _)
|
||||
| ty::ConstKind::Error(_) => return Ok(()),
|
||||
ty::ConstKind::Infer(_) => return Err(NotConstEvaluatable::MentionsInfer),
|
||||
};
|
||||
@@ -173,8 +173,7 @@ fn satisfied_from_param_env<'tcx>(
|
||||
debug!("is_const_evaluatable: candidate={:?}", c);
|
||||
if self.infcx.probe(|_| {
|
||||
let ocx = ObligationCtxt::new(self.infcx);
|
||||
ocx.eq(&ObligationCause::dummy(), self.param_env, c.ty(), self.ct.ty()).is_ok()
|
||||
&& ocx.eq(&ObligationCause::dummy(), self.param_env, c, self.ct).is_ok()
|
||||
ocx.eq(&ObligationCause::dummy(), self.param_env, c, self.ct).is_ok()
|
||||
&& ocx.select_all_or_error().is_empty()
|
||||
}) {
|
||||
self.single_match = match self.single_match {
|
||||
@@ -215,7 +214,6 @@ fn satisfied_from_param_env<'tcx>(
|
||||
|
||||
if let Some(Ok(c)) = single_match {
|
||||
let ocx = ObligationCtxt::new(infcx);
|
||||
assert!(ocx.eq(&ObligationCause::dummy(), param_env, c.ty(), ct.ty()).is_ok());
|
||||
assert!(ocx.eq(&ObligationCause::dummy(), param_env, c, ct).is_ok());
|
||||
assert!(ocx.select_all_or_error().is_empty());
|
||||
return true;
|
||||
|
||||
@@ -876,56 +876,25 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
||||
}
|
||||
}
|
||||
|
||||
ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(..)) => {
|
||||
// Errors for `ConstEvaluatable` predicates show up as
|
||||
// `SelectionError::ConstEvalFailure`,
|
||||
// not `Unimplemented`.
|
||||
// Errors for `ConstEvaluatable` predicates show up as
|
||||
// `SelectionError::ConstEvalFailure`,
|
||||
// not `Unimplemented`.
|
||||
ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(..))
|
||||
// Errors for `ConstEquate` predicates show up as
|
||||
// `SelectionError::ConstEvalFailure`,
|
||||
// not `Unimplemented`.
|
||||
| ty::PredicateKind::ConstEquate { .. }
|
||||
// Ambiguous predicates should never error
|
||||
| ty::PredicateKind::Ambiguous
|
||||
| ty::PredicateKind::NormalizesTo { .. }
|
||||
| ty::PredicateKind::AliasRelate { .. }
|
||||
| ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType { .. }) => {
|
||||
span_bug!(
|
||||
span,
|
||||
"const-evaluatable requirement gave wrong error: `{:?}`",
|
||||
"Unexpected `Predicate` for `SelectionError`: `{:?}`",
|
||||
obligation
|
||||
)
|
||||
}
|
||||
|
||||
ty::PredicateKind::ConstEquate(..) => {
|
||||
// Errors for `ConstEquate` predicates show up as
|
||||
// `SelectionError::ConstEvalFailure`,
|
||||
// not `Unimplemented`.
|
||||
span_bug!(
|
||||
span,
|
||||
"const-equate requirement gave wrong error: `{:?}`",
|
||||
obligation
|
||||
)
|
||||
}
|
||||
|
||||
ty::PredicateKind::Ambiguous => span_bug!(span, "ambiguous"),
|
||||
|
||||
ty::PredicateKind::NormalizesTo(..) => span_bug!(
|
||||
span,
|
||||
"NormalizesTo predicate should never be the predicate cause of a SelectionError"
|
||||
),
|
||||
|
||||
ty::PredicateKind::AliasRelate(..) => span_bug!(
|
||||
span,
|
||||
"AliasRelate predicate should never be the predicate cause of a SelectionError"
|
||||
),
|
||||
|
||||
ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(ct, ty)) => {
|
||||
let mut diag = self.dcx().struct_span_err(
|
||||
span,
|
||||
format!("the constant `{ct}` is not of type `{ty}`"),
|
||||
);
|
||||
self.note_type_err(
|
||||
&mut diag,
|
||||
&obligation.cause,
|
||||
None,
|
||||
None,
|
||||
TypeError::Sorts(ty::error::ExpectedFound::new(true, ty, ct.ty())),
|
||||
false,
|
||||
false,
|
||||
);
|
||||
diag
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -988,6 +957,24 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
||||
Overflow(_) => {
|
||||
bug!("overflow should be handled before the `report_selection_error` path");
|
||||
}
|
||||
|
||||
SelectionError::ConstArgHasWrongType { ct, ct_ty, expected_ty } => {
|
||||
let mut diag = self.dcx().struct_span_err(
|
||||
span,
|
||||
format!("the constant `{ct}` is not of type `{expected_ty}`"),
|
||||
);
|
||||
|
||||
self.note_type_err(
|
||||
&mut diag,
|
||||
&obligation.cause,
|
||||
None,
|
||||
None,
|
||||
TypeError::Sorts(ty::error::ExpectedFound::new(true, expected_ty, ct_ty)),
|
||||
false,
|
||||
false,
|
||||
);
|
||||
diag
|
||||
}
|
||||
};
|
||||
|
||||
self.note_obligation_cause(&mut err, &obligation);
|
||||
|
||||
@@ -439,37 +439,50 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
|
||||
// This is because this is not ever a useful obligation to report
|
||||
// as the cause of an overflow.
|
||||
ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(ct, ty)) => {
|
||||
// FIXME(BoxyUwU): Really we should not be calling `ct.ty()` for any variant
|
||||
// other than `ConstKind::Value`. Unfortunately this would require looking in the
|
||||
// env for any `ConstArgHasType` assumptions for parameters and placeholders. I
|
||||
// don't really want to implement this in the old solver so I haven't.
|
||||
//
|
||||
// We do still stall on infer vars though as otherwise a goal like:
|
||||
// `ConstArgHasType(?x: usize, usize)` can succeed even though it might later
|
||||
// get unified with some const that is not of type `usize`.
|
||||
let ct = self.selcx.infcx.shallow_resolve_const(ct);
|
||||
match ct.kind() {
|
||||
ty::ConstKind::Infer(ty::InferConst::Var(vid)) => {
|
||||
let ct = infcx.shallow_resolve_const(ct);
|
||||
let ct_ty = match ct.kind() {
|
||||
ty::ConstKind::Infer(var) => {
|
||||
let var = match var {
|
||||
ty::InferConst::Var(vid) => TyOrConstInferVar::Const(vid),
|
||||
ty::InferConst::EffectVar(vid) => TyOrConstInferVar::Effect(vid),
|
||||
ty::InferConst::Fresh(_) => {
|
||||
bug!("encountered fresh const in fulfill")
|
||||
}
|
||||
};
|
||||
pending_obligation.stalled_on.clear();
|
||||
pending_obligation.stalled_on.extend([TyOrConstInferVar::Const(vid)]);
|
||||
ProcessResult::Unchanged
|
||||
pending_obligation.stalled_on.extend([var]);
|
||||
return ProcessResult::Unchanged;
|
||||
}
|
||||
ty::ConstKind::Error(_) => return ProcessResult::Changed(vec![]),
|
||||
_ => {
|
||||
match self.selcx.infcx.at(&obligation.cause, obligation.param_env).eq(
|
||||
// Only really excercised by generic_const_exprs
|
||||
DefineOpaqueTypes::Yes,
|
||||
ct.ty(),
|
||||
ty,
|
||||
) {
|
||||
Ok(inf_ok) => {
|
||||
ProcessResult::Changed(mk_pending(inf_ok.into_obligations()))
|
||||
}
|
||||
Err(_) => ProcessResult::Error(FulfillmentErrorCode::Select(
|
||||
SelectionError::Unimplemented,
|
||||
)),
|
||||
}
|
||||
ty::ConstKind::Value(ty, _) => ty,
|
||||
ty::ConstKind::Unevaluated(uv) => {
|
||||
infcx.tcx.type_of(uv.def).instantiate(infcx.tcx, uv.args)
|
||||
}
|
||||
// FIXME(generic_const_exprs): we should construct an alias like
|
||||
// `<lhs_ty as Add<rhs_ty>>::Output` when this is an `Expr` representing
|
||||
// `lhs + rhs`.
|
||||
ty::ConstKind::Expr(_) => {
|
||||
return ProcessResult::Changed(mk_pending(vec![]));
|
||||
}
|
||||
ty::ConstKind::Placeholder(_) => {
|
||||
bug!("placeholder const {:?} in old solver", ct)
|
||||
}
|
||||
ty::ConstKind::Bound(_, _) => bug!("escaping bound vars in {:?}", ct),
|
||||
ty::ConstKind::Param(param_ct) => {
|
||||
param_ct.find_ty_from_env(obligation.param_env)
|
||||
}
|
||||
};
|
||||
|
||||
match infcx.at(&obligation.cause, obligation.param_env).eq(
|
||||
// Only really excercised by generic_const_exprs
|
||||
DefineOpaqueTypes::Yes,
|
||||
ct_ty,
|
||||
ty,
|
||||
) {
|
||||
Ok(inf_ok) => ProcessResult::Changed(mk_pending(inf_ok.into_obligations())),
|
||||
Err(_) => ProcessResult::Error(FulfillmentErrorCode::Select(
|
||||
SelectionError::ConstArgHasWrongType { ct, ct_ty, expected_ty: ty },
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -633,7 +646,6 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
|
||||
match self.selcx.infcx.try_const_eval_resolve(
|
||||
obligation.param_env,
|
||||
unevaluated,
|
||||
c.ty(),
|
||||
obligation.cause.span,
|
||||
) {
|
||||
Ok(val) => Ok(val),
|
||||
|
||||
@@ -359,7 +359,7 @@ pub fn normalize_param_env_or_error<'tcx>(
|
||||
// `ty::Const::normalize` can only work with properly preserved binders.
|
||||
|
||||
if c.has_escaping_bound_vars() {
|
||||
return ty::Const::new_misc_error(self.0, c.ty());
|
||||
return ty::Const::new_misc_error(self.0);
|
||||
}
|
||||
// While it is pretty sus to be evaluating things with an empty param env, it
|
||||
// should actually be okay since without `feature(generic_const_exprs)` the only
|
||||
|
||||
@@ -523,16 +523,9 @@ fn normalize_to_error<'a, 'tcx>(
|
||||
| ty::AliasTermKind::InherentTy
|
||||
| ty::AliasTermKind::OpaqueTy
|
||||
| ty::AliasTermKind::WeakTy => selcx.infcx.next_ty_var(cause.span).into(),
|
||||
ty::AliasTermKind::UnevaluatedConst | ty::AliasTermKind::ProjectionConst => selcx
|
||||
.infcx
|
||||
.next_const_var(
|
||||
selcx
|
||||
.tcx()
|
||||
.type_of(projection_term.def_id)
|
||||
.instantiate(selcx.tcx(), projection_term.args),
|
||||
cause.span,
|
||||
)
|
||||
.into(),
|
||||
ty::AliasTermKind::UnevaluatedConst | ty::AliasTermKind::ProjectionConst => {
|
||||
selcx.infcx.next_const_var(cause.span).into()
|
||||
}
|
||||
};
|
||||
let trait_obligation = Obligation {
|
||||
cause,
|
||||
@@ -744,8 +737,6 @@ fn project<'cx, 'tcx>(
|
||||
obligation.predicate.def_id,
|
||||
obligation.predicate.args,
|
||||
),
|
||||
tcx.type_of(obligation.predicate.def_id)
|
||||
.instantiate(tcx, obligation.predicate.args),
|
||||
)
|
||||
.into(),
|
||||
kind => {
|
||||
@@ -2071,15 +2062,14 @@ fn confirm_impl_candidate<'cx, 'tcx>(
|
||||
// * `args` ends up as `[u32, S]`
|
||||
let args = obligation.predicate.args.rebase_onto(tcx, trait_def_id, args);
|
||||
let args = translate_args(selcx.infcx, param_env, impl_def_id, args, assoc_ty.defining_node);
|
||||
let ty = tcx.type_of(assoc_ty.item.def_id);
|
||||
let is_const = matches!(tcx.def_kind(assoc_ty.item.def_id), DefKind::AssocConst);
|
||||
let term: ty::EarlyBinder<'tcx, ty::Term<'tcx>> = if is_const {
|
||||
let did = assoc_ty.item.def_id;
|
||||
let identity_args = crate::traits::GenericArgs::identity_for_item(tcx, did);
|
||||
let uv = ty::UnevaluatedConst::new(did, identity_args);
|
||||
ty.map_bound(|ty| ty::Const::new_unevaluated(tcx, uv, ty).into())
|
||||
ty::EarlyBinder::bind(ty::Const::new_unevaluated(tcx, uv).into())
|
||||
} else {
|
||||
ty.map_bound(|ty| ty.into())
|
||||
tcx.type_of(assoc_ty.item.def_id).map_bound(|ty| ty.into())
|
||||
};
|
||||
if !tcx.check_args_compatible(assoc_ty.item.def_id, args) {
|
||||
let err = Ty::new_error_with_message(
|
||||
|
||||
@@ -665,9 +665,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
|
||||
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"),
|
||||
)
|
||||
.into()
|
||||
}
|
||||
|
||||
@@ -947,7 +947,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
|
||||
match self.infcx.try_const_eval_resolve(
|
||||
obligation.param_env,
|
||||
unevaluated,
|
||||
c.ty(),
|
||||
obligation.cause.span,
|
||||
) {
|
||||
Ok(val) => Ok(val),
|
||||
@@ -995,21 +994,25 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
|
||||
}
|
||||
ty::PredicateKind::Ambiguous => Ok(EvaluatedToAmbig),
|
||||
ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(ct, ty)) => {
|
||||
// FIXME(BoxyUwU): Really we should not be calling `ct.ty()` for any variant
|
||||
// other than `ConstKind::Value`. Unfortunately this would require looking in the
|
||||
// env for any `ConstArgHasType` assumptions for parameters and placeholders. I
|
||||
// don't really want to implement this in the old solver so I haven't.
|
||||
//
|
||||
// We do still stall on infer vars though as otherwise a goal like:
|
||||
// `ConstArgHasType(?x: usize, usize)` can succeed even though it might later
|
||||
// get unified with some const that is not of type `usize`.
|
||||
let ct = self.infcx.shallow_resolve_const(ct);
|
||||
let ct_ty = match ct.kind() {
|
||||
ty::ConstKind::Infer(ty::InferConst::Var(_)) => {
|
||||
ty::ConstKind::Infer(_) => {
|
||||
return Ok(EvaluatedToAmbig);
|
||||
}
|
||||
ty::ConstKind::Error(_) => return Ok(EvaluatedToOk),
|
||||
_ => ct.ty(),
|
||||
ty::ConstKind::Value(ty, _) => ty,
|
||||
ty::ConstKind::Unevaluated(uv) => {
|
||||
self.tcx().type_of(uv.def).instantiate(self.tcx(), uv.args)
|
||||
}
|
||||
// FIXME(generic_const_exprs): See comment in `fulfill.rs`
|
||||
ty::ConstKind::Expr(_) => return Ok(EvaluatedToOk),
|
||||
ty::ConstKind::Placeholder(_) => {
|
||||
bug!("placeholder const {:?} in old solver", ct)
|
||||
}
|
||||
ty::ConstKind::Bound(_, _) => bug!("escaping bound vars in {:?}", ct),
|
||||
ty::ConstKind::Param(param_ct) => {
|
||||
param_ct.find_ty_from_env(obligation.param_env)
|
||||
}
|
||||
};
|
||||
|
||||
match self.infcx.at(&obligation.cause, obligation.param_env).eq(
|
||||
|
||||
@@ -483,7 +483,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);
|
||||
ty::Const::new_placeholder(self.infcx.tcx, p, ct.ty())
|
||||
ty::Const::new_placeholder(self.infcx.tcx, p)
|
||||
}
|
||||
_ => ct.super_fold_with(self),
|
||||
}
|
||||
@@ -626,7 +626,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,
|
||||
);
|
||||
ty::Const::new_bound(self.infcx.tcx, db, *replace_var, ct.ty())
|
||||
ty::Const::new_bound(self.infcx.tcx, db, *replace_var)
|
||||
}
|
||||
None => {
|
||||
if ct.has_infer() {
|
||||
|
||||
Reference in New Issue
Block a user