@@ -80,6 +80,7 @@ use rustc_middle::ty::{
80
80
use rustc_span:: { sym, symbol:: kw, BytePos , DesugaringKind , Pos , Span } ;
81
81
use rustc_target:: spec:: abi;
82
82
use std:: ops:: { ControlFlow , Deref } ;
83
+ use std:: path:: PathBuf ;
83
84
use std:: { cmp, fmt, iter} ;
84
85
85
86
mod note;
@@ -1351,10 +1352,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
1351
1352
. map ( |( mod_str, _) | mod_str. len ( ) + separator_len)
1352
1353
. sum ( ) ;
1353
1354
1354
- debug ! (
1355
- "cmp: separator_len={}, split_idx={}, min_len={}" ,
1356
- separator_len, split_idx, min_len
1357
- ) ;
1355
+ debug ! ( ?separator_len, ?split_idx, ?min_len, "cmp" ) ;
1358
1356
1359
1357
if split_idx >= min_len {
1360
1358
// paths are identical, highlight everything
@@ -1365,7 +1363,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
1365
1363
} else {
1366
1364
let ( common, uniq1) = t1_str. split_at ( split_idx) ;
1367
1365
let ( _, uniq2) = t2_str. split_at ( split_idx) ;
1368
- debug ! ( "cmp: common={}, uniq1={}, uniq2={}" , common , uniq1 , uniq2 ) ;
1366
+ debug ! ( ? common, ? uniq1, ? uniq2, "cmp" ) ;
1369
1367
1370
1368
values. 0 . push_normal ( common) ;
1371
1369
values. 0 . push_highlighted ( uniq1) ;
@@ -1658,17 +1656,14 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
1658
1656
}
1659
1657
ValuePairs :: Regions ( _) => ( false , Mismatch :: Fixed ( "lifetime" ) ) ,
1660
1658
} ;
1661
- let vals = match self . values_str ( values) {
1662
- Some ( ( expected, found) ) => Some ( ( expected, found) ) ,
1663
- None => {
1664
- // Derived error. Cancel the emitter.
1665
- // NOTE(eddyb) this was `.cancel()`, but `diag`
1666
- // is borrowed, so we can't fully defuse it.
1667
- diag. downgrade_to_delayed_bug ( ) ;
1668
- return ;
1669
- }
1659
+ let Some ( vals) = self . values_str ( values) else {
1660
+ // Derived error. Cancel the emitter.
1661
+ // NOTE(eddyb) this was `.cancel()`, but `diag`
1662
+ // is borrowed, so we can't fully defuse it.
1663
+ diag. downgrade_to_delayed_bug ( ) ;
1664
+ return ;
1670
1665
} ;
1671
- ( vals, exp_found, is_simple_error, Some ( values) )
1666
+ ( Some ( vals) , exp_found, is_simple_error, Some ( values) )
1672
1667
}
1673
1668
} ;
1674
1669
@@ -1700,7 +1695,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
1700
1695
label_or_note ( span, & terr. to_string ( ) ) ;
1701
1696
}
1702
1697
1703
- if let Some ( ( expected, found) ) = expected_found {
1698
+ if let Some ( ( expected, found, exp_p , found_p ) ) = expected_found {
1704
1699
let ( expected_label, found_label, exp_found) = match exp_found {
1705
1700
Mismatch :: Variable ( ef) => (
1706
1701
ef. expected . prefix_string ( self . tcx ) ,
@@ -1817,32 +1812,41 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
1817
1812
}
1818
1813
TypeError :: Sorts ( values) => {
1819
1814
let extra = expected == found;
1820
- let sort_string = |ty : Ty < ' tcx > | match ( extra, ty. kind ( ) ) {
1821
- ( true , ty:: Opaque ( def_id, _) ) => {
1822
- let sm = self . tcx . sess . source_map ( ) ;
1823
- let pos = sm. lookup_char_pos ( self . tcx . def_span ( * def_id) . lo ( ) ) ;
1824
- format ! (
1825
- " (opaque type at <{}:{}:{}>)" ,
1826
- sm. filename_for_diagnostics( & pos. file. name) ,
1827
- pos. line,
1828
- pos. col. to_usize( ) + 1 ,
1829
- )
1830
- }
1831
- ( true , ty:: Projection ( proj) )
1832
- if self . tcx . def_kind ( proj. item_def_id )
1833
- == DefKind :: ImplTraitPlaceholder =>
1834
- {
1835
- let sm = self . tcx . sess . source_map ( ) ;
1836
- let pos = sm. lookup_char_pos ( self . tcx . def_span ( proj. item_def_id ) . lo ( ) ) ;
1837
- format ! (
1838
- " (trait associated opaque type at <{}:{}:{}>)" ,
1839
- sm. filename_for_diagnostics( & pos. file. name) ,
1840
- pos. line,
1841
- pos. col. to_usize( ) + 1 ,
1842
- )
1815
+ let sort_string = |ty : Ty < ' tcx > , path : Option < PathBuf > | {
1816
+ let mut s = match ( extra, ty. kind ( ) ) {
1817
+ ( true , ty:: Opaque ( def_id, _) ) => {
1818
+ let sm = self . tcx . sess . source_map ( ) ;
1819
+ let pos = sm. lookup_char_pos ( self . tcx . def_span ( * def_id) . lo ( ) ) ;
1820
+ format ! (
1821
+ " (opaque type at <{}:{}:{}>)" ,
1822
+ sm. filename_for_diagnostics( & pos. file. name) ,
1823
+ pos. line,
1824
+ pos. col. to_usize( ) + 1 ,
1825
+ )
1826
+ }
1827
+ ( true , ty:: Projection ( proj) )
1828
+ if self . tcx . def_kind ( proj. item_def_id )
1829
+ == DefKind :: ImplTraitPlaceholder =>
1830
+ {
1831
+ let sm = self . tcx . sess . source_map ( ) ;
1832
+ let pos = sm. lookup_char_pos ( self . tcx . def_span ( proj. item_def_id ) . lo ( ) ) ;
1833
+ format ! (
1834
+ " (trait associated opaque type at <{}:{}:{}>)" ,
1835
+ sm. filename_for_diagnostics( & pos. file. name) ,
1836
+ pos. line,
1837
+ pos. col. to_usize( ) + 1 ,
1838
+ )
1839
+ }
1840
+ ( true , _) => format ! ( " ({})" , ty. sort_string( self . tcx) ) ,
1841
+ ( false , _) => "" . to_string ( ) ,
1842
+ } ;
1843
+ if let Some ( path) = path {
1844
+ s. push_str ( & format ! (
1845
+ "\n the full type name has been written to '{}'" ,
1846
+ path. display( ) ,
1847
+ ) ) ;
1843
1848
}
1844
- ( true , _) => format ! ( " ({})" , ty. sort_string( self . tcx) ) ,
1845
- ( false , _) => "" . to_string ( ) ,
1849
+ s
1846
1850
} ;
1847
1851
if !( values. expected . is_simple_text ( ) && values. found . is_simple_text ( ) )
1848
1852
|| ( exp_found. map_or ( false , |ef| {
@@ -1864,8 +1868,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
1864
1868
expected,
1865
1869
& found_label,
1866
1870
found,
1867
- & sort_string ( values. expected ) ,
1868
- & sort_string ( values. found ) ,
1871
+ & sort_string ( values. expected , exp_p ) ,
1872
+ & sort_string ( values. found , found_p ) ,
1869
1873
) ;
1870
1874
}
1871
1875
}
@@ -2338,7 +2342,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
2338
2342
let code = trace. cause . code ( ) ;
2339
2343
if let & MatchExpressionArm ( box MatchExpressionArmCause { source, .. } ) = code
2340
2344
&& let hir:: MatchSource :: TryDesugar = source
2341
- && let Some ( ( expected_ty, found_ty) ) = self . values_str ( trace. values )
2345
+ && let Some ( ( expected_ty, found_ty, _ , _ ) ) = self . values_str ( trace. values )
2342
2346
{
2343
2347
err. note ( & format ! (
2344
2348
"`?` operator cannot convert from `{}` to `{}`" ,
@@ -2454,7 +2458,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
2454
2458
fn values_str (
2455
2459
& self ,
2456
2460
values : ValuePairs < ' tcx > ,
2457
- ) -> Option < ( DiagnosticStyledString , DiagnosticStyledString ) > {
2461
+ ) -> Option < ( DiagnosticStyledString , DiagnosticStyledString , Option < PathBuf > , Option < PathBuf > ) >
2462
+ {
2458
2463
match values {
2459
2464
infer:: Regions ( exp_found) => self . expected_found_str ( exp_found) ,
2460
2465
infer:: Terms ( exp_found) => self . expected_found_str_term ( exp_found) ,
@@ -2464,7 +2469,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
2464
2469
found : exp_found. found . print_only_trait_path ( ) ,
2465
2470
} ;
2466
2471
match self . expected_found_str ( pretty_exp_found) {
2467
- Some ( ( expected, found) ) if expected == found => {
2472
+ Some ( ( expected, found, _ , _ ) ) if expected == found => {
2468
2473
self . expected_found_str ( exp_found)
2469
2474
}
2470
2475
ret => ret,
@@ -2476,7 +2481,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
2476
2481
found : exp_found. found . print_only_trait_path ( ) ,
2477
2482
} ;
2478
2483
match self . expected_found_str ( pretty_exp_found) {
2479
- Some ( ( expected, found) ) if expected == found => {
2484
+ Some ( ( expected, found, _ , _ ) ) if expected == found => {
2480
2485
self . expected_found_str ( exp_found)
2481
2486
}
2482
2487
ret => ret,
@@ -2488,17 +2493,38 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
2488
2493
fn expected_found_str_term (
2489
2494
& self ,
2490
2495
exp_found : ty:: error:: ExpectedFound < ty:: Term < ' tcx > > ,
2491
- ) -> Option < ( DiagnosticStyledString , DiagnosticStyledString ) > {
2496
+ ) -> Option < ( DiagnosticStyledString , DiagnosticStyledString , Option < PathBuf > , Option < PathBuf > ) >
2497
+ {
2492
2498
let exp_found = self . resolve_vars_if_possible ( exp_found) ;
2493
2499
if exp_found. references_error ( ) {
2494
2500
return None ;
2495
2501
}
2496
2502
2497
2503
Some ( match ( exp_found. expected . unpack ( ) , exp_found. found . unpack ( ) ) {
2498
- ( ty:: TermKind :: Ty ( expected) , ty:: TermKind :: Ty ( found) ) => self . cmp ( expected, found) ,
2504
+ ( ty:: TermKind :: Ty ( expected) , ty:: TermKind :: Ty ( found) ) => {
2505
+ let ( mut exp, mut fnd) = self . cmp ( expected, found) ;
2506
+ let len = self . tcx . sess ( ) . diagnostic_width ( ) . saturating_sub ( 20 ) ;
2507
+ let exp_s = exp. content ( ) ;
2508
+ let fnd_s = fnd. content ( ) ;
2509
+ let mut exp_p = None ;
2510
+ let mut fnd_p = None ;
2511
+ if exp_s. len ( ) > len {
2512
+ let ( exp_s, exp_path) = self . tcx . short_ty_string ( expected) ;
2513
+ exp = DiagnosticStyledString :: highlighted ( exp_s) ;
2514
+ exp_p = exp_path;
2515
+ }
2516
+ if fnd_s. len ( ) > len {
2517
+ let ( fnd_s, fnd_path) = self . tcx . short_ty_string ( found) ;
2518
+ fnd = DiagnosticStyledString :: highlighted ( fnd_s) ;
2519
+ fnd_p = fnd_path;
2520
+ }
2521
+ ( exp, fnd, exp_p, fnd_p)
2522
+ }
2499
2523
_ => (
2500
2524
DiagnosticStyledString :: highlighted ( exp_found. expected . to_string ( ) ) ,
2501
2525
DiagnosticStyledString :: highlighted ( exp_found. found . to_string ( ) ) ,
2526
+ None ,
2527
+ None ,
2502
2528
) ,
2503
2529
} )
2504
2530
}
@@ -2507,7 +2533,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
2507
2533
fn expected_found_str < T : fmt:: Display + TypeFoldable < ' tcx > > (
2508
2534
& self ,
2509
2535
exp_found : ty:: error:: ExpectedFound < T > ,
2510
- ) -> Option < ( DiagnosticStyledString , DiagnosticStyledString ) > {
2536
+ ) -> Option < ( DiagnosticStyledString , DiagnosticStyledString , Option < PathBuf > , Option < PathBuf > ) >
2537
+ {
2511
2538
let exp_found = self . resolve_vars_if_possible ( exp_found) ;
2512
2539
if exp_found. references_error ( ) {
2513
2540
return None ;
@@ -2516,6 +2543,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
2516
2543
Some ( (
2517
2544
DiagnosticStyledString :: highlighted ( exp_found. expected . to_string ( ) ) ,
2518
2545
DiagnosticStyledString :: highlighted ( exp_found. found . to_string ( ) ) ,
2546
+ None ,
2547
+ None ,
2519
2548
) )
2520
2549
}
2521
2550
@@ -2849,36 +2878,29 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
2849
2878
debug ! ( "report_sub_sup_conflict: sup_region={:?}" , sup_region) ;
2850
2879
debug ! ( "report_sub_sup_conflict: sup_origin={:?}" , sup_origin) ;
2851
2880
2852
- if let ( & infer:: Subtype ( ref sup_trace) , & infer:: Subtype ( ref sub_trace) ) =
2853
- ( & sup_origin, & sub_origin)
2881
+ if let infer:: Subtype ( ref sup_trace) = sup_origin
2882
+ && let infer:: Subtype ( ref sub_trace) = sub_origin
2883
+ && let Some ( ( sup_expected, sup_found, _, _) ) = self . values_str ( sup_trace. values )
2884
+ && let Some ( ( sub_expected, sub_found, _, _) ) = self . values_str ( sub_trace. values )
2885
+ && sub_expected == sup_expected
2886
+ && sub_found == sup_found
2854
2887
{
2855
- debug ! ( "report_sub_sup_conflict: sup_trace={:?}" , sup_trace) ;
2856
- debug ! ( "report_sub_sup_conflict: sub_trace={:?}" , sub_trace) ;
2857
- debug ! ( "report_sub_sup_conflict: sup_trace.values={:?}" , sup_trace. values) ;
2858
- debug ! ( "report_sub_sup_conflict: sub_trace.values={:?}" , sub_trace. values) ;
2859
-
2860
- if let ( Some ( ( sup_expected, sup_found) ) , Some ( ( sub_expected, sub_found) ) ) =
2861
- ( self . values_str ( sup_trace. values ) , self . values_str ( sub_trace. values ) )
2862
- {
2863
- if sub_expected == sup_expected && sub_found == sup_found {
2864
- note_and_explain_region (
2865
- self . tcx ,
2866
- & mut err,
2867
- "...but the lifetime must also be valid for " ,
2868
- sub_region,
2869
- "..." ,
2870
- None ,
2871
- ) ;
2872
- err. span_note (
2873
- sup_trace. cause . span ,
2874
- & format ! ( "...so that the {}" , sup_trace. cause. as_requirement_str( ) ) ,
2875
- ) ;
2888
+ note_and_explain_region (
2889
+ self . tcx ,
2890
+ & mut err,
2891
+ "...but the lifetime must also be valid for " ,
2892
+ sub_region,
2893
+ "..." ,
2894
+ None ,
2895
+ ) ;
2896
+ err. span_note (
2897
+ sup_trace. cause . span ,
2898
+ & format ! ( "...so that the {}" , sup_trace. cause. as_requirement_str( ) ) ,
2899
+ ) ;
2876
2900
2877
- err. note_expected_found ( & "" , sup_expected, & "" , sup_found) ;
2878
- err. emit ( ) ;
2879
- return ;
2880
- }
2881
- }
2901
+ err. note_expected_found ( & "" , sup_expected, & "" , sup_found) ;
2902
+ err. emit ( ) ;
2903
+ return ;
2882
2904
}
2883
2905
2884
2906
self . note_region_origin ( & mut err, & sup_origin) ;
0 commit comments