diff options
| author | Nicholas Nethercote <nnethercote@mozilla.com> | 2019-10-24 06:33:12 +1100 |
|---|---|---|
| committer | Nicholas Nethercote <nnethercote@mozilla.com> | 2019-11-06 23:05:07 +1100 |
| commit | eea6f23a0ed67fd8c6b8e1b02cda3628fee56b2f (patch) | |
| tree | effdb244138f311440b21c9fd52e4028edb575ea /src/libsyntax/parse/mod.rs | |
| parent | 69bc4aba785e071740d2d46f109623b9951aae5d (diff) | |
| download | rust-eea6f23a0ed67fd8c6b8e1b02cda3628fee56b2f.tar.gz rust-eea6f23a0ed67fd8c6b8e1b02cda3628fee56b2f.zip | |
Make doc comments cheaper with `AttrKind`.
`AttrKind` is a new type with two variants, `Normal` and `DocComment`. It's a
big performance win (over 10% in some cases) because `DocComment` lets doc
comments (which are common) be represented very cheaply.
`Attribute` gets some new helper methods to ease the transition:
- `has_name()`: check if the attribute name matches a single `Symbol`; for
`DocComment` variants it succeeds if the symbol is `sym::doc`.
- `is_doc_comment()`: check if it has a `DocComment` kind.
- `{get,unwrap}_normal_item()`: extract the item from a `Normal` variant;
panic otherwise.
Fixes #60935.
Diffstat (limited to 'src/libsyntax/parse/mod.rs')
| -rw-r--r-- | src/libsyntax/parse/mod.rs | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index 6cfa0dfad82..b688dce87c1 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -287,7 +287,7 @@ pub fn parse_in_attr<'a, T>( ) -> PResult<'a, T> { let mut parser = Parser::new( sess, - attr.item.tokens.clone(), + attr.get_normal_item().tokens.clone(), None, false, false, @@ -393,18 +393,22 @@ fn prepend_attrs( let source = pprust::attribute_to_string(attr); let macro_filename = FileName::macro_expansion_source_code(&source); - if attr.is_sugared_doc { - let stream = parse_stream_from_source_str(macro_filename, source, sess, Some(span)); - builder.push(stream); - continue - } + + let item = match attr.kind { + ast::AttrKind::Normal(ref item) => item, + ast::AttrKind::DocComment(_) => { + let stream = parse_stream_from_source_str(macro_filename, source, sess, Some(span)); + builder.push(stream); + continue + } + }; // synthesize # [ $path $tokens ] manually here let mut brackets = tokenstream::TokenStreamBuilder::new(); // For simple paths, push the identifier directly - if attr.item.path.segments.len() == 1 && attr.item.path.segments[0].args.is_none() { - let ident = attr.item.path.segments[0].ident; + if item.path.segments.len() == 1 && item.path.segments[0].args.is_none() { + let ident = item.path.segments[0].ident; let token = token::Ident(ident.name, ident.as_str().starts_with("r#")); brackets.push(tokenstream::TokenTree::token(token, ident.span)); @@ -415,7 +419,7 @@ fn prepend_attrs( brackets.push(stream); } - brackets.push(attr.item.tokens.clone()); + brackets.push(item.tokens.clone()); // The span we list here for `#` and for `[ ... ]` are both wrong in // that it encompasses more than each token, but it hopefully is "good |
