Skip to content

Commit

Permalink
Rollup merge of rust-lang#121359 - lcnr:typesystem-cleanup, r=compile…
Browse files Browse the repository at this point in the history
…r-errors

miscellaneous type system improvements

see review comments for rationale

r? `@compiler-errors`
  • Loading branch information
fmease authored Feb 21, 2024
2 parents 2d98f05 + 5fb67e2 commit 8d27fc8
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 151 deletions.
24 changes: 12 additions & 12 deletions compiler/rustc_infer/src/infer/relate/combine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,15 @@ impl<'tcx> InferCtxt<'tcx> {
ty::ConstKind::Infer(InferConst::Var(b_vid)),
) => {
self.inner.borrow_mut().const_unification_table().union(a_vid, b_vid);
return Ok(a);
Ok(a)
}

(
ty::ConstKind::Infer(InferConst::EffectVar(a_vid)),
ty::ConstKind::Infer(InferConst::EffectVar(b_vid)),
) => {
self.inner.borrow_mut().effect_unification_table().union(a_vid, b_vid);
return Ok(a);
Ok(a)
}

// All other cases of inference with other variables are errors.
Expand All @@ -220,42 +220,42 @@ impl<'tcx> InferCtxt<'tcx> {
}

(ty::ConstKind::Infer(InferConst::Var(vid)), _) => {
return self.instantiate_const_var(vid, b);
self.instantiate_const_var(relation, relation.a_is_expected(), vid, b)?;
Ok(b)
}

(_, ty::ConstKind::Infer(InferConst::Var(vid))) => {
return self.instantiate_const_var(vid, a);
self.instantiate_const_var(relation, !relation.a_is_expected(), vid, a)?;
Ok(a)
}

(ty::ConstKind::Infer(InferConst::EffectVar(vid)), _) => {
return Ok(self.unify_effect_variable(vid, b));
Ok(self.unify_effect_variable(vid, b))
}

(_, ty::ConstKind::Infer(InferConst::EffectVar(vid))) => {
return Ok(self.unify_effect_variable(vid, a));
Ok(self.unify_effect_variable(vid, a))
}

(ty::ConstKind::Unevaluated(..), _) | (_, ty::ConstKind::Unevaluated(..))
if self.tcx.features().generic_const_exprs || self.next_trait_solver() =>
{
let (a, b) = if relation.a_is_expected() { (a, b) } else { (b, a) };

relation.register_predicates([ty::Binder::dummy(if self.next_trait_solver() {
relation.register_predicates([if self.next_trait_solver() {
ty::PredicateKind::AliasRelate(
a.into(),
b.into(),
ty::AliasRelationDirection::Equate,
)
} else {
ty::PredicateKind::ConstEquate(a, b)
})]);
}]);

return Ok(b);
Ok(b)
}
_ => {}
_ => ty::relate::structurally_relate_consts(relation, a, b),
}

ty::relate::structurally_relate_consts(relation, a, b)
}

