@@ -1725,32 +1725,42 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
1725
1725
}
1726
1726
}
1727
1727
TypeError :: Sorts ( values) => {
1728
- let extra = expected == found;
1728
+ let extra = expected == found
1729
+ // Ensure that we don't ever say something like
1730
+ // expected `impl Trait` (opaque type `impl Trait`)
1731
+ // found `impl Trait` (opaque type `impl Trait`)
1732
+ && values. expected . sort_string ( self . tcx )
1733
+ != values. found . sort_string ( self . tcx ) ;
1729
1734
let sort_string = |ty : Ty < ' tcx > | match ( extra, ty. kind ( ) ) {
1730
1735
( true , ty:: Alias ( ty:: Opaque , ty:: AliasTy { def_id, .. } ) ) => {
1731
1736
let sm = self . tcx . sess . source_map ( ) ;
1732
1737
let pos = sm. lookup_char_pos ( self . tcx . def_span ( * def_id) . lo ( ) ) ;
1733
- format ! (
1738
+ DiagStyledString :: normal ( format ! (
1734
1739
" (opaque type at <{}:{}:{}>)" ,
1735
1740
sm. filename_for_diagnostics( & pos. file. name) ,
1736
1741
pos. line,
1737
1742
pos. col. to_usize( ) + 1 ,
1738
- )
1743
+ ) )
1739
1744
}
1740
1745
( true , ty:: Alias ( ty:: Projection , proj) )
1741
1746
if self . tcx . is_impl_trait_in_trait ( proj. def_id ) =>
1742
1747
{
1743
1748
let sm = self . tcx . sess . source_map ( ) ;
1744
1749
let pos = sm. lookup_char_pos ( self . tcx . def_span ( proj. def_id ) . lo ( ) ) ;
1745
- format ! (
1750
+ DiagStyledString :: normal ( format ! (
1746
1751
" (trait associated opaque type at <{}:{}:{}>)" ,
1747
1752
sm. filename_for_diagnostics( & pos. file. name) ,
1748
1753
pos. line,
1749
1754
pos. col. to_usize( ) + 1 ,
1750
- )
1755
+ ) )
1751
1756
}
1752
- ( true , _) => format ! ( " ({})" , ty. sort_string( self . tcx) ) ,
1753
- ( false , _) => "" . to_string ( ) ,
1757
+ ( true , _) => {
1758
+ let mut s = DiagStyledString :: normal ( " (" ) ;
1759
+ s. push_highlighted ( ty. sort_string ( self . tcx ) ) ;
1760
+ s. push_normal ( ")" ) ;
1761
+ s
1762
+ }
1763
+ ( false , _) => DiagStyledString :: normal ( "" ) ,
1754
1764
} ;
1755
1765
if !( values. expected . is_simple_text ( ) && values. found . is_simple_text ( ) )
1756
1766
|| ( exp_found. is_some_and ( |ef| {
@@ -1767,23 +1777,23 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
1767
1777
}
1768
1778
} ) )
1769
1779
{
1770
- if let Some ( ExpectedFound { found : found_ty, .. } ) = exp_found {
1780
+ if let Some ( ExpectedFound { found : found_ty, .. } ) = exp_found
1781
+ && !self . tcx . ty_is_opaque_future ( found_ty)
1782
+ {
1771
1783
// `Future` is a special opaque type that the compiler
1772
1784
// will try to hide in some case such as `async fn`, so
1773
1785
// to make an error more use friendly we will
1774
1786
// avoid to suggest a mismatch type with a
1775
1787
// type that the user usually are not using
1776
1788
// directly such as `impl Future<Output = u8>`.
1777
- if !self . tcx . ty_is_opaque_future ( found_ty) {
1778
- diag. note_expected_found_extra (
1779
- & expected_label,
1780
- expected,
1781
- & found_label,
1782
- found,
1783
- & sort_string ( values. expected ) ,
1784
- & sort_string ( values. found ) ,
1785
- ) ;
1786
- }
1789
+ diag. note_expected_found_extra (
1790
+ & expected_label,
1791
+ expected,
1792
+ & found_label,
1793
+ found,
1794
+ sort_string ( values. expected ) ,
1795
+ sort_string ( values. found ) ,
1796
+ ) ;
1787
1797
}
1788
1798
}
1789
1799
}
0 commit comments