@@ -1809,6 +1809,25 @@ impl HirDisplay for Path {
1809
1809
}
1810
1810
}
1811
1811
1812
+ // Convert trait's `Self` bound back to the surface syntax. Note there is no associated
1813
+ // trait, so there can only be one path segment that `has_self_type`. The `Self` type
1814
+ // itself can contain further qualified path through, which will be handled by recursive
1815
+ // `hir_fmt`s.
1816
+ //
1817
+ // `trait_mod::Trait<Self = type_mod::Type, Args>::Assoc`
1818
+ // =>
1819
+ // `<type_mod::Type as trait_mod::Trait<Args>>::Assoc`
1820
+ let trait_self_ty = self . segments ( ) . iter ( ) . find_map ( |seg| {
1821
+ let generic_args = seg. args_and_bindings ?;
1822
+ generic_args. has_self_type . then ( || & generic_args. args [ 0 ] )
1823
+ } ) ;
1824
+ if let Some ( ty) = trait_self_ty {
1825
+ write ! ( f, "<" ) ?;
1826
+ ty. hir_fmt ( f) ?;
1827
+ write ! ( f, " as " ) ?;
1828
+ // Now format the path of the trait...
1829
+ }
1830
+
1812
1831
for ( seg_idx, segment) in self . segments ( ) . iter ( ) . enumerate ( ) {
1813
1832
if !matches ! ( self . kind( ) , PathKind :: Plain ) || seg_idx > 0 {
1814
1833
write ! ( f, "::" ) ?;
@@ -1840,15 +1859,12 @@ impl HirDisplay for Path {
1840
1859
return Ok ( ( ) ) ;
1841
1860
}
1842
1861
1843
- write ! ( f, "<" ) ?;
1844
1862
let mut first = true ;
1845
- for arg in generic_args. args . iter ( ) {
1863
+ // Skip the `Self` bound if exists. It's handled outside the loop.
1864
+ for arg in & generic_args. args [ generic_args. has_self_type as usize ..] {
1846
1865
if first {
1847
1866
first = false ;
1848
- if generic_args. has_self_type {
1849
- // FIXME: Convert to `<Ty as Trait>` form.
1850
- write ! ( f, "Self = " ) ?;
1851
- }
1867
+ write ! ( f, "<" ) ?;
1852
1868
} else {
1853
1869
write ! ( f, ", " ) ?;
1854
1870
}
@@ -1857,6 +1873,7 @@ impl HirDisplay for Path {
1857
1873
for binding in generic_args. bindings . iter ( ) {
1858
1874
if first {
1859
1875
first = false ;
1876
+ write ! ( f, "<" ) ?;
1860
1877
} else {
1861
1878
write ! ( f, ", " ) ?;
1862
1879
}
@@ -1872,9 +1889,20 @@ impl HirDisplay for Path {
1872
1889
}
1873
1890
}
1874
1891
}
1875
- write ! ( f, ">" ) ?;
1892
+
1893
+ // There may be no generic arguments to print, in case of a trait having only a
1894
+ // single `Self` bound which is converted to `<Ty as Trait>::Assoc`.
1895
+ if !first {
1896
+ write ! ( f, ">" ) ?;
1897
+ }
1898
+
1899
+ // Current position: `<Ty as Trait<Args>|`
1900
+ if generic_args. has_self_type {
1901
+ write ! ( f, ">" ) ?;
1902
+ }
1876
1903
}
1877
1904
}
1905
+
1878
1906
Ok ( ( ) )
1879
1907
}
1880
1908
}
0 commit comments