From 35c948ff4a36b9c031bda96f79b8bf9e0d5bda26 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Tue, 25 May 2021 14:24:08 +0200 Subject: Fix lowering of FnOnce() without return type This should result in an implicit `-> ()`, not leaving out the binding. --- crates/hir_ty/src/tests/traits.rs | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) (limited to 'crates/hir_ty') diff --git a/crates/hir_ty/src/tests/traits.rs b/crates/hir_ty/src/tests/traits.rs index a5a2df54c..714f12937 100644 --- a/crates/hir_ty/src/tests/traits.rs +++ b/crates/hir_ty/src/tests/traits.rs @@ -3041,7 +3041,7 @@ fn infer_fn_trait_arg() { #[test] fn infer_box_fn_arg() { - // The type mismatch is a bug + // The type mismatch is because we don't define Unsize and CoerceUnsized check_infer_with_mismatches( r#" //- /lib.rs deps:std @@ -3095,16 +3095,16 @@ fn foo() { 478..576 '{ ...&s); }': () 488..489 's': Option 492..504 'Option::None': Option - 514..515 'f': Box)> + 514..515 'f': Box) -> ()> 549..562 'box (|ps| {})': Box<|{unknown}| -> ()> 554..561 '|ps| {}': |{unknown}| -> () 555..557 'ps': {unknown} 559..561 '{}': () - 568..569 'f': Box)> - 568..573 'f(&s)': FnOnce::Output), (&Option,)> + 568..569 'f': Box) -> ()> + 568..573 'f(&s)': () 570..572 '&s': &Option 571..572 's': Option - 549..562: expected Box)>, got Box<|{unknown}| -> ()> + 549..562: expected Box) -> ()>, got Box<|{unknown}| -> ()> "#]], ); } @@ -3571,3 +3571,25 @@ fn main() { "#]], ) } + +#[test] +fn fn_returning_unit() { + check_infer_with_mismatches( + r#" +#[lang = "fn_once"] +trait FnOnce { + type Output; +} + +fn test(f: F) { + let _: () = f(); +}"#, + expect![[r#" + 82..83 'f': F + 88..112 '{ ...f(); }': () + 98..99 '_': () + 106..107 'f': F + 106..109 'f()': () + "#]], + ); +} -- cgit v1.2.3 From 7c6f764ad6d7c6fc964be9fd2e96da717e2b2cc1 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Tue, 25 May 2021 15:23:52 +0200 Subject: Hide `-> ()` in Fn traits --- crates/hir_ty/src/display.rs | 6 ++++-- crates/hir_ty/src/tests/traits.rs | 6 +++--- 2 files changed, 7 insertions(+), 5 deletions(-) (limited to 'crates/hir_ty') diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index 637bbc634..44f843bf3 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs @@ -778,8 +778,10 @@ fn write_bounds_like_dyn_trait( } WhereClause::AliasEq(alias_eq) if is_fn_trait => { is_fn_trait = false; - write!(f, " -> ")?; - alias_eq.ty.hir_fmt(f)?; + if !alias_eq.ty.is_unit() { + write!(f, " -> ")?; + alias_eq.ty.hir_fmt(f)?; + } } WhereClause::AliasEq(AliasEq { ty, alias }) => { // in types in actual Rust, these will always come diff --git a/crates/hir_ty/src/tests/traits.rs b/crates/hir_ty/src/tests/traits.rs index 714f12937..eea98fd4f 100644 --- a/crates/hir_ty/src/tests/traits.rs +++ b/crates/hir_ty/src/tests/traits.rs @@ -3095,16 +3095,16 @@ fn foo() { 478..576 '{ ...&s); }': () 488..489 's': Option 492..504 'Option::None': Option - 514..515 'f': Box) -> ()> + 514..515 'f': Box)> 549..562 'box (|ps| {})': Box<|{unknown}| -> ()> 554..561 '|ps| {}': |{unknown}| -> () 555..557 'ps': {unknown} 559..561 '{}': () - 568..569 'f': Box) -> ()> + 568..569 'f': Box)> 568..573 'f(&s)': () 570..572 '&s': &Option 571..572 's': Option - 549..562: expected Box) -> ()>, got Box<|{unknown}| -> ()> + 549..562: expected Box)>, got Box<|{unknown}| -> ()> "#]], ); } -- cgit v1.2.3