Skip to content

Commit 5f9d1d4

Browse files
authored
Rollup merge of #119900 - compiler-errors:closure-checking, r=oli-obk
Inline `check_closure`, simplify `deduce_sig_from_projection` Some minor cleanups that I've collected when reimplementing async closure r? oli-obk
2 parents d4b276c + 6a63183 commit 5f9d1d4

File tree

1 file changed

+15
-52
lines changed

1 file changed

+15
-52
lines changed

compiler/rustc_hir_typeck/src/closure.rs

+15-52
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_middle::ty::visit::{TypeVisitable, TypeVisitableExt};
1414
use rustc_middle::ty::GenericArgs;
1515
use rustc_middle::ty::{self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitor};
1616
use rustc_span::def_id::LocalDefId;
17-
use rustc_span::{sym, Span};
17+
use rustc_span::Span;
1818
use rustc_target::spec::abi::Abi;
1919
use rustc_trait_selection::traits;
2020
use rustc_trait_selection::traits::error_reporting::ArgKind;
@@ -49,7 +49,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
4949
expr_span: Span,
5050
expected: Expectation<'tcx>,
5151
) -> Ty<'tcx> {
52-
trace!("decl = {:#?}", closure.fn_decl);
52+
let tcx = self.tcx;
53+
let body = tcx.hir().body(closure.body);
54+
let expr_def_id = closure.def_id;
5355

5456
// It's always helpful for inference if we know the kind of
5557
// closure sooner rather than later, so first examine the expected
@@ -61,24 +63,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
6163
None => (None, None),
6264
};
6365

64-
self.check_closure(closure, expr_span, expected_kind, expected_sig)
65-
}
66-
67-
#[instrument(skip(self, closure), level = "debug", ret)]
68-
fn check_closure(
69-
&self,
70-
closure: &hir::Closure<'tcx>,
71-
expr_span: Span,
72-
opt_kind: Option<ty::ClosureKind>,
73-
expected_sig: Option<ExpectedSig<'tcx>>,
74-
) -> Ty<'tcx> {
75-
let tcx = self.tcx;
76-
let body = tcx.hir().body(closure.body);
77-
78-
trace!("decl = {:#?}", closure.fn_decl);
79-
let expr_def_id = closure.def_id;
80-
debug!(?expr_def_id);
81-
8266
let ClosureSignatures { bound_sig, liberated_sig } =
8367
self.sig_of_closure(expr_def_id, closure.fn_decl, closure.kind, expected_sig);
8468

@@ -139,9 +123,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
139123
}
140124
};
141125

142-
let mut fcx = FnCtxt::new(self, self.param_env, closure.def_id);
143126
check_fn(
144-
&mut fcx,
127+
&mut FnCtxt::new(self, self.param_env, closure.def_id),
145128
liberated_sig,
146129
coroutine_types,
147130
closure.fn_decl,
@@ -174,9 +157,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
174157
)
175158
});
176159

177-
debug!(?sig, ?opt_kind);
160+
debug!(?sig, ?expected_kind);
178161

179-
let closure_kind_ty = match opt_kind {
162+
let closure_kind_ty = match expected_kind {
180163
Some(kind) => Ty::from_closure_kind(tcx, kind),
181164

182165
// Create a type variable (for now) to represent the closure kind.
@@ -204,11 +187,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
204187
let Some(CoroutineTypes { resume_ty, yield_ty }) = coroutine_types else {
205188
bug!("expected coroutine to have yield/resume types");
206189
};
207-
let interior = fcx.next_ty_var(TypeVariableOrigin {
190+
let interior = self.next_ty_var(TypeVariableOrigin {
208191
kind: TypeVariableOriginKind::MiscVariable,
209192
span: body.value.span,
210193
});
211-
fcx.deferred_coroutine_interiors.borrow_mut().push((
194+
self.deferred_coroutine_interiors.borrow_mut().push((
212195
expr_def_id,
213196
body.id(),
214197
interior,
@@ -364,36 +347,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
364347
let tcx = self.tcx;
365348

366349
let trait_def_id = projection.trait_def_id(tcx);
367-
368-
let is_fn = tcx.is_fn_trait(trait_def_id);
369-
370-
let coroutine_trait = tcx.lang_items().coroutine_trait();
371-
let is_gen = coroutine_trait == Some(trait_def_id);
372-
373-
if !is_fn && !is_gen {
374-
debug!("not fn or coroutine");
350+
// For now, we only do signature deduction based off of the `Fn` traits.
351+
if !tcx.is_fn_trait(trait_def_id) {
375352
return None;
376353
}
377354

378-
// Check that we deduce the signature from the `<_ as std::ops::Coroutine>::Return`
379-
// associated item and not yield.
380-
if is_gen && self.tcx.associated_item(projection.projection_def_id()).name != sym::Return {
381-
debug!("not `Return` assoc item of `Coroutine`");
382-
return None;
383-
}
384-
385-
let input_tys = if is_fn {
386-
let arg_param_ty = projection.skip_binder().projection_ty.args.type_at(1);
387-
let arg_param_ty = self.resolve_vars_if_possible(arg_param_ty);
388-
debug!(?arg_param_ty);
355+
let arg_param_ty = projection.skip_binder().projection_ty.args.type_at(1);
356+
let arg_param_ty = self.resolve_vars_if_possible(arg_param_ty);
357+
debug!(?arg_param_ty);
389358

390-
match arg_param_ty.kind() {
391-
&ty::Tuple(tys) => tys,
392-
_ => return None,
393-
}
394-
} else {
395-
// Coroutines with a `()` resume type may be defined with 0 or 1 explicit arguments,
396-
// else they must have exactly 1 argument. For now though, just give up in this case.
359+
let ty::Tuple(input_tys) = *arg_param_ty.kind() else {
397360
return None;
398361
};
399362

0 commit comments

Comments
 (0)