88// option. This file may not be copied, modified, or distributed
99// except according to those terms.
1010
11+ //! This module implements some validity checks for attributes.
12+ //! In particular it verifies that `#[inline]` and `#[repr]` attributes are
13+ //! attached to items that actually support them and if there are
14+ //! conflicts between multiple such attributes attached to the same
15+ //! item.
16+
1117use session:: Session ;
1218
1319use syntax:: ast;
@@ -40,6 +46,18 @@ struct CheckAttrVisitor<'a> {
4046}
4147
4248impl < ' a > CheckAttrVisitor < ' a > {
49+ /// Check any attribute.
50+ fn check_attribute ( & self , attr : & ast:: Attribute , target : Target ) {
51+ if let Some ( name) = attr. name ( ) {
52+ match & * name. as_str ( ) {
53+ "inline" => self . check_inline ( attr, target) ,
54+ "repr" => self . check_repr ( attr, target) ,
55+ _ => ( ) ,
56+ }
57+ }
58+ }
59+
60+ /// Check if an `#[inline]` is applied to a function.
4361 fn check_inline ( & self , attr : & ast:: Attribute , target : Target ) {
4462 if target != Target :: Fn {
4563 struct_span_err ! ( self . sess, attr. span, E0518 , "attribute should be applied to function" )
@@ -48,6 +66,7 @@ impl<'a> CheckAttrVisitor<'a> {
4866 }
4967 }
5068
69+ /// Check if an `#[repr]` attr is valid.
5170 fn check_repr ( & self , attr : & ast:: Attribute , target : Target ) {
5271 let words = match attr. meta_item_list ( ) {
5372 Some ( words) => words,
@@ -135,16 +154,6 @@ impl<'a> CheckAttrVisitor<'a> {
135154 "conflicting packed and align representation hints" ) . emit ( ) ;
136155 }
137156 }
138-
139- fn check_attribute ( & self , attr : & ast:: Attribute , target : Target ) {
140- if let Some ( name) = attr. name ( ) {
141- match & * name. as_str ( ) {
142- "inline" => self . check_inline ( attr, target) ,
143- "repr" => self . check_repr ( attr, target) ,
144- _ => ( ) ,
145- }
146- }
147- }
148157}
149158
150159impl < ' a > Visitor < ' a > for CheckAttrVisitor < ' a > {
0 commit comments