Skip to content

Commit 1c54494

Browse files
committed
only instantiate opaques with rigid types
1 parent 1f12f1c commit 1c54494

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

compiler/rustc_trait_selection/src/solve/mod.rs

+1-17
Original file line numberDiff line numberDiff line change
@@ -312,26 +312,10 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
312312
return None;
313313
}
314314

315-
let ty::Alias(kind, projection_ty) = *ty.kind() else {
315+
let ty::Alias(_, projection_ty) = *ty.kind() else {
316316
return Some(ty);
317317
};
318318

319-
// We do no always define opaque types eagerly to allow non-defining uses in the defining scope.
320-
if let (DefineOpaqueTypes::No, ty::AliasKind::Opaque) = (define_opaque_types, kind) {
321-
if let Some(def_id) = projection_ty.def_id.as_local() {
322-
if self
323-
.unify_existing_opaque_tys(
324-
param_env,
325-
OpaqueTypeKey { def_id, args: projection_ty.args },
326-
self.next_ty_infer(),
327-
)
328-
.is_empty()
329-
{
330-
return Some(ty);
331-
}
332-
}
333-
}
334-
335319
// FIXME(@lcnr): If the normalization of the alias adds an inference constraint which
336320
// causes a previously added goal to fail, then we treat the alias as rigid.
337321
//

compiler/rustc_trait_selection/src/solve/project_goals/opaques.rs

+21
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
4444
// Prefer opaques registered already.
4545
let opaque_type_key =
4646
ty::OpaqueTypeKey { def_id: opaque_ty_def_id, args: opaque_ty.args };
47+
// FIXME: This also unifies the previous hidden type with the expected.
48+
//
49+
// If that fails, we insert `expected` as a new hidden type instead of
50+
// eagerly emitting an error.
4751
let matches =
4852
self.unify_existing_opaque_tys(goal.param_env, opaque_type_key, expected);
4953
if !matches.is_empty() {
@@ -53,6 +57,23 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
5357
return self.flounder(&matches);
5458
}
5559
}
60+
61+
let expected = match self.try_normalize_ty(goal.param_env, expected) {
62+
Some(ty) => {
63+
if ty.is_ty_var() {
64+
return self.evaluate_added_goals_and_make_canonical_response(
65+
Certainty::AMBIGUOUS,
66+
);
67+
} else {
68+
ty
69+
}
70+
}
71+
None => {
72+
return self
73+
.evaluate_added_goals_and_make_canonical_response(Certainty::OVERFLOW);
74+
}
75+
};
76+
5677
// Otherwise, define a new opaque type
5778
self.insert_hidden_type(opaque_type_key, goal.param_env, expected)?;
5879
self.add_item_bounds_for_hidden_type(

0 commit comments

Comments
 (0)