@@ -8,20 +8,33 @@ use rustc_span::symbol::sym;
88use rustc_span:: Span ;
99use rustc_target:: abi:: { HasDataLayout , TargetDataLayout } ;
1010
11- use crate :: errors:: { Abi , Align , HomogeneousAggregate , LayoutOf , Size , UnrecognizedField } ;
11+ use crate :: errors:: {
12+ LayoutAbi , LayoutAlign , LayoutHomogeneousAggregate , LayoutInvalidAttribute , LayoutOf ,
13+ LayoutSize , UnrecognizedField ,
14+ } ;
1215
1316pub fn test_layout ( tcx : TyCtxt < ' _ > ) {
1417 if !tcx. features ( ) . rustc_attrs {
1518 // if the `rustc_attrs` feature is not enabled, don't bother testing layout
1619 return ;
1720 }
1821 for id in tcx. hir ( ) . items ( ) {
19- if matches ! (
20- tcx. def_kind( id. owner_id) ,
21- DefKind :: TyAlias { .. } | DefKind :: Enum | DefKind :: Struct | DefKind :: Union
22- ) {
23- for attr in tcx. get_attrs ( id. owner_id , sym:: rustc_layout) {
24- dump_layout_of ( tcx, id. owner_id . def_id , attr) ;
22+ for attr in tcx. get_attrs ( id. owner_id , sym:: rustc_layout) {
23+ match tcx. def_kind ( id. owner_id ) {
24+ DefKind :: TyAlias { .. } | DefKind :: Enum | DefKind :: Struct | DefKind :: Union => {
25+ dump_layout_of ( tcx, id. owner_id . def_id , attr) ;
26+ }
27+ _ => {
28+ tcx. sess . emit_err ( LayoutInvalidAttribute { span : tcx. def_span ( id. owner_id ) } ) ;
29+ }
30+ }
31+ }
32+ if matches ! ( tcx. def_kind( id. owner_id) , DefKind :: Impl { .. } ) {
33+ // To find associated functions we need to go into the child items here.
34+ for & id in tcx. associated_item_def_ids ( id. owner_id ) {
35+ for _attr in tcx. get_attrs ( id, sym:: rustc_layout) {
36+ tcx. sess . emit_err ( LayoutInvalidAttribute { span : tcx. def_span ( id) } ) ;
37+ }
2538 }
2639 }
2740 }
@@ -38,28 +51,28 @@ fn dump_layout_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribute) {
3851 for meta_item in meta_items {
3952 match meta_item. name_or_empty ( ) {
4053 sym:: abi => {
41- tcx. sess . emit_err ( Abi {
54+ tcx. sess . emit_err ( LayoutAbi {
4255 span : tcx. def_span ( item_def_id. to_def_id ( ) ) ,
4356 abi : format ! ( "{:?}" , ty_layout. abi) ,
4457 } ) ;
4558 }
4659
4760 sym:: align => {
48- tcx. sess . emit_err ( Align {
61+ tcx. sess . emit_err ( LayoutAlign {
4962 span : tcx. def_span ( item_def_id. to_def_id ( ) ) ,
5063 align : format ! ( "{:?}" , ty_layout. align) ,
5164 } ) ;
5265 }
5366
5467 sym:: size => {
55- tcx. sess . emit_err ( Size {
68+ tcx. sess . emit_err ( LayoutSize {
5669 span : tcx. def_span ( item_def_id. to_def_id ( ) ) ,
5770 size : format ! ( "{:?}" , ty_layout. size) ,
5871 } ) ;
5972 }
6073
6174 sym:: homogeneous_aggregate => {
62- tcx. sess . emit_err ( HomogeneousAggregate {
75+ tcx. sess . emit_err ( LayoutHomogeneousAggregate {
6376 span : tcx. def_span ( item_def_id. to_def_id ( ) ) ,
6477 homogeneous_aggregate : format ! (
6578 "{:?}" ,
0 commit comments