Skip to content

Commit

Permalink
reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
BoxyUwU committed Nov 12, 2024
1 parent b0b1ad1 commit 5558a84
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 34 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_trait_selection/src/solve/delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl<'tcx> rustc_next_trait_solver::delegate::SolverDelegate for SolverDelegate<
) -> Option<ty::Const<'tcx>> {
match crate::traits::try_evaluate_const(&self.0, ct, param_env) {
Ok(ct) => Some(ct),
Err(EvaluateConstErr::CTFEFailure(e)) => Some(ty::Const::new_error(self.tcx, e)),
Err(EvaluateConstErr::EvaluationFailure(e)) => Some(ty::Const::new_error(self.tcx, e)),
Err(
EvaluateConstErr::InvalidConstParamTy(_) | EvaluateConstErr::HasGenericsOrInfers,
) => None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ pub fn is_const_evaluatable<'tcx>(
)))
}
Err(
EvaluateConstErr::CTFEFailure(e) | EvaluateConstErr::InvalidConstParamTy(e),
EvaluateConstErr::EvaluationFailure(e)
| EvaluateConstErr::InvalidConstParamTy(e),
) => Err(NotConstEvaluatable::Error(e.into())),
Ok(_) => Ok(()),
}
Expand Down Expand Up @@ -137,9 +138,9 @@ pub fn is_const_evaluatable<'tcx>(