fn unify_integral_variable(
Expand Down
123 changes: 71 additions & 52 deletions compiler/rustc_infer/src/infer/relate/generalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl<'tcx> InferCtxt<'tcx> {
/// subtyping could occur. This also does the occurs checks, detecting whether
/// instantiating `target_vid` would result in a cyclic type. We eagerly error
/// in this case.
#[instrument(skip(self, relation, target_is_expected), level = "debug")]
#[instrument(level = "debug", skip(self, relation, target_is_expected))]
pub(super) fn instantiate_ty_var<R: ObligationEmittingRelation<'tcx>>(
&self,
relation: &mut R,
Expand Down Expand Up @@ -158,36 +158,48 @@ impl<'tcx> InferCtxt<'tcx> {
/// As `3 + 4` contains `N` in its args, this must not succeed.
///
/// See `tests/ui/const-generics/occurs-check/` for more examples where this is relevant.
#[instrument(level = "debug", skip(self))]
pub(super) fn instantiate_const_var(
#[instrument(level = "debug", skip(self, relation))]
pub(super) fn instantiate_const_var<R: ObligationEmittingRelation<'tcx>>(
&self,
relation: &mut R,
target_is_expected: bool,
target_vid: ty::ConstVid,
source_ct: ty::Const<'tcx>,
) -> RelateResult<'tcx, ty::Const<'tcx>> {
let span = match self.inner.borrow_mut().const_unification_table().probe_value(target_vid) {
ConstVariableValue::Known { value } => {
bug!("instantiating a known const var: {target_vid:?} {value} {source_ct}")
}
ConstVariableValue::Unknown { origin, universe: _ } => origin.span,
};
) -> RelateResult<'tcx, ()> {
// FIXME(generic_const_exprs): Occurs check failures for unevaluated
// constants and generic expressions are not yet handled correctly.
let Generalization { value_may_be_infer: generalized_ct, has_unconstrained_ty_var } =
self.generalize(span, target_vid, ty::Variance::Invariant, source_ct)?;
self.generalize(relation.span(), target_vid, ty::Variance::Invariant, source_ct)?;

debug_assert!(!generalized_ct.is_ct_infer());
if has_unconstrained_ty_var {
span_bug!(span, "unconstrained ty var when generalizing `{source_ct:?}`");
bug!("unconstrained ty var when generalizing `{source_ct:?}`");
}

self.inner
.borrow_mut()
.const_unification_table()
.union_value(target_vid, ConstVariableValue::Known { value: generalized_ct });

// FIXME(generic_const_exprs): We have to make sure we actually equate
// `generalized_ct` and `source_ct` here.
Ok(generalized_ct)
// HACK: make sure that we `a_is_expected` continues to be
// correct when relating the generalized type with the source.
if target_is_expected == relation.a_is_expected() {
relation.relate_with_variance(
ty::Variance::Invariant,
ty::VarianceDiagInfo::default(),
generalized_ct,
source_ct,
)?;
} else {
relation.relate_with_variance(
ty::Variance::Invariant,
ty::VarianceDiagInfo::default(),
source_ct,
generalized_ct,
)?;
}

Ok(())
}

/// Attempts to generalize `source_term` for the type variable `target_vid`.
Expand Down Expand Up @@ -287,6 +299,49 @@ impl<'tcx> Generalizer<'_, 'tcx> {
ty::TermKind::Const(ct) => TypeError::CyclicConst(ct),
}
}

/// An occurs check failure inside of an alias does not mean
/// that the types definitely don't unify. We may be able
/// to normalize the alias after all.
///
/// We handle this by lazily equating the alias and generalizing
/// it to an inference variable.
///
/// This is incomplete and will hopefully soon get fixed by #119106.
fn generalize_alias_ty(
&mut self,
alias: ty::AliasTy<'tcx>,
) -> Result<Ty<'tcx>, TypeError<'tcx>> {
let is_nested_alias = mem::replace(&mut self.in_alias, true);
let result = match self.relate(alias, alias) {
Ok(alias) => Ok(alias.to_ty(self.tcx())),
Err(e) => {
if is_nested_alias {
return Err(e);
} else {
let mut visitor = MaxUniverse::new();
alias.visit_with(&mut visitor);
let infer_replacement_is_complete =
self.for_universe.can_name(visitor.max_universe())
&& !alias.has_escaping_bound_vars();
if !infer_replacement_is_complete {
warn!("may incompletely handle alias type: {alias:?}");
}

debug!("generalization failure in alias");
Ok(self.infcx.next_ty_var_in_universe(
TypeVariableOrigin {
kind: TypeVariableOriginKind::MiscVariable,
span: self.span,
},
self.for_universe,
))
}
}
};
self.in_alias = is_nested_alias;
result
}
}

impl<'tcx> TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
Expand Down Expand Up @@ -433,43 +488,7 @@ impl<'tcx> TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
}
}

