Skip to content

Commit

Permalink
Rollup merge of #101614 - compiler-errors:rpitit-eq, r=jackh726
Browse files Browse the repository at this point in the history
Equate fn outputs when inferring RPITIT hidden types

When we are trying to infer the hidden types for RPITITs, we need to equate the output tys instead of just subtyping them. For example:

```rust
trait Foo { fn bar() -> impl Sized {} }

impl Foo for () { fn bar() -> &'static str { "" } }
```

If we just subtype the signatures `fn() -> &'static str <: fn() -> _#1t` (where `_#1t` is the variable we've used to infer `impl Sized`), we'll end up `&'static str <: _#1t`, which causes us to infer `_#1t = #'_#2r str`, where `'_#2r` is unconstrained, which gets fixed up to `ReEmpty`, and which is certainly not what we want.

I can't actually think of a way to make this fail to compile, because during borrowck we've already done the method probe, and so we just look at the `impl` method signature and see the `&'static str` any time we call `<() as Foo>::bar()`. But this _does_ cause the ICE [here](#98559 (comment)) in `@jackh726's` "Remove ReEmpty" PR (#98559) to stop ICEing, because after that PR we were leaking unconstrained region variables into the typeck results.

r? types
  • Loading branch information
Dylan-DPC authored Sep 10, 2022
2 parents 33d54c4 + 022e3fe commit f1412a6
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions compiler/rustc_typeck/src/check/compare_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,12 +295,26 @@ pub(super) fn compare_predicates_and_trait_impl_trait_tys<'tcx>(
// type would be more appropriate. In other places we have a `Vec<Span>`
// corresponding to their `Vec<Predicate>`, but we don't have that here.
// Fixing this would improve the output of test `issue-83765.rs`.
let sub_result = infcx
let mut result = infcx
.at(&cause, param_env)
.sup(trait_fty, impl_fty)
.map(|infer_ok| ocx.register_infer_ok_obligations(infer_ok));

if let Err(terr) = sub_result {
// HACK(RPITIT): #101614. When we are trying to infer the hidden types for
// RPITITs, we need to equate the output tys instead of just subtyping. If
// we just use `sup` above, we'll end up `&'static str <: _#1t`, which causes
// us to infer `_#1t = #'_#2r str`, where `'_#2r` is unconstrained, which gets
// fixed up to `ReEmpty`, and which is certainly not what we want.
if trait_fty.has_infer_types() {
result = result.and_then(|()| {
infcx
.at(&cause, param_env)
.eq(trait_sig.output(), impl_sig.output())
.map(|infer_ok| ocx.register_infer_ok_obligations(infer_ok))
});
}

if let Err(terr) = result {
debug!("sub_types failed: impl ty {:?}, trait ty {:?}", impl_fty, trait_fty);

let (impl_err_span, trait_err_span) =
Expand Down Expand Up @@ -445,6 +459,7 @@ pub(super) fn compare_predicates_and_trait_impl_trait_tys<'tcx>(
region
}
});
debug!(%ty);
collected_tys.insert(def_id, ty);
}
Err(err) => {
Expand Down

0 comments on commit f1412a6

Please sign in to comment.