summaryrefslogtreecommitdiffstats
path: root/src/tools/rust-analyzer/crates/rust-analyzer/src/diagnostics/to_proto.rs
blob: 415fa4e02f20cf7805c46b5d456c54916da65c8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
//! This module provides the functionality needed to convert diagnostics from
//! `cargo check` json format to the LSP diagnostic format.
use std::collections::HashMap;

use flycheck::{Applicability, DiagnosticLevel, DiagnosticSpan};
use ide_db::line_index::WideEncoding;
use itertools::Itertools;
use stdx::format_to;
use vfs::{AbsPath, AbsPathBuf};

use crate::{
    global_state::GlobalStateSnapshot, line_index::PositionEncoding, lsp_ext,
    to_proto::url_from_abs_path,
};

use super::{DiagnosticsMapConfig, Fix};

/// Determines the LSP severity from a diagnostic
fn diagnostic_severity(
    config: &DiagnosticsMapConfig,
    level: flycheck::DiagnosticLevel,
    code: Option<flycheck::DiagnosticCode>,
) -> Option<lsp_types::DiagnosticSeverity> {
    let res = match level {
        DiagnosticLevel::Ice => lsp_types::DiagnosticSeverity::ERROR,
        DiagnosticLevel::Error => lsp_types::DiagnosticSeverity::ERROR,
        DiagnosticLevel::Warning => match &code {
            // HACK: special case for `warnings` rustc lint.
            Some(code)
                if config.warnings_as_hint.iter().any(|lint| {
                    lint == "warnings" || ide_db::helpers::lint_eq_or_in_group(&code.code, lint)
                }) =>
            {
                lsp_types::DiagnosticSeverity::HINT
            }
            // HACK: special case for `warnings` rustc lint.
            Some(code)
                if config.warnings_as_info.iter().any(|lint| {
                    lint == "warnings" || ide_db::helpers::lint_eq_or_in_group(&code.code, lint)
                }) =>
            {
                lsp_types::DiagnosticSeverity::INFORMATION
            }
            _ => lsp_types::DiagnosticSeverity::WARNING,
        },
        DiagnosticLevel::Note => lsp_types::DiagnosticSeverity::INFORMATION,
        DiagnosticLevel::Help => lsp_types::DiagnosticSeverity::HINT,
        _ => return None,
    };
    Some(res)
}

/// Checks whether a file name is from macro invocation and does not refer to an actual file.
fn is_dummy_macro_file(file_name: &str) -> bool {
    // FIXME: current rustc does not seem to emit `<macro file>` files anymore?
    file_name.starts_with('<') && file_name.ends_with('>')
}

/// Converts a Rust span to a LSP location
fn location(
    config: &DiagnosticsMapConfig,
    workspace_root: &AbsPath,
    span: &DiagnosticSpan,
    snap: &GlobalStateSnapshot,
) -> lsp_types::Location {
    let file_name = resolve_path(config, workspace_root, &span.file_name);
    let uri = url_from_abs_path(&file_name);

    let range = {
        let position_encoding = snap.config.position_encoding();
        lsp_types::Range::new(
            position(&position_encoding, span, span.line_start, span.column_start),
            position(&position_encoding, span, span.line_end, span.column_end),
        )
    };
    lsp_types::Location::new(uri, range)
}

fn position(
    position_encoding: &PositionEncoding,
    span: &DiagnosticSpan,
    line_offset: usize,
    column_offset: usize,
) -> lsp_types::Position {
    let line_index = line_offset - span.line_start;

    let mut true_column_offset = column_offset;
    if let Some(line) = span.text.get(line_index) {
        if line.text.chars().count() == line.text.len() {
            // all one byte utf-8 char
            return lsp_types::Position {
                line: (line_offset as u32).saturating_sub(1),
                character: (column_offset as u32).saturating_sub(1),
            };
        }
        let mut char_offset = 0;
        let len_func = match position_encoding {
            PositionEncoding::Utf8 => char::len_utf8,
            PositionEncoding::Wide(WideEncoding::Utf16) => char::len_utf16,
            PositionEncoding::Wide(WideEncoding::Utf32) => |_| 1,
        };
        for c in line.text.chars() {
            char_offset += 1;
            if char_offset > column_offset {
                break;
            }
            true_column_offset += len_func(c) - 1;
        }
    }

    lsp_types::Position {
        line: (line_offset as u32).saturating_sub(1),
        character: (true_column_offset as u32).saturating_sub(1),
    }
}

/// Extracts a suitable "primary" location from a rustc diagnostic.
///
/// This takes locations pointing into the standard library, or generally outside the current
/// workspace into account and tries to avoid those, in case macros are involved.
fn primary_location(
    config: &DiagnosticsMapConfig,
    workspace_root: &AbsPath,
    span: &DiagnosticSpan,
    snap: &GlobalStateSnapshot,
) -> lsp_types::Location {
    let span_stack = std::iter::successors(Some(span), |span| Some(&span.expansion.as_ref()?.span));
    for span in span_stack.clone() {
        let abs_path = resolve_path(config, workspace_root, &span.file_name);
        if !is_dummy_macro_file(&span.file_name) && abs_path.starts_with(workspace_root) {
            return location(config, workspace_root, span, snap);
        }
    }

    // Fall back to the outermost macro invocation if no suitable span comes up.
    let last_span = span_stack.last().unwrap();
    location(config, workspace_root, last_span, snap)
}

/// Converts a secondary Rust span to a LSP related information
///
/// If the span is unlabelled this will return `None`.
fn diagnostic_related_information(
    config: &DiagnosticsMapConfig,
    workspace_root: &AbsPath,
    span: &DiagnosticSpan,
    snap: &GlobalStateSnapshot,
) -> Option<lsp_types::DiagnosticRelatedInformation> {
    let message = span.label.clone()?;
    let location = location(config, workspace_root, span, snap);
    Some(lsp_types::DiagnosticRelatedInformation { location, message })
}

/// Resolves paths applying any matching path prefix remappings, and then
/// joining the path to the workspace root.
fn resolve_path(
    config: &DiagnosticsMapConfig,
    workspace_root: &AbsPath,
    file_name: &str,
) -> AbsPathBuf {
    match config
        .remap_prefix
        .iter()
        .find_map(|(from, to)| file_name.strip_prefix(from).map(|file_name| (to, file_name)))
    {
        Some((to, file_name)) => workspace_root.join(format!("{to}{file_name}")),
        None => workspace_root.join(file_name),
    }
}

struct SubDiagnostic {
    related: lsp_types::DiagnosticRelatedInformation,
    suggested_fix: Option<Fix>,
}

enum MappedRustChildDiagnostic {
    SubDiagnostic(SubDiagnostic),
    MessageLine(String),
}

