@@ -62,7 +62,7 @@ impl Attribute {
6262 pub fn get_normal_item ( & self ) -> & AttrItem {
6363 match & self . kind {
6464 AttrKind :: Normal ( normal) => & normal. item ,
65- AttrKind :: DocComment ( ..) => panic ! ( "unexpected doc comment" ) ,
65+ AttrKind :: DocComment ( ..) | AttrKind :: Comment ( .. ) => panic ! ( "unexpected ( doc) comment" ) ,
6666 }
6767 }
6868
@@ -71,14 +71,14 @@ impl Attribute {
7171 pub fn replace_args ( & mut self , new_args : AttrItemKind ) {
7272 match & mut self . kind {
7373 AttrKind :: Normal ( normal) => normal. item . args = new_args,
74- AttrKind :: DocComment ( ..) => panic ! ( "unexpected doc comment" ) ,
74+ AttrKind :: DocComment ( ..) | AttrKind :: Comment ( .. ) => panic ! ( "unexpected ( doc) comment" ) ,
7575 }
7676 }
7777
7878 pub fn unwrap_normal_item ( self ) -> AttrItem {
7979 match self . kind {
8080 AttrKind :: Normal ( normal) => normal. item ,
81- AttrKind :: DocComment ( ..) => panic ! ( "unexpected doc comment" ) ,
81+ AttrKind :: DocComment ( ..) | AttrKind :: Comment ( .. ) => panic ! ( "unexpected ( doc) comment" ) ,
8282 }
8383 }
8484}
@@ -94,7 +94,7 @@ impl AttributeExt for Attribute {
9494 AttrArgs :: Eq { expr, .. } => Some ( expr. span ) ,
9595 _ => None ,
9696 } ,
97- AttrKind :: DocComment ( ..) => None ,
97+ AttrKind :: DocComment ( ..) | AttrKind :: Comment ( .. ) => None ,
9898 }
9999 }
100100
@@ -103,7 +103,7 @@ impl AttributeExt for Attribute {
103103 /// a doc comment) will return `false`.
104104 fn is_doc_comment ( & self ) -> Option < Span > {
105105 match self . kind {
106- AttrKind :: Normal ( ..) => None ,
106+ AttrKind :: Normal ( ..) | AttrKind :: Comment ( .. ) => None ,
107107 AttrKind :: DocComment ( ..) => Some ( self . span ) ,
108108 }
109109 }
@@ -118,7 +118,7 @@ impl AttributeExt for Attribute {
118118 None
119119 }
120120 }
121- AttrKind :: DocComment ( ..) => None ,
121+ AttrKind :: DocComment ( ..) | AttrKind :: Comment ( .. ) => None ,
122122 }
123123 }
124124
@@ -127,14 +127,14 @@ impl AttributeExt for Attribute {
127127 AttrKind :: Normal ( p) => {
128128 Some ( p. item . path . segments . iter ( ) . map ( |i| i. ident . name ) . collect ( ) )
129129 }
130- AttrKind :: DocComment ( _, _) => None ,
130+ AttrKind :: DocComment ( _, _) | AttrKind :: Comment ( _ , _ ) => None ,
131131 }
132132 }
133133
134134 fn path_span ( & self ) -> Option < Span > {
135135 match & self . kind {
136136 AttrKind :: Normal ( attr) => Some ( attr. item . path . span ) ,
137- AttrKind :: DocComment ( _, _) => None ,
137+ AttrKind :: DocComment ( _, _) | AttrKind :: Comment ( _ , _ ) => None ,
138138 }
139139 }
140140
@@ -150,7 +150,7 @@ impl AttributeExt for Attribute {
150150 . zip ( name)
151151 . all ( |( s, n) | s. args . is_none ( ) && s. ident . name == * n)
152152 }
153- AttrKind :: DocComment ( ..) => false ,
153+ AttrKind :: DocComment ( ..) | AttrKind :: Comment ( .. ) => false ,
154154 }
155155 }
156156
@@ -176,7 +176,7 @@ impl AttributeExt for Attribute {
176176 fn meta_item_list ( & self ) -> Option < ThinVec < MetaItemInner > > {
177177 match & self . kind {
178178 AttrKind :: Normal ( normal) => normal. item . meta_item_list ( ) ,
179- AttrKind :: DocComment ( ..) => None ,
179+ AttrKind :: DocComment ( ..) | AttrKind :: Comment ( .. ) => None ,
180180 }
181181 }
182182
@@ -198,7 +198,7 @@ impl AttributeExt for Attribute {
198198 fn value_str ( & self ) -> Option < Symbol > {
199199 match & self . kind {
200200 AttrKind :: Normal ( normal) => normal. item . value_str ( ) ,
201- AttrKind :: DocComment ( ..) => None ,
201+ AttrKind :: DocComment ( ..) | AttrKind :: Comment ( .. ) => None ,
202202 }
203203 }
204204
@@ -266,6 +266,7 @@ impl AttributeExt for Attribute {
266266 fn doc_resolution_scope ( & self ) -> Option < AttrStyle > {
267267 match & self . kind {
268268 AttrKind :: DocComment ( ..) => Some ( self . style ) ,
269+ AttrKind :: Comment ( ..) => None ,
269270 AttrKind :: Normal ( normal)
270271 if normal. item . path == sym:: doc && normal. item . value_str ( ) . is_some ( ) =>
271272 {
@@ -307,6 +308,11 @@ impl Attribute {
307308 self . style
308309 }
309310
311+ /// Returns `true` if this is a regular (non-doc) comment (`//` or `/* */`).
312+ pub fn is_comment ( & self ) -> bool {
313+ matches ! ( self . kind, AttrKind :: Comment ( ..) )
314+ }
315+
310316 pub fn may_have_doc_links ( & self ) -> bool {
311317 self . doc_str ( ) . is_some_and ( |s| comments:: may_have_doc_links ( s. as_str ( ) ) )
312318 || self . deprecation_note ( ) . is_some_and ( |s| comments:: may_have_doc_links ( s. as_str ( ) ) )
@@ -316,14 +322,14 @@ impl Attribute {
316322 pub fn meta ( & self ) -> Option < MetaItem > {
317323 match & self . kind {
318324 AttrKind :: Normal ( normal) => normal. item . meta ( self . span ) ,
319- AttrKind :: DocComment ( ..) => None ,
325+ AttrKind :: DocComment ( ..) | AttrKind :: Comment ( .. ) => None ,
320326 }
321327 }
322328
323329 pub fn meta_kind ( & self ) -> Option < MetaItemKind > {
324330 match & self . kind {
325331 AttrKind :: Normal ( normal) => normal. item . meta_kind ( ) ,
326- AttrKind :: DocComment ( ..) => None ,
332+ AttrKind :: DocComment ( ..) | AttrKind :: Comment ( .. ) => None ,
327333 }
328334 }
329335
@@ -339,6 +345,9 @@ impl Attribute {
339345 token:: DocComment ( comment_kind, self . style, data) ,
340346 self . span,
341347 ) ] ,
348+ // Regular comments are never part of any real token stream; returning
349+ // an empty vec prevents them from being injected into macro inputs.
350+ AttrKind :: Comment ( ..) => vec ! [ ] ,
342351 }
343352 }
344353}
@@ -737,6 +746,20 @@ pub fn mk_doc_comment(
737746 Attribute { kind : AttrKind :: DocComment ( comment_kind, data) , id : g. mk_attr_id ( ) , style, span }
738747}
739748
749+ pub fn mk_comment (
750+ g : & AttrIdGenerator ,
751+ comment_kind : CommentKind ,
752+ data : Symbol ,
753+ span : Span ,
754+ ) -> Attribute {
755+ Attribute {
756+ kind : AttrKind :: Comment ( comment_kind, data) ,
757+ id : g. mk_attr_id ( ) ,
758+ style : AttrStyle :: Outer ,
759+ span,
760+ }
761+ }
762+
740763fn mk_attr (
741764 g : & AttrIdGenerator ,
742765 style : AttrStyle ,
0 commit comments