Skip to content

Commit 3bf07a5

Browse files
committed
Simplify
1 parent af175dd commit 3bf07a5

File tree

3 files changed

+52
-71
lines changed

3 files changed

+52
-71
lines changed

crates/hir-def/src/body/pretty.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,14 @@ impl<'a> Printer<'a> {
360360
w!(self, "]");
361361
}
362362
Expr::Closure { args, arg_types, ret_type, body, closure_kind } => {
363-
if let ClosureKind::Generator(Movability::Static) = closure_kind {
364-
w!(self, "static ");
363+
match closure_kind {
364+
ClosureKind::Generator(Movability::Static) => {
365+
w!(self, "static ");
366+
}
367+
ClosureKind::Async => {
368+
w!(self, "async ");
369+
}
370+
_ => (),
365371
}
366372
w!(self, "|");
367373
for (i, (pat, ty)) in args.iter().zip(arg_types.iter()).enumerate() {
@@ -375,20 +381,9 @@ impl<'a> Printer<'a> {
375381
}
376382
}
377383
w!(self, "|");
378-
match (ret_type, closure_kind) {
379-
(Some(ret_ty), ClosureKind::Async) => {
380-
w!(self, " -> impl Future<Output = ");
381-
self.print_type_ref(ret_ty);
382-
w!(self, ">");
383-
}
384-
(Some(ret_ty), _) => {
385-
w!(self, " -> ");
386-
self.print_type_ref(ret_ty);
387-
}
388-
(None, ClosureKind::Async) => {
389-
w!(self, " -> impl Future<Output = {{unknown}}>");
390-
}
391-
(None, _) => {}
384+
if let Some(ret_ty) = ret_type {
385+
w!(self, " -> ");
386+
self.print_type_ref(ret_ty);
392387
}
393388
self.whitespace();
394389
self.print_expr(*body);

crates/hir-ty/src/infer/expr.rs

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,23 @@ impl<'a> InferenceContext<'a> {
275275
Some(type_ref) => self.make_ty(type_ref),
276276
None => self.table.new_type_var(),
277277
};
278-
sig_tys.push(ret_ty.clone());
278+
if let ClosureKind::Async = closure_kind {
279+
// Use the first type parameter as the output type of future.
280+
// existential type AsyncBlockImplTrait<InnerType>: Future<Output = InnerType>
281+
let impl_trait_id =
282+
crate::ImplTraitId::AsyncBlockTypeImplTrait(self.owner, *body);
283+
let opaque_ty_id = self.db.intern_impl_trait_id(impl_trait_id).into();
284+
sig_tys.push(
285+
TyKind::OpaqueType(
286+
opaque_ty_id,
287+
Substitution::from1(Interner, ret_ty.clone()),
288+
)
289+
.intern(Interner),
290+
);
291+
} else {
292+
sig_tys.push(ret_ty.clone());
293+
}
294+
279295
let sig_ty = TyKind::Function(FnPointer {
280296
num_binders: 0,
281297
sig: FnSig { abi: (), safety: chalk_ir::Safety::Safe, variadic: false },
@@ -286,7 +302,7 @@ impl<'a> InferenceContext<'a> {
286302
})
287303
.intern(Interner);
288304

289-
let (closure_id, ty, resume_yield_tys) = match closure_kind {
305+
let (ty, resume_yield_tys) = match closure_kind {
290306
ClosureKind::Generator(_) => {
291307
// FIXME: report error when there are more than 1 parameter.
292308
let resume_ty = match sig_tys.first() {
@@ -306,17 +322,17 @@ impl<'a> InferenceContext<'a> {
306322
let generator_id = self.db.intern_generator((self.owner, tgt_expr)).into();
307323
let generator_ty = TyKind::Generator(generator_id, subst).intern(Interner);
308324

309-
(None, generator_ty, Some((resume_ty, yield_ty)))
325+
(generator_ty, Some((resume_ty, yield_ty)))
310326
}
311-
ClosureKind::Async | ClosureKind::Closure => {
327+
ClosureKind::Closure | ClosureKind::Async => {
312328
let closure_id = self.db.intern_closure((self.owner, tgt_expr)).into();
313329
let closure_ty = TyKind::Closure(
314330
closure_id,
315331
Substitution::from1(Interner, sig_ty.clone()),
316332
)
317333
.intern(Interner);
318334

319-
(Some(closure_id), closure_ty, None)
335+
(closure_ty, None)
320336
}
321337
};
322338

@@ -338,47 +354,16 @@ impl<'a> InferenceContext<'a> {
338354
let prev_resume_yield_tys =
339355
mem::replace(&mut self.resume_yield_tys, resume_yield_tys);
340356

341-
let (breaks, ()) =
342-
self.with_breakable_ctx(BreakableKind::Border, None, None, |this| {
343-
this.infer_return(*body);
344-
});
345-
346-
let inner_ty = if matches!(closure_kind, ClosureKind::Async) {
347-
// Use the first type parameter as the output type of future.
348-
// existential type AsyncBlockImplTrait<InnerType>: Future<Output = InnerType>
349-
let impl_trait_id =
350-
crate::ImplTraitId::AsyncBlockTypeImplTrait(self.owner, *body);
351-
let opaque_ty_id = self.db.intern_impl_trait_id(impl_trait_id).into();
352-
TyKind::OpaqueType(opaque_ty_id, Substitution::from1(Interner, ret_ty.clone()))
353-
.intern(Interner)
354-
} else {
355-
ret_ty.clone()
356-
};
357+
self.with_breakable_ctx(BreakableKind::Border, None, None, |this| {
358+
this.infer_return(*body);
359+
});
357360

358361
self.diverges = prev_diverges;
359362
self.return_ty = prev_ret_ty;
360363
self.return_coercion = prev_ret_coercion;
361364
self.resume_yield_tys = prev_resume_yield_tys;
362365

363-
sig_tys.pop();
364-
sig_tys.push(inner_ty);
365-
366-
let sig_ty = TyKind::Function(FnPointer {
367-
num_binders: 0,
368-
sig: FnSig { abi: (), safety: chalk_ir::Safety::Safe, variadic: false },
369-
substitution: FnSubst(
370-
Substitution::from_iter(Interner, sig_tys.clone()).shifted_in(Interner),
371-
),
372-
})
373-
.intern(Interner);
374-
375-
match closure_id {
376-
Some(closure_id) => {
377-
TyKind::Closure(closure_id, Substitution::from1(Interner, sig_ty.clone()))
378-
.intern(Interner)
379-
}
380-
None => ty,
381-
}
366+
ty
382367
}
383368
Expr::Call { callee, args, .. } => {
384369
let callee_ty = self.infer_expr(*callee, &Expectation::none());

crates/hir-ty/src/tests/traits.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -533,29 +533,30 @@ fn tuple_struct_with_fn() {
533533
r#"
534534
struct S(fn(u32) -> u64);
535535
fn test() -> u64 {
536-
let a = S(|i| 2*i);
536+
let a = S(|i| 2*i as u64);
537537
let b = a.0(4);
538538
a.0(2)
539539
}"#,
540540
expect![[r#"
541-
43..101 '{ ...0(2) }': u64
541+
43..108 '{ ...0(2) }': u64
542542
53..54 'a': S
543543
57..58 'S': S(fn(u32) -> u64) -> S
544-
57..67 'S(|i| 2*i)': S
545-
59..66 '|i| 2*i': |u32| -> u64
544+
57..74 'S(|i| ...s u64)': S
545+
59..73 '|i| 2*i as u64': |u32| -> u64
546546
60..61 'i': u32
547-
63..64 '2': u32
548-
63..66 '2*i': u32
547+
63..64 '2': u64
548+
63..73 '2*i as u64': u64
549549
65..66 'i': u32
550-
77..78 'b': u64
551-
81..82 'a': S
552-
81..84 'a.0': fn(u32) -> u64
553-
81..87 'a.0(4)': u64
554-
85..86 '4': u32
555-
93..94 'a': S
556-
93..96 'a.0': fn(u32) -> u64
557-
93..99 'a.0(2)': u64
558-
97..98 '2': u32
550+
65..73 'i as u64': u64
551+
84..85 'b': u64
552+
88..89 'a': S
553+
88..91 'a.0': fn(u32) -> u64
554+
88..94 'a.0(4)': u64
555+
92..93 '4': u32
556+
100..101 'a': S
557+
100..103 'a.0': fn(u32) -> u64
558+
100..106 'a.0(2)': u64
559+
104..105 '2': u32
559560
"#]],
560561
);
561562
}

0 commit comments

Comments
 (0)