fn map_rust_child_diagnostic(
    config: &DiagnosticsMapConfig,
    workspace_root: &AbsPath,
    rd: &flycheck::Diagnostic,
    snap: &GlobalStateSnapshot,
) -> MappedRustChildDiagnostic {
    let spans: Vec<&DiagnosticSpan> = rd.spans.iter().filter(|s| s.is_primary).collect();
    if spans.is_empty() {
        // `rustc` uses these spanless children as a way to print multi-line
        // messages
        return MappedRustChildDiagnostic::MessageLine(rd.message.clone());
    }

    let mut edit_map: HashMap<lsp_types::Url, Vec<lsp_types::TextEdit>> = HashMap::new();
    let mut suggested_replacements = Vec::new();
    let mut is_preferred = true;
    for &span in &spans {
        if let Some(suggested_replacement) = &span.suggested_replacement {
            if !suggested_replacement.is_empty() {
                suggested_replacements.push(suggested_replacement);
            }
            let location = location(config, workspace_root, span, snap);
            let edit = lsp_types::TextEdit::new(location.range, suggested_replacement.clone());

            // Only actually emit a quickfix if the suggestion is "valid enough".
            // We accept both "MaybeIncorrect" and "MachineApplicable". "MaybeIncorrect" means that
            // the suggestion is *complete* (contains no placeholders where code needs to be
            // inserted), but might not be what the user wants, or might need minor adjustments.
            if matches!(
                span.suggestion_applicability,
                None | Some(Applicability::MaybeIncorrect | Applicability::MachineApplicable)
            ) {
                edit_map.entry(location.uri).or_default().push(edit);
            }
            is_preferred &=
                matches!(span.suggestion_applicability, Some(Applicability::MachineApplicable));
        }
    }

    // rustc renders suggestion diagnostics by appending the suggested replacement, so do the same
    // here, otherwise the diagnostic text is missing useful information.
    let mut message = rd.message.clone();
    if !suggested_replacements.is_empty() {
        message.push_str(": ");
        let suggestions =
            suggested_replacements.iter().map(|suggestion| format!("`{suggestion}`")).join(", ");
        message.push_str(&suggestions);
    }

    if edit_map.is_empty() {
        MappedRustChildDiagnostic::SubDiagnostic(SubDiagnostic {
            related: lsp_types::DiagnosticRelatedInformation {
                location: location(config, workspace_root, spans[0], snap),
                message,
            },
            suggested_fix: None,
        })
    } else {
        MappedRustChildDiagnostic::SubDiagnostic(SubDiagnostic {
            related: lsp_types::DiagnosticRelatedInformation {
                location: location(config, workspace_root, spans[0], snap),
                message: message.clone(),
            },
            suggested_fix: Some(Fix {
                ranges: spans
                    .iter()
                    .map(|&span| location(config, workspace_root, span, snap).range)
                    .collect(),
                action: lsp_ext::CodeAction {
                    title: message,
                    group: None,
                    kind: Some(lsp_types::CodeActionKind::QUICKFIX),
                    edit: Some(lsp_ext::SnippetWorkspaceEdit {
                        // FIXME: there's no good reason to use edit_map here....
                        changes: Some(edit_map),
                        document_changes: None,
                        change_annotations: None,
                    }),
                    is_preferred: Some(is_preferred),
                    data: None,
                    command: None,
                },
            }),
        })
    }
}

#[derive(Debug)]
pub(crate) struct MappedRustDiagnostic {
    pub(crate) url: lsp_types::Url,
    pub(crate) diagnostic: lsp_types::Diagnostic,
    pub(crate) fix: Option<Fix>,
}

/// Converts a Rust root diagnostic to LSP form
///
/// This flattens the Rust diagnostic by:
///
/// 1. Creating a LSP diagnostic with the root message and primary span.
/// 2. Adding any labelled secondary spans to `relatedInformation`
/// 3. Categorising child diagnostics as either `SuggestedFix`es,
///    `relatedInformation` or additional message lines.
///
/// If the diagnostic has no primary span this will return `None`
pub(crate) fn map_rust_diagnostic_to_lsp(
    config: &DiagnosticsMapConfig,
    rd: &flycheck::Diagnostic,
    workspace_root: &AbsPath,
    snap: &GlobalStateSnapshot,
) -> Vec<MappedRustDiagnostic> {
    let primary_spans: Vec<&DiagnosticSpan> = rd.spans.iter().filter(|s| s.is_primary).collect();
    if primary_spans.is_empty() {
        return Vec::new();
    }

    let severity = diagnostic_severity(config, rd.level, rd.code.clone());

    let mut source = String::from("rustc");
    let mut code = rd.code.as_ref().map(|c| c.code.clone());
    if let Some(code_val) = &code {
        // See if this is an RFC #2103 scoped lint (e.g. from Clippy)
        let scoped_code: Vec<&str> = code_val.split("::").collect();
        if scoped_code.len() == 2 {
            source = String::from(scoped_code[0]);
            code = Some(String::from(scoped_code[1]));
        }
    }

    let mut needs_primary_span_label = true;
    let mut subdiagnostics = Vec::new();
    let mut tags = Vec::new();

    for secondary_span in rd.spans.iter().filter(|s| !s.is_primary) {
        let related = diagnostic_related_information(config, workspace_root, secondary_span, snap);
        if let Some(related) = related {
            subdiagnostics.push(SubDiagnostic { related, suggested_fix: None });
        }
    }

    let mut message = rd.message.clone();
    for child in &rd.children {
        let child = map_rust_child_diagnostic(config, workspace_root, child, snap);
        match child {
            MappedRustChildDiagnostic::SubDiagnostic(sub) => {
                subdiagnostics.push(sub);
            }
            MappedRustChildDiagnostic::MessageLine(message_line) => {
                format_to!(message, "\n{}", message_line);

                // These secondary messages usually duplicate the content of the
                // primary span label.
                needs_primary_span_label = false;
            }
        }
    }

    if let Some(code) = &rd.code {
        let code = code.code.as_str();
        if matches!(
            code,
            "dead_code"
                | "unknown_lints"
                | "unreachable_code"
                | "unused_attributes"
                | "unused_imports"
                | "unused_macros"
                | "unused_variables"
        ) {
            tags.push(lsp_types::DiagnosticTag::UNNECESSARY);
        }

        if matches!(code, "deprecated") {
            tags.push(lsp_types::DiagnosticTag::DEPRECATED);
        }
    }

    let code_description = match source.as_str() {
        "rustc" => rustc_code_description(code.as_deref()),
        "clippy" => clippy_code_description(code.as_deref()),
        _ => None,
    };

    primary_spans
        .iter()
        .flat_map(|primary_span| {
            let primary_location = primary_location(config, workspace_root, primary_span, snap);
            let message = {
                let mut message = message.clone();
                if needs_primary_span_label {
                    if let Some(primary_span_label) = &primary_span.label {
                        format_to!(message, "\n{}", primary_span_label);
                    }
                }
                message
            };
            // Each primary diagnostic span may result in multiple LSP diagnostics.
            let mut diagnostics = Vec::new();

            let mut related_info_macro_calls = vec![];

            // If error occurs from macro expansion, add related info pointing to
            // where the error originated
            // Also, we would generate an additional diagnostic, so that exact place of macro
            // will be highlighted in the error origin place.
            let span_stack = std::iter::successors(Some(*primary_span), |span| {
                Some(&span.expansion.as_ref()?.span)
            });
            for (i, span) in span_stack.enumerate() {
                if is_dummy_macro_file(&span.file_name) {
                    continue;
                }

                // First span is the original diagnostic, others are macro call locations that
                // generated that code.
                let is_in_macro_call = i != 0;

                let secondary_location = location(config, workspace_root, span, snap);
                if secondary_location == primary_location {
                    continue;
                }
                related_info_macro_calls.push(lsp_types::DiagnosticRelatedInformation {
                    location: secondary_location.clone(),
                    message: if is_in_macro_call {
                        "Error originated from macro call here".to_string()
                    } else {
                        "Actual error occurred here".to_string()
                    },
                });
                // For the additional in-macro diagnostic we add the inverse message pointing to the error location in code.
                let information_for_additional_diagnostic =
                    vec![lsp_types::DiagnosticRelatedInformation {
                        location: primary_location.clone(),
                        message: "Exact error occurred here".to_string(),
                    }];

                let diagnostic = lsp_types::Diagnostic {
                    range: secondary_location.range,
                    // downgrade to hint if we're pointing at the macro
                    severity: Some(lsp_types::DiagnosticSeverity::HINT),
                    code: code.clone().map(lsp_types::NumberOrString::String),
                    code_description: code_description.clone(),
                    source: Some(source.clone()),
                    message: message.clone(),
                    related_information: Some(information_for_additional_diagnostic),
                    tags: if tags.is_empty() { None } else { Some(tags.clone()) },
                    data: Some(serde_json::json!({ "rendered": rd.rendered })),
                };
                diagnostics.push(MappedRustDiagnostic {
                    url: secondary_location.uri,
                    diagnostic,
                    fix: None,
                });
            }

            // Emit the primary diagnostic.
            diagnostics.push(MappedRustDiagnostic {
                url: primary_location.uri.clone(),
                diagnostic: lsp_types::Diagnostic {
                    range: primary_location.range,
                    severity,
                    code: code.clone().map(lsp_types::NumberOrString::String),
                    code_description: code_description.clone(),
                    source: Some(source.clone()),
                    message,
                    related_information: {
                        let info = related_info_macro_calls
                            .iter()
                            .cloned()
                            .chain(subdiagnostics.iter().map(|sub| sub.related.clone()))
                            .collect::<Vec<_>>();
                        if info.is_empty() {
                            None
                        } else {
                            Some(info)
                        }
                    },
                    tags: if tags.is_empty() { None } else { Some(tags.clone()) },
                    data: Some(serde_json::json!({ "rendered": rd.rendered })),
                },
                fix: None,
            });

            // Emit hint-level diagnostics for all `related_information` entries such as "help"s.
            // This is useful because they will show up in the user's editor, unlike
            // `related_information`, which just produces hard-to-read links, at least in VS Code.
            let back_ref = lsp_types::DiagnosticRelatedInformation {
                location: primary_location,
                message: "original diagnostic".to_string(),
            };
            for sub in &subdiagnostics {
                diagnostics.push(MappedRustDiagnostic {
                    url: sub.related.location.uri.clone(),
                    fix: sub.suggested_fix.clone(),
                    diagnostic: lsp_types::Diagnostic {
                        range: sub.related.location.range,
                        severity: Some(lsp_types::DiagnosticSeverity::HINT),
                        code: code.clone().map(lsp_types::NumberOrString::String),
                        code_description: code_description.clone(),
                        source: Some(source.clone()),
                        message: sub.related.message.clone(),
                        related_information: Some(vec![back_ref.clone()]),
                        tags: None, // don't apply modifiers again
                        data: None,
                    },
                });
            }

            diagnostics
        })
        .collect()
}

