@@ -5,7 +5,7 @@ use rustc_ast::{
5
5
self as ast, CRATE_NODE_ID , Crate , ItemKind , MetaItemInner , MetaItemKind , ModKind , NodeId , Path ,
6
6
} ;
7
7
use rustc_ast_pretty:: pprust;
8
- use rustc_data_structures:: fx:: FxHashSet ;
8
+ use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
9
9
use rustc_errors:: codes:: * ;
10
10
use rustc_errors:: {
11
11
Applicability , Diag , DiagCtxtHandle , ErrorGuaranteed , MultiSpan , SuggestionStyle ,
@@ -1416,6 +1416,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
1416
1416
parent_scope : & ParentScope < ' ra > ,
1417
1417
ident : Ident ,
1418
1418
krate : & Crate ,
1419
+ sugg_span : Option < Span > ,
1419
1420
) {
1420
1421
let is_expected = & |res : Res | res. macro_kind ( ) == Some ( macro_kind) ;
1421
1422
let suggestion = self . early_lookup_typo_candidate (
@@ -1424,7 +1425,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
1424
1425
ident,
1425
1426
is_expected,
1426
1427
) ;
1427
- self . add_typo_suggestion ( err, suggestion, ident. span ) ;
1428
+ if !self . add_typo_suggestion ( err, suggestion, ident. span ) {
1429
+ self . detect_derive_attribute ( err, ident, parent_scope, sugg_span) ;
1430
+ }
1428
1431
1429
1432
let import_suggestions =
1430
1433
self . lookup_import_candidates ( ident, Namespace :: MacroNS , parent_scope, is_expected) ;
@@ -1557,6 +1560,106 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
1557
1560
}
1558
1561
}
1559
1562
1563
+ /// Given an attribute macro that failed to be resolved, look for `derive` macros that could
1564
+ /// provide it, either as-is or with small typos.
1565
+ fn detect_derive_attribute (
1566
+ & self ,
1567
+ err : & mut Diag < ' _ > ,
1568
+ ident : Ident ,
1569
+ parent_scope : & ParentScope < ' ra > ,
1570
+ sugg_span : Option < Span > ,
1571
+ ) {
1572
+ // Find all of the `derive`s in scope and collect their corresponding declared
1573
+ // attributes.
1574
+ // FIXME: this only works if the crate that owns the macro that has the helper_attr
1575
+ // has already been imported.
1576
+ let mut derives = vec ! [ ] ;
1577
+ let mut all_attrs: FxHashMap < Symbol , Vec < _ > > = FxHashMap :: default ( ) ;
1578
+ for ( def_id, data) in & self . macro_map {
1579
+ for helper_attr in & data. ext . helper_attrs {
1580
+ let item_name = self . tcx . item_name ( * def_id) ;
1581
+ all_attrs. entry ( * helper_attr) . or_default ( ) . push ( item_name) ;
1582
+ if helper_attr == & ident. name {
1583
+ derives. push ( item_name) ;
1584
+ }
1585
+ }
1586
+ }
1587
+ let kind = MacroKind :: Derive . descr ( ) ;
1588
+ if !derives. is_empty ( ) {
1589
+ // We found an exact match for the missing attribute in a `derive` macro. Suggest it.
1590
+ derives. sort ( ) ;
1591
+ derives. dedup ( ) ;
1592
+ let msg = match & derives[ ..] {
1593
+ [ derive] => format ! ( " `{derive}`" ) ,
1594
+ [ start @ .., last] => format ! (
1595
+ "s {} and `{last}`" ,
1596
+ start. iter( ) . map( |d| format!( "`{d}`" ) ) . collect:: <Vec <_>>( ) . join( ", " )
1597
+ ) ,
1598
+ [ ] => unreachable ! ( "we checked for this to be non-empty 10 lines above!?" ) ,
1599
+ } ;
1600
+ let msg = format ! (
1601
+ "`{}` is an attribute that can be used by the {kind}{msg}, you might be \
1602
+ missing a `derive` attribute",
1603
+ ident. name,
1604
+ ) ;
1605
+ let sugg_span = if let ModuleKind :: Def ( DefKind :: Enum , id, _) = parent_scope. module . kind
1606
+ {
1607
+ let span = self . def_span ( id) ;
1608
+ if span. from_expansion ( ) {
1609
+ None
1610
+ } else {
1611
+ // For enum variants sugg_span is empty but we can get the enum's Span.
1612
+ Some ( span. shrink_to_lo ( ) )
1613
+ }
1614
+ } else {
1615
+ // For items this `Span` will be populated, everything else it'll be None.
1616
+ sugg_span
1617
+ } ;
1618
+ match sugg_span {
1619
+ Some ( span) => {
1620
+ err. span_suggestion_verbose (
1621
+ span,
1622
+ msg,
1623
+ format ! (
1624
+ "#[derive({})]\n " ,
1625
+ derives
1626
+ . iter( )
1627
+ . map( |d| d. to_string( ) )
1628
+ . collect:: <Vec <String >>( )
1629
+ . join( ", " )
1630
+ ) ,
1631
+ Applicability :: MaybeIncorrect ,
1632
+ ) ;
1633
+ }
1634
+ None => {
1635
+ err. note ( msg) ;
1636
+ }
1637
+ }
1638
+ } else {
1639
+ // We didn't find an exact match. Look for close matches. If any, suggest fixing typo.
1640
+ let all_attr_names: Vec < Symbol > = all_attrs. keys ( ) . cloned ( ) . collect ( ) ;
1641
+ if let Some ( best_match) = find_best_match_for_name ( & all_attr_names, ident. name , None )
1642
+ && let Some ( macros) = all_attrs. get ( & best_match)
1643
+ {
1644
+ let msg = match & macros[ ..] {
1645
+ [ ] => return ,
1646
+ [ name] => format ! ( " `{name}` accepts" ) ,
1647
+ [ start @ .., end] => format ! (
1648
+ "s {} and `{end}` accept" ,
1649
+ start. iter( ) . map( |m| format!( "`{m}`" ) ) . collect:: <Vec <_>>( ) . join( ", " ) ,
1650
+ ) ,
1651
+ } ;
1652
+ let msg = format ! ( "the {kind}{msg} the similarly named `{best_match}` attribute" ) ;
1653
+ err. span_suggestion_verbose (
1654
+ ident. span ,
1655
+ msg,
1656
+ best_match,
1657
+ Applicability :: MaybeIncorrect ,
1658
+ ) ;
1659
+ }
1660
+ }
1661
+ }
1662
+
1560
1663
pub ( crate ) fn add_typo_suggestion (
1561
1664
& self ,
1562
1665
err : & mut Diag < ' _ > ,
0 commit comments