Skip to content

Commit

Permalink
Rollup merge of rust-lang#65789 - Centril:with-desugared-doc, r=david…
Browse files Browse the repository at this point in the history
…twco

move Attribute::with_desugared_doc to librustdoc

From rust-lang#65324.

r? @varkor
  • Loading branch information
Centril authored Oct 25, 2019
2 parents 0bfe483 + 9e3e3a4 commit cbcbba2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 31 deletions.
28 changes: 26 additions & 2 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ use rustc::ty::{self, DefIdTree, TyCtxt, Region, RegionVid, Ty, AdtKind};
use rustc::ty::fold::TypeFolder;
use rustc::ty::layout::VariantIdx;
use rustc::util::nodemap::{FxHashMap, FxHashSet};
use syntax::ast::{self, AttrStyle, Ident};
use syntax::ast::{self, Attribute, AttrStyle, AttrItem, Ident};
use syntax::attr;
use syntax_expand::base::MacroKind;
use syntax::parse::lexer::comments;
use syntax::source_map::DUMMY_SP;
use syntax::symbol::{Symbol, kw, sym};
use syntax_pos::{self, Pos, FileName};
Expand Down Expand Up @@ -858,8 +859,31 @@ impl Attributes {
let mut cfg = Cfg::True;
let mut doc_line = 0;

/// Converts `attr` to a normal `#[doc="foo"]` comment, if it is a
/// comment like `///` or `/** */`. (Returns `attr` unchanged for
/// non-sugared doc attributes.)
pub fn with_desugared_doc<T>(attr: &Attribute, f: impl FnOnce(&Attribute) -> T) -> T {
if attr.is_sugared_doc {
let comment = attr.value_str().unwrap();
let meta = attr::mk_name_value_item_str(
Ident::with_dummy_span(sym::doc),
Symbol::intern(&comments::strip_doc_comment_decoration(&comment.as_str())),
DUMMY_SP,
);
f(&Attribute {
item: AttrItem { path: meta.path, tokens: meta.kind.tokens(meta.span) },
id: attr.id,
style: attr.style,
is_sugared_doc: true,
span: attr.span,
})
} else {
f(attr)
}
}

let other_attrs = attrs.iter().filter_map(|attr| {
attr.with_desugared_doc(|attr| {
with_desugared_doc(attr, |attr| {
if attr.check_name(sym::doc) {
if let Some(mi) = attr.meta() {
if let Some(value) = mi.value_str() {
Expand Down
29 changes: 2 additions & 27 deletions src/libsyntax/attr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use crate::ast::{AttrItem, AttrId, AttrStyle, Name, Ident, Path, PathSegment};
use crate::ast::{MetaItem, MetaItemKind, NestedMetaItem};
use crate::ast::{Lit, LitKind, Expr, Item, Local, Stmt, StmtKind, GenericParam};
use crate::mut_visit::visit_clobber;
use crate::source_map::{BytePos, Spanned, DUMMY_SP};
use crate::parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration};
use crate::source_map::{BytePos, Spanned};
use crate::parse::lexer::comments::doc_comment_style;
use crate::parse::parser::Parser;
use crate::parse::PResult;
use crate::parse::token::{self, Token};
Expand Down Expand Up @@ -312,31 +312,6 @@ impl Attribute {
span: self.span,
})
}

/// Converts `self` to a normal `#[doc="foo"]` comment, if it is a
/// comment like `///` or `/** */`. (Returns `self` unchanged for
/// non-sugared doc attributes.)
pub fn with_desugared_doc<T, F>(&self, f: F) -> T where
F: FnOnce(&Attribute) -> T,
{
if self.is_sugared_doc {
let comment = self.value_str().unwrap();
let meta = mk_name_value_item_str(
Ident::with_dummy_span(sym::doc),
Symbol::intern(&strip_doc_comment_decoration(&comment.as_str())),
DUMMY_SP,
);
f(&Attribute {
item: AttrItem { path: meta.path, tokens: meta.kind.tokens(meta.span) },
id: self.id,
style: self.style,
is_sugared_doc: true,
span: self.span,
})
} else {
f(self)
}
}
}

/* Constructors */
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/parse/lexer/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ fn split_block_comment_into_lines(

// it appears this function is called only from pprust... that's
// probably not a good thing.
pub fn gather_comments(sess: &ParseSess, path: FileName, src: String) -> Vec<Comment> {
crate fn gather_comments(sess: &ParseSess, path: FileName, src: String) -> Vec<Comment> {
let cm = SourceMap::new(sess.source_map().path_mapping().clone());
let source_file = cm.new_source_file(path, src);
let text = (*source_file.src.as_ref().unwrap()).clone();
Expand Down
3 changes: 2 additions & 1 deletion src/libsyntax/parse/lexer/tokentrees.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use rustc_data_structures::fx::FxHashMap;
use syntax_pos::Span;

use super::{StringReader, UnmatchedBrace};

use crate::print::pprust::token_to_string;
use crate::parse::lexer::{StringReader, UnmatchedBrace};
use crate::parse::token::{self, Token};
use crate::parse::PResult;
use crate::tokenstream::{DelimSpan, IsJoint::{self, *}, TokenStream, TokenTree, TreeAndJoint};
Expand Down

0 comments on commit cbcbba2

Please sign in to comment.