fn rustc_code_description(code: Option<&str>) -> Option<lsp_types::CodeDescription> {
    code.filter(|code| {
        let mut chars = code.chars();
        chars.next().map_or(false, |c| c == 'E')
            && chars.by_ref().take(4).all(|c| c.is_ascii_digit())
            && chars.next().is_none()
    })
    .and_then(|code| {
        lsp_types::Url::parse(&format!("https://doc.rust-lang.org/error-index.html#{code}"))
            .ok()
            .map(|href| lsp_types::CodeDescription { href })
    })
}

fn clippy_code_description(code: Option<&str>) -> Option<lsp_types::CodeDescription> {
    code.and_then(|code| {
        lsp_types::Url::parse(&format!(
            "https://rust-lang.github.io/rust-clippy/master/index.html#{code}"
        ))
        .ok()
        .map(|href| lsp_types::CodeDescription { href })
    })
}

#[cfg(test)]
#[cfg(not(windows))]
mod tests {
    use std::path::Path;

    use crate::{config::Config, global_state::GlobalState};

    use super::*;

    use expect_test::{expect_file, ExpectFile};
    use lsp_types::ClientCapabilities;

    fn check(diagnostics_json: &str, expect: ExpectFile) {
        check_with_config(DiagnosticsMapConfig::default(), diagnostics_json, expect)
    }

    fn check_with_config(config: DiagnosticsMapConfig, diagnostics_json: &str, expect: ExpectFile) {
        let diagnostic: flycheck::Diagnostic = serde_json::from_str(diagnostics_json).unwrap();
        let workspace_root: &AbsPath = Path::new("/test/").try_into().unwrap();
        let (sender, _) = crossbeam_channel::unbounded();
        let state = GlobalState::new(
            sender,
            Config::new(workspace_root.to_path_buf(), ClientCapabilities::default(), Vec::new()),
        );
        let snap = state.snapshot();
        let mut actual = map_rust_diagnostic_to_lsp(&config, &diagnostic, workspace_root, &snap);
        actual.iter_mut().for_each(|diag| diag.diagnostic.data = None);
        expect.assert_debug_eq(&actual)
    }

