Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move syntax::ext to a syntax_expand and refactor some attribute logic #65465

Merged
merged 15 commits into from
Oct 17, 2019
6 changes: 3 additions & 3 deletions src/libsyntax/attr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ impl Attribute {
self.item.meta(self.span)
}

pub fn parse<'a, T, F>(&self, sess: &'a ParseSess, mut f: F) -> PResult<'a, T>
crate fn parse<'a, T, F>(&self, sess: &'a ParseSess, mut f: F) -> PResult<'a, T>
where F: FnMut(&mut Parser<'a>) -> PResult<'a, T>,
{
let mut parser = Parser::new(
Expand All @@ -298,14 +298,14 @@ impl Attribute {
Ok(result)
}

pub fn parse_derive_paths<'a>(&self, sess: &'a ParseSess) -> PResult<'a, Vec<Path>> {
crate fn parse_derive_paths<'a>(&self, sess: &'a ParseSess) -> PResult<'a, Vec<Path>> {
if self.tokens.is_empty() {
return Ok(Vec::new());
}
self.parse(sess, |p| p.parse_derive_paths())
}

pub fn parse_meta<'a>(&self, sess: &'a ParseSess) -> PResult<'a, MetaItem> {
crate fn parse_meta<'a>(&self, sess: &'a ParseSess) -> PResult<'a, MetaItem> {
Ok(MetaItem {
path: self.path.clone(),
kind: self.parse(sess, |parser| parser.parse_meta_item_kind())?,
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/ext/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ impl<'a> Parser<'a> {
self.this_token_to_string());
// Avoid emitting backtrace info twice.
let def_site_span = self.token.span.with_ctxt(SyntaxContext::root());
let mut err = self.diagnostic().struct_span_err(def_site_span, &msg);
let mut err = self.struct_span_err(def_site_span, &msg);
err.span_label(span, "caused by the macro expansion here");
let msg = format!(
"the usage of `{}!` is likely invalid in {} context",
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/parse/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ impl Lit {
/// Attempts to recover an AST literal from semantic literal.
/// This function is used when the original token doesn't exist (e.g. the literal is created
/// by an AST-based macro) or unavailable (e.g. from HIR pretty-printing).
pub fn from_lit_kind(kind: LitKind, span: Span) -> Lit {
crate fn from_lit_kind(kind: LitKind, span: Span) -> Lit {
Lit { token: kind.to_lit_token(), kind, span }
}

Expand Down
60 changes: 29 additions & 31 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ mod attr;
mod expr;
mod pat;
mod item;
pub use item::AliasKind;
mod module;
pub use module::{ModulePath, ModulePathSuccess};
mod ty;
mod path;
pub use path::PathStyle;
crate use path::PathStyle;
mod stmt;
mod generics;
mod diagnostics;
Expand Down Expand Up @@ -46,14 +44,14 @@ bitflags::bitflags! {
}

#[derive(Clone, Copy, PartialEq, Debug)]
crate enum SemiColonMode {
enum SemiColonMode {
Break,
Ignore,
Comma,
}

#[derive(Clone, Copy, PartialEq, Debug)]
crate enum BlockMode {
enum BlockMode {
Break,
Ignore,
}
Expand Down Expand Up @@ -126,33 +124,33 @@ pub struct Parser<'a> {
prev_token_kind: PrevTokenKind,
restrictions: Restrictions,
/// Used to determine the path to externally loaded source files.
crate directory: Directory<'a>,
pub(super) directory: Directory<'a>,
/// `true` to parse sub-modules in other files.
pub recurse_into_file_modules: bool,
pub(super) recurse_into_file_modules: bool,
/// Name of the root module this parser originated from. If `None`, then the
/// name is not known. This does not change while the parser is descending
/// into modules, and sub-parsers have new values for this name.
pub root_module_name: Option<String>,
crate expected_tokens: Vec<TokenType>,
crate root_module_name: Option<String>,
expected_tokens: Vec<TokenType>,
token_cursor: TokenCursor,
desugar_doc_comments: bool,
/// `true` we should configure out of line modules as we parse.
pub cfg_mods: bool,
cfg_mods: bool,
/// This field is used to keep track of how many left angle brackets we have seen. This is
/// required in order to detect extra leading left angle brackets (`<` characters) and error
/// appropriately.
///
/// See the comments in the `parse_path_segment` function for more details.
crate unmatched_angle_bracket_count: u32,
crate max_angle_bracket_count: u32,
unmatched_angle_bracket_count: u32,
max_angle_bracket_count: u32,
/// A list of all unclosed delimiters found by the lexer. If an entry is used for error recovery
/// it gets removed from here. Every entry left at the end gets emitted as an independent
/// error.
crate unclosed_delims: Vec<UnmatchedBrace>,
crate last_unexpected_token_span: Option<Span>,
pub(super) unclosed_delims: Vec<UnmatchedBrace>,
last_unexpected_token_span: Option<Span>,
crate last_type_ascription: Option<(Span, bool /* likely path typo */)>,
/// If present, this `Parser` is not parsing Rust code but rather a macro call.
crate subparser_name: Option<&'static str>,
subparser_name: Option<&'static str>,
}

impl<'a> Drop for Parser<'a> {
Expand Down Expand Up @@ -196,7 +194,7 @@ struct TokenCursorFrame {
/// You can find some more example usage of this in the `collect_tokens` method
/// on the parser.
#[derive(Clone)]
crate enum LastToken {
enum LastToken {
Collecting(Vec<TreeAndJoint>),
Was(Option<TreeAndJoint>),
}
Expand Down Expand Up @@ -299,7 +297,7 @@ impl TokenCursor {
}

#[derive(Clone, PartialEq)]
crate enum TokenType {
enum TokenType {
Token(TokenKind),
Keyword(Symbol),
Operator,
Expand All @@ -311,7 +309,7 @@ crate enum TokenType {
}

impl TokenType {
crate fn to_string(&self) -> String {
fn to_string(&self) -> String {
match *self {
TokenType::Token(ref t) => format!("`{}`", pprust::token_kind_to_string(t)),
TokenType::Keyword(kw) => format!("`{}`", kw),
Expand All @@ -326,13 +324,13 @@ impl TokenType {
}

#[derive(Copy, Clone, Debug)]
crate enum TokenExpectType {
enum TokenExpectType {
Expect,
NoExpect,
}

impl<'a> Parser<'a> {
pub fn new(
crate fn new(
sess: &'a ParseSess,
tokens: TokenStream,
directory: Option<Directory<'a>>,
Expand Down Expand Up @@ -407,7 +405,7 @@ impl<'a> Parser<'a> {
pprust::token_to_string(&self.token)
}

crate fn token_descr(&self) -> Option<&'static str> {
fn token_descr(&self) -> Option<&'static str> {
Some(match &self.token.kind {
_ if self.token.is_special_ident() => "reserved identifier",
_ if self.token.is_used_keyword() => "keyword",
Expand All @@ -417,7 +415,7 @@ impl<'a> Parser<'a> {
})
}

crate fn this_token_descr(&self) -> String {
pub(super) fn this_token_descr(&self) -> String {
if let Some(prefix) = self.token_descr() {
format!("{} `{}`", prefix, self.this_token_to_string())
} else {
Expand Down Expand Up @@ -467,7 +465,7 @@ impl<'a> Parser<'a> {
}
}

pub fn parse_ident(&mut self) -> PResult<'a, ast::Ident> {
fn parse_ident(&mut self) -> PResult<'a, ast::Ident> {
self.parse_ident_common(true)
}

Expand Down Expand Up @@ -500,7 +498,7 @@ impl<'a> Parser<'a> {
///
/// This method will automatically add `tok` to `expected_tokens` if `tok` is not
/// encountered.
crate fn check(&mut self, tok: &TokenKind) -> bool {
fn check(&mut self, tok: &TokenKind) -> bool {
let is_present = self.token == *tok;
if !is_present { self.expected_tokens.push(TokenType::Token(tok.clone())); }
is_present
Expand All @@ -522,7 +520,7 @@ impl<'a> Parser<'a> {

/// If the next token is the given keyword, eats it and returns `true`.
/// Otherwise, returns `false`. An expectation is also added for diagnostics purposes.
pub fn eat_keyword(&mut self, kw: Symbol) -> bool {
fn eat_keyword(&mut self, kw: Symbol) -> bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Centril Please reconsider before making these kinds of 'refactorings.' Tools like rustfmt which rely on the libsyntax suffer severely from reduced visibility. Now I have to iterate through the changes, re-publish hidden fields and methods, make a PR and wait until it gets merged.

I understand that the libsyntax is primarily for the rustc itself, and its interface is unstable. But please be conservative about the visibility of its interface.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@topecongiro If you rely on something then there should be a comment to that effect in the rustc sources. Reducing visibility is an important tool for cleaning up the rustc internals and should be used.

if self.check_keyword(kw) {
self.bump();
true
Expand Down Expand Up @@ -560,7 +558,7 @@ impl<'a> Parser<'a> {
}
}

crate fn check_ident(&mut self) -> bool {
fn check_ident(&mut self) -> bool {
self.check_or_expected(self.token.is_ident(), TokenType::Ident)
}

Expand Down Expand Up @@ -725,7 +723,7 @@ impl<'a> Parser<'a> {
/// Parses a sequence, including the closing delimiter. The function
/// `f` must consume tokens until reaching the next separator or
/// closing bracket.
pub fn parse_seq_to_end<T>(
fn parse_seq_to_end<T>(
&mut self,
ket: &TokenKind,
sep: SeqSep,
Expand All @@ -741,7 +739,7 @@ impl<'a> Parser<'a> {
/// Parses a sequence, not including the closing delimiter. The function
/// `f` must consume tokens until reaching the next separator or
/// closing bracket.
pub fn parse_seq_to_before_end<T>(
fn parse_seq_to_before_end<T>(
&mut self,
ket: &TokenKind,
sep: SeqSep,
Expand All @@ -759,7 +757,7 @@ impl<'a> Parser<'a> {
})
}

crate fn parse_seq_to_before_tokens<T>(
fn parse_seq_to_before_tokens<T>(
&mut self,
kets: &[&TokenKind],
sep: SeqSep,
Expand Down Expand Up @@ -1101,7 +1099,7 @@ impl<'a> Parser<'a> {
/// If the following element can't be a tuple (i.e., it's a function definition), then
/// it's not a tuple struct field), and the contents within the parentheses isn't valid,
/// so emit a proper diagnostic.
pub fn parse_visibility(&mut self, can_take_tuple: bool) -> PResult<'a, Visibility> {
crate fn parse_visibility(&mut self, can_take_tuple: bool) -> PResult<'a, Visibility> {
maybe_whole!(self, NtVis, |x| x);

self.expected_tokens.push(TokenType::Keyword(kw::Crate));
Expand Down Expand Up @@ -1325,7 +1323,7 @@ impl<'a> Parser<'a> {
*t == token::BinOp(token::Star))
}

pub fn parse_optional_str(&mut self) -> Option<(Symbol, ast::StrStyle, Option<ast::Name>)> {
fn parse_optional_str(&mut self) -> Option<(Symbol, ast::StrStyle, Option<ast::Name>)> {
let ret = match self.token.kind {
token::Literal(token::Lit { kind: token::Str, symbol, suffix }) =>
(symbol, ast::StrStyle::Cooked, suffix),
Expand Down
11 changes: 6 additions & 5 deletions src/libsyntax/parse/parser/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const DEFAULT_UNEXPECTED_INNER_ATTR_ERR_MSG: &str = "an inner attribute is not \

impl<'a> Parser<'a> {
/// Parses attributes that appear before an item.
crate fn parse_outer_attributes(&mut self) -> PResult<'a, Vec<ast::Attribute>> {
pub(super) fn parse_outer_attributes(&mut self) -> PResult<'a, Vec<ast::Attribute>> {
let mut attrs: Vec<ast::Attribute> = Vec::new();
let mut just_parsed_doc_comment = false;
loop {
Expand Down Expand Up @@ -66,7 +66,7 @@ impl<'a> Parser<'a> {
///
/// If `permit_inner` is `true`, then a leading `!` indicates an inner
/// attribute.
pub fn parse_attribute(&mut self, permit_inner: bool) -> PResult<'a, ast::Attribute> {
fn parse_attribute(&mut self, permit_inner: bool) -> PResult<'a, ast::Attribute> {
debug!("parse_attribute: permit_inner={:?} self.token={:?}",
permit_inner,
self.token);
Expand All @@ -84,9 +84,10 @@ impl<'a> Parser<'a> {

/// The same as `parse_attribute`, except it takes in an `InnerAttributeParsePolicy`
/// that prescribes how to handle inner attributes.
fn parse_attribute_with_inner_parse_policy(&mut self,
inner_parse_policy: InnerAttributeParsePolicy<'_>)
-> PResult<'a, ast::Attribute> {
fn parse_attribute_with_inner_parse_policy(
&mut self,
inner_parse_policy: InnerAttributeParsePolicy<'_>
) -> PResult<'a, ast::Attribute> {
debug!("parse_attribute_with_inner_parse_policy: inner_parse_policy={:?} self.token={:?}",
inner_parse_policy,
self.token);
Expand Down
Loading