Skip to content

Commit c0f799a

Browse files
committed
Auto merge of #123864 - oli-obk:define_opaque_types3, r=<try>
Remove a HACK by instead inferring opaque types during expected/formal type checking I was wondering why I couldn't come up with a test that hits the code path of the argument check checking the types we inferred from the return type... Turns out we reject those attempts early during fudging. I have absolutely no information for you as to what kind of type inference changes this may incur, but I think we should just land this out of two reasons: * had I found the other place to use opaque type inference on before I added the hack, we'd be using that today and this PR would never have happened * if it is possible to hit this path, it requires some god awful recursive RPIT logic that I doubt anyone would have written without actively trying to write obscure code r? `@ghost`
2 parents bd71213 + 24653a5 commit c0f799a

File tree

2 files changed

+6
-36
lines changed

2 files changed

+6
-36
lines changed

Diff for: compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

-26
Original file line numberDiff line numberDiff line change
@@ -711,32 +711,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
711711
let formal_ret = self.resolve_vars_with_obligations(formal_ret);
712712
let ret_ty = expected_ret.only_has_type(self)?;
713713

714-
// HACK(oli-obk): This is a hack to keep RPIT and TAIT in sync wrt their behaviour.
715-
// Without it, the inference
716-
// variable will get instantiated with the opaque type. The inference variable often
717-
// has various helpful obligations registered for it that help closures figure out their
718-
// signature. If we infer the inference var to the opaque type, the closure won't be able
719-
// to find those obligations anymore, and it can't necessarily find them from the opaque
720-
// type itself. We could be more powerful with inference if we *combined* the obligations
721-
// so that we got both the obligations from the opaque type and the ones from the inference
722-
// variable. That will accept more code than we do right now, so we need to carefully consider
723-
// the implications.
724-
// Note: this check is pessimistic, as the inference type could be matched with something other
725-
// than the opaque type, but then we need a new `TypeRelation` just for this specific case and
726-
// can't re-use `sup` below.
727-
// See tests/ui/impl-trait/hidden-type-is-opaque.rs and
728-
// tests/ui/impl-trait/hidden-type-is-opaque-2.rs for examples that hit this path.
729-
if formal_ret.has_infer_types() {
730-
for ty in ret_ty.walk() {
731-
if let ty::GenericArgKind::Type(ty) = ty.unpack()
732-
&& let ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }) = *ty.kind()
733-
&& self.can_define_opaque_ty(def_id)
734-
{
735-
return None;
736-
}
737-
}
738-
}
739-
740714
let expect_args = self
741715
.fudge_inference_if_ok(|| {
742716
let ocx = ObligationCtxt::new(self);

Diff for: compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -297,22 +297,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
297297
// 3. Check if the formal type is a supertype of the checked one
298298
// and register any such obligations for future type checks
299299
let supertype_error = self.at(&self.misc(provided_arg.span), self.param_env).sup(
300-
DefineOpaqueTypes::No,
300+
DefineOpaqueTypes::Yes,
301301
formal_input_ty,
302302
coerced_ty,
303303
);
304-
let subtyping_error = match supertype_error {
304+
305+
// If neither check failed, the types are compatible
306+
match supertype_error {
305307
Ok(InferOk { obligations, value: () }) => {
306308
self.register_predicates(obligations);
307-
None
309+
Compatibility::Compatible
308310
}
309-
Err(err) => Some(err),
310-
};
311-
312-
// If neither check failed, the types are compatible
313-
match subtyping_error {
314-
None => Compatibility::Compatible,
315-
Some(_) => Compatibility::Incompatible(subtyping_error),
311+
Err(err) => Compatibility::Incompatible(Some(err)),
316312
}
317313
};
318314

0 commit comments

Comments
 (0)