Skip to content

Commit ac88926

Browse files
committed
Properly generalize unevaluated consts
1 parent 44e6620 commit ac88926

21 files changed

Lines changed: 334 additions & 175 deletions

File tree

compiler/rustc_borrowck/src/type_check/relate_tys.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -617,8 +617,8 @@ impl<'b, 'tcx> PredicateEmittingRelation<InferCtxt<'tcx>> for NllTypeRelating<'_
617617
})]);
618618
}
619619

620-
fn try_eagerly_normalize_alias(&mut self, _alias: ty::AliasTy<'tcx>) -> Ty<'tcx> {
620+
fn try_eagerly_normalize_alias(&mut self, alias: ty::AliasTerm<'tcx>) -> ty::Term<'tcx> {
621621
// Past hir typeck, so we don't have to worry about type inference anymore.
622-
self.type_checker.infcx.next_ty_var(self.span())
622+
self.type_checker.infcx.next_term_var_of_alias_kind(alias, self.span())
623623
}
624624
}

compiler/rustc_infer/src/infer/mod.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,18 @@ impl<'tcx> InferCtxt<'tcx> {
855855
}
856856
}
857857

858+
pub fn next_term_var_of_alias_kind(
859+
&self,
860+
alias: ty::AliasTerm<'tcx>,
861+
span: Span,
862+
) -> ty::Term<'tcx> {
863+
if alias.kind(self.tcx).is_type() {
864+
self.next_ty_var(span).into()
865+
} else {
866+
self.next_const_var(span).into()
867+
}
868+
}
869+
858870
/// Return the universe that the region `r` was created in. For
859871
/// most regions (e.g., `'static`, named regions from the user,
860872
/// etc) this is the root universe U0. For inference variables or
@@ -1533,8 +1545,8 @@ impl<'tcx> InferCtxt<'tcx> {
15331545
&'a self,
15341546
param_env: ty::ParamEnv<'tcx>,
15351547
span: Span,
1536-
alias: ty::AliasTy<'tcx>,
1537-
) -> Ty<'tcx> {
1548+
alias: ty::AliasTerm<'tcx>,
1549+
) -> Term<'tcx> {
15381550
let erased =
15391551
unsafe { mem::transmute::<&'a InferCtxt<'tcx>, TypeErasedInfcx<'a, 'tcx>>(self) };
15401552
self.tcx.try_eagerly_normalize_alias(erased, param_env, span, alias)

compiler/rustc_infer/src/infer/relate/generalize.rs

Lines changed: 204 additions & 140 deletions
Large diffs are not rendered by default.

compiler/rustc_infer/src/infer/relate/lattice.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ impl<'tcx> PredicateEmittingRelation<InferCtxt<'tcx>> for LatticeOp<'_, 'tcx> {
300300
))]);
301301
}
302302

303-
fn try_eagerly_normalize_alias(&mut self, alias: ty::AliasTy<'tcx>) -> Ty<'tcx> {
303+
fn try_eagerly_normalize_alias(&mut self, alias: ty::AliasTerm<'tcx>) -> ty::Term<'tcx> {
304304
self.infcx.try_eagerly_normalize_alias(self.param_env(), self.span(), alias)
305305
}
306306
}

compiler/rustc_infer/src/infer/relate/type_relating.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,8 @@ impl<'tcx> PredicateEmittingRelation<InferCtxt<'tcx>> for TypeRelating<'_, 'tcx>
399399

400400
fn try_eagerly_normalize_alias(
401401
&mut self,
402-
_alias: rustc_type_ir::AliasTy<<InferCtxt<'tcx> as rustc_type_ir::InferCtxtLike>::Interner>,
403-
) -> <<InferCtxt<'tcx> as rustc_type_ir::InferCtxtLike>::Interner as rustc_type_ir::Interner>::Ty
402+
_alias: rustc_type_ir::AliasTerm<<InferCtxt<'tcx> as rustc_type_ir::InferCtxtLike>::Interner>,
403+
) -> <<InferCtxt<'tcx> as rustc_type_ir::InferCtxtLike>::Interner as rustc_type_ir::Interner>::Term
404404
{
405405
// We only try to eagerly normalize aliases if we're using the new solver.
406406
unreachable!()

compiler/rustc_infer/src/infer/unify_key.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ impl<'tcx> ConstVariableValue<'tcx> {
110110
ConstVariableValue::Known { value } => Some(value),
111111
}
112112
}
113+
114+
pub(crate) fn is_unknown(&self) -> bool {
115+
match *self {
116+
ConstVariableValue::Unknown { .. } => true,
117+
ConstVariableValue::Known { .. } => false,
118+
}
119+
}
113120
}
114121

