Skip to content

Commit 79b8fa5

Browse files
author
Lukas Markeffsky
committed
Don't ICE if TAIT-defining fn contains a closure with _ in return type
1 parent 665d2c6 commit 79b8fa5

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

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

+10-3
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,16 @@ 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() {
138+
// Function items with `_` in their return type already emit an error, skip any
139+
// "non-defining use" errors for them.
140+
match self.tcx.hir_node_by_def_id(item_def_id) {
141+
Node::TraitItem(TraitItem { kind: hir::TraitItemKind::Fn(fn_sig, _), .. })
142+
| Node::ImplItem(ImplItem { kind: hir::ImplItemKind::Fn(fn_sig, _), .. })
143+
| Node::Item(Item { kind: hir::ItemKind::Fn(fn_sig, _, _), .. })
144+
if fn_sig.decl.output.get_infer_ret_ty().is_some() =>
145+
{
140146
let guar = self.tcx.dcx().span_delayed_bug(
141-
hir_sig.output.span(),
147+
fn_sig.decl.output.span(),
142148
"inferring return types and opaque types do not mix well",
143149
);
144150
self.found = Some(ty::OpaqueHiddenType {
@@ -147,6 +153,7 @@ impl TaitConstraintLocator<'_> {
147153
});
148154
return;
149155
}
156+
_ => {}
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() {}

0 commit comments

Comments
 (0)