Skip to content

Commit a3eef90

Browse files
authored
Unrolled build for rust-lang#119975
Rollup merge of rust-lang#119975 - lukas-code:inferring-return-types-and-opaque-types-do-mix-sometimes, r=compiler-errors Don't ICE if TAIT-defining fn contains a closure with `_` in return type The `delay_span_bug` got added in rust-lang@0e82aae to reduce the amount of errors emitted for functions that have `_` in their return type, because inference doesn't apply to function items. But this logic shouldn't apply to closures, because their return types *can* be inferred. Fixes rust-lang#119916.
2 parents 6ae4cfb + 22833c1 commit a3eef90

File tree

4 files changed

+69
-13
lines changed

4 files changed

+69
-13
lines changed

compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs

+19-12
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,25 @@ impl TaitConstraintLocator<'_> {
135135
return;
136136
}
137137

138-
if let Some(hir_sig) = self.tcx.hir_node_by_def_id(item_def_id).fn_decl() {
139-
if hir_sig.output.get_infer_ret_ty().is_some() {
140-
let guar = self.tcx.dcx().span_delayed_bug(
141-
hir_sig.output.span(),
142-
"inferring return types and opaque types do not mix well",
143-
);
144-
self.found = Some(ty::OpaqueHiddenType {
145-
span: DUMMY_SP,
146-
ty: Ty::new_error(self.tcx, guar),
147-
});
148-
return;
149-
}
138+
// Function items with `_` in their return type already emit an error, skip any
139+
// "non-defining use" errors for them.
140+
// Note that we use `Node::fn_sig` instead of `Node::fn_decl` here, because the former
141+
// excludes closures, which are allowed to have `_` in their return type.
142+
let hir_node = self.tcx.hir_node_by_def_id(item_def_id);
143+
debug_assert!(
144+
!matches!(hir_node, Node::ForeignItem(..)),
145+
"foreign items cannot constrain opaque types",
146+
);
147+
if let Some(hir_sig) = hir_node.fn_sig()
148+
&& hir_sig.decl.output.get_infer_ret_ty().is_some()
149+
{
150+
let guar = self.tcx.dcx().span_delayed_bug(
151+
hir_sig.decl.output.span(),
152+
"inferring return types and opaque types do not mix well",
153+
);
154+
self.found =
155+
Some(ty::OpaqueHiddenType { span: DUMMY_SP, ty: Ty::new_error(self.tcx, guar) });
156+
return;
150157
}
151158

152159
// Calling `mir_borrowck` can lead to cycle errors through
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// check-pass
2+
3+
// Regression test for an ICE: https://github.com/rust-lang/rust/issues/119916
4+
5+
#![feature(impl_trait_in_assoc_type)]
6+
#![feature(type_alias_impl_trait)]
7+
8+
// `impl_trait_in_assoc_type` example from the bug report.
9+
pub trait StreamConsumer {
10+
type BarrierStream;
11+
fn execute() -> Self::BarrierStream;
12+
}
13+
14+
pub struct DispatchExecutor;
15+
16+
impl StreamConsumer for DispatchExecutor {
17+
type BarrierStream = impl Sized;
18+
fn execute() -> Self::BarrierStream {
19+
|| -> _ {}
20+
}
21+
}
22+
23+
// Functions that constrain TAITs can contain closures with an `_` in the return type.
24+
type Foo = impl Sized;
25+
fn foo() -> Foo {
26+
|| -> _ {}
27+
}
28+
29+
// The `_` in the closure return type can also be the TAIT itself.
30+
type Bar = impl Sized;
31+
fn bar() -> impl FnOnce() -> Bar {
32+
|| -> _ {}
33+
}
34+
35+
fn main() {}

tests/ui/type-alias-impl-trait/issue-77179.rs

+5
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,8 @@ fn test() -> Pointer<_> {
1212
fn main() {
1313
test();
1414
}
15+
16+
extern "Rust" {
17+
fn bar() -> Pointer<_>;
18+
//~^ ERROR: the placeholder `_` is not allowed within types
19+
}

tests/ui/type-alias-impl-trait/issue-77179.stderr

+10-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ LL | fn test() -> Pointer<_> {
77
| | not allowed in type signatures
88
| help: replace with the correct return type: `Pointer<i32>`
99

10-
error: aborting due to 1 previous error
10+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for functions
11+
--> $DIR/issue-77179.rs:17:25
12+
|
13+
LL | fn bar() -> Pointer<_>;
14+
| ^
15+
| |
16+
| not allowed in type signatures
17+
| help: use type parameters instead: `T`
18+
19+
error: aborting due to 2 previous errors
1120

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

0 commit comments

Comments
 (0)