Repository: rust-lang/rust
File: compiler/rustc_next_trait_solver/src/solve/normalizes_to/free_alias.rs (Rust)

Code before:
use crate::solve::{Certainty, EvalCtxt, Goal, GoalSource, QueryResult};

impl<D, I> EvalCtxt<'_, D>
where
    D: SolverDelegate<Interner = I>,
    I: Interner,
{
    pub(super) fn normalize_free_alias(
        &mut self,
        goal: Goal<I, ty::NormalizesTo<I>>,
    ) -> QueryResult<I> {
        let cx = self.cx();
        let free_alias = goal.predicate.alias;

        // Check where clauses
        self.add_goals(
            GoalSource::Misc,
            cx.predicates_of(free_alias.def_id)
                .iter_instantiated(cx, free_alias.args)
                .map(|pred| goal.with(cx, pred)),
        );

        let actual = if free_alias.kind(cx).is_type() {
            cx.type_of(free_alias.def_id).instantiate(cx, free_alias.args).into()
        } else {
            let ct = cx.const_of_item(free_alias.def_id).instantiate(cx, free_alias.args);
            if self.typing_mode() == TypingMode::Coherence
                && let ty::ConstKind::Unevaluated(uv) = ct.kind()
                && self.cx().anon_const_kind(uv.def.into()) == ty::AnonConstKind::OGCA
            {
                // During coherence, OGCA consts should be normalized ambiguously
                // because they are opaque but eventually resolved to a real value.
                // We don't want two OGCAs that have the same value to be treated
                // as distinct for coherence purposes.
                // (Just like opaque types.)
                return self.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS);
            }
            ct.into()
        };

        self.instantiate_normalizes_to_term(goal, actual);
        self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
    }
}


Review comment (suggestion):
I think we basically don't want this change 🤔 The soundness critical thing for coherence is that normalizing oGCA anon consts is ambiguous. We handle this (incidentally) in `normalize_anon_const` which will also handle this when normalizing associated constants, not just free consts like we do here.

I would drop this change. I expect this will make diagnostics worse in coherence but I think that's fine for now. Or at least I think we should improve diagnostics of errors involving oGCA anon consts in a different way than complicating soundness critical normalization logic 🤔  

Code after:

impl<D, I> EvalCtxt<'_, D>
where
    D: SolverDelegate<Interner = I>,
    I: Interner,
{
    pub(super) fn normalize_free_alias(
        &mut self,
        goal: Goal<I, ty::NormalizesTo<I>>,
    ) -> QueryResult<I> {
        let cx = self.cx();
        let free_alias = goal.predicate.alias;

        // Check where clauses
        self.add_goals(
            GoalSource::Misc,
            cx.predicates_of(free_alias.def_id)
                .iter_instantiated(cx, free_alias.args)
                .map(|pred| goal.with(cx, pred)),
        );

        let actual = if free_alias.kind(cx).is_type() {
            cx.type_of(free_alias.def_id).instantiate(cx, free_alias.args).into()
        } else {
            cx.const_of_item(free_alias.def_id).instantiate(cx, free_alias.args).into()
        };

        self.instantiate_normalizes_to_term(goal, actual);
        self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
    }
}