@@ -338,7 +338,8 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
338338 match ty_res {
339339 Res :: Def ( DefKind :: Enum , did) => match tcx. type_of ( did) . kind ( ) {
340340 ty:: Adt ( def, _) if def. is_enum ( ) => {
341- if let Some ( field) = def. all_fields ( ) . find ( |f| f. name == variant_field_name) {
341+ if let Some ( variant) = def. variants ( ) . iter ( ) . find ( |v| v. name == variant_name)
342+ && let Some ( field) = variant. fields . iter ( ) . find ( |f| f. name == variant_field_name) {
342343 Ok ( ( ty_res, field. did ) )
343344 } else {
344345 Err ( UnresolvedPath {
@@ -1768,15 +1769,35 @@ fn resolution_failure(
17681769
17691770 // Otherwise, it must be an associated item or variant
17701771 let res = partial_res. expect ( "None case was handled by `last_found_module`" ) ;
1771- let kind = match res {
1772- Res :: Def ( kind, _ ) => Some ( kind) ,
1772+ let kind_did = match res {
1773+ Res :: Def ( kind, did ) => Some ( ( kind, did ) ) ,
17731774 Res :: Primitive ( _) => None ,
17741775 } ;
1775- let path_description = if let Some ( kind) = kind {
1776+ let is_struct_variant = |did| {
1777+ if let ty:: Adt ( def, _) = tcx. type_of ( did) . kind ( )
1778+ && def. is_enum ( )
1779+ && let Some ( variant) = def. variants ( ) . iter ( ) . find ( |v| v. name == res. name ( tcx) ) {
1780+ // ctor is `None` if variant is a struct
1781+ variant. ctor . is_none ( )
1782+ } else {
1783+ false
1784+ }
1785+ } ;
1786+ let path_description = if let Some ( ( kind, did) ) = kind_did {
17761787 match kind {
17771788 Mod | ForeignMod => "inner item" ,
17781789 Struct => "field or associated item" ,
17791790 Enum | Union => "variant or associated item" ,
1791+ Variant if is_struct_variant ( did) => {
1792+ let variant = res. name ( tcx) ;
1793+ let note = format ! ( "variant `{variant}` has no such field" ) ;
1794+ if let Some ( span) = sp {
1795+ diag. span_label ( span, & note) ;
1796+ } else {
1797+ diag. note ( & note) ;
1798+ }
1799+ return ;
1800+ }
17801801 Variant
17811802 | Field
17821803 | Closure
0 commit comments