Err(err)
}
Err(EvaluateConstErr::CTFEFailure(e) | EvaluateConstErr::InvalidConstParamTy(e)) => {
Err(NotConstEvaluatable::Error(e.into()))
}
Err(
EvaluateConstErr::EvaluationFailure(e) | EvaluateConstErr::InvalidConstParamTy(e),
) => Err(NotConstEvaluatable::Error(e.into())),
Ok(_) => Ok(()),
}
}
Expand Down
48 changes: 26 additions & 22 deletions compiler/rustc_trait_selection/src/traits/fulfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,28 +662,32 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {

let stalled_on = &mut pending_obligation.stalled_on;

let mut evaluate =
|c: Const<'tcx>| {
if let ty::ConstKind::Unevaluated(unevaluated) = c.kind() {
match super::try_evaluate_const(
self.selcx.infcx,
c,
obligation.param_env,
) {
Ok(val) => Ok(val),
Err(e) => {
if let EvaluateConstErr::HasGenericsOrInfers = e {
stalled_on.extend(unevaluated.args.iter().filter_map(
TyOrConstInferVar::maybe_from_generic_arg,
));
}
Err(e)
}
let mut evaluate = |c: Const<'tcx>| {
if let ty::ConstKind::Unevaluated(unevaluated) = c.kind() {
match super::try_evaluate_const(
self.selcx.infcx,
c,
obligation.param_env,
) {
Ok(val) => Ok(val),
e @ Err(EvaluateConstErr::HasGenericsOrInfers) => {
stalled_on.extend(
unevaluated
.args
.iter()
.filter_map(TyOrConstInferVar::maybe_from_generic_arg),
);
e
}
} else {
Ok(c)
e @ Err(
EvaluateConstErr::EvaluationFailure(_)
| EvaluateConstErr::InvalidConstParamTy(_),
) => e,
}
};
} else {
Ok(c)
}
};

match (evaluate(c1), evaluate(c2)) {
(Ok(c1), Ok(c2)) => {
Expand Down Expand Up @@ -711,8 +715,8 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
SelectionError::NotConstEvaluatable(NotConstEvaluatable::Error(e)),
))
}
(Err(EvaluateConstErr::CTFEFailure(e)), _)
| (_, Err(EvaluateConstErr::CTFEFailure(e))) => {
(Err(EvaluateConstErr::EvaluationFailure(e)), _)
| (_, Err(EvaluateConstErr::EvaluationFailure(e))) => {
ProcessResult::Error(FulfillmentErrorCode::Select(
SelectionError::NotConstEvaluatable(NotConstEvaluatable::Error(e)),
))
Expand Down
18 changes: 13 additions & 5 deletions compiler/rustc_trait_selection/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,9 +485,17 @@ pub fn normalize_param_env_or_error<'tcx>(

#[derive(Debug)]
pub enum EvaluateConstErr {
/// The constant being evaluated was either a generic parameter or inference variable, *or*,
/// some unevaluated constant with either generic parameters or inference variables in its
/// generic arguments.
HasGenericsOrInfers,
/// The type this constant evalauted to is not valid for use in const generics. This should
/// always result in an error when checking the constant is correctly typed for the parameter
/// it is an argument to, so a bug is delayed when encountering this.
InvalidConstParamTy(ErrorGuaranteed),
CTFEFailure(ErrorGuaranteed),
/// CTFE failed to evaluate the constant in some unrecoverable way (e.g. encountered a `panic!`).
/// This is also used when the constant was already tainted by error.
EvaluationFailure(ErrorGuaranteed),
}

// FIXME(BoxyUwU): Private this once we `generic_const_exprs` isn't doing its own normalization routine
Expand All @@ -504,7 +512,7 @@ pub fn evaluate_const<'tcx>(
) -> ty::Const<'tcx> {
match try_evaluate_const(infcx, ct, param_env) {
Ok(ct) => ct,
Err(EvaluateConstErr::CTFEFailure(e) | EvaluateConstErr::InvalidConstParamTy(e)) => {
Err(EvaluateConstErr::EvaluationFailure(e) | EvaluateConstErr::InvalidConstParamTy(e)) => {
ty::Const::new_error(infcx.tcx, e)
}
Err(EvaluateConstErr::HasGenericsOrInfers) => ct,
Expand All @@ -529,7 +537,7 @@ pub fn try_evaluate_const<'tcx>(

match ct.kind() {
ty::ConstKind::Value(..) => Ok(ct),
ty::ConstKind::Error(e) => Err(EvaluateConstErr::CTFEFailure(e)),
ty::ConstKind::Error(e) => Err(EvaluateConstErr::EvaluationFailure(e)),
ty::ConstKind::Param(_)
| ty::ConstKind::Infer(_)
| ty::ConstKind::Bound(_, _)
Expand All @@ -547,7 +555,7 @@ pub fn try_evaluate_const<'tcx>(
Ok(Some(ct)) => {
let ct = tcx.expand_abstract_consts(ct.instantiate(tcx, uv.args));
if let Err(e) = ct.error_reported() {
return Err(EvaluateConstErr::CTFEFailure(e));
return Err(EvaluateConstErr::EvaluationFailure(e));
} else if ct.has_non_region_infer() || ct.has_non_region_param() {
// If the anon const *does* actually use generic parameters or inference variables from
// the generic arguments provided for it, then we should *not* attempt to evaluate it.
Expand Down Expand Up @@ -593,7 +601,7 @@ pub fn try_evaluate_const<'tcx>(
Err(EvaluateConstErr::InvalidConstParamTy(e))
}
Err(ErrorHandled::Reported(info, _)) => {
Err(EvaluateConstErr::CTFEFailure(info.into()))
Err(EvaluateConstErr::EvaluationFailure(info.into()))
}
Err(ErrorHandled::TooGeneric(_)) => Err(EvaluateConstErr::HasGenericsOrInfers),
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_trait_selection/src/traits/select/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -962,8 +962,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
}
(Err(EvaluateConstErr::InvalidConstParamTy(..)), _)
| (_, Err(EvaluateConstErr::InvalidConstParamTy(..))) => Ok(EvaluatedToErr),
(Err(EvaluateConstErr::CTFEFailure(..)), _)
| (_, Err(EvaluateConstErr::CTFEFailure(..))) => Ok(EvaluatedToErr),
(Err(EvaluateConstErr::EvaluationFailure(..)), _)
| (_, Err(EvaluateConstErr::EvaluationFailure(..))) => Ok(EvaluatedToErr),
(Err(EvaluateConstErr::HasGenericsOrInfers), _)
| (_, Err(EvaluateConstErr::HasGenericsOrInfers)) => {
if c1.has_non_region_infer() || c2.has_non_region_infer() {
Expand Down

0 comments on commit 5558a84

Please sign in to comment.