Skip to content

Commit

Permalink
Remove the Cow from Directory.
Browse files Browse the repository at this point in the history
The previous commit wrapped `Parser` within a `Cow` for the hot macro
parsing path. As a result, there's no need for the `Cow` within
`Directory`, because it lies within `Parser`.
  • Loading branch information
nnethercote committed Feb 6, 2020
1 parent 6bf2cc2 commit f840a95
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/librustc_expand/mbe/macro_rules.rs
Expand Up @@ -260,7 +260,7 @@ fn generic_extension<'cx>(
}

let directory = Directory {
path: Cow::from(cx.current_expansion.module.directory.as_path()),
path: cx.current_expansion.module.directory.clone(),
ownership: cx.current_expansion.directory_ownership,
};
let mut p = Parser::new(cx.parse_sess(), tts, Some(directory), true, false, None);
Expand Down Expand Up @@ -1218,7 +1218,7 @@ fn base_parser_from_cx<'cx>(
tts: TokenStream,
) -> Parser<'cx> {
let directory = Directory {
path: Cow::from(current_expansion.module.directory.as_path()),
path: current_expansion.module.directory.clone(),
ownership: current_expansion.directory_ownership,
};
Parser::new(sess, tts, Some(directory), true, true, rustc_parse::MACRO_ARGUMENTS)
Expand Down
9 changes: 4 additions & 5 deletions src/librustc_parse/lib.rs
Expand Up @@ -12,8 +12,7 @@ use syntax::ast;
use syntax::token::{self, Nonterminal};
use syntax::tokenstream::{self, TokenStream, TokenTree};

use std::borrow::Cow;
use std::path::Path;
use std::path::{Path, PathBuf};
use std::str;

use log::info;
Expand All @@ -29,8 +28,8 @@ pub mod validate_attr;
pub mod config;

#[derive(Clone)]
pub struct Directory<'a> {
pub path: Cow<'a, Path>,
pub struct Directory {
pub path: PathBuf,
pub ownership: DirectoryOwnership,
}

Expand Down Expand Up @@ -274,7 +273,7 @@ pub fn stream_to_parser<'a>(
pub fn stream_to_parser_with_base_dir<'a>(
sess: &'a ParseSess,
stream: TokenStream,
base_dir: Directory<'a>,
base_dir: Directory,
) -> Parser<'a> {
Parser::new(sess, stream, Some(base_dir), true, false, None)
}
Expand Down
9 changes: 4 additions & 5 deletions src/librustc_parse/parser/mod.rs
Expand Up @@ -29,7 +29,6 @@ use syntax::token::{self, DelimToken, Token, TokenKind};
use syntax::tokenstream::{self, DelimSpan, TokenStream, TokenTree, TreeAndJoint};
use syntax::util::comments::{doc_comment_style, strip_doc_comment_decoration};

use std::borrow::Cow;
use std::path::PathBuf;
use std::{cmp, mem, slice};

Expand Down Expand Up @@ -114,7 +113,7 @@ pub struct Parser<'a> {
prev_token_kind: PrevTokenKind,
restrictions: Restrictions,
/// Used to determine the path to externally loaded source files.
pub(super) directory: Directory<'a>,
pub(super) directory: Directory,
/// `true` to parse sub-modules in other files.
// Public for rustfmt usage.
pub recurse_into_file_modules: bool,
Expand Down Expand Up @@ -376,7 +375,7 @@ impl<'a> Parser<'a> {
pub fn new(
sess: &'a ParseSess,
tokens: TokenStream,
directory: Option<Directory<'a>>,
directory: Option<Directory>,
recurse_into_file_modules: bool,
desugar_doc_comments: bool,
subparser_name: Option<&'static str>,
Expand All @@ -390,7 +389,7 @@ impl<'a> Parser<'a> {
restrictions: Restrictions::empty(),
recurse_into_file_modules,
directory: Directory {
path: Cow::from(PathBuf::new()),
path: PathBuf::new(),
ownership: DirectoryOwnership::Owned { relative: None },
},
root_module_name: None,
Expand Down Expand Up @@ -418,7 +417,7 @@ impl<'a> Parser<'a> {
&sess.source_map().lookup_char_pos(parser.token.span.lo()).file.unmapped_path
{
if let Some(directory_path) = path.parent() {
parser.directory.path = Cow::from(directory_path.to_path_buf());
parser.directory.path = directory_path.to_path_buf();
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_parse/parser/module.rs
Expand Up @@ -285,7 +285,7 @@ impl<'a> Parser<'a> {

fn push_directory(&mut self, id: Ident, attrs: &[Attribute]) {
if let Some(path) = attr::first_attr_value_str_by_name(attrs, sym::path) {
self.directory.path.to_mut().push(&*path.as_str());
self.directory.path.push(&*path.as_str());
self.directory.ownership = DirectoryOwnership::Owned { relative: None };
} else {
// We have to push on the current module name in the case of relative
Expand All @@ -297,10 +297,10 @@ impl<'a> Parser<'a> {
if let DirectoryOwnership::Owned { relative } = &mut self.directory.ownership {
if let Some(ident) = relative.take() {
// remove the relative offset
self.directory.path.to_mut().push(&*ident.as_str());
self.directory.path.push(&*ident.as_str());
}
}
self.directory.path.to_mut().push(&*id.as_str());
self.directory.path.push(&*id.as_str());
}
}
}

0 comments on commit f840a95

Please sign in to comment.