Auto merge of #149645 - GuillaumeGomez:doc-attr-based, r=jdonszelmann… · rust-lang/rust@5b150d2 (original) (raw)

`@@ -13,7 +13,9 @@ use crate::ast::{

`

13

13

`Expr, ExprKind, LitKind, MetaItem, MetaItemInner, MetaItemKind, MetaItemLit, NormalAttr, Path,

`

14

14

`PathSegment, Safety,

`

15

15

`};

`

16

``

`-

use crate::token::{self, CommentKind, Delimiter, InvisibleOrigin, MetaVarKind, Token};

`

``

16

`+

use crate::token::{

`

``

17

`+

self, CommentKind, Delimiter, DocFragmentKind, InvisibleOrigin, MetaVarKind, Token,

`

``

18

`+

};

`

17

19

`use crate::tokenstream::{

`

18

20

`DelimSpan, LazyAttrTokenStream, Spacing, TokenStream, TokenStreamIter, TokenTree,

`

19

21

`};

`

`@@ -179,15 +181,21 @@ impl AttributeExt for Attribute {

`

179

181

`}

`

180

182

``

181

183

`/// Returns the documentation and its kind if this is a doc comment or a sugared doc comment.

`

182

``

`` -

/// * ///doc returns Some(("doc", CommentKind::Line)).

``

183

``

`` -

/// * /** doc */ returns Some(("doc", CommentKind::Block)).

``

184

``

`` -

/// * #[doc = "doc"] returns Some(("doc", CommentKind::Line)).

``

``

184

`` +

/// * ///doc returns Some(("doc", DocFragmentKind::Sugared(CommentKind::Line))).

``

``

185

`` +

/// * /** doc */ returns Some(("doc", DocFragmentKind::Sugared(CommentKind::Block))).

``

``

186

`` +

/// * #[doc = "doc"] returns Some(("doc", DocFragmentKind::Raw)).

``

185

187

`` /// * #[doc(...)] returns None.

``

186

``

`-

fn doc_str_and_comment_kind(&self) -> Option<(Symbol, CommentKind)> {

`

``

188

`+

fn doc_str_and_fragment_kind(&self) -> Option<(Symbol, DocFragmentKind)> {

`

187

189

`match &self.kind {

`

188

``

`-

AttrKind::DocComment(kind, data) => Some((*data, *kind)),

`

``

190

`+

AttrKind::DocComment(kind, data) => Some((*data, DocFragmentKind::Sugared(*kind))),

`

189

191

`AttrKind::Normal(normal) if normal.item.path == sym::doc => {

`

190

``

`-

normal.item.value_str().map(|s| (s, CommentKind::Line))

`

``

192

`+

if let Some(value) = normal.item.value_str()

`

``

193

`+

&& let Some(value_span) = normal.item.value_span()

`

``

194

`+

{

`

``

195

`+

Some((value, DocFragmentKind::Raw(value_span)))

`

``

196

`+

} else {

`

``

197

`+

None

`

``

198

`+

}

`

191

199

`}

`

192

200

` _ => None,

`

193

201

`}

`

`@@ -220,6 +228,24 @@ impl AttributeExt for Attribute {

`

220

228

`fn is_automatically_derived_attr(&self) -> bool {

`

221

229

`self.has_name(sym::automatically_derived)

`

222

230

`}

`

``

231

+

``

232

`+

fn is_doc_hidden(&self) -> bool {

`

``

233

`+

self.has_name(sym::doc)

`

``

234

`+

&& self.meta_item_list().is_some_and(|l| list_contains_name(&l, sym::hidden))

`

``

235

`+

}

`

``

236

+

``

237

`+

fn is_doc_keyword_or_attribute(&self) -> bool {

`

``

238

`+

if self.has_name(sym::doc)

`

``

239

`+

&& let Some(items) = self.meta_item_list()

`

``

240

`+

{

`

``

241

`+

for item in items {

`

``

242

`+

if item.has_name(sym::keyword) || item.has_name(sym::attribute) {

`

``

243

`+

return true;

`

``

244

`+

}

`

``

245

`+

}

`

``

246

`+

}

`

``

247

`+

false

`

``

248

`+

}

`

223

249

`}

`

224

250

``

225

251

`impl Attribute {

`

`@@ -300,6 +326,25 @@ impl AttrItem {

`

300

326

`}

`

301

327

`}

`

302

328

``

``

329

`+

/// Returns the span in:

`

``

330

`+

///

`

``

331


/// ```text

``

332

`+

/// #[attribute = "value"]

`

``

333

`+

/// ^^^^^^^

`

``

334


/// ```

``

335

`+

///

`

``

336

`` +

/// It returns None in any other cases like:

``

``

337

`+

///

`

``

338


/// ```text

``

339

`+

/// #[attr("value")]

`

``

340


/// ```

``

341

`+

fn value_span(&self) -> Option {

`

``

342

`+

match &self.args {

`

``

343

`+

AttrArgs::Eq { expr, .. } => Some(expr.span),

`

``

344

`+

AttrArgs::Delimited(_) | AttrArgs::Empty => None,

`

``

345

`+

}

`

``

346

`+

}

`

``

347

+

303

348

`pub fn meta(&self, span: Span) -> Option {

`

304

349

`Some(MetaItem {

`

305

350

`unsafety: Safety::Default,

`

`@@ -820,7 +865,7 @@ pub trait AttributeExt: Debug {

`

820

865

`` /// * /** doc */ returns Some(("doc", CommentKind::Block)).

``

821

866

`` /// * #[doc = "doc"] returns Some(("doc", CommentKind::Line)).

``

822

867

`` /// * #[doc(...)] returns None.

``

823

``

`-

fn doc_str_and_comment_kind(&self) -> Option<(Symbol, CommentKind)>;

`

``

868

`+

fn doc_str_and_fragment_kind(&self) -> Option<(Symbol, DocFragmentKind)>;

`

824

869

``

825

870

`/// Returns outer or inner if this is a doc attribute or a sugared doc

`

826

871

`/// comment, otherwise None.

`

`@@ -830,6 +875,12 @@ pub trait AttributeExt: Debug {

`

830

875

`/// commented module (for inner doc) vs within its parent module (for outer

`

831

876

`/// doc).

`

832

877

`fn doc_resolution_scope(&self) -> Option;

`

``

878

+

``

879

`` +

/// Returns true if this attribute contains doc(hidden).

``

``

880

`+

fn is_doc_hidden(&self) -> bool;

`

``

881

+

``

882

`` +

/// Returns true is this attribute contains doc(keyword) or doc(attribute).

``

``

883

`+

fn is_doc_keyword_or_attribute(&self) -> bool;

`

833

884

`}

`

834

885

``

835

886

`// FIXME(fn_delegation): use function delegation instead of manually forwarding

`

`@@ -902,7 +953,7 @@ impl Attribute {

`

902

953

`AttributeExt::is_proc_macro_attr(self)

`

903

954

`}

`

904

955

``

905

``

`-

pub fn doc_str_and_comment_kind(&self) -> Option<(Symbol, CommentKind)> {

`

906

``

`-

AttributeExt::doc_str_and_comment_kind(self)

`

``

956

`+

pub fn doc_str_and_fragment_kind(&self) -> Option<(Symbol, DocFragmentKind)> {

`

``

957

`+

AttributeExt::doc_str_and_fragment_kind(self)

`

907

958

`}

`

908

959

`}

`