    #[test]
    fn rustc_incompatible_type_for_trait() {
        check(
            r##"{
                "message": "method `next` has an incompatible type for trait",
                "code": {
                    "code": "E0053",
                    "explanation": "\nThe parameters of any trait method must match between a trait implementation\nand the trait definition.\n\nHere are a couple examples of this error:\n\n```compile_fail,E0053\ntrait Foo {\n    fn foo(x: u16);\n    fn bar(&self);\n}\n\nstruct Bar;\n\nimpl Foo for Bar {\n    // error, expected u16, found i16\n    fn foo(x: i16) { }\n\n    // error, types differ in mutability\n    fn bar(&mut self) { }\n}\n```\n"
                },
                "level": "error",
                "spans": [
                    {
                        "file_name": "compiler/ty/list_iter.rs",
                        "byte_start": 1307,
                        "byte_end": 1350,
                        "line_start": 52,
                        "line_end": 52,
                        "column_start": 5,
                        "column_end": 48,
                        "is_primary": true,
                        "text": [
                            {
                                "text": "    fn next(&self) -> Option<&'list ty::Ref<M>> {",
                                "highlight_start": 5,
                                "highlight_end": 48
                            }
                        ],
                        "label": "types differ in mutability",
                        "suggested_replacement": null,
                        "suggestion_applicability": null,
                        "expansion": null
                    }
                ],
                "children": [
                    {
                        "message": "expected type `fn(&mut ty::list_iter::ListIterator<'list, M>) -> std::option::Option<&ty::Ref<M>>`\n   found type `fn(&ty::list_iter::ListIterator<'list, M>) -> std::option::Option<&'list ty::Ref<M>>`",
                        "code": null,
                        "level": "note",
                        "spans": [],
                        "children": [],
                        "rendered": null
                    }
                ],
                "rendered": "error[E0053]: method `next` has an incompatible type for trait\n  --> compiler/ty/list_iter.rs:52:5\n   |\n52 |     fn next(&self) -> Option<&'list ty::Ref<M>> {\n   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ types differ in mutability\n   |\n   = note: expected type `fn(&mut ty::list_iter::ListIterator<'list, M>) -> std::option::Option<&ty::Ref<M>>`\n              found type `fn(&ty::list_iter::ListIterator<'list, M>) -> std::option::Option<&'list ty::Ref<M>>`\n\n"
            }
            "##,
            expect_file!["./test_data/rustc_incompatible_type_for_trait.txt"],
        );
    }

    #[test]
    fn rustc_unused_variable() {
        check(
            r##"{
    "message": "unused variable: `foo`",
    "code": {
        "code": "unused_variables",
        "explanation": null
    },
    "level": "warning",
    "spans": [
        {
            "file_name": "driver/subcommand/repl.rs",
            "byte_start": 9228,
            "byte_end": 9231,
            "line_start": 291,
            "line_end": 291,
            "column_start": 9,
            "column_end": 12,
            "is_primary": true,
            "text": [
                {
                    "text": "    let foo = 42;",
                    "highlight_start": 9,
                    "highlight_end": 12
                }
            ],
            "label": null,
            "suggested_replacement": null,
            "suggestion_applicability": null,
            "expansion": null
        }
    ],
    "children": [
        {
            "message": "#[warn(unused_variables)] on by default",
            "code": null,
            "level": "note",
            "spans": [],
            "children": [],
            "rendered": null
        },
        {
            "message": "consider prefixing with an underscore",
            "code": null,
            "level": "help",
            "spans": [
                {
                    "file_name": "driver/subcommand/repl.rs",
                    "byte_start": 9228,
                    "byte_end": 9231,
                    "line_start": 291,
                    "line_end": 291,
                    "column_start": 9,
                    "column_end": 12,
                    "is_primary": true,
                    "text": [
                        {
                            "text": "    let foo = 42;",
                            "highlight_start": 9,
                            "highlight_end": 12
                        }
                    ],
                    "label": null,
                    "suggested_replacement": "_foo",
                    "suggestion_applicability": "MachineApplicable",
                    "expansion": null
                }
            ],
            "children": [],
            "rendered": null
        }
    ],
    "rendered": "warning: unused variable: `foo`\n   --> driver/subcommand/repl.rs:291:9\n    |\n291 |     let foo = 42;\n    |         ^^^ help: consider prefixing with an underscore: `_foo`\n    |\n    = note: #[warn(unused_variables)] on by default\n\n"
    }"##,
            expect_file!["./test_data/rustc_unused_variable.txt"],
        );
    }

    #[test]
    #[cfg(not(windows))]
    fn rustc_unused_variable_as_info() {
        check_with_config(
            DiagnosticsMapConfig {
                warnings_as_info: vec!["unused_variables".to_string()],
                ..DiagnosticsMapConfig::default()
            },
            r##"{
    "message": "unused variable: `foo`",
    "code": {
        "code": "unused_variables",
        "explanation": null
    },
    "level": "warning",
    "spans": [
        {
            "file_name": "driver/subcommand/repl.rs",
            "byte_start": 9228,
            "byte_end": 9231,
            "line_start": 291,
            "line_end": 291,
            "column_start": 9,
            "column_end": 12,
            "is_primary": true,
            "text": [
                {
                    "text": "    let foo = 42;",
                    "highlight_start": 9,
                    "highlight_end": 12
                }
            ],
            "label": null,
            "suggested_replacement": null,
            "suggestion_applicability": null,
            "expansion": null
        }
    ],
    "children": [
        {
            "message": "#[warn(unused_variables)] on by default",
            "code": null,
            "level": "note",
            "spans": [],
            "children": [],
            "rendered": null
        },
        {
            "message": "consider prefixing with an underscore",
            "code": null,
            "level": "help",
            "spans": [
                {
                    "file_name": "driver/subcommand/repl.rs",
                    "byte_start": 9228,
                    "byte_end": 9231,
                    "line_start": 291,
                    "line_end": 291,
                    "column_start": 9,
                    "column_end": 12,
                    "is_primary": true,
                    "text": [
                        {
                            "text": "    let foo = 42;",
                            "highlight_start": 9,
                            "highlight_end": 12
                        }
                    ],
                    "label": null,
                    "suggested_replacement": "_foo",
                    "suggestion_applicability": "MachineApplicable",
                    "expansion": null
                }
            ],
            "children": [],
            "rendered": null
        }
    ],
    "rendered": "warning: unused variable: `foo`\n   --> driver/subcommand/repl.rs:291:9\n    |\n291 |     let foo = 42;\n    |         ^^^ help: consider prefixing with an underscore: `_foo`\n    |\n    = note: #[warn(unused_variables)] on by default\n\n"
    }"##,
            expect_file!["./test_data/rustc_unused_variable_as_info.txt"],
        );
    }

    #[test]
    #[cfg(not(windows))]
    fn rustc_unused_variable_as_hint() {
        check_with_config(
            DiagnosticsMapConfig {
                warnings_as_hint: vec!["unused_variables".to_string()],
                ..DiagnosticsMapConfig::default()
            },
            r##"{
    "message": "unused variable: `foo`",
    "code": {
        "code": "unused_variables",
        "explanation": null
    },
    "level": "warning",
    "spans": [
        {
            "file_name": "driver/subcommand/repl.rs",
            "byte_start": 9228,
            "byte_end": 9231,
            "line_start": 291,
            "line_end": 291,
            "column_start": 9,
            "column_end": 12,
            "is_primary": true,
            "text": [
                {
                    "text": "    let foo = 42;",
                    "highlight_start": 9,
                    "highlight_end": 12
                }
            ],
            "label": null,
            "suggested_replacement": null,
            "suggestion_applicability": null,
            "expansion": null
        }
    ],
    "children": [
        {
            "message": "#[warn(unused_variables)] on by default",
            "code": null,
            "level": "note",
            "spans": [],
            "children": [],
            "rendered": null
        },
        {
            "message": "consider prefixing with an underscore",
            "code": null,
            "level": "help",
            "spans": [
                {
                    "file_name": "driver/subcommand/repl.rs",
                    "byte_start": 9228,
                    "byte_end": 9231,
                    "line_start": 291,
                    "line_end": 291,
                    "column_start": 9,
                    "column_end": 12,
                    "is_primary": true,
                    "text": [
                        {
                            "text": "    let foo = 42;",
                            "highlight_start": 9,
                            "highlight_end": 12
                        }
                    ],
                    "label": null,
                    "suggested_replacement": "_foo",
                    "suggestion_applicability": "MachineApplicable",
                    "expansion": null
                }
            ],
            "children": [],
            "rendered": null
        }
    ],
    "rendered": "warning: unused variable: `foo`\n   --> driver/subcommand/repl.rs:291:9\n    |\n291 |     let foo = 42;\n    |         ^^^ help: consider prefixing with an underscore: `_foo`\n    |\n    = note: #[warn(unused_variables)] on by default\n\n"
    }"##,
            expect_file!["./test_data/rustc_unused_variable_as_hint.txt"],
        );
    }

    #[test]
    fn rustc_wrong_number_of_parameters() {
        check(
            r##"{
    "message": "this function takes 2 parameters but 3 parameters were supplied",
    "code": {
        "code": "E0061",
        "explanation": "\nThe number of arguments passed to a function must match the number of arguments\nspecified in the function signature.\n\nFor example, a function like:\n\n```\nfn f(a: u16, b: &str) {}\n```\n\nMust always be called with exactly two arguments, e.g., `f(2, \"test\")`.\n\nNote that Rust does not have a notion of optional function arguments or\nvariadic functions (except for its C-FFI).\n"
    },
    "level": "error",
    "spans": [
        {
            "file_name": "compiler/ty/select.rs",
            "byte_start": 8787,
            "byte_end": 9241,
            "line_start": 219,
            "line_end": 231,
            "column_start": 5,
            "column_end": 6,
            "is_primary": false,
            "text": [
                {
                    "text": "    pub fn add_evidence(",
                    "highlight_start": 5,
                    "highlight_end": 25
                },
                {
                    "text": "        &mut self,",
                    "highlight_start": 1,
                    "highlight_end": 19
                },
                {
                    "text": "        target_poly: &ty::Ref<ty::Poly>,",
                    "highlight_start": 1,
                    "highlight_end": 41
                },
                {
                    "text": "        evidence_poly: &ty::Ref<ty::Poly>,",
                    "highlight_start": 1,
                    "highlight_end": 43
                },
                {
                    "text": "    ) {",
                    "highlight_start": 1,
                    "highlight_end": 8
                },
                {
                    "text": "        match target_poly {",
                    "highlight_start": 1,
                    "highlight_end": 28
                },
                {
                    "text": "            ty::Ref::Var(tvar, _) => self.add_var_evidence(tvar, evidence_poly),",
                    "highlight_start": 1,
                    "highlight_end": 81
                },
                {
                    "text": "            ty::Ref::Fixed(target_ty) => {",
                    "highlight_start": 1,
                    "highlight_end": 43
                },
                {
                    "text": "                let evidence_ty = evidence_poly.resolve_to_ty();",
                    "highlight_start": 1,
                    "highlight_end": 65
                },
                {
                    "text": "                self.add_evidence_ty(target_ty, evidence_poly, evidence_ty)",
                    "highlight_start": 1,
                    "highlight_end": 76
                },
                {
                    "text": "            }",
                    "highlight_start": 1,
                    "highlight_end": 14
                },
                {
                    "text": "        }",
                    "highlight_start": 1,
                    "highlight_end": 10
                },
                {
                    "text": "    }",
                    "highlight_start": 1,
                    "highlight_end": 6
                }
            ],
            "label": "defined here",
            "suggested_replacement": null,
            "suggestion_applicability": null,
            "expansion": null
        },
        {
            "file_name": "compiler/ty/select.rs",
            "byte_start": 4045,
            "byte_end": 4057,
            "line_start": 104,
            "line_end": 104,
            "column_start": 18,
            "column_end": 30,
            "is_primary": true,
            "text": [
                {
                    "text": "            self.add_evidence(target_fixed, evidence_fixed, false);",
                    "highlight_start": 18,
                    "highlight_end": 30
                }
            ],
            "label": "expected 2 parameters",
            "suggested_replacement": null,
            "suggestion_applicability": null,
            "expansion": null
        }
    ],
    "children": [],
    "rendered": "error[E0061]: this function takes 2 parameters but 3 parameters were supplied\n   --> compiler/ty/select.rs:104:18\n    |\n104 |               self.add_evidence(target_fixed, evidence_fixed, false);\n    |                    ^^^^^^^^^^^^ expected 2 parameters\n...\n219 | /     pub fn add_evidence(\n220 | |         &mut self,\n221 | |         target_poly: &ty::Ref<ty::Poly>,\n222 | |         evidence_poly: &ty::Ref<ty::Poly>,\n...   |\n230 | |         }\n231 | |     }\n    | |_____- defined here\n\n"
    }"##,
            expect_file!["./test_data/rustc_wrong_number_of_parameters.txt"],
        );
    }

    #[test]
    fn clippy_pass_by_ref() {
        check(
            r##"{
    "message": "this argument is passed by reference, but would be more efficient if passed by value",
    "code": {
        "code": "clippy::trivially_copy_pass_by_ref",
        "explanation": null
    },
    "level": "warning",
    "spans": [
        {
            "file_name": "compiler/mir/tagset.rs",
            "byte_start": 941,
            "byte_end": 946,
            "line_start": 42,
            "line_end": 42,
            "column_start": 24,
            "column_end": 29,
            "is_primary": true,
            "text": [
                {
                    "text": "    pub fn is_disjoint(&self, other: Self) -> bool {",
                    "highlight_start": 24,
                    "highlight_end": 29
                }
            ],
            "label": null,
            "suggested_replacement": null,
            "suggestion_applicability": null,
            "expansion": null
        }
    ],
    "children": [
        {
            "message": "lint level defined here",
            "code": null,
            "level": "note",
            "spans": [
                {
                    "file_name": "compiler/lib.rs",
                    "byte_start": 8,
                    "byte_end": 19,
                    "line_start": 1,
                    "line_end": 1,
                    "column_start": 9,
                    "column_end": 20,
                    "is_primary": true,
                    "text": [
                        {
                            "text": "#![warn(clippy::all)]",
                            "highlight_start": 9,
                            "highlight_end": 20
                        }
                    ],
                    "label": null,
                    "suggested_replacement": null,
                    "suggestion_applicability": null,
                    "expansion": null
                }
            ],
            "children": [],
            "rendered": null
        },
        {
            "message": "#[warn(clippy::trivially_copy_pass_by_ref)] implied by #[warn(clippy::all)]",
            "code": null,
            "level": "note",
            "spans": [],
            "children": [],
            "rendered": null
        },
        {
            "message": "for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#trivially_copy_pass_by_ref",
            "code": null,
            "level": "help",
            "spans": [],
            "children": [],
            "rendered": null
        },
        {
            "message": "consider passing by value instead",
            "code": null,
            "level": "help",
            "spans": [
                {
                    "file_name": "compiler/mir/tagset.rs",
                    "byte_start": 941,
                    "byte_end": 946,
                    "line_start": 42,
                    "line_end": 42,
                    "column_start": 24,
                    "column_end": 29,
                    "is_primary": true,
                    "text": [
                        {
                            "text": "    pub fn is_disjoint(&self, other: Self) -> bool {",
                            "highlight_start": 24,
                            "highlight_end": 29
                        }
                    ],
                    "label": null,
                    "suggested_replacement": "self",
                    "suggestion_applicability": "Unspecified",
                    "expansion": null
                }
            ],
            "children": [],
            "rendered": null
        }
    ],
    "rendered": "warning: this argument is passed by reference, but would be more efficient if passed by value\n  --> compiler/mir/tagset.rs:42:24\n   |\n42 |     pub fn is_disjoint(&self, other: Self) -> bool {\n   |                        ^^^^^ help: consider passing by value instead: `self`\n   |\nnote: lint level defined here\n  --> compiler/lib.rs:1:9\n   |\n1  | #![warn(clippy::all)]\n   |         ^^^^^^^^^^^\n   = note: #[warn(clippy::trivially_copy_pass_by_ref)] implied by #[warn(clippy::all)]\n   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#trivially_copy_pass_by_ref\n\n"
    }"##,
            expect_file!["./test_data/clippy_pass_by_ref.txt"],
        );
    }

    #[test]
    fn rustc_range_map_lsp_position() {
        check(
            r##"{
            "message": "mismatched types",
            "code": {
                "code": "E0308",
                "explanation": "Expected type did not match the received type.\n\nErroneous code examples:\n\n```compile_fail,E0308\nfn plus_one(x: i32) -> i32 {\n    x + 1\n}\n\nplus_one(\"Not a number\");\n//       ^^^^^^^^^^^^^^ expected `i32`, found `&str`\n\nif \"Not a bool\" {\n// ^^^^^^^^^^^^ expected `bool`, found `&str`\n}\n\nlet x: f32 = \"Not a float\";\n//     ---   ^^^^^^^^^^^^^ expected `f32`, found `&str`\n//     |\n//     expected due to this\n```\n\nThis error occurs when an expression was used in a place where the compiler\nexpected an expression of a different type. It can occur in several cases, the\nmost common being when calling a function and passing an argument which has a\ndifferent type than the matching type in the function declaration.\n"
            },
            "level": "error",
            "spans": [
                {
                    "file_name": "crates/test_diagnostics/src/main.rs",
                    "byte_start": 87,
                    "byte_end": 105,
                    "line_start": 4,
                    "line_end": 4,
                    "column_start": 18,
                    "column_end": 24,
                    "is_primary": true,
                    "text": [
                        {
                            "text": "    let x: u32 = \"𐐀𐐀𐐀𐐀\"; // 17-23",
                            "highlight_start": 18,
                            "highlight_end": 24
                        }
                    ],
                    "label": "expected `u32`, found `&str`",
                    "suggested_replacement": null,
                    "suggestion_applicability": null,
                    "expansion": null
                },
                {
                    "file_name": "crates/test_diagnostics/src/main.rs",
                    "byte_start": 81,
                    "byte_end": 84,
                    "line_start": 4,
                    "line_end": 4,
                    "column_start": 12,
                    "column_end": 15,
                    "is_primary": false,
                    "text": [
                        {
                            "text": "    let x: u32 = \"𐐀𐐀𐐀𐐀\"; // 17-23",
                            "highlight_start": 12,
                            "highlight_end": 15
                        }
                    ],
                    "label": "expected due to this",
                    "suggested_replacement": null,
                    "suggestion_applicability": null,
                    "expansion": null
                }
            ],
            "children": [],
            "rendered": "error[E0308]: mismatched types\n --> crates/test_diagnostics/src/main.rs:4:18\n  |\n4 |     let x: u32 = \"𐐀𐐀𐐀𐐀\"; // 17-23\n  |            ---   ^^^^^^ expected `u32`, found `&str`\n  |            |\n  |            expected due to this\n\n"
        }"##,
            expect_file!("./test_data/rustc_range_map_lsp_position.txt"),
        )
    }

    #[test]
    fn rustc_mismatched_type() {
        check(
            r##"{
    "message": "mismatched types",
    "code": {
        "code": "E0308",
        "explanation": "\nThis error occurs when the compiler was unable to infer the concrete type of a\nvariable. It can occur for several cases, the most common of which is a\nmismatch in the expected type that the compiler inferred for a variable's\ninitializing expression, and the actual type explicitly assigned to the\nvariable.\n\nFor example:\n\n```compile_fail,E0308\nlet x: i32 = \"I am not a number!\";\n//     ~~~   ~~~~~~~~~~~~~~~~~~~~\n//      |             |\n//      |    initializing expression;\n//      |    compiler infers type `&str`\n//      |\n//    type `i32` assigned to variable `x`\n```\n"
    },
    "level": "error",
    "spans": [
        {
            "file_name": "runtime/compiler_support.rs",
            "byte_start": 1589,
            "byte_end": 1594,
            "line_start": 48,
            "line_end": 48,
            "column_start": 65,
            "column_end": 70,
            "is_primary": true,
            "text": [
                {
                    "text": "    let layout = alloc::Layout::from_size_align_unchecked(size, align);",
                    "highlight_start": 65,
                    "highlight_end": 70
                }
            ],
            "label": "expected usize, found u32",
            "suggested_replacement": null,
            "suggestion_applicability": null,
            "expansion": null
        }
    ],
    "children": [],
    "rendered": "error[E0308]: mismatched types\n  --> runtime/compiler_support.rs:48:65\n   |\n48 |     let layout = alloc::Layout::from_size_align_unchecked(size, align);\n   |                                                                 ^^^^^ expected usize, found u32\n\n"
    }"##,
            expect_file!["./test_data/rustc_mismatched_type.txt"],
        );
    }

    #[test]
    fn handles_macro_location() {
        check(
            r##"{
    "rendered": "error[E0277]: can't compare `{integer}` with `&str`\n --> src/main.rs:2:5\n  |\n2 |     assert_eq!(1, \"love\");\n  |     ^^^^^^^^^^^^^^^^^^^^^^ no implementation for `{integer} == &str`\n  |\n  = help: the trait `std::cmp::PartialEq<&str>` is not implemented for `{integer}`\n  = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)\n\n",
    "children": [
        {
            "children": [],
            "code": null,
            "level": "help",
            "message": "the trait `std::cmp::PartialEq<&str>` is not implemented for `{integer}`",
            "rendered": null,
            "spans": []
        }
    ],
    "code": {
        "code": "E0277",
        "explanation": "\nYou tried to use a type which doesn't implement some trait in a place which\nexpected that trait. Erroneous code example:\n\n```compile_fail,E0277\n// here we declare the Foo trait with a bar method\ntrait Foo {\n    fn bar(&self);\n}\n\n// we now declare a function which takes an object implementing the Foo trait\nfn some_func<T: Foo>(foo: T) {\n    foo.bar();\n}\n\nfn main() {\n    // we now call the method with the i32 type, which doesn't implement\n    // the Foo trait\n    some_func(5i32); // error: the trait bound `i32 : Foo` is not satisfied\n}\n```\n\nIn order to fix this error, verify that the type you're using does implement\nthe trait. Example:\n\n```\ntrait Foo {\n    fn bar(&self);\n}\n\nfn some_func<T: Foo>(foo: T) {\n    foo.bar(); // we can now use this method since i32 implements the\n               // Foo trait\n}\n\n// we implement the trait on the i32 type\nimpl Foo for i32 {\n    fn bar(&self) {}\n}\n\nfn main() {\n    some_func(5i32); // ok!\n}\n```\n\nOr in a generic context, an erroneous code example would look like:\n\n```compile_fail,E0277\nfn some_func<T>(foo: T) {\n    println!(\"{:?}\", foo); // error: the trait `core::fmt::Debug` is not\n                           //        implemented for the type `T`\n}\n\nfn main() {\n    // We now call the method with the i32 type,\n    // which *does* implement the Debug trait.\n    some_func(5i32);\n}\n```\n\nNote that the error here is in the definition of the generic function: Although\nwe only call it with a parameter that does implement `Debug`, the compiler\nstill rejects the function: It must work with all possible input types. In\norder to make this example compile, we need to restrict the generic type we're\naccepting:\n\n```\nuse std::fmt;\n\n// Restrict the input type to types that implement Debug.\nfn some_func<T: fmt::Debug>(foo: T) {\n    println!(\"{:?}\", foo);\n}\n\nfn main() {\n    // Calling the method is still fine, as i32 implements Debug.\n    some_func(5i32);\n\n    // This would fail to compile now:\n    // struct WithoutDebug;\n    // some_func(WithoutDebug);\n}\n```\n\nRust only looks at the signature of the called function, as such it must\nalready specify all requirements that will be used for every type parameter.\n"
    },
    "level": "error",
    "message": "can't compare `{integer}` with `&str`",
    "spans": [
        {
            "byte_end": 155,
            "byte_start": 153,
            "column_end": 33,
            "column_start": 31,
            "expansion": {
                "def_site_span": {
                    "byte_end": 940,
                    "byte_start": 0,
                    "column_end": 6,
                    "column_start": 1,
                    "expansion": null,
                    "file_name": "<::core::macros::assert_eq macros>",
                    "is_primary": false,
                    "label": null,
                    "line_end": 36,
                    "line_start": 1,
                    "suggested_replacement": null,
                    "suggestion_applicability": null,
                    "text": [
                        {
                            "highlight_end": 35,
                            "highlight_start": 1,
                            "text": "($ left : expr, $ right : expr) =>"
                        },
                        {
                            "highlight_end": 3,
                            "highlight_start": 1,
                            "text": "({"
                        },
                        {
                            "highlight_end": 33,
                            "highlight_start": 1,
                            "text": "     match (& $ left, & $ right)"
                        },
                        {
                            "highlight_end": 7,
                            "highlight_start": 1,
                            "text": "     {"
                        },
                        {
                            "highlight_end": 34,
                            "highlight_start": 1,
                            "text": "         (left_val, right_val) =>"
                        },
                        {
                            "highlight_end": 11,
                            "highlight_start": 1,
                            "text": "         {"
                        },
                        {
                            "highlight_end": 46,
                            "highlight_start": 1,
                            "text": "             if ! (* left_val == * right_val)"
                        },
                        {
                            "highlight_end": 15,
                            "highlight_start": 1,
                            "text": "             {"
                        },
                        {
                            "highlight_end": 25,
                            "highlight_start": 1,
                            "text": "                 panic !"
                        },
                        {
                            "highlight_end": 57,
                            "highlight_start": 1,
                            "text": "                 (r#\"assertion failed: `(left == right)`"
                        },
                        {
                            "highlight_end": 16,
                            "highlight_start": 1,
                            "text": "  left: `{:?}`,"
                        },
                        {
                            "highlight_end": 18,
                            "highlight_start": 1,
                            "text": " right: `{:?}`\"#,"
                        },
                        {
                            "highlight_end": 47,
                            "highlight_start": 1,
                            "text": "                  & * left_val, & * right_val)"
                        },
                        {
                            "highlight_end": 15,
                            "highlight_start": 1,
                            "text": "             }"
                        },
                        {
                            "highlight_end": 11,
                            "highlight_start": 1,
                            "text": "         }"
                        },
                        {
                            "highlight_end": 7,
                            "highlight_start": 1,
                            "text": "     }"
                        },
                        {
                            "highlight_end": 42,
                            "highlight_start": 1,
                            "text": " }) ; ($ left : expr, $ right : expr,) =>"
                        },
                        {
                            "highlight_end": 49,
                            "highlight_start": 1,
                            "text": "({ $ crate :: assert_eq ! ($ left, $ right) }) ;"
                        },
                        {
                            "highlight_end": 53,
                            "highlight_start": 1,
                            "text": "($ left : expr, $ right : expr, $ ($ arg : tt) +) =>"
                        },
                        {
                            "highlight_end": 3,
                            "highlight_start": 1,
                            "text": "({"
                        },
                        {
                            "highlight_end": 37,
                            "highlight_start": 1,
                            "text": "     match (& ($ left), & ($ right))"
                        },
                        {
                            "highlight_end": 7,
                            "highlight_start": 1,
                            "text": "     {"
                        },
                        {
                            "highlight_end": 34,
                            "highlight_start": 1,
                            "text": "         (left_val, right_val) =>"
                        },
                        {
                            "highlight_end": 11,
                            "highlight_start": 1,
                            "text": "         {"
                        },
                        {
                            "highlight_end": 46,
                            "highlight_start": 1,
                            "text": "             if ! (* left_val == * right_val)"
                        },
                        {
                            "highlight_end": 15,
                            "highlight_start": 1,
                            "text": "             {"
                        },
                        {
                            "highlight_end": 25,
                            "highlight_start": 1,
                            "text": "                 panic !"
                        },
                        {
                            "highlight_end": 57,
                            "highlight_start": 1,
                            "text": "                 (r#\"assertion failed: `(left == right)`"
                        },
                        {
                            "highlight_end": 16,
                            "highlight_start": 1,
                            "text": "  left: `{:?}`,"
                        },
                        {
                            "highlight_end": 22,
                            "highlight_start": 1,
                            "text": " right: `{:?}`: {}\"#,"
                        },
                        {
                            "highlight_end": 72,
                            "highlight_start": 1,
                            "text": "                  & * left_val, & * right_val, $ crate :: format_args !"
                        },
                        {
                            "highlight_end": 33,
                            "highlight_start": 1,
                            "text": "                  ($ ($ arg) +))"
                        },
                        {
                            "highlight_end": 15,
                            "highlight_start": 1,
                            "text": "             }"
                        },
                        {
                            "highlight_end": 11,
                            "highlight_start": 1,
                            "text": "         }"
                        },
                        {
                            "highlight_end": 7,
                            "highlight_start": 1,
                            "text": "     }"
                        },
                        {
                            "highlight_end": 6,
                            "highlight_start": 1,
                            "text": " }) ;"
                        }
                    ]
                },
                "macro_decl_name": "assert_eq!",
                "span": {
                    "byte_end": 38,
                    "byte_start": 16,
                    "column_end": 27,
                    "column_start": 5,
                    "expansion": null,
                    "file_name": "src/main.rs",
                    "is_primary": false,
                    "label": null,
                    "line_end": 2,
                    "line_start": 2,
                    "suggested_replacement": null,
                    "suggestion_applicability": null,
                    "text": [
                        {
                            "highlight_end": 27,
                            "highlight_start": 5,
                            "text": "    assert_eq!(1, \"love\");"
                        }
                    ]
                }
            },
            "file_name": "<::core::macros::assert_eq macros>",
            "is_primary": true,
            "label": "no implementation for `{integer} == &str`",
            "line_end": 7,
            "line_start": 7,
            "suggested_replacement": null,
            "suggestion_applicability": null,
            "text": [
                {
                    "highlight_end": 33,
                    "highlight_start": 31,
                    "text": "             if ! (* left_val == * right_val)"
                }
            ]
        }
    ]
    }"##,
            expect_file!["./test_data/handles_macro_location.txt"],
        );
    }

    #[test]
    fn macro_compiler_error() {
        check(
            r##"{
        "rendered": "error: Please register your known path in the path module\n   --> crates/hir_def/src/path.rs:265:9\n    |\n265 |         compile_error!(\"Please register your known path in the path module\")\n    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n    | \n   ::: crates/hir_def/src/data.rs:80:16\n    |\n80  |     let path = path![std::future::Future];\n    |                -------------------------- in this macro invocation\n\n",
        "children": [],
        "code": null,
        "level": "error",
        "message": "Please register your known path in the path module",
        "spans": [
            {
                "byte_end": 8285,
                "byte_start": 8217,
                "column_end": 77,
                "column_start": 9,
                "expansion": {
                    "def_site_span": {
                        "byte_end": 8294,
                        "byte_start": 7858,
                        "column_end": 2,
                        "column_start": 1,
                        "expansion": null,
                        "file_name": "crates/hir_def/src/path.rs",
                        "is_primary": false,
                        "label": null,
                        "line_end": 267,
                        "line_start": 254,
                        "suggested_replacement": null,
                        "suggestion_applicability": null,
                        "text": [
                            {
                                "highlight_end": 28,
                                "highlight_start": 1,
                                "text": "macro_rules! __known_path {"
                            },
                            {
                                "highlight_end": 37,
                                "highlight_start": 1,
                                "text": "    (std::iter::IntoIterator) => {};"
                            },
                            {
                                "highlight_end": 33,
                                "highlight_start": 1,
                                "text": "    (std::result::Result) => {};"
                            },
                            {
                                "highlight_end": 29,
                                "highlight_start": 1,
                                "text": "    (std::ops::Range) => {};"
                            },
                            {
                                "highlight_end": 33,
                                "highlight_start": 1,
                                "text": "    (std::ops::RangeFrom) => {};"
                            },
                            {
                                "highlight_end": 33,
                                "highlight_start": 1,
                                "text": "    (std::ops::RangeFull) => {};"
                            },
                            {
                                "highlight_end": 31,
                                "highlight_start": 1,
                                "text": "    (std::ops::RangeTo) => {};"
                            },
                            {
                                "highlight_end": 40,
                                "highlight_start": 1,
                                "text": "    (std::ops::RangeToInclusive) => {};"
                            },
                            {
                                "highlight_end": 38,
                                "highlight_start": 1,
                                "text": "    (std::ops::RangeInclusive) => {};"
                            },
                            {
                                "highlight_end": 27,
                                "highlight_start": 1,
                                "text": "    (std::ops::Try) => {};"
                            },
                            {
                                "highlight_end": 22,
                                "highlight_start": 1,
                                "text": "    ($path:path) => {"
                            },
                            {
                                "highlight_end": 77,
                                "highlight_start": 1,
                                "text": "        compile_error!(\"Please register your known path in the path module\")"
                            },
                            {
                                "highlight_end": 7,
                                "highlight_start": 1,
                                "text": "    };"
                            },
                            {
                                "highlight_end": 2,
                                "highlight_start": 1,
                                "text": "}"
                            }
                        ]
                    },
                    "macro_decl_name": "$crate::__known_path!",
                    "span": {
                        "byte_end": 8427,
                        "byte_start": 8385,
                        "column_end": 51,
                        "column_start": 9,
                        "expansion": {
                            "def_site_span": {
                                "byte_end": 8611,
                                "byte_start": 8312,
                                "column_end": 2,
                                "column_start": 1,
                                "expansion": null,
                                "file_name": "crates/hir_def/src/path.rs",
                                "is_primary": false,
                                "label": null,
                                "line_end": 277,
                                "line_start": 270,
                                "suggested_replacement": null,
                                "suggestion_applicability": null,
                                "text": [
                                    {
                                        "highlight_end": 22,
                                        "highlight_start": 1,
                                        "text": "macro_rules! __path {"
                                    },
                                    {
                                        "highlight_end": 43,
                                        "highlight_start": 1,
                                        "text": "    ($start:ident $(:: $seg:ident)*) => ({"
                                    },
                                    {
                                        "highlight_end": 51,
                                        "highlight_start": 1,
                                        "text": "        $crate::__known_path!($start $(:: $seg)*);"
                                    },
                                    {
                                        "highlight_end": 87,
                                        "highlight_start": 1,
                                        "text": "        $crate::path::ModPath::from_simple_segments($crate::path::PathKind::Abs, vec!["
                                    },
                                    {
                                        "highlight_end": 76,
                                        "highlight_start": 1,
                                        "text": "            $crate::path::__name![$start], $($crate::path::__name![$seg],)*"
                                    },
                                    {
                                        "highlight_end": 11,
                                        "highlight_start": 1,
                                        "text": "        ])"
                                    },
                                    {
                                        "highlight_end": 8,
                                        "highlight_start": 1,
                                        "text": "    });"
                                    },
                                    {
                                        "highlight_end": 2,
                                        "highlight_start": 1,
                                        "text": "}"
                                    }
                                ]
                            },
                            "macro_decl_name": "path!",
                            "span": {
                                "byte_end": 2966,
                                "byte_start": 2940,
                                "column_end": 42,
                                "column_start": 16,
                                "expansion": null,
                                "file_name": "crates/hir_def/src/data.rs",
                                "is_primary": false,
                                "label": null,
                                "line_end": 80,
                                "line_start": 80,
                                "suggested_replacement": null,
                                "suggestion_applicability": null,
                                "text": [
                                    {
                                        "highlight_end": 42,
                                        "highlight_start": 16,
                                        "text": "    let path = path![std::future::Future];"
                                    }
                                ]
                            }
                        },
                        "file_name": "crates/hir_def/src/path.rs",
                        "is_primary": false,
                        "label": null,
                        "line_end": 272,
                        "line_start": 272,
                        "suggested_replacement": null,
                        "suggestion_applicability": null,
                        "text": [
                            {
                                "highlight_end": 51,
                                "highlight_start": 9,
                                "text": "        $crate::__known_path!($start $(:: $seg)*);"
                            }
                        ]
                    }
                },
                "file_name": "crates/hir_def/src/path.rs",
                "is_primary": true,
                "label": null,
                "line_end": 265,
                "line_start": 265,
                "suggested_replacement": null,
                "suggestion_applicability": null,
                "text": [
                    {
                        "highlight_end": 77,
                        "highlight_start": 9,
                        "text": "        compile_error!(\"Please register your known path in the path module\")"
                    }
                ]
            }
        ]
    }
            "##,
            expect_file!["./test_data/macro_compiler_error.txt"],
        );
    }

    #[test]
    fn snap_multi_line_fix() {
        check(
            r##"{
                "rendered": "warning: returning the result of a let binding from a block\n --> src/main.rs:4:5\n  |\n3 |     let a = (0..10).collect();\n  |     -------------------------- unnecessary let binding\n4 |     a\n  |     ^\n  |\n  = note: `#[warn(clippy::let_and_return)]` on by default\n  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_and_return\nhelp: return the expression directly\n  |\n3 |     \n4 |     (0..10).collect()\n  |\n\n",
                "children": [
                    {
                    "children": [],
                    "code": null,
                    "level": "note",
                    "message": "`#[warn(clippy::let_and_return)]` on by default",
                    "rendered": null,
                    "spans": []
                    },
                    {
                    "children": [],
                    "code": null,
                    "level": "help",
                    "message": "for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_and_return",
                    "rendered": null,
                    "spans": []
                    },
                    {
                    "children": [],
                    "code": null,
                    "level": "help",
                    "message": "return the expression directly",
                    "rendered": null,
                    "spans": [
                        {
                        "byte_end": 55,
                        "byte_start": 29,
                        "column_end": 31,
                        "column_start": 5,
                        "expansion": null,
                        "file_name": "src/main.rs",
                        "is_primary": true,
                        "label": null,
                        "line_end": 3,
                        "line_start": 3,
                        "suggested_replacement": "",
                        "suggestion_applicability": "MachineApplicable",
                        "text": [
                            {
                            "highlight_end": 31,
                            "highlight_start": 5,
                            "text": "    let a = (0..10).collect();"
                            }
                        ]
                        },
                        {
                        "byte_end": 61,
                        "byte_start": 60,
                        "column_end": 6,
                        "column_start": 5,
                        "expansion": null,
                        "file_name": "src/main.rs",
                        "is_primary": true,
                        "label": null,
                        "line_end": 4,
                        "line_start": 4,
                        "suggested_replacement": "(0..10).collect()",
                        "suggestion_applicability": "MachineApplicable",
                        "text": [
                            {
                            "highlight_end": 6,
                            "highlight_start": 5,
                            "text": "    a"
                            }
                        ]
                        }
                    ]
                    }
                ],
                "code": {
                    "code": "clippy::let_and_return",
                    "explanation": null
                },
                "level": "warning",
                "message": "returning the result of a let binding from a block",
                "spans": [
                    {
                    "byte_end": 55,
                    "byte_start": 29,
                    "column_end": 31,
                    "column_start": 5,
                    "expansion": null,
                    "file_name": "src/main.rs",
                    "is_primary": false,
                    "label": "unnecessary let binding",
                    "line_end": 3,
                    "line_start": 3,
                    "suggested_replacement": null,
                    "suggestion_applicability": null,
                    "text": [
                        {
                        "highlight_end": 31,
                        "highlight_start": 5,
                        "text": "    let a = (0..10).collect();"
                        }
                    ]
                    },
                    {
                    "byte_end": 61,
                    "byte_start": 60,
                    "column_end": 6,
                    "column_start": 5,
                    "expansion": null,
                    "file_name": "src/main.rs",
                    "is_primary": true,
                    "label": null,
                    "line_end": 4,
                    "line_start": 4,
                    "suggested_replacement": null,
                    "suggestion_applicability": null,
                    "text": [
                        {
                        "highlight_end": 6,
                        "highlight_start": 5,
                        "text": "    a"
                        }
                    ]
                    }
                ]
            }
            "##,
            expect_file!["./test_data/snap_multi_line_fix.txt"],
        );
    }

    #[test]
    fn reasonable_line_numbers_from_empty_file() {
        check(
            r##"{
                "message": "`main` function not found in crate `current`",
                "code": {
                    "code": "E0601",
                    "explanation": "No `main` function was found in a binary crate.\n\nTo fix this error, add a `main` function:\n\n```\nfn main() {\n    // Your program will start here.\n    println!(\"Hello world!\");\n}\n```\n\nIf you don't know the basics of Rust, you can look at the\n[Rust Book][rust-book] to get started.\n\n[rust-book]: https://doc.rust-lang.org/book/\n"
                },
                "level": "error",
                "spans": [
                    {
                        "file_name": "src/bin/current.rs",
                        "byte_start": 0,
                        "byte_end": 0,
                        "line_start": 0,
                        "line_end": 0,
                        "column_start": 1,
                        "column_end": 1,
                        "is_primary": true,
                        "text": [],
                        "label": null,
                        "suggested_replacement": null,
                        "suggestion_applicability": null,
                        "expansion": null
                    }
                ],
                "children": [
                    {
                        "message": "consider adding a `main` function to `src/bin/current.rs`",
                        "code": null,
                        "level": "note",
                        "spans": [],
                        "children": [],
                        "rendered": null
                    }
                ],
                "rendered": "error[E0601]: `main` function not found in crate `current`\n  |\n  = note: consider adding a `main` function to `src/bin/current.rs`\n\n"
            }"##,
            expect_file!["./test_data/reasonable_line_numbers_from_empty_file.txt"],
        );
    }
}