From 7d62280a713de08b361a08df077ff361b2e1fa99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= Date: Sun, 5 Apr 2020 18:25:47 +0300 Subject: [PATCH] Hide unit fn return types --- crates/ra_hir_ty/src/display.rs | 20 ++++++++++++++------ crates/ra_hir_ty/src/tests/coercion.rs | 8 ++++---- crates/ra_hir_ty/src/tests/regression.rs | 2 +- crates/ra_hir_ty/src/tests/simple.rs | 6 +++--- crates/ra_hir_ty/src/tests/traits.rs | 12 ++++++------ 5 files changed, 28 insertions(+), 20 deletions(-) diff --git a/crates/ra_hir_ty/src/display.rs b/crates/ra_hir_ty/src/display.rs index 13ecd537ad..1665e25e4f 100644 --- a/crates/ra_hir_ty/src/display.rs +++ b/crates/ra_hir_ty/src/display.rs @@ -155,7 +155,11 @@ impl HirDisplay for ApplicationTy { let sig = FnSig::from_fn_ptr_substs(&self.parameters); write!(f, "fn(")?; f.write_joined(sig.params(), ", ")?; - write!(f, ") -> {}", sig.ret().display(f.db))?; + write!(f, ")")?; + let ret = sig.ret(); + if *ret != Ty::unit() { + write!(f, " -> {}", ret.display(f.db))?; + } } TypeCtor::FnDef(def) => { let sig = f.db.callable_item_signature(def).subst(&self.parameters); @@ -180,7 +184,11 @@ impl HirDisplay for ApplicationTy { } write!(f, "(")?; f.write_joined(sig.params(), ", ")?; - write!(f, ") -> {}", sig.ret().display(f.db))?; + write!(f, ")")?; + let ret = sig.ret(); + if *ret != Ty::unit() { + write!(f, " -> {}", ret.display(f.db))?; + } } TypeCtor::Adt(def_id) => { let name = match def_id { @@ -242,16 +250,16 @@ impl HirDisplay for ApplicationTy { let sig = self.parameters[0] .callable_sig(f.db) .expect("first closure parameter should contain signature"); - let return_type_hint = sig.ret().display(f.db); if sig.params().is_empty() { - write!(f, "|| -> {}", return_type_hint)?; + write!(f, "||")?; } else if f.omit_verbose_types() { - write!(f, "|{}| -> {}", TYPE_HINT_TRUNCATION, return_type_hint)?; + write!(f, "|{}|", TYPE_HINT_TRUNCATION)?; } else { write!(f, "|")?; f.write_joined(sig.params(), ", ")?; - write!(f, "| -> {}", return_type_hint)?; + write!(f, "|")?; }; + write!(f, " -> {}", sig.ret().display(f.db))?; } } Ok(()) diff --git a/crates/ra_hir_ty/src/tests/coercion.rs b/crates/ra_hir_ty/src/tests/coercion.rs index 3e3d55c041..b2c971f024 100644 --- a/crates/ra_hir_ty/src/tests/coercion.rs +++ b/crates/ra_hir_ty/src/tests/coercion.rs @@ -407,16 +407,16 @@ fn test() { [30; 31) 'x': &Foo [39; 41) '{}': () [52; 133) '{ ...oo); }': () - [58; 71) 'takes_ref_foo': fn takes_ref_foo(&Foo) -> () + [58; 71) 'takes_ref_foo': fn takes_ref_foo(&Foo) [58; 77) 'takes_...(&Foo)': () [72; 76) '&Foo': &Foo [73; 76) 'Foo': Foo - [83; 96) 'takes_ref_foo': fn takes_ref_foo(&Foo) -> () + [83; 96) 'takes_ref_foo': fn takes_ref_foo(&Foo) [83; 103) 'takes_...&&Foo)': () [97; 102) '&&Foo': &&Foo [98; 102) '&Foo': &Foo [99; 102) 'Foo': Foo - [109; 122) 'takes_ref_foo': fn takes_ref_foo(&Foo) -> () + [109; 122) 'takes_ref_foo': fn takes_ref_foo(&Foo) [109; 130) 'takes_...&&Foo)': () [123; 129) '&&&Foo': &&&Foo [124; 129) '&&Foo': &&Foo @@ -484,7 +484,7 @@ fn test() { [171; 178) 'loop {}': ! [176; 178) '{}': () [191; 236) '{ ... }); }': () - [197; 210) 'takes_ref_str': fn takes_ref_str(&str) -> () + [197; 210) 'takes_ref_str': fn takes_ref_str(&str) [197; 233) 'takes_...g() })': () [211; 232) '&{ ret...ng() }': &String [212; 232) '{ retu...ng() }': String diff --git a/crates/ra_hir_ty/src/tests/regression.rs b/crates/ra_hir_ty/src/tests/regression.rs index 2ee9b8f101..3402e0cb5a 100644 --- a/crates/ra_hir_ty/src/tests/regression.rs +++ b/crates/ra_hir_ty/src/tests/regression.rs @@ -376,7 +376,7 @@ fn issue_2669() { ), @r###" [147; 262) '{ ... }': () - [161; 164) 'end': fn end<{unknown}>() -> () + [161; 164) 'end': fn end<{unknown}>() [161; 166) 'end()': () [199; 252) '{ ... }': () [221; 223) '_x': ! diff --git a/crates/ra_hir_ty/src/tests/simple.rs b/crates/ra_hir_ty/src/tests/simple.rs index 141f0245aa..c2f1ded2f4 100644 --- a/crates/ra_hir_ty/src/tests/simple.rs +++ b/crates/ra_hir_ty/src/tests/simple.rs @@ -521,7 +521,7 @@ fn test() -> &mut &f64 { [88; 89) 'a': u32 [92; 108) 'unknow...nction': {unknown} [92; 110) 'unknow...tion()': u32 - [116; 125) 'takes_u32': fn takes_u32(u32) -> () + [116; 125) 'takes_u32': fn takes_u32(u32) [116; 128) 'takes_u32(a)': () [126; 127) 'a': u32 [138; 139) 'b': i32 @@ -1749,9 +1749,9 @@ fn main() { [67; 91) 'Vtable...| {} }': Vtable [84; 89) '|| {}': || -> () [87; 89) '{}': () - [101; 102) 'm': fn() -> () + [101; 102) 'm': fn() [105; 111) 'vtable': Vtable - [105; 118) 'vtable.method': fn() -> () + [105; 118) 'vtable.method': fn() "### ); } diff --git a/crates/ra_hir_ty/src/tests/traits.rs b/crates/ra_hir_ty/src/tests/traits.rs index f009a708c3..081aa943a4 100644 --- a/crates/ra_hir_ty/src/tests/traits.rs +++ b/crates/ra_hir_ty/src/tests/traits.rs @@ -263,7 +263,7 @@ fn test() { [119; 120) 'S': S(u32) -> S [119; 129) 'S(unknown)': S [121; 128) 'unknown': u32 - [135; 138) 'foo': fn foo>(S) -> () + [135; 138) 'foo': fn foo>(S) [135; 141) 'foo(s)': () [139; 140) 's': S "### @@ -962,7 +962,7 @@ fn test(x: impl Trait, y: &impl Trait) { [224; 225) 'S': S(u16) -> S [224; 228) 'S(1)': S [226; 227) '1': u16 - [234; 237) 'bar': fn bar(S) -> () + [234; 237) 'bar': fn bar(S) [234; 240) 'bar(z)': () [238; 239) 'z': S [246; 247) 'x': impl Trait @@ -1108,8 +1108,8 @@ fn test() { [40; 47) 'loop {}': ! [45; 47) '{}': () [91; 124) '{ ...foo; }': () - [101; 102) 'f': fn(S) -> () - [118; 121) 'foo': fn foo(S) -> () + [101; 102) 'f': fn(S) + [118; 121) 'foo': fn foo(S) "### ); } @@ -1962,7 +1962,7 @@ fn test() -> impl Trait { [229; 241) 'S(default())': S [231; 238) 'default': fn default() -> u32 [231; 240) 'default()': u32 - [247; 250) 'foo': fn foo(S) -> () + [247; 250) 'foo': fn foo(S) [247; 254) 'foo(s1)': () [251; 253) 's1': S [264; 265) 'x': i32 @@ -2012,7 +2012,7 @@ fn main() { [147; 149) '_v': F [192; 195) '{ }': () [207; 238) '{ ... }); }': () - [213; 223) 'f::<(), _>': fn f<(), |&()| -> ()>(|&()| -> ()) -> () + [213; 223) 'f::<(), _>': fn f<(), |&()| -> ()>(|&()| -> ()) [213; 235) 'f::<()... z; })': () [224; 234) '|z| { z; }': |&()| -> () [225; 226) 'z': &()