Skip to content

Commit cbcbba2

Browse files
authored
Rollup merge of rust-lang#65789 - Centril:with-desugared-doc, r=davidtwco
move Attribute::with_desugared_doc to librustdoc From rust-lang#65324. r? @varkor
2 parents 0bfe483 + 9e3e3a4 commit cbcbba2

File tree

4 files changed

+31
-31
lines changed

4 files changed

+31
-31
lines changed

src/librustdoc/clean/mod.rs

+26-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ use rustc::ty::{self, DefIdTree, TyCtxt, Region, RegionVid, Ty, AdtKind};
2626
use rustc::ty::fold::TypeFolder;
2727
use rustc::ty::layout::VariantIdx;
2828
use rustc::util::nodemap::{FxHashMap, FxHashSet};
29-
use syntax::ast::{self, AttrStyle, Ident};
29+
use syntax::ast::{self, Attribute, AttrStyle, AttrItem, Ident};
3030
use syntax::attr;
3131
use syntax_expand::base::MacroKind;
32+
use syntax::parse::lexer::comments;
3233
use syntax::source_map::DUMMY_SP;
3334
use syntax::symbol::{Symbol, kw, sym};
3435
use syntax_pos::{self, Pos, FileName};
@@ -858,8 +859,31 @@ impl Attributes {
858859
let mut cfg = Cfg::True;
859860
let mut doc_line = 0;
860861

862+
/// Converts `attr` to a normal `#[doc="foo"]` comment, if it is a
863+
/// comment like `///` or `/** */`. (Returns `attr` unchanged for
864+
/// non-sugared doc attributes.)
865+
pub fn with_desugared_doc<T>(attr: &Attribute, f: impl FnOnce(&Attribute) -> T) -> T {
866+
if attr.is_sugared_doc {
867+
let comment = attr.value_str().unwrap();
868+
let meta = attr::mk_name_value_item_str(
869+
Ident::with_dummy_span(sym::doc),
870+
Symbol::intern(&comments::strip_doc_comment_decoration(&comment.as_str())),
871+
DUMMY_SP,
872+
);
873+
f(&Attribute {
874+
item: AttrItem { path: meta.path, tokens: meta.kind.tokens(meta.span) },
875+
id: attr.id,
876+
style: attr.style,
877+
is_sugared_doc: true,
878+
span: attr.span,
879+
})
880+
} else {
881+
f(attr)
882+
}
883+
}
884+
861885
let other_attrs = attrs.iter().filter_map(|attr| {
862-
attr.with_desugared_doc(|attr| {
886+
with_desugared_doc(attr, |attr| {
863887
if attr.check_name(sym::doc) {
864888
if let Some(mi) = attr.meta() {
865889
if let Some(value) = mi.value_str() {

src/libsyntax/attr/mod.rs

+2-27
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use crate::ast::{AttrItem, AttrId, AttrStyle, Name, Ident, Path, PathSegment};
1313
use crate::ast::{MetaItem, MetaItemKind, NestedMetaItem};
1414
use crate::ast::{Lit, LitKind, Expr, Item, Local, Stmt, StmtKind, GenericParam};
1515
use crate::mut_visit::visit_clobber;
16-
use crate::source_map::{BytePos, Spanned, DUMMY_SP};
17-
use crate::parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration};
16+
use crate::source_map::{BytePos, Spanned};
17+
use crate::parse::lexer::comments::doc_comment_style;
1818
use crate::parse::parser::Parser;
1919
use crate::parse::PResult;
2020
use crate::parse::token::{self, Token};
@@ -312,31 +312,6 @@ impl Attribute {
312312
span: self.span,
313313
})
314314
}
315-
316-
/// Converts `self` to a normal `#[doc="foo"]` comment, if it is a
317-
/// comment like `///` or `/** */`. (Returns `self` unchanged for
318-
/// non-sugared doc attributes.)
319-
pub fn with_desugared_doc<T, F>(&self, f: F) -> T where
320-
F: FnOnce(&Attribute) -> T,
321-
{
322-
if self.is_sugared_doc {
323-
let comment = self.value_str().unwrap();
324-
let meta = mk_name_value_item_str(
325-
Ident::with_dummy_span(sym::doc),
326-
Symbol::intern(&strip_doc_comment_decoration(&comment.as_str())),
327-
DUMMY_SP,
328-
);
329-
f(&Attribute {
330-
item: AttrItem { path: meta.path, tokens: meta.kind.tokens(meta.span) },
331-
id: self.id,
332-
style: self.style,
333-
is_sugared_doc: true,
334-
span: self.span,
335-
})
336-
} else {
337-
f(self)
338-
}
339-
}
340315
}
341316

342317
/* Constructors */

src/libsyntax/parse/lexer/comments.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ fn split_block_comment_into_lines(
176176

177177
// it appears this function is called only from pprust... that's
178178
// probably not a good thing.
179-
pub fn gather_comments(sess: &ParseSess, path: FileName, src: String) -> Vec<Comment> {
179+
crate fn gather_comments(sess: &ParseSess, path: FileName, src: String) -> Vec<Comment> {
180180
let cm = SourceMap::new(sess.source_map().path_mapping().clone());
181181
let source_file = cm.new_source_file(path, src);
182182
let text = (*source_file.src.as_ref().unwrap()).clone();

src/libsyntax/parse/lexer/tokentrees.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use rustc_data_structures::fx::FxHashMap;
22
use syntax_pos::Span;
33

4+
use super::{StringReader, UnmatchedBrace};
5+
46
use crate::print::pprust::token_to_string;
5-
use crate::parse::lexer::{StringReader, UnmatchedBrace};
67
use crate::parse::token::{self, Token};
78
use crate::parse::PResult;
89
use crate::tokenstream::{DelimSpan, IsJoint::{self, *}, TokenStream, TokenTree, TreeAndJoint};

0 commit comments

Comments
 (0)