Skip to content

Commit

Permalink
Revert "Fix regression from lazy opaque types"
Browse files Browse the repository at this point in the history
This reverts commit 239f1e7.
  • Loading branch information
oli-obk committed Feb 11, 2022
1 parent e646f3d commit 7a71b7a
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 126 deletions.
13 changes: 7 additions & 6 deletions compiler/rustc_typeck/src/check/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,21 +259,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
/// The `cause_span` should be the span that caused us to
/// have this expected signature, or `None` if we can't readily
/// know that.
#[instrument(level = "debug", skip(self, cause_span))]
fn deduce_sig_from_projection(
&self,
cause_span: Option<Span>,
projection: ty::PolyProjectionPredicate<'tcx>,
) -> Option<ExpectedSig<'tcx>> {
let tcx = self.tcx;

debug!("deduce_sig_from_projection({:?})", projection);

let trait_def_id = projection.trait_def_id(tcx);

let is_fn = tcx.fn_trait_kind_from_lang_item(trait_def_id).is_some();
let gen_trait = tcx.require_lang_item(LangItem::Generator, cause_span);
let is_gen = gen_trait == trait_def_id;
if !is_fn && !is_gen {
debug!("not fn or generator");
debug!("deduce_sig_from_projection: not fn or generator");
return None;
}

Expand All @@ -282,15 +283,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// associated item and not yield.
let return_assoc_item = self.tcx.associated_item_def_ids(gen_trait)[1];
if return_assoc_item != projection.projection_def_id() {
debug!("not return assoc item of generator");
debug!("deduce_sig_from_projection: not return assoc item of generator");
return None;
}
}

let input_tys = if is_fn {
let arg_param_ty = projection.skip_binder().projection_ty.substs.type_at(1);
let arg_param_ty = self.resolve_vars_if_possible(arg_param_ty);
debug!(?arg_param_ty);
debug!("deduce_sig_from_projection: arg_param_ty={:?}", arg_param_ty);

match arg_param_ty.kind() {
ty::Tuple(tys) => tys.into_iter().map(|k| k.expect_ty()).collect::<Vec<_>>(),
Expand All @@ -305,7 +306,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// Since this is a return parameter type it is safe to unwrap.
let ret_param_ty = projection.skip_binder().term.ty().unwrap();
let ret_param_ty = self.resolve_vars_if_possible(ret_param_ty);
debug!(?ret_param_ty);
debug!("deduce_sig_from_projection: ret_param_ty={:?}", ret_param_ty);

let sig = projection.rebind(self.tcx.mk_fn_sig(
input_tys.iter(),
Expand All @@ -314,7 +315,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
hir::Unsafety::Normal,
Abi::Rust,
));
debug!(?sig);
debug!("deduce_sig_from_projection: sig={:?}", sig);

Some(ExpectedSig { cause_span, sig })
}
Expand Down
27 changes: 1 addition & 26 deletions compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -730,32 +730,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
) -> Vec<Ty<'tcx>> {
let formal_ret = self.resolve_vars_with_obligations(formal_ret);
let ret_ty = match expected_ret.only_has_type(self) {
Some(ret) => {
// HACK(oli-obk): This is a backwards compatibility hack. Without it, the inference
// variable will get instantiated with the opaque type. The inference variable often
// has various helpful obligations registered for it that help closures figure out their
// signature. If we infer the inference var to the opaque type, the closure won't be able
// to find those obligations anymore, and it can't necessarily find them from the opaque
// type itself. We could be more powerful with inference if we *combined* the obligations
// so that we got both the obligations from the opaque type and the ones from the inference
// variable. That will accept more code than we do right now, so we need to carefully consider
// the implications.
// Note: this check is pessimistic, as the inference type could be matched with something other
// than the opaque type, but then we need a new `TypeRelation` just for this specific case and
// can't re-use `sup` below.
if formal_ret.has_infer_types() {
for ty in ret.walk() {
if let ty::subst::GenericArgKind::Type(ty) = ty.unpack() {
if let ty::Opaque(def_id, _) = *ty.kind() {
if self.infcx.opaque_type_origin(def_id, DUMMY_SP).is_some() {
return Vec::new();
}
}
}
}
}
ret
}
Some(ret) => ret,
None => return Vec::new(),
};
let expect_args = self
Expand Down
34 changes: 0 additions & 34 deletions src/test/ui/impl-trait/hidden-type-is-opaque-2.rs

This file was deleted.

11 changes: 0 additions & 11 deletions src/test/ui/impl-trait/hidden-type-is-opaque-2.stderr

This file was deleted.

32 changes: 0 additions & 32 deletions src/test/ui/impl-trait/hidden-type-is-opaque.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/test/ui/impl-trait/issues/issue-70877.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl Iterator for Bar {
type Item = FooItem;

fn next(&mut self) -> Option<Self::Item> {
Some(Box::new(quux)) //~ ERROR mismatched types
Some(Box::new(quux))
}
}

Expand Down
17 changes: 1 addition & 16 deletions src/test/ui/impl-trait/issues/issue-70877.stderr
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
error[E0308]: mismatched types
--> $DIR/issue-70877.rs:16:9
|
LL | type FooRet = impl std::fmt::Debug;
| -------------------- the expected opaque type
...
LL | fn next(&mut self) -> Option<Self::Item> {
| ------------------ expected `Option<Box<(dyn for<'r> Fn(&'r (dyn ToString + 'r)) -> FooRet + 'static)>>` because of return type
LL | Some(Box::new(quux))
| ^^^^^^^^^^^^^^^^^^^^ expected trait object `dyn Fn`, found fn item
|
= note: expected enum `Option<Box<(dyn for<'r> Fn(&'r (dyn ToString + 'r)) -> FooRet + 'static)>>`
found enum `Option<Box<for<'r> fn(&'r (dyn ToString + 'r)) -> FooRet {quux}>>`

error: opaque type's hidden type cannot be another opaque type from the same scope
--> $DIR/issue-70877.rs:31:12
|
Expand All @@ -29,6 +15,5 @@ note: opaque type being used as hidden type
LL | type FooRet = impl std::fmt::Debug;
| ^^^^^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors
error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.

0 comments on commit 7a71b7a

Please sign in to comment.