Skip to content

Commit 48170d5

Browse files
authored
Rollup merge of #96727 - oli-obk:no_expect, r=lcnr
Make TAIT behave exactly like RPIT fixes #96552 This makes type-alias-impl-trait behave like return-position-impl-trait. Unfortunately it also causes some cases to stop compiling due to "needing type annotations" and makes panicking cause fallback for the hidden type to `()`. All of these are addressable, but we should probably address them for RPIT and TAIT together r? ``@lcnr``
2 parents ddcbba0 + c33b127 commit 48170d5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+378
-246
lines changed

compiler/rustc_infer/src/infer/opaque_types.rs

+7-14
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use hir::{HirId, OpaqueTyOrigin};
55
use rustc_data_structures::sync::Lrc;
66
use rustc_data_structures::vec_map::VecMap;
77
use rustc_hir as hir;
8-
use rustc_middle::traits::{ObligationCause, ObligationCauseCode};
8+
use rustc_middle::traits::ObligationCause;
99
use rustc_middle::ty::fold::BottomUpFolder;
1010
use rustc_middle::ty::subst::{GenericArgKind, Subst};
1111
use rustc_middle::ty::{
@@ -44,30 +44,23 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
4444
ty: Ty<'tcx>,
4545
body_id: HirId,
4646
span: Span,
47-
code: ObligationCauseCode<'tcx>,
4847
param_env: ty::ParamEnv<'tcx>,
4948
) -> InferOk<'tcx, Ty<'tcx>> {
5049
if !ty.has_opaque_types() {
5150
return InferOk { value: ty, obligations: vec![] };
5251
}
5352
let mut obligations = vec![];
53+
let replace_opaque_type = |def_id| self.opaque_type_origin(def_id, span).is_some();
5454
let value = ty.fold_with(&mut ty::fold::BottomUpFolder {
5555
tcx: self.tcx,
5656
lt_op: |lt| lt,
5757
ct_op: |ct| ct,
5858
ty_op: |ty| match *ty.kind() {
59-
// Closures can't create hidden types for opaque types of their parent, as they
60-
// do not have all the outlives information available. Also `type_of` looks for
61-
// hidden types in the owner (so the closure's parent), so it would not find these
62-
// definitions.
63-
ty::Opaque(def_id, _substs)
64-
if matches!(
65-
self.opaque_type_origin(def_id, span),
66-
Some(OpaqueTyOrigin::FnReturn(..))
67-
) =>
68-
{
69-
let span = if span.is_dummy() { self.tcx.def_span(def_id) } else { span };
70-
let cause = ObligationCause::new(span, body_id, code.clone());
59+
ty::Opaque(def_id, _substs) if replace_opaque_type(def_id) => {
60+
let def_span = self.tcx.def_span(def_id);
61+
let span = if span.contains(def_span) { def_span } else { span };
62+
let code = traits::ObligationCauseCode::OpaqueReturnType(None);
63+
let cause = ObligationCause::new(span, body_id, code);
7164
// FIXME(compiler-errors): We probably should add a new TypeVariableOriginKind
7265
// for opaque types, and then use that kind to fix the spans for type errors
7366
// that we see later on.

compiler/rustc_typeck/src/check/check.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ pub(super) fn check_fn<'a, 'tcx>(
9595
fcx.register_infer_ok_obligations(fcx.infcx.replace_opaque_types_with_inference_vars(
9696
declared_ret_ty,
9797
body.value.hir_id,
98-
DUMMY_SP,
99-
traits::ObligationCauseCode::OpaqueReturnType(None),
98+
decl.output.span(),
10099
param_env,
101100
));
102101
// If we replaced declared_ret_ty with infer vars, then we must be infering

compiler/rustc_typeck/src/check/closure.rs

+59-59
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@ use rustc_hir::lang_items::LangItem;
1010
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
1111
use rustc_infer::infer::LateBoundRegionConversionTime;
1212
use rustc_infer::infer::{InferOk, InferResult};
13-
use rustc_infer::traits::ObligationCauseCode;
1413
use rustc_middle::ty::fold::TypeFoldable;
1514
use rustc_middle::ty::subst::InternalSubsts;
1615
use rustc_middle::ty::{self, Ty};
1716
use rustc_span::source_map::Span;
18-
use rustc_span::DUMMY_SP;
1917
use rustc_target::spec::abi::Abi;
2018
use rustc_trait_selection::traits::error_reporting::ArgKind;
2119
use rustc_trait_selection::traits::error_reporting::InferCtxtExt as _;
@@ -429,14 +427,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
429427
// in this binder we are creating.
430428
assert!(!expected_sig.sig.skip_binder().has_vars_bound_above(ty::INNERMOST));
431429
let bound_sig = expected_sig.sig.map_bound(|sig| {
432-
let output = self.hide_parent_opaque_types(
433-
sig.output(),
434-
expected_sig.cause_span.unwrap_or(DUMMY_SP),
435-
body.id().hir_id,
436-
);
437430
self.tcx.mk_fn_sig(
438431
sig.inputs().iter().cloned(),
439-
output,
432+
sig.output(),
440433
sig.c_variadic,
441434
hir::Unsafety::Normal,
442435
Abi::RustCall,
@@ -608,23 +601,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
608601
// function.
609602
Some(hir::GeneratorKind::Async(hir::AsyncGeneratorKind::Fn)) => {
610603
debug!("closure is async fn body");
611-
self.deduce_future_output_from_obligations(expr_def_id).unwrap_or_else(|| {
612-
// AFAIK, deducing the future output
613-
// always succeeds *except* in error cases
614-
// like #65159. I'd like to return Error
615-
// here, but I can't because I can't
616-
// easily (and locally) prove that we
617-
// *have* reported an
618-
// error. --nikomatsakis
619-
astconv.ty_infer(None, decl.output.span())
620-
})
604+
self.deduce_future_output_from_obligations(expr_def_id, body.id().hir_id)
605+
.unwrap_or_else(|| {
606+
// AFAIK, deducing the future output
607+
// always succeeds *except* in error cases
608+
// like #65159. I'd like to return Error
609+
// here, but I can't because I can't
610+
// easily (and locally) prove that we
611+
// *have* reported an
612+
// error. --nikomatsakis
613+
astconv.ty_infer(None, decl.output.span())
614+
})
621615
}
622616

623617
_ => astconv.ty_infer(None, decl.output.span()),
624618
},
625619
};
626-
let supplied_return =
627-
self.hide_parent_opaque_types(supplied_return, decl.output.span(), body.id().hir_id);
628620

629621
let result = ty::Binder::bind_with_vars(
630622
self.tcx.mk_fn_sig(
@@ -645,67 +637,75 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
645637
result
646638
}
647639

648-
fn hide_parent_opaque_types(&self, ty: Ty<'tcx>, span: Span, body_id: hir::HirId) -> Ty<'tcx> {
649-
let InferOk { value, obligations } = self.replace_opaque_types_with_inference_vars(
650-
ty,
651-
body_id,
652-
span,
653-
ObligationCauseCode::MiscObligation,
654-
self.param_env,
655-
);
656-
self.register_predicates(obligations);
657-
value
658-
}
659-
660640
/// Invoked when we are translating the generator that results
661641
/// from desugaring an `async fn`. Returns the "sugared" return
662642
/// type of the `async fn` -- that is, the return type that the
663643
/// user specified. The "desugared" return type is an `impl
664644
/// Future<Output = T>`, so we do this by searching through the
665645
/// obligations to extract the `T`.
666646
#[instrument(skip(self), level = "debug")]
667-
fn deduce_future_output_from_obligations(&self, expr_def_id: DefId) -> Option<Ty<'tcx>> {
647+
fn deduce_future_output_from_obligations(
648+
&self,
649+
expr_def_id: DefId,
650+
body_id: hir::HirId,
651+
) -> Option<Ty<'tcx>> {
668652
let ret_coercion = self.ret_coercion.as_ref().unwrap_or_else(|| {
669653
span_bug!(self.tcx.def_span(expr_def_id), "async fn generator outside of a fn")
670654
});
671655

672656
let ret_ty = ret_coercion.borrow().expected_ty();
673657
let ret_ty = self.inh.infcx.shallow_resolve(ret_ty);
674-
let (def_id, substs) = match *ret_ty.kind() {
675-
ty::Opaque(def_id, substs) => (def_id, substs),
658+
659+
let get_future_output = |predicate: ty::Predicate<'tcx>, span| {
660+
// Search for a pending obligation like
661+
//
662+
// `<R as Future>::Output = T`
663+
//
664+
// where R is the return type we are expecting. This type `T`
665+
// will be our output.
666+
let bound_predicate = predicate.kind();
667+
if let ty::PredicateKind::Projection(proj_predicate) = bound_predicate.skip_binder() {
668+
self.deduce_future_output_from_projection(
669+
span,
670+
bound_predicate.rebind(proj_predicate),
671+
)
672+
} else {
673+
None
674+
}
675+
};
676+
677+
let output_ty = match *ret_ty.kind() {
678+
ty::Infer(ty::TyVar(ret_vid)) => {
679+
self.obligations_for_self_ty(ret_vid).find_map(|(_, obligation)| {
680+
get_future_output(obligation.predicate, obligation.cause.span)
681+
})?
682+
}
683+
ty::Opaque(def_id, substs) => self
684+
.tcx
685+
.bound_explicit_item_bounds(def_id)
686+
.transpose_iter()
687+
.map(|e| e.map_bound(|e| *e).transpose_tuple2())
688+
.find_map(|(p, s)| get_future_output(p.subst(self.tcx, substs), s.0))?,
676689
ty::Error(_) => return None,
677690
_ => span_bug!(
678691
self.tcx.def_span(expr_def_id),
679692
"async fn generator return type not an inference variable"
680693
),
681694
};
682695

683-
let item_bounds = self.tcx.bound_explicit_item_bounds(def_id);
684-
685-
// Search for a pending obligation like
686-
//
687-
// `<R as Future>::Output = T`
688-
//
689-
// where R is the return type we are expecting. This type `T`
690-
// will be our output.
691-
let output_ty = item_bounds
692-
.transpose_iter()
693-
.map(|e| e.map_bound(|e| *e).transpose_tuple2())
694-
.find_map(|(predicate, span)| {
695-
let bound_predicate = predicate.subst(self.tcx, substs).kind();
696-
if let ty::PredicateKind::Projection(proj_predicate) = bound_predicate.skip_binder()
697-
{
698-
self.deduce_future_output_from_projection(
699-
span.0,
700-
bound_predicate.rebind(proj_predicate),
701-
)
702-
} else {
703-
None
704-
}
705-
});
696+
// async fn that have opaque types in their return type need to redo the conversion to inference variables
697+
// as they fetch the still opaque version from the signature.
698+
let InferOk { value: output_ty, obligations } = self
699+
.replace_opaque_types_with_inference_vars(
700+
output_ty,
701+
body_id,
702+
self.tcx.def_span(expr_def_id),
703+
self.param_env,
704+
);
705+
self.register_predicates(obligations);
706706

707707
debug!("deduce_future_output_from_obligations: output_ty={:?}", output_ty);
708-
output_ty
708+
Some(output_ty)
709709
}
710710

711711
/// Given a projection like

src/test/ui/associated-type-bounds/assoc-type-eq-with-dyn-atb-fail.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl Thing for AssocNoCopy {
3030
type Out = Box<dyn Bar<Assoc: Copy>>;
3131

3232
fn func() -> Self::Out {
33-
Box::new(AssocNoCopy)
3433
//~^ ERROR the trait bound `String: Copy` is not satisfied
34+
Box::new(AssocNoCopy)
3535
}
3636
}

src/test/ui/associated-type-bounds/assoc-type-eq-with-dyn-atb-fail.stderr

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
error[E0277]: the trait bound `String: Copy` is not satisfied
2-
--> $DIR/assoc-type-eq-with-dyn-atb-fail.rs:33:9
2+
--> $DIR/assoc-type-eq-with-dyn-atb-fail.rs:32:18
33
|
4-
LL | Box::new(AssocNoCopy)
5-
| ^^^^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `String`
6-
|
7-
= note: required for the cast from `AssocNoCopy` to the object type `dyn Bar<Assoc = <AssocNoCopy as Thing>::Out::{opaque#0}>`
4+
LL | fn func() -> Self::Out {
5+
| ^^^^^^^^^ the trait `Copy` is not implemented for `String`
86

97
error: aborting due to previous error
108

src/test/ui/async-await/issues/issue-65159.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ async fn copy() -> Result<()>
66
//~^ ERROR this enum takes 2 generic arguments
77
{
88
Ok(())
9+
//~^ ERROR type annotations needed
910
}
1011

1112
fn main() { }

src/test/ui/async-await/issues/issue-65159.stderr

+14-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ help: add missing generic argument
1616
LL | async fn copy() -> Result<(), E>
1717
| +++
1818

19-
error: aborting due to previous error
19+
error[E0282]: type annotations needed
20+
--> $DIR/issue-65159.rs:8:5
21+
|
22+
LL | Ok(())
23+
| ^^ cannot infer type of the type parameter `E` declared on the enum `Result`
24+
|
25+
help: consider specifying the generic arguments
26+
|
27+
LL | Ok::<(), E>(())
28+
| +++++++++
29+
30+
error: aborting due to 2 previous errors
2031

21-
For more information about this error, try `rustc --explain E0107`.
32+
Some errors have detailed explanations: E0107, E0282.
33+
For more information about an error, try `rustc --explain E0107`.

src/test/ui/borrowck/issue-82126-mismatched-subst-and-hir.rs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ async fn buy_lock(generator: &Mutex<MarketMultiplier>) -> LockedMarket<'_> {
1717
//~^ ERROR this struct takes 0 lifetime arguments but 1 lifetime argument was supplied
1818
//~^^ ERROR this struct takes 1 generic argument but 0 generic arguments were supplied
1919
LockedMarket(generator.lock().unwrap().buy())
20+
//~^ ERROR cannot return value referencing temporary
2021
}
2122

2223
struct LockedMarket<T>(T);

src/test/ui/borrowck/issue-82126-mismatched-subst-and-hir.stderr

+14-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | async fn buy_lock(generator: &Mutex<MarketMultiplier>) -> LockedMarket<'_>
77
| expected 0 lifetime arguments
88
|
99
note: struct defined here, with 0 lifetime parameters
10-
--> $DIR/issue-82126-mismatched-subst-and-hir.rs:22:8
10+
--> $DIR/issue-82126-mismatched-subst-and-hir.rs:23:8
1111
|
1212
LL | struct LockedMarket<T>(T);
1313
| ^^^^^^^^^^^^
@@ -19,7 +19,7 @@ LL | async fn buy_lock(generator: &Mutex<MarketMultiplier>) -> LockedMarket<'_>
1919
| ^^^^^^^^^^^^ expected 1 generic argument
2020
|
2121
note: struct defined here, with 1 generic parameter: `T`
22-
--> $DIR/issue-82126-mismatched-subst-and-hir.rs:22:8
22+
--> $DIR/issue-82126-mismatched-subst-and-hir.rs:23:8
2323
|
2424
LL | struct LockedMarket<T>(T);
2525
| ^^^^^^^^^^^^ -
@@ -28,6 +28,16 @@ help: add missing generic argument
2828
LL | async fn buy_lock(generator: &Mutex<MarketMultiplier>) -> LockedMarket<'_, T> {
2929
| +++
3030

31-
error: aborting due to 2 previous errors
31+
error[E0515]: cannot return value referencing temporary value
32+
--> $DIR/issue-82126-mismatched-subst-and-hir.rs:19:5
33+
|
34+
LL | LockedMarket(generator.lock().unwrap().buy())
35+
| ^^^^^^^^^^^^^-------------------------^^^^^^^
36+
| | |
37+
| | temporary value created here
38+
| returns a value referencing data owned by the current function
39+
40+
error: aborting due to 3 previous errors
3241

33-
For more information about this error, try `rustc --explain E0107`.
42+
Some errors have detailed explanations: E0107, E0515.
43+
For more information about an error, try `rustc --explain E0107`.

src/test/ui/chalkify/bugs/async.stderr

+3-6
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,10 @@ LL | T: Generator<ResumeTy, Yield = ()>,
2929
| ^^^^^^^^^^ required by this bound in `std::future::from_generator`
3030

3131
error[E0280]: the requirement `<impl Future<Output = u32> as Future>::Output == u32` is not satisfied
32-
--> $DIR/async.rs:7:29
32+
--> $DIR/async.rs:7:25
3333
|
34-
LL | async fn foo(x: u32) -> u32 {
35-
| _____________________________^
36-
LL | | x
37-
LL | | }
38-
| |_^
34+
LL | async fn foo(x: u32) -> u32 {
35+
| ^^^
3936

4037
error: aborting due to 3 previous errors
4138

src/test/ui/generic-associated-types/bugs/issue-89008.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
error[E0271]: type mismatch resolving `<Empty<_> as Stream>::Item == Repr`
2-
--> $DIR/issue-89008.rs:40:9
2+
--> $DIR/issue-89008.rs:39:43
33
|
44
LL | fn line_stream<'a, Repr>(&'a self) -> Self::LineStreamFut<'a, Repr> {
5-
| ---- this type parameter
6-
LL | async {empty()}
7-
| ^^^^^^^^^^^^^^^ type mismatch resolving `<Empty<_> as Stream>::Item == Repr`
5+
| ---- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type mismatch resolving `<Empty<_> as Stream>::Item == Repr`
6+
| |
7+
| this type parameter
88
|
99
note: expected this to be `()`
1010
--> $DIR/issue-89008.rs:18:17

src/test/ui/impl-trait/issue-55872-1.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ impl<S: Default> Bar for S {
1111

1212
fn foo<T: Default>() -> Self::E {
1313
//~^ ERROR impl has stricter requirements than trait
14-
(S::default(), T::default())
15-
//~^ ERROR the trait bound `S: Copy` is not satisfied in `(S, T)` [E0277]
14+
//~| ERROR the trait bound `S: Copy` is not satisfied in `(S, T)` [E0277]
1615
//~| ERROR the trait bound `T: Copy` is not satisfied in `(S, T)` [E0277]
16+
(S::default(), T::default())
1717
}
1818
}
1919

0 commit comments

Comments
 (0)