Skip to content

Commit 1adea68

Browse files
committed
address review
1 parent 9aa065b commit 1adea68

4 files changed

Lines changed: 44 additions & 33 deletions

File tree

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

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -613,26 +613,30 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for Generalizer<'_, 'tcx> {
613613
// of each other. This is currently only used for diagnostics.
614614
// To see why, see the docs in the `type_variables` module.
615615
inner.type_variables().sub_unify(vid, new_var_id);
616-
// If we're in the new solver and create a new inference
617-
// variable inside of an alias we eagerly constrain that
618-
// inference variable to prevent unexpected ambiguity errors.
619-
//
620-
// This is incomplete as it pulls down the universe of the
621-
// original inference variable, even though the alias could
622-
// normalize to a type which does not refer to that type at
623-
// all. I don't expect this to cause unexpected errors in
624-
// practice.
625-
//
626-
// We only need to do so for type and const variables, as
627-
// region variables do not impact normalization, and will get
628-
// correctly constrained by `AliasRelate` later on.
629-
//
630-
// cc trait-system-refactor-initiative#108
631-
if self.infcx.next_trait_solver()
632-
&& !matches!(self.infcx.typing_mode(), TypingMode::Coherence)
633-
{
634-
match self.state {
635-
GeneralizerState::IncompletelyRelateHigherRankedAlias => {
616+
617+
match self.state {
618+
GeneralizerState::IncompletelyRelateHigherRankedAlias => {
619+
if self.infcx.next_trait_solver()
620+
&& !matches!(
621+
self.infcx.typing_mode(),
622+
TypingMode::Coherence
623+
)
624+
{
625+
// If we're in the new solver and create a new inference
626+
// variable inside of an alias we eagerly constrain that
627+
// inference variable to prevent unexpected ambiguity errors.
628+
//
629+
// This is incomplete as it pulls down the universe of the
630+
// original inference variable, even though the alias could
631+
// normalize to a type which does not refer to that type at
632+
// all. I don't expect this to cause unexpected errors in
633+
// practice.
634+
//
635+
// We only need to do so for type and const variables, as
636+
// region variables do not impact normalization, and will get
637+
// correctly constrained by `AliasRelate` later on.
638+
//
639+
// cc trait-system-refactor-initiative#108
636640
inner.type_variables().equate(vid, new_var_id);
637641
}
638642
}
@@ -759,13 +763,16 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for Generalizer<'_, 'tcx> {
759763
})
760764
.vid;
761765

762-
// See the comment for type inference variables
763-
// for more details.
764-
if self.infcx.next_trait_solver()
765-
&& !matches!(self.infcx.typing_mode(), TypingMode::Coherence)
766-
{
767-
match self.state {
768-
GeneralizerState::IncompletelyRelateHigherRankedAlias => {
766+
match self.state {
767+
GeneralizerState::IncompletelyRelateHigherRankedAlias => {
768+
// See the comment for type inference variables
769+
// for more details.
770+
if self.infcx.next_trait_solver()
771+
&& !matches!(
772+
self.infcx.typing_mode(),
773+
TypingMode::Coherence
774+
)
775+
{
769776
variable_table.union(vid, new_var_id);
770777
}
771778
}

compiler/rustc_middle/src/hooks/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,12 @@ declare_hooks! {
132132
) -> Ty<'tcx>;
133133
}
134134

135-
// `repr(transparent)` so we can transmute a `&'a Infcx<'tcx>` to this struct.
135+
/// The `try_eagerly_normalize_alias` hook passes an `Infcx` from where it's called (in `rustc_infer`)
136+
/// to where it's provided (in `rustc_trait_selection`).
137+
/// Both of those crates have that type available, but `rustc_middle` does not.
138+
/// Instead we pass this type-erased `Infcx` and transmute on both sides.
139+
///
140+
/// Has to be `repr(transparent)` so we can transmute a `&'a Infcx<'tcx>` to this struct.
136141
#[repr(transparent)]
137142
pub struct TypeErasedInfcx<'a, 'tcx> {
138143
_infcx: *const (),

compiler/rustc_trait_selection/src/solve.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ fn try_eagerly_normalize_alias<'a, 'tcx>(
5656
let cause = ObligationCause::dummy_with_span(span);
5757
let obligation = Obligation::new(
5858
tcx,
59-
// we ignore the error anyway
60-
ObligationCause::dummy_with_span(span),
59+
cause,
6160
param_env,
6261
ty::PredicateKind::AliasRelate(
6362
alias.to_ty(tcx).into(),
@@ -68,7 +67,8 @@ fn try_eagerly_normalize_alias<'a, 'tcx>(
6867

6968
ocx.register_obligation(obligation);
7069

71-
// This only tries to eagerly resolve, if it errors we don't care.
70+
// We only use this to constrain inference variables.
71+
// We don't care if it errors.
7272
let _ = ocx.try_evaluate_obligations();
7373

7474
infcx.resolve_vars_if_possible(infer_term)

tests/ui/impl-trait/unsized_coercion.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
44
//@ revisions: next old
55
//@[next] compile-flags: -Znext-solver
6-
//@[old] check-pass
7-
//@[next] check-pass
6+
//@ check-pass
87

98
trait Trait {}
109

0 commit comments

Comments
 (0)