ty::Alias(kind, data) => {
// An occurs check failure inside of an alias does not mean
// that the types definitely don't unify. We may be able
// to normalize the alias after all.
//
// We handle this by lazily equating the alias and generalizing
// it to an inference variable.
let is_nested_alias = mem::replace(&mut self.in_alias, true);
let result = match self.relate(data, data) {
Ok(data) => Ok(Ty::new_alias(self.tcx(), kind, data)),
Err(e) => {
if is_nested_alias {
return Err(e);
} else {
let mut visitor = MaxUniverse::new();
t.visit_with(&mut visitor);
let infer_replacement_is_complete =
self.for_universe.can_name(visitor.max_universe())
&& !t.has_escaping_bound_vars();
if !infer_replacement_is_complete {
warn!("may incompletely handle alias type: {t:?}");
}

debug!("generalization failure in alias");
Ok(self.infcx.next_ty_var_in_universe(
TypeVariableOrigin {
kind: TypeVariableOriginKind::MiscVariable,
span: self.span,
},
self.for_universe,
))
}
}
};
self.in_alias = is_nested_alias;
result
}
ty::Alias(_, data) => self.generalize_alias_ty(data),

_ => relate::structurally_relate_tys(self, t, t),
}?;
Expand Down
29 changes: 11 additions & 18 deletions compiler/rustc_trait_selection/src/solve/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,25 +85,16 @@ impl<'tcx> NormalizationFolder<'_, 'tcx> {
),
);

// Do not emit an error if normalization is known to fail but instead
// keep the projection unnormalized. This is the case for projections
// with a `T: Trait` where-clause and opaque types outside of the defining
// scope.
let result = if infcx.predicate_may_hold(&obligation) {
self.fulfill_cx.register_predicate_obligation(infcx, obligation);
let errors = self.fulfill_cx.select_all_or_error(infcx);
if !errors.is_empty() {
return Err(errors);
}
let ty = infcx.resolve_vars_if_possible(new_infer_ty);

// Alias is guaranteed to be fully structurally resolved,
// so we can super fold here.
ty.try_super_fold_with(self)?
} else {
alias_ty.try_super_fold_with(self)?
};
self.fulfill_cx.register_predicate_obligation(infcx, obligation);
let errors = self.fulfill_cx.select_all_or_error(infcx);
if !errors.is_empty() {
return Err(errors);
}

// Alias is guaranteed to be fully structurally resolved,
// so we can super fold here.
let ty = infcx.resolve_vars_if_possible(new_infer_ty);
let result = ty.try_super_fold_with(self)?;
self.depth -= 1;
Ok(result)
}
Expand Down Expand Up @@ -178,6 +169,7 @@ impl<'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for NormalizationFolder<'_, 'tcx> {
Ok(t)
}

#[instrument(level = "debug", skip(self), ret)]
fn try_fold_ty(&mut self, ty: Ty<'tcx>) -> Result<Ty<'tcx>, Self::Error> {
let infcx = self.at.infcx;
debug_assert_eq!(ty, infcx.shallow_resolve(ty));
Expand All @@ -204,6 +196,7 @@ impl<'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for NormalizationFolder<'_, 'tcx> {
}
}

#[instrument(level = "debug", skip(self), ret)]
fn try_fold_const(&mut self, ct: ty::Const<'tcx>) -> Result<ty::Const<'tcx>, Self::Error> {
let infcx = self.at.infcx;
debug_assert_eq!(ct, infcx.shallow_resolve(ct));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use crate::solve::EvalCtxt;
use rustc_middle::traits::solve::{Certainty, Goal, QueryResult};
use rustc_middle::ty;

impl<'tcx> EvalCtxt<'_, 'tcx> {
#[instrument(level = "debug", skip(self), ret)]
pub(super) fn normalize_anon_const(
&mut self,
goal: Goal<'tcx, ty::NormalizesTo<'tcx>>,
) -> QueryResult<'tcx> {
if let Some(normalized_const) = self.try_const_eval_resolve(
goal.param_env,
ty::UnevaluatedConst::new(goal.predicate.alias.def_id, goal.predicate.alias.args),
self.tcx()
.type_of(goal.predicate.alias.def_id)
.no_bound_vars()
.expect("const ty should not rely on other generics"),
) {
self.eq(goal.param_env, normalized_const, goal.predicate.term.ct().unwrap())?;
self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
} else {
self.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
}
}
}
Loading

0 comments on commit 8d27fc8

Please sign in to comment.