Reduce pub exposure.

This commit is contained in:
Nicholas Nethercote 2024-06-03 15:47:46 +10:00
parent 7ebd2bdbf6
commit 95b4c07ef8
8 changed files with 51 additions and 42 deletions

View file

@ -130,14 +130,14 @@ pub enum AttemptLocalParseRecovery {
} }
impl AttemptLocalParseRecovery { impl AttemptLocalParseRecovery {
pub fn yes(&self) -> bool { pub(super) fn yes(&self) -> bool {
match self { match self {
AttemptLocalParseRecovery::Yes => true, AttemptLocalParseRecovery::Yes => true,
AttemptLocalParseRecovery::No => false, AttemptLocalParseRecovery::No => false,
} }
} }
pub fn no(&self) -> bool { pub(super) fn no(&self) -> bool {
match self { match self {
AttemptLocalParseRecovery::Yes => false, AttemptLocalParseRecovery::Yes => false,
AttemptLocalParseRecovery::No => true, AttemptLocalParseRecovery::No => true,
@ -891,7 +891,7 @@ impl<'a> Parser<'a> {
} }
} }
pub fn maybe_suggest_struct_literal( pub(super) fn maybe_suggest_struct_literal(
&mut self, &mut self,
lo: Span, lo: Span,
s: BlockCheckMode, s: BlockCheckMode,
@ -2459,7 +2459,7 @@ impl<'a> Parser<'a> {
/// Handle encountering a symbol in a generic argument list that is not a `,` or `>`. In this /// Handle encountering a symbol in a generic argument list that is not a `,` or `>`. In this
/// case, we emit an error and try to suggest enclosing a const argument in braces if it looks /// case, we emit an error and try to suggest enclosing a const argument in braces if it looks
/// like the user has forgotten them. /// like the user has forgotten them.
pub fn handle_ambiguous_unbraced_const_arg( pub(super) fn handle_ambiguous_unbraced_const_arg(
&mut self, &mut self,
args: &mut ThinVec<AngleBracketedArg>, args: &mut ThinVec<AngleBracketedArg>,
) -> PResult<'a, bool> { ) -> PResult<'a, bool> {
@ -2500,7 +2500,7 @@ impl<'a> Parser<'a> {
/// - Single-segment paths (i.e. standalone generic const parameters). /// - Single-segment paths (i.e. standalone generic const parameters).
/// All other expressions that can be parsed will emit an error suggesting the expression be /// All other expressions that can be parsed will emit an error suggesting the expression be
/// wrapped in braces. /// wrapped in braces.
pub fn handle_unambiguous_unbraced_const_arg(&mut self) -> PResult<'a, P<Expr>> { pub(super) fn handle_unambiguous_unbraced_const_arg(&mut self) -> PResult<'a, P<Expr>> {
let start = self.token.span; let start = self.token.span;
let expr = self.parse_expr_res(Restrictions::CONST_EXPR, None).map_err(|mut err| { let expr = self.parse_expr_res(Restrictions::CONST_EXPR, None).map_err(|mut err| {
err.span_label( err.span_label(
@ -2559,7 +2559,7 @@ impl<'a> Parser<'a> {
Some(GenericArg::Const(AnonConst { id: ast::DUMMY_NODE_ID, value })) Some(GenericArg::Const(AnonConst { id: ast::DUMMY_NODE_ID, value }))
} }
pub fn recover_const_param_declaration( pub(super) fn recover_const_param_declaration(
&mut self, &mut self,
ty_generics: Option<&Generics>, ty_generics: Option<&Generics>,
) -> PResult<'a, Option<GenericArg>> { ) -> PResult<'a, Option<GenericArg>> {
@ -2589,7 +2589,11 @@ impl<'a> Parser<'a> {
/// When encountering code like `foo::< bar + 3 >` or `foo::< bar - baz >` we suggest /// When encountering code like `foo::< bar + 3 >` or `foo::< bar - baz >` we suggest
/// `foo::<{ bar + 3 }>` and `foo::<{ bar - baz }>`, respectively. We only provide a suggestion /// `foo::<{ bar + 3 }>` and `foo::<{ bar - baz }>`, respectively. We only provide a suggestion
/// if we think that the resulting expression would be well formed. /// if we think that the resulting expression would be well formed.
pub fn recover_const_arg(&mut self, start: Span, mut err: Diag<'a>) -> PResult<'a, GenericArg> { pub(super) fn recover_const_arg(
&mut self,
start: Span,
mut err: Diag<'a>,
) -> PResult<'a, GenericArg> {
let is_op_or_dot = AssocOp::from_token(&self.token) let is_op_or_dot = AssocOp::from_token(&self.token)
.and_then(|op| { .and_then(|op| {
if let AssocOp::Greater if let AssocOp::Greater
@ -2690,7 +2694,7 @@ impl<'a> Parser<'a> {
} }
/// Creates a dummy const argument, and reports that the expression must be enclosed in braces /// Creates a dummy const argument, and reports that the expression must be enclosed in braces
pub fn dummy_const_arg_needs_braces(&self, mut err: Diag<'a>, span: Span) -> GenericArg { pub(super) fn dummy_const_arg_needs_braces(&self, mut err: Diag<'a>, span: Span) -> GenericArg {
err.multipart_suggestion( err.multipart_suggestion(
"expressions must be enclosed in braces to be used as const generic \ "expressions must be enclosed in braces to be used as const generic \
arguments", arguments",
@ -2961,7 +2965,7 @@ impl<'a> Parser<'a> {
/// * `=====` /// * `=====`
/// * `<<<<<` /// * `<<<<<`
/// ///
pub fn is_vcs_conflict_marker( pub(super) fn is_vcs_conflict_marker(
&mut self, &mut self,
long_kind: &TokenKind, long_kind: &TokenKind,
short_kind: &TokenKind, short_kind: &TokenKind,
@ -2981,14 +2985,14 @@ impl<'a> Parser<'a> {
None None
} }
pub fn recover_vcs_conflict_marker(&mut self) { pub(super) fn recover_vcs_conflict_marker(&mut self) {
if let Err(err) = self.err_vcs_conflict_marker() { if let Err(err) = self.err_vcs_conflict_marker() {
err.emit(); err.emit();
FatalError.raise(); FatalError.raise();
} }
} }
pub fn err_vcs_conflict_marker(&mut self) -> PResult<'a, ()> { pub(crate) fn err_vcs_conflict_marker(&mut self) -> PResult<'a, ()> {
let Some(start) = self.conflict_marker(&TokenKind::BinOp(token::Shl), &TokenKind::Lt) let Some(start) = self.conflict_marker(&TokenKind::BinOp(token::Shl), &TokenKind::Lt)
else { else {
return Ok(()); return Ok(());

View file

@ -445,7 +445,7 @@ impl<'a> Parser<'a> {
/// The method does not advance the current token. /// The method does not advance the current token.
/// ///
/// Also performs recovery for `and` / `or` which are mistaken for `&&` and `||` respectively. /// Also performs recovery for `and` / `or` which are mistaken for `&&` and `||` respectively.
pub fn check_assoc_op(&self) -> Option<Spanned<AssocOp>> { pub(super) fn check_assoc_op(&self) -> Option<Spanned<AssocOp>> {
let (op, span) = match (AssocOp::from_token(&self.token), self.token.ident()) { let (op, span) = match (AssocOp::from_token(&self.token), self.token.ident()) {
// When parsing const expressions, stop parsing when encountering `>`. // When parsing const expressions, stop parsing when encountering `>`.
( (
@ -1022,7 +1022,11 @@ impl<'a> Parser<'a> {
} }
} }
pub fn parse_dot_suffix_expr(&mut self, lo: Span, base: P<Expr>) -> PResult<'a, P<Expr>> { pub(super) fn parse_dot_suffix_expr(
&mut self,
lo: Span,
base: P<Expr>,
) -> PResult<'a, P<Expr>> {
// At this point we've consumed something like `expr.` and `self.token` holds the token // At this point we've consumed something like `expr.` and `self.token` holds the token
// after the dot. // after the dot.
match self.token.uninterpolate().kind { match self.token.uninterpolate().kind {

View file

@ -2256,7 +2256,7 @@ pub(crate) struct FnParseMode {
/// to true. /// to true.
/// * The span is from Edition 2015. In particular, you can get a /// * The span is from Edition 2015. In particular, you can get a
/// 2015 span inside a 2021 crate using macros. /// 2015 span inside a 2021 crate using macros.
pub req_name: ReqName, pub(super) req_name: ReqName,
/// If this flag is set to `true`, then plain, semicolon-terminated function /// If this flag is set to `true`, then plain, semicolon-terminated function
/// prototypes are not allowed here. /// prototypes are not allowed here.
/// ///
@ -2275,7 +2275,7 @@ pub(crate) struct FnParseMode {
/// This field should only be set to false if the item is inside of a trait /// This field should only be set to false if the item is inside of a trait
/// definition or extern block. Within an impl block or a module, it should /// definition or extern block. Within an impl block or a module, it should
/// always be set to true. /// always be set to true.
pub req_body: bool, pub(super) req_body: bool,
} }
/// Parsing of functions and methods. /// Parsing of functions and methods.

View file

@ -11,14 +11,13 @@ mod stmt;
mod ty; mod ty;
use crate::lexer::UnmatchedDelim; use crate::lexer::UnmatchedDelim;
pub use attr_wrapper::AttrWrapper; use attr_wrapper::AttrWrapper;
pub use diagnostics::AttemptLocalParseRecovery; pub use diagnostics::AttemptLocalParseRecovery;
pub(crate) use expr::ForbiddenLetReason; pub(crate) use expr::ForbiddenLetReason;
pub(crate) use item::FnParseMode; pub(crate) use item::FnParseMode;
pub use pat::{CommaRecoveryMode, RecoverColon, RecoverComma}; pub use pat::{CommaRecoveryMode, RecoverColon, RecoverComma};
pub use path::PathStyle; use path::PathStyle;
use core::fmt;
use rustc_ast::ptr::P; use rustc_ast::ptr::P;
use rustc_ast::token::{self, Delimiter, IdentIsRaw, Nonterminal, Token, TokenKind}; use rustc_ast::token::{self, Delimiter, IdentIsRaw, Nonterminal, Token, TokenKind};
use rustc_ast::tokenstream::{AttributesData, DelimSpacing, DelimSpan, Spacing}; use rustc_ast::tokenstream::{AttributesData, DelimSpacing, DelimSpan, Spacing};
@ -37,7 +36,7 @@ use rustc_session::parse::ParseSess;
use rustc_span::symbol::{kw, sym, Ident, Symbol}; use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::{Span, DUMMY_SP}; use rustc_span::{Span, DUMMY_SP};
use std::ops::Range; use std::ops::Range;
use std::{mem, slice}; use std::{fmt, mem, slice};
use thin_vec::ThinVec; use thin_vec::ThinVec;
use tracing::debug; use tracing::debug;
@ -146,7 +145,7 @@ pub struct Parser<'a> {
/// The current token. /// The current token.
pub token: Token, pub token: Token,
/// The spacing for the current token. /// The spacing for the current token.
pub token_spacing: Spacing, token_spacing: Spacing,
/// The previous token. /// The previous token.
pub prev_token: Token, pub prev_token: Token,
pub capture_cfg: bool, pub capture_cfg: bool,
@ -187,7 +186,7 @@ pub struct Parser<'a> {
current_closure: Option<ClosureSpans>, current_closure: Option<ClosureSpans>,
/// Whether the parser is allowed to do recovery. /// Whether the parser is allowed to do recovery.
/// This is disabled when parsing macro arguments, see #103534 /// This is disabled when parsing macro arguments, see #103534
pub recovery: Recovery, recovery: Recovery,
} }
// This type is used a lot, e.g. it's cloned when matching many declarative macro rules with nonterminals. Make sure // This type is used a lot, e.g. it's cloned when matching many declarative macro rules with nonterminals. Make sure
@ -197,10 +196,10 @@ rustc_data_structures::static_assert_size!(Parser<'_>, 264);
/// Stores span information about a closure. /// Stores span information about a closure.
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct ClosureSpans { struct ClosureSpans {
pub whole_closure: Span, whole_closure: Span,
pub closing_pipe: Span, closing_pipe: Span,
pub body: Span, body: Span,
} }
/// Indicates a range of tokens that should be replaced by /// Indicates a range of tokens that should be replaced by
@ -220,13 +219,13 @@ pub struct ClosureSpans {
/// the first macro inner attribute to invoke a proc-macro). /// the first macro inner attribute to invoke a proc-macro).
/// When create a `TokenStream`, the inner attributes get inserted /// When create a `TokenStream`, the inner attributes get inserted
/// into the proper place in the token stream. /// into the proper place in the token stream.
pub type ReplaceRange = (Range<u32>, Vec<(FlatToken, Spacing)>); type ReplaceRange = (Range<u32>, Vec<(FlatToken, Spacing)>);
/// Controls how we capture tokens. Capturing can be expensive, /// Controls how we capture tokens. Capturing can be expensive,
/// so we try to avoid performing capturing in cases where /// so we try to avoid performing capturing in cases where
/// we will never need an `AttrTokenStream`. /// we will never need an `AttrTokenStream`.
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
pub enum Capturing { enum Capturing {
/// We aren't performing any capturing - this is the default mode. /// We aren't performing any capturing - this is the default mode.
No, No,
/// We are capturing tokens /// We are capturing tokens
@ -374,13 +373,13 @@ pub enum FollowedByType {
} }
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
pub enum Trailing { enum Trailing {
No, No,
Yes, Yes,
} }
#[derive(Clone, Copy, Debug, PartialEq, Eq)] #[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TokenDescription { pub(super) enum TokenDescription {
ReservedIdentifier, ReservedIdentifier,
Keyword, Keyword,
ReservedKeyword, ReservedKeyword,
@ -388,7 +387,7 @@ pub enum TokenDescription {
} }
impl TokenDescription { impl TokenDescription {
pub fn from_token(token: &Token) -> Option<Self> { pub(super) fn from_token(token: &Token) -> Option<Self> {
match token.kind { match token.kind {
_ if token.is_special_ident() => Some(TokenDescription::ReservedIdentifier), _ if token.is_special_ident() => Some(TokenDescription::ReservedIdentifier),
_ if token.is_used_keyword() => Some(TokenDescription::Keyword), _ if token.is_used_keyword() => Some(TokenDescription::Keyword),
@ -502,7 +501,7 @@ impl<'a> Parser<'a> {
/// Expect next token to be edible or inedible token. If edible, /// Expect next token to be edible or inedible token. If edible,
/// then consume it; if inedible, then return without consuming /// then consume it; if inedible, then return without consuming
/// anything. Signal a fatal error if next token is unexpected. /// anything. Signal a fatal error if next token is unexpected.
pub fn expect_one_of( fn expect_one_of(
&mut self, &mut self,
edible: &[TokenKind], edible: &[TokenKind],
inedible: &[TokenKind], inedible: &[TokenKind],
@ -572,7 +571,7 @@ impl<'a> Parser<'a> {
/// the main purpose of this function is to reduce the cluttering of the suggestions list /// the main purpose of this function is to reduce the cluttering of the suggestions list
/// which using the normal eat method could introduce in some cases. /// which using the normal eat method could introduce in some cases.
#[inline] #[inline]
pub fn eat_noexpect(&mut self, tok: &TokenKind) -> bool { fn eat_noexpect(&mut self, tok: &TokenKind) -> bool {
let is_present = self.check_noexpect(tok); let is_present = self.check_noexpect(tok);
if is_present { if is_present {
self.bump() self.bump()
@ -1515,7 +1514,7 @@ impl<'a> Parser<'a> {
} }
} }
pub fn collect_tokens_no_attrs<R: HasAttrs + HasTokens>( fn collect_tokens_no_attrs<R: HasAttrs + HasTokens>(
&mut self, &mut self,
f: impl FnOnce(&mut Self) -> PResult<'a, R>, f: impl FnOnce(&mut Self) -> PResult<'a, R>,
) -> PResult<'a, R> { ) -> PResult<'a, R> {
@ -1536,8 +1535,10 @@ impl<'a> Parser<'a> {
}) })
} }
// debug view of the parser's token stream, up to `{lookahead}` tokens // Debug view of the parser's token stream, up to `{lookahead}` tokens.
pub fn debug_lookahead(&self, lookahead: usize) -> impl fmt::Debug + '_ { // Only used when debugging.
#[allow(unused)]
pub(crate) fn debug_lookahead(&self, lookahead: usize) -> impl fmt::Debug + '_ {
struct DebugParser<'dbg> { struct DebugParser<'dbg> {
parser: &'dbg Parser<'dbg>, parser: &'dbg Parser<'dbg>,
lookahead: usize, lookahead: usize,
@ -1613,7 +1614,7 @@ pub(crate) fn make_unclosed_delims_error(
/// is then 'parsed' to build up an `AttrTokenStream` with nested /// is then 'parsed' to build up an `AttrTokenStream` with nested
/// `AttrTokenTree::Delimited` tokens. /// `AttrTokenTree::Delimited` tokens.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum FlatToken { enum FlatToken {
/// A token - this holds both delimiter (e.g. '{' and '}') /// A token - this holds both delimiter (e.g. '{' and '}')
/// and non-delimiter tokens /// and non-delimiter tokens
Token(Token), Token(Token),

View file

@ -20,7 +20,7 @@ use tracing::debug;
/// Specifies how to parse a path. /// Specifies how to parse a path.
#[derive(Copy, Clone, PartialEq)] #[derive(Copy, Clone, PartialEq)]
pub enum PathStyle { pub(super) enum PathStyle {
/// In some contexts, notably in expressions, paths with generic arguments are ambiguous /// In some contexts, notably in expressions, paths with generic arguments are ambiguous
/// with something else. For example, in expressions `segment < ....` can be interpreted /// with something else. For example, in expressions `segment < ....` can be interpreted
/// as a comparison and `segment ( ....` can be interpreted as a function call. /// as a comparison and `segment ( ....` can be interpreted as a function call.

View file

@ -31,7 +31,7 @@ impl<'a> Parser<'a> {
/// Parses a statement. This stops just before trailing semicolons on everything but items. /// Parses a statement. This stops just before trailing semicolons on everything but items.
/// e.g., a `StmtKind::Semi` parses to a `StmtKind::Expr`, leaving the trailing `;` unconsumed. /// e.g., a `StmtKind::Semi` parses to a `StmtKind::Expr`, leaving the trailing `;` unconsumed.
// Public for rustfmt usage. // Public for rustfmt usage.
pub fn parse_stmt(&mut self, force_collect: ForceCollect) -> PResult<'a, Option<Stmt>> { pub(super) fn parse_stmt(&mut self, force_collect: ForceCollect) -> PResult<'a, Option<Stmt>> {
Ok(self.parse_stmt_without_recovery(false, force_collect).unwrap_or_else(|e| { Ok(self.parse_stmt_without_recovery(false, force_collect).unwrap_or_else(|e| {
e.emit(); e.emit();
self.recover_stmt_(SemiColonMode::Break, BlockMode::Ignore); self.recover_stmt_(SemiColonMode::Break, BlockMode::Ignore);

View file

@ -127,7 +127,7 @@ impl<'a> Parser<'a> {
/// Parse a type suitable for a field definition. /// Parse a type suitable for a field definition.
/// The difference from `parse_ty` is that this version /// The difference from `parse_ty` is that this version
/// allows anonymous structs and unions. /// allows anonymous structs and unions.
pub fn parse_ty_for_field_def(&mut self) -> PResult<'a, P<Ty>> { pub(super) fn parse_ty_for_field_def(&mut self) -> PResult<'a, P<Ty>> {
if self.can_begin_anon_struct_or_union() { if self.can_begin_anon_struct_or_union() {
self.parse_anon_struct_or_union() self.parse_anon_struct_or_union()
} else { } else {

View file

@ -103,7 +103,7 @@ pub fn parse_meta<'a>(psess: &'a ParseSess, attr: &Attribute) -> PResult<'a, Met
}) })
} }
pub fn check_meta_bad_delim(psess: &ParseSess, span: DelimSpan, delim: Delimiter) { fn check_meta_bad_delim(psess: &ParseSess, span: DelimSpan, delim: Delimiter) {
if let Delimiter::Parenthesis = delim { if let Delimiter::Parenthesis = delim {
return; return;
} }
@ -113,7 +113,7 @@ pub fn check_meta_bad_delim(psess: &ParseSess, span: DelimSpan, delim: Delimiter
}); });
} }
pub fn check_cfg_attr_bad_delim(psess: &ParseSess, span: DelimSpan, delim: Delimiter) { pub(super) fn check_cfg_attr_bad_delim(psess: &ParseSess, span: DelimSpan, delim: Delimiter) {
if let Delimiter::Parenthesis = delim { if let Delimiter::Parenthesis = delim {
return; return;
} }
@ -133,7 +133,7 @@ fn is_attr_template_compatible(template: &AttributeTemplate, meta: &ast::MetaIte
} }
} }
pub fn check_builtin_attribute( fn check_builtin_attribute(
psess: &ParseSess, psess: &ParseSess,
attr: &Attribute, attr: &Attribute,
name: Symbol, name: Symbol,