@@ -7,8 +7,6 @@ use std::slice;
7
7
use crate :: meta:: { self , ParseNestedMeta } ;
8
8
#[ cfg( feature = "parsing" ) ]
9
9
use crate :: parse:: { Parse , ParseStream , Parser , Result } ;
10
- #[ cfg( feature = "parsing" ) ]
11
- use std:: fmt:: Write ;
12
10
13
11
ast_struct ! {
14
12
/// An attribute, like `#[repr(transparent)]`.
@@ -238,20 +236,23 @@ impl Attribute {
238
236
#[ cfg_attr( doc_cfg, doc( cfg( feature = "parsing" ) ) ) ]
239
237
pub fn parse_args_with < F : Parser > ( & self , parser : F ) -> Result < F :: Output > {
240
238
match & self . meta {
241
- Meta :: Path ( path) => {
242
- let expected = parsing:: expected_parentheses ( & self . style , path) ;
243
- let msg = format ! ( "expected attribute arguments in parentheses: {}" , expected) ;
244
- Err ( crate :: error:: new2 (
245
- path. segments . first ( ) . unwrap ( ) . ident . span ( ) ,
246
- path. segments . last ( ) . unwrap ( ) . ident . span ( ) ,
247
- msg,
248
- ) )
249
- }
250
- Meta :: NameValue ( meta) => {
251
- let expected = parsing:: expected_parentheses ( & self . style , & meta. path ) ;
252
- let msg = format ! ( "expected parentheses: {}" , expected) ;
253
- Err ( Error :: new ( meta. eq_token . span , msg) )
254
- }
239
+ Meta :: Path ( path) => Err ( crate :: error:: new2 (
240
+ path. segments . first ( ) . unwrap ( ) . ident . span ( ) ,
241
+ path. segments . last ( ) . unwrap ( ) . ident . span ( ) ,
242
+ format ! (
243
+ "expected attribute arguments in parentheses: {}[{}(...)]" ,
244
+ parsing:: DisplayAttrStyle ( & self . style) ,
245
+ parsing:: DisplayPath ( path) ,
246
+ ) ,
247
+ ) ) ,
248
+ Meta :: NameValue ( meta) => Err ( Error :: new (
249
+ meta. eq_token . span ,
250
+ format_args ! (
251
+ "expected parentheses: {}[{}(...)]" ,
252
+ parsing:: DisplayAttrStyle ( & self . style) ,
253
+ parsing:: DisplayPath ( & meta. path) ,
254
+ ) ,
255
+ ) ) ,
255
256
Meta :: List ( meta) => meta. parse_args_with ( parser) ,
256
257
}
257
258
}
@@ -569,6 +570,7 @@ pub(crate) mod parsing {
569
570
use super :: * ;
570
571
use crate :: parse:: discouraged:: Speculative ;
571
572
use crate :: parse:: { Parse , ParseStream , Result } ;
573
+ use std:: fmt:: { self , Display } ;
572
574
573
575
pub ( crate ) fn parse_inner ( input : ParseStream , attrs : & mut Vec < Attribute > ) -> Result < ( ) > {
574
576
while input. peek ( Token ! [ #] ) && input. peek2 ( Token ! [ !] ) {
@@ -662,23 +664,29 @@ pub(crate) mod parsing {
662
664
} )
663
665
}
664
666
665
- pub ( super ) fn expected_parentheses ( style : & AttrStyle , path : & Path ) -> String {
666
- let mut suggestion = String :: new ( ) ;
667
- match style {
668
- AttrStyle :: Outer => suggestion. push ( '#' ) ,
669
- AttrStyle :: Inner ( _) => suggestion. push_str ( "#!" ) ,
667
+ pub ( super ) struct DisplayAttrStyle < ' a > ( pub & ' a AttrStyle ) ;
668
+
669
+ impl < ' a > Display for DisplayAttrStyle < ' a > {
670
+ fn fmt ( & self , formatter : & mut fmt:: Formatter ) -> fmt:: Result {
671
+ formatter. write_str ( match self . 0 {
672
+ AttrStyle :: Outer => "#" ,
673
+ AttrStyle :: Inner ( _) => "#!" ,
674
+ } )
670
675
}
671
- suggestion . push ( '[' ) ;
676
+ }
672
677
673
- for ( i, segment) in path. segments . iter ( ) . enumerate ( ) {
674
- if i > 0 || path. leading_colon . is_some ( ) {
675
- suggestion. push_str ( "::" ) ;
678
+ pub ( super ) struct DisplayPath < ' a > ( pub & ' a Path ) ;
679
+
680
+ impl < ' a > Display for DisplayPath < ' a > {
681
+ fn fmt ( & self , formatter : & mut fmt:: Formatter ) -> fmt:: Result {
682
+ for ( i, segment) in self . 0 . segments . iter ( ) . enumerate ( ) {
683
+ if i > 0 || self . 0 . leading_colon . is_some ( ) {
684
+ formatter. write_str ( "::" ) ?;
685
+ }
686
+ write ! ( formatter, "{}" , segment. ident) ?;
676
687
}
677
- write ! ( suggestion , "{}" , segment . ident ) . unwrap ( ) ;
688
+ Ok ( ( ) )
678
689
}
679
-
680
- suggestion. push_str ( "(...)]" ) ;
681
- suggestion
682
690
}
683
691
}
684
692
0 commit comments