115122
#[derive(PartialEq, Copy, Clone, Debug)]

compiler/rustc_middle/src/hooks/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ declare_hooks! {
126126
type_erased_infcx: TypeErasedInfcx<'_, 'tcx>,
127127
param_env: ty::ParamEnv<'tcx>,
128128
span: Span,
129-
alias: ty::AliasTy<'tcx>
130-
) -> Ty<'tcx>;
129+
alias: ty::AliasTerm<'tcx>
130+
) -> ty::Term<'tcx>;
131131
}
132132

133133
/// The `try_eagerly_normalize_alias` hook passes an `Infcx` from where it's called (in `rustc_infer`)

compiler/rustc_middle/src/ty/consts.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,17 @@ impl<'tcx> Const<'tcx> {
323323
matches!(self.kind(), ty::ConstKind::Infer(_))
324324
}
325325

326+
pub fn is_ct_var(self) -> bool {
327+
matches!(self.kind(), ConstKind::Infer(ty::InferConst::Var(_)))
328+
}
329+
330+
pub fn ct_vid(self) -> Option<ty::ConstVid> {
331+
match self.kind() {
332+
ConstKind::Infer(ty::InferConst::Var(vid)) => Some(vid),
333+
_ => None,
334+
}
335+
}
336+
326337
/// Iterator that walks `self` and any types reachable from
327338
/// `self`, in depth-first order. Note that just walks the types
328339
/// that appear in `self`, it does not descend into the fields of

compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,15 +1051,16 @@ where
10511051
let delegate = self.delegate;
10521052
let origin_span = self.origin_span;
10531053

1054-
let mut normalize = |alias: ty::AliasTy<I>| {
1055-
let inference_var = self.next_ty_infer();
1054+
let mut normalize = |alias: ty::AliasTerm<I>| {
1055+
let term = alias.to_term(cx);
1056+
let inference_var = self.next_term_infer_of_kind(term);
10561057

10571058
let goal = Goal::new(
10581059
cx,
10591060
param_env,
10601061
ty::PredicateKind::AliasRelate(
1061-
alias.to_ty(cx).into(),
1062-
inference_var.into(),
1062+
term,
1063+
inference_var,
10631064
ty::AliasRelationDirection::Equate,
10641065
),
10651066
);

compiler/rustc_trait_selection/src/solve.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub use normalize::{
1818
deeply_normalize, deeply_normalize_with_skipped_universes,
1919
deeply_normalize_with_skipped_universes_and_ambiguous_coroutine_goals,
2020
};
21-
use rustc_middle::ty::{self, Ty, TyCtxt};
21+
use rustc_middle::ty::{self, TyCtxt};
2222
use rustc_middle::util::Providers;
2323
use rustc_span::Span;
2424
pub use select::InferCtxtSelectExt;
@@ -40,15 +40,15 @@ fn try_eagerly_normalize_alias<'a, 'tcx>(
4040
type_erased_infcx: TypeErasedInfcx<'a, 'tcx>,
4141
param_env: ty::ParamEnv<'tcx>,
4242
span: Span,
43-
alias: ty::AliasTy<'tcx>,
44-
) -> Ty<'tcx> {
43+
alias: ty::AliasTerm<'tcx>,
44+
) -> ty::Term<'tcx> {
4545
let infcx = unsafe {
4646
mem::transmute::<TypeErasedInfcx<'a, 'tcx>, &'a InferCtxt<'tcx>>(type_erased_infcx)
4747
};
4848

4949
let ocx = ObligationCtxt::new(infcx);
5050

51-
let infer_term = infcx.next_ty_var(span);
51+
let infer_term = infcx.next_term_var_of_alias_kind(alias, span);
5252

5353
// Dummy because we ignore the error anyway.
5454
// We do provide a span, because this span is used when registering opaque types.
@@ -59,8 +59,8 @@ fn try_eagerly_normalize_alias<'a, 'tcx>(
5959
cause,
6060
param_env,
6161
ty::PredicateKind::AliasRelate(
62-
alias.to_ty(tcx).into(),
63-
infer_term.into(),
62+
alias.to_term(tcx),
63+
infer_term,
6464
ty::AliasRelationDirection::Equate,
6565
),
6666
);

0 commit comments

Comments
 (0)