From 49d2fd1725510fd3bf6f2937e178b1aa055ddb02 Mon Sep 17 00:00:00 2001 From: Alexander Regueiro Date: Fri, 6 Sep 2019 03:56:45 +0100 Subject: [PATCH 1/2] Aggregation of cosmetic changes made during work on REPL PRs: libsyntax --- src/libsyntax/ast.rs | 52 ++++---- src/libsyntax/attr/mod.rs | 43 +++--- src/libsyntax/ext/base.rs | 2 +- src/libsyntax/feature_gate/active.rs | 18 +-- src/libsyntax/feature_gate/builtin_attrs.rs | 2 +- src/libsyntax/feature_gate/check.rs | 4 +- src/libsyntax/mut_visit.rs | 28 ++-- src/libsyntax/mut_visit/tests.rs | 10 +- src/libsyntax/parse/attr.rs | 38 +++--- src/libsyntax/parse/diagnostics.rs | 76 +++++------ src/libsyntax/parse/lexer/tests.rs | 17 +-- src/libsyntax/parse/mod.rs | 53 ++++---- src/libsyntax/parse/parser.rs | 50 +++---- src/libsyntax/parse/parser/expr.rs | 141 ++++++++++---------- src/libsyntax/parse/parser/item.rs | 115 ++++++++-------- src/libsyntax/parse/parser/module.rs | 6 +- src/libsyntax/parse/parser/pat.rs | 4 +- src/libsyntax/parse/parser/stmt.rs | 64 ++++----- src/libsyntax/parse/tests.rs | 8 +- src/libsyntax/print/pprust.rs | 52 ++++---- src/libsyntax/print/pprust/tests.rs | 5 +- src/libsyntax/source_map.rs | 128 +++++++++--------- src/libsyntax/source_map/tests.rs | 98 ++++++++------ src/libsyntax/tests.rs | 18 +-- src/libsyntax/tokenstream.rs | 7 +- src/libsyntax/visit.rs | 23 ++-- 26 files changed, 538 insertions(+), 524 deletions(-) diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index c93e6d11ce711..bfb2db9596363 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -413,11 +413,11 @@ impl WherePredicate { #[derive(Clone, RustcEncodable, RustcDecodable, Debug)] pub struct WhereBoundPredicate { pub span: Span, - /// Any generics from a `for` binding + /// Any generics from a `for` binding. pub bound_generic_params: Vec, - /// The type being bounded + /// The type being bounded. pub bounded_ty: P, - /// Trait and lifetime bounds (`Clone+Send+'static`) + /// Trait and lifetime bounds (`Clone + Send + 'static`). pub bounds: GenericBounds, } @@ -495,15 +495,15 @@ pub enum MetaItemKind { NameValue(Lit), } -/// A Block (`{ .. }`). +/// A block (`{ .. }`). /// /// E.g., `{ .. }` as in `fn foo() { .. }`. #[derive(Clone, RustcEncodable, RustcDecodable, Debug)] pub struct Block { - /// Statements in a block + /// The statements in the block. pub stmts: Vec, pub id: NodeId, - /// Distinguishes between `unsafe { ... }` and `{ ... }` + /// Distinguishes between `unsafe { ... }` and `{ ... }`. pub rules: BlockCheckMode, pub span: Span, } @@ -908,11 +908,11 @@ pub enum MacStmtStyle { /// Local represents a `let` statement, e.g., `let : = ;`. #[derive(Clone, RustcEncodable, RustcDecodable, Debug)] pub struct Local { + pub id: NodeId, pub pat: P, pub ty: Option>, /// Initializer expression to set the value, if any. pub init: Option>, - pub id: NodeId, pub span: Span, pub attrs: ThinVec, } @@ -970,7 +970,7 @@ pub struct AnonConst { pub value: P, } -/// An expression +/// An expression. #[derive(Clone, RustcEncodable, RustcDecodable)] pub struct Expr { pub id: NodeId, @@ -984,26 +984,26 @@ pub struct Expr { static_assert_size!(Expr, 96); impl Expr { - /// Whether this expression would be valid somewhere that expects a value; for example, an `if` - /// condition. + /// Returns `true` if this expression would be valid somewhere that expects a value; + /// for example, an `if` condition. pub fn returns(&self) -> bool { if let ExprKind::Block(ref block, _) = self.node { match block.stmts.last().map(|last_stmt| &last_stmt.node) { - // implicit return + // Implicit return Some(&StmtKind::Expr(_)) => true, Some(&StmtKind::Semi(ref expr)) => { if let ExprKind::Ret(_) = expr.node { - // last statement is explicit return + // Last statement is explicit return. true } else { false } } - // This is a block that doesn't end in either an implicit or explicit return + // This is a block that doesn't end in either an implicit or explicit return. _ => false, } } else { - // This is not a block, it is a value + // This is not a block, it is a value. true } } @@ -2307,37 +2307,37 @@ impl Default for FnHeader { #[derive(Clone, RustcEncodable, RustcDecodable, Debug)] pub enum ItemKind { - /// An `extern crate` item, with optional *original* crate name if the crate was renamed. + /// An `extern crate` item, with the optional *original* crate name if the crate was renamed. /// /// E.g., `extern crate foo` or `extern crate foo_bar as foo`. ExternCrate(Option), - /// A use declaration (`use` or `pub use`) item. + /// A use declaration item (`use`). /// /// E.g., `use foo;`, `use foo::bar;` or `use foo::bar as FooBar;`. Use(P), - /// A static item (`static` or `pub static`). + /// A static item (`static`). /// /// E.g., `static FOO: i32 = 42;` or `static FOO: &'static str = "bar";`. Static(P, Mutability, P), - /// A constant item (`const` or `pub const`). + /// A constant item (`const`). /// /// E.g., `const FOO: i32 = 42;`. Const(P, P), - /// A function declaration (`fn` or `pub fn`). + /// A function declaration (`fn`). /// /// E.g., `fn foo(bar: usize) -> usize { .. }`. Fn(P, FnHeader, Generics, P), - /// A module declaration (`mod` or `pub mod`). + /// A module declaration (`mod`). /// /// E.g., `mod foo;` or `mod foo { .. }`. Mod(Mod), - /// An external module (`extern` or `pub extern`). + /// An external module (`extern`). /// /// E.g., `extern {}` or `extern "C" {}`. ForeignMod(ForeignMod), /// Module-level inline assembly (from `global_asm!()`). GlobalAsm(P), - /// A type alias (`type` or `pub type`). + /// A type alias (`type`). /// /// E.g., `type Foo = Bar;`. TyAlias(P, Generics), @@ -2345,19 +2345,19 @@ pub enum ItemKind { /// /// E.g., `type Foo = impl Bar + Boo;`. OpaqueTy(GenericBounds, Generics), - /// An enum definition (`enum` or `pub enum`). + /// An enum definition (`enum`). /// /// E.g., `enum Foo { C, D }`. Enum(EnumDef, Generics), - /// A struct definition (`struct` or `pub struct`). + /// A struct definition (`struct`). /// /// E.g., `struct Foo { x: A }`. Struct(VariantData, Generics), - /// A union definition (`union` or `pub union`). + /// A union definition (`union`). /// /// E.g., `union Foo { x: A, y: B }`. Union(VariantData, Generics), - /// A Trait declaration (`trait` or `pub trait`). + /// A trait declaration (`trait`). /// /// E.g., `trait Foo { .. }`, `trait Foo { .. }` or `auto trait Foo {}`. Trait(IsAuto, Unsafety, Generics, GenericBounds, Vec), diff --git a/src/libsyntax/attr/mod.rs b/src/libsyntax/attr/mod.rs index 69de3150354e7..1f954064944dc 100644 --- a/src/libsyntax/attr/mod.rs +++ b/src/libsyntax/attr/mod.rs @@ -1,4 +1,4 @@ -//! Functions dealing with attributes and meta items +//! Functions dealing with attributes and meta items. mod builtin; @@ -61,7 +61,7 @@ pub fn is_known_lint_tool(m_item: Ident) -> bool { } impl NestedMetaItem { - /// Returns the MetaItem if self is a NestedMetaItem::MetaItem. + /// Returns the `MetaItem` if `self` is a `NestedMetaItem::MetaItem`. pub fn meta_item(&self) -> Option<&MetaItem> { match *self { NestedMetaItem::MetaItem(ref item) => Some(item), @@ -69,7 +69,7 @@ impl NestedMetaItem { } } - /// Returns the Lit if self is a NestedMetaItem::Literal. + /// Returns the `Lit` if `self` is a `NestedMetaItem::Literal`s. pub fn literal(&self) -> Option<&Lit> { match *self { NestedMetaItem::Literal(ref lit) => Some(lit), @@ -82,7 +82,7 @@ impl NestedMetaItem { self.meta_item().map_or(false, |meta_item| meta_item.check_name(name)) } - /// For a single-segment meta-item returns its name, otherwise returns `None`. + /// For a single-segment meta item, returns its name; otherwise, returns `None`. pub fn ident(&self) -> Option { self.meta_item().and_then(|meta_item| meta_item.ident()) } @@ -90,13 +90,13 @@ impl NestedMetaItem { self.ident().unwrap_or(Ident::invalid()).name } - /// Gets the string value if self is a MetaItem and the MetaItem is a - /// MetaItemKind::NameValue variant containing a string, otherwise None. + /// Gets the string value if `self` is a `MetaItem` and the `MetaItem` is a + /// `MetaItemKind::NameValue` variant containing a string, otherwise `None`. pub fn value_str(&self) -> Option { self.meta_item().and_then(|meta_item| meta_item.value_str()) } - /// Returns a name and single literal value tuple of the MetaItem. + /// Returns a name and single literal value tuple of the `MetaItem`. pub fn name_value_literal(&self) -> Option<(Name, &Lit)> { self.meta_item().and_then( |meta_item| meta_item.meta_item_list().and_then( @@ -112,32 +112,32 @@ impl NestedMetaItem { })) } - /// Gets a list of inner meta items from a list MetaItem type. + /// Gets a list of inner meta items from a list `MetaItem` type. pub fn meta_item_list(&self) -> Option<&[NestedMetaItem]> { self.meta_item().and_then(|meta_item| meta_item.meta_item_list()) } - /// Returns `true` if the variant is MetaItem. + /// Returns `true` if the variant is `MetaItem`. pub fn is_meta_item(&self) -> bool { self.meta_item().is_some() } - /// Returns `true` if the variant is Literal. + /// Returns `true` if the variant is `Literal`. pub fn is_literal(&self) -> bool { self.literal().is_some() } - /// Returns `true` if self is a MetaItem and the meta item is a word. + /// Returns `true` if `self` is a `MetaItem` and the meta item is a word. pub fn is_word(&self) -> bool { self.meta_item().map_or(false, |meta_item| meta_item.is_word()) } - /// Returns `true` if self is a MetaItem and the meta item is a ValueString. + /// Returns `true` if `self` is a `MetaItem` and the meta item is a `ValueString`. pub fn is_value_str(&self) -> bool { self.value_str().is_some() } - /// Returns `true` if self is a MetaItem and the meta item is a list. + /// Returns `true` if `self` is a `MetaItem` and the meta item is a list. pub fn is_meta_item_list(&self) -> bool { self.meta_item_list().is_some() } @@ -156,7 +156,7 @@ impl Attribute { matches } - /// For a single-segment attribute returns its name, otherwise returns `None`. + /// For a single-segment attribute, returns its name; otherwise, returns `None`. pub fn ident(&self) -> Option { if self.path.segments.len() == 1 { Some(self.path.segments[0].ident) @@ -187,14 +187,14 @@ impl Attribute { self.meta_item_list().is_some() } - /// Indicates if the attribute is a Value String. + /// Indicates if the attribute is a `ValueString`. pub fn is_value_str(&self) -> bool { self.value_str().is_some() } } impl MetaItem { - /// For a single-segment meta-item returns its name, otherwise returns `None`. + /// For a single-segment meta item, returns its name; otherwise, returns `None`. pub fn ident(&self) -> Option { if self.path.segments.len() == 1 { Some(self.path.segments[0].ident) @@ -206,8 +206,9 @@ impl MetaItem { self.ident().unwrap_or(Ident::invalid()).name } - // #[attribute(name = "value")] - // ^^^^^^^^^^^^^^ + // Example: + // #[attribute(name = "value")] + // ^^^^^^^^^^^^^^ pub fn name_value_literal(&self) -> Option<&Lit> { match &self.node { MetaItemKind::NameValue(v) => Some(v), @@ -255,7 +256,7 @@ impl MetaItem { } impl Attribute { - /// Extracts the MetaItem from inside this Attribute. + /// Extracts the `MetaItem` from inside this `Attribute`. pub fn meta(&self) -> Option { let mut tokens = self.tokens.trees().peekable(); Some(MetaItem { @@ -318,8 +319,8 @@ impl Attribute { }) } - /// Converts self to a normal #[doc="foo"] comment, if it is a - /// comment like `///` or `/** */`. (Returns self unchanged for + /// 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(&self, f: F) -> T where F: FnOnce(&Attribute) -> T, diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index 5b2515d20cbab..c4569b3fba1be 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -958,7 +958,7 @@ impl<'a> ExtCtxt<'a> { self.resolver.check_unused_macros(); } - /// Resolve a path mentioned inside Rust code. + /// Resolves a path mentioned inside Rust code. /// /// This unifies the logic used for resolving `include_X!`, and `#[doc(include)]` file paths. /// diff --git a/src/libsyntax/feature_gate/active.rs b/src/libsyntax/feature_gate/active.rs index c947b09fdcb57..a15fc050141a3 100644 --- a/src/libsyntax/feature_gate/active.rs +++ b/src/libsyntax/feature_gate/active.rs @@ -1,9 +1,11 @@ //! List of the active feature gates. +use super::{State, Feature}; + use crate::edition::Edition; use crate::symbol::{Symbol, sym}; + use syntax_pos::Span; -use super::{State, Feature}; macro_rules! set { ($field: ident) => {{ @@ -37,9 +39,9 @@ macro_rules! declare_features { /// A set of features to be used by later passes. #[derive(Clone)] pub struct Features { - /// `#![feature]` attrs for language features, for error reporting + /// `#![feature]` attrs for language features, for error reporting. pub declared_lang_features: Vec<(Symbol, Span, Option)>, - /// `#![feature]` attrs for non-language (library) features + /// `#![feature]` attrs for non-language (library) features. pub declared_lib_features: Vec<(Symbol, Span)>, $( $(#[doc = $doc])* @@ -66,11 +68,11 @@ macro_rules! declare_features { } impl Feature { - /// Set this feature in `Features`. Panics if called on a non-active feature. + /// Sets this feature in `Features`. Panics if called on a non-active feature. pub fn set(&self, features: &mut Features, span: Span) { match self.state { State::Active { set } => set(features, span), - _ => panic!("Called `set` on feature `{}` which is not `active`", self.name) + _ => panic!("called `set` on feature `{}` which is not `active`", self.name) } } } @@ -478,7 +480,7 @@ declare_features! ( (active, precise_pointer_size_matching, "1.32.0", Some(56354), None), /// Allows relaxing the coherence rules such that - /// `impl ForeignTrait for ForeignType is permitted. + /// `impl ForeignTrait for ForeignType` is permitted. (active, re_rebalance_coherence, "1.32.0", Some(55437), None), /// Allows using `#[ffi_returns_twice]` on foreign functions. @@ -520,7 +522,7 @@ declare_features! ( /// Allows `async || body` closures. (active, async_closure, "1.37.0", Some(62290), None), - /// Allows the use of `#[cfg(doctest)]`, set when rustdoc is collecting doctests + /// Allows the use of `#[cfg(doctest)]`; set when rustdoc is collecting doctests. (active, cfg_doctest, "1.37.0", Some(62210), None), /// Allows `[x; N]` where `x` is a constant (RFC 2203). @@ -529,7 +531,7 @@ declare_features! ( /// Allows `impl Trait` to be used inside type aliases (RFC 2515). (active, type_alias_impl_trait, "1.38.0", Some(63063), None), - /// Allows the use of or-patterns, e.g. `0 | 1`. + /// Allows the use of or-patterns (e.g., `0 | 1`). (active, or_patterns, "1.38.0", Some(54883), None), // ------------------------------------------------------------------------- diff --git a/src/libsyntax/feature_gate/builtin_attrs.rs b/src/libsyntax/feature_gate/builtin_attrs.rs index ee7ac3b15d955..763c3ffd782df 100644 --- a/src/libsyntax/feature_gate/builtin_attrs.rs +++ b/src/libsyntax/feature_gate/builtin_attrs.rs @@ -169,7 +169,7 @@ const INTERAL_UNSTABLE: &str = "this is an internal attribute that will never be pub type BuiltinAttribute = (Symbol, AttributeType, AttributeTemplate, AttributeGate); -/// Attributes that have a special meaning to rustc or rustdoc +/// Attributes that have a special meaning to rustc or rustdoc. pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[ // ========================================================================== // Stable attributes: diff --git a/src/libsyntax/feature_gate/check.rs b/src/libsyntax/feature_gate/check.rs index f3a9d135125ae..5711b269ff092 100644 --- a/src/libsyntax/feature_gate/check.rs +++ b/src/libsyntax/feature_gate/check.rs @@ -920,9 +920,9 @@ pub enum UnstableFeatures { impl UnstableFeatures { pub fn from_environment() -> UnstableFeatures { - // Whether this is a feature-staged build, i.e., on the beta or stable channel + // `true` if this is a feature-staged build, i.e., on the beta or stable channel. let disable_unstable_features = option_env!("CFG_DISABLE_UNSTABLE_FEATURES").is_some(); - // Whether we should enable unstable features for bootstrapping + // `true` if we should enable unstable features for bootstrapping. let bootstrap = env::var("RUSTC_BOOTSTRAP").is_ok(); match (disable_unstable_features, bootstrap) { (_, true) => UnstableFeatures::Cheat, diff --git a/src/libsyntax/mut_visit.rs b/src/libsyntax/mut_visit.rs index 6023c5149d05b..1c35688666836 100644 --- a/src/libsyntax/mut_visit.rs +++ b/src/libsyntax/mut_visit.rs @@ -1,10 +1,10 @@ -//! A MutVisitor represents an AST modification; it accepts an AST piece and -//! and mutates it in place. So, for instance, macro expansion is a MutVisitor +//! A `MutVisitor` represents an AST modification; it accepts an AST piece and +//! and mutates it in place. So, for instance, macro expansion is a `MutVisitor` //! that walks over an AST and modifies it. //! -//! Note: using a MutVisitor (other than the MacroExpander MutVisitor) on +//! Note: using a `MutVisitor` (other than the `MacroExpander` `MutVisitor`) on //! an AST before macro expansion is probably a bad idea. For instance, -//! a MutVisitor renaming item names in a module will miss all of those +//! a `MutVisitor` renaming item names in a module will miss all of those //! that are created by the expansion of a macro. use crate::ast::*; @@ -614,7 +614,7 @@ pub fn noop_visit_tts(TokenStream(tts): &mut TokenStream, vis: &m }) } -// Apply ident visitor if it's an ident, apply other visits to interpolated nodes. +// Applies ident visitor if it's an ident; applies other visits to interpolated nodes. // In practice the ident part is not actually used by specific visitors right now, // but there's a test below checking that it works. pub fn noop_visit_token(t: &mut Token, vis: &mut T) { @@ -625,7 +625,7 @@ pub fn noop_visit_token(t: &mut Token, vis: &mut T) { vis.visit_ident(&mut ident); *name = ident.name; *span = ident.span; - return; // avoid visiting the span for the second time + return; // Avoid visiting the span for the second time. } token::Interpolated(nt) => { let mut nt = Lrc::make_mut(nt); @@ -636,28 +636,28 @@ pub fn noop_visit_token(t: &mut Token, vis: &mut T) { vis.visit_span(span); } -/// Apply visitor to elements of interpolated nodes. +/// Applies the visitor to elements of interpolated nodes. // // N.B., this can occur only when applying a visitor to partially expanded // code, where parsed pieces have gotten implanted ito *other* macro // invocations. This is relevant for macro hygiene, but possibly not elsewhere. // // One problem here occurs because the types for flat_map_item, flat_map_stmt, -// etc. allow the visitor to return *multiple* items; this is a problem for the +// etc., allow the visitor to return *multiple* items; this is a problem for the // nodes here, because they insist on having exactly one piece. One solution // would be to mangle the MutVisitor trait to include one-to-many and // one-to-one versions of these entry points, but that would probably confuse a // lot of people and help very few. Instead, I'm just going to put in dynamic // checks. I think the performance impact of this will be pretty much -// nonexistent. The danger is that someone will apply a MutVisitor to a +// nonexistent. The danger is that someone will apply a `MutVisitor` to a // partially expanded node, and will be confused by the fact that their -// "flat_map_item" or "flat_map_stmt" isn't getting called on NtItem or NtStmt +// `flat_map_item` or `flat_map_stmt` isn't getting called on `NtItem` or `NtStmt` // nodes. Hopefully they'll wind up reading this comment, and doing something // appropriate. // -// BTW, design choice: I considered just changing the type of, e.g., NtItem to +// BTW, design choice: I considered just changing the type of, e.g., `NtItem` to // contain multiple items, but decided against it when I looked at -// parse_item_or_view_item and tried to figure out what I would do with +// `parse_item_or_view_item` and tried to figure out what I would do with // multiple items there.... pub fn noop_visit_interpolated(nt: &mut token::Nonterminal, vis: &mut T) { match nt { @@ -1014,7 +1014,7 @@ pub fn noop_visit_crate(krate: &mut Crate, vis: &mut T) { }); } -// Mutate one item into possibly many items. +// Mutates one item into possibly many items. pub fn noop_flat_map_item(mut item: P, visitor: &mut T) -> SmallVec<[P; 1]> { let Item { ident, attrs, id, node, vis, span, tokens: _ } = item.deref_mut(); @@ -1224,7 +1224,7 @@ pub fn noop_visit_expr(Expr { node, id, span, attrs }: &mut Expr, ExprKind::Paren(expr) => { vis.visit_expr(expr); - // Nodes that are equal modulo `Paren` sugar no-ops should have the same ids. + // Nodes that are equal modulo `Paren` sugar no-ops should have the same IDs. *id = expr.id; vis.visit_span(span); visit_thin_attrs(attrs, vis); diff --git a/src/libsyntax/mut_visit/tests.rs b/src/libsyntax/mut_visit/tests.rs index 6868736976b25..f779e0d0a6014 100644 --- a/src/libsyntax/mut_visit/tests.rs +++ b/src/libsyntax/mut_visit/tests.rs @@ -6,13 +6,13 @@ use crate::print::pprust; use crate::mut_visit; use crate::with_default_globals; -// this version doesn't care about getting comments or docstrings in. +// This version doesn't care about getting comments or doc-strings in. fn fake_print_crate(s: &mut pprust::State<'_>, krate: &ast::Crate) { s.print_mod(&krate.module, &krate.attrs) } -// change every identifier to "zz" +// Change every identifier to "zz". struct ToZzIdentMutVisitor; impl MutVisitor for ToZzIdentMutVisitor { @@ -24,7 +24,7 @@ impl MutVisitor for ToZzIdentMutVisitor { } } -// maybe add to expand.rs... +// Maybe add to `expand.rs`. macro_rules! assert_pred { ($pred:expr, $predname:expr, $a:expr , $b:expr) => ( { @@ -39,7 +39,7 @@ macro_rules! assert_pred { ) } -// make sure idents get transformed everywhere +// Make sure idents get transformed everywhere. #[test] fn ident_transformation () { with_default_globals(|| { let mut zz_visitor = ToZzIdentMutVisitor; @@ -54,7 +54,7 @@ macro_rules! assert_pred { }) } -// even inside macro defs.... +// Make sure idents get transformed even inside macro defs. #[test] fn ident_transformation_in_defs () { with_default_globals(|| { let mut zz_visitor = ToZzIdentMutVisitor; diff --git a/src/libsyntax/parse/attr.rs b/src/libsyntax/parse/attr.rs index d9c4baad49ded..9aa1ec0b14fe9 100644 --- a/src/libsyntax/parse/attr.rs +++ b/src/libsyntax/parse/attr.rs @@ -26,7 +26,7 @@ impl<'a> Parser<'a> { Ok(attrs) } - /// Parse attributes that appear before an item + /// Parses attributes that appear before an item. crate fn parse_outer_attributes(&mut self) -> PResult<'a, Vec> { let mut attrs: Vec = Vec::new(); let mut just_parsed_doc_comment = false; @@ -69,10 +69,10 @@ impl<'a> Parser<'a> { Ok(attrs) } - /// Matches `attribute = # ! [ meta_item ]` + /// Matches `attribute = # ! [ meta_item ]`. /// - /// If permit_inner is true, then a leading `!` indicates an inner - /// attribute + /// 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> { debug!("parse_attribute: permit_inner={:?} self.token={:?}", permit_inner, @@ -167,14 +167,14 @@ impl<'a> Parser<'a> { }) } - /// Parse an inner part of attribute - path and following tokens. + /// Parses an inner part of an attribute (the path and following tokens). /// The tokens must be either a delimited token stream, or empty token stream, /// or the "legacy" key-value form. - /// PATH `(` TOKEN_STREAM `)` - /// PATH `[` TOKEN_STREAM `]` - /// PATH `{` TOKEN_STREAM `}` - /// PATH - /// PATH `=` TOKEN_TREE + /// PATH `(` TOKEN_STREAM `)` + /// PATH `[` TOKEN_STREAM `]` + /// PATH `{` TOKEN_STREAM `}` + /// PATH + /// PATH `=` TOKEN_TREE /// The delimiters or `=` are still put into the resulting token stream. pub fn parse_meta_item_unrestricted(&mut self) -> PResult<'a, (ast::Path, TokenStream)> { let meta = match self.token.kind { @@ -217,11 +217,11 @@ impl<'a> Parser<'a> { }) } - /// Parse attributes that appear after the opening of an item. These should + /// Parses attributes that appear after the opening of an item. These should /// be preceded by an exclamation mark, but we accept and warn about one /// terminated by a semicolon. - - /// matches inner_attrs* + /// + /// Matches `inner_attrs*`. crate fn parse_inner_attributes(&mut self) -> PResult<'a, Vec> { let mut attrs: Vec = vec![]; loop { @@ -237,7 +237,7 @@ impl<'a> Parser<'a> { attrs.push(attr); } token::DocComment(s) => { - // we need to get the position of this token before we bump. + // We need to get the position of this token before we bump. let attr = attr::mk_sugared_doc_attr(s, self.token.span); if attr.style == ast::AttrStyle::Inner { attrs.push(attr); @@ -268,10 +268,10 @@ impl<'a> Parser<'a> { Ok(lit) } - /// Per RFC#1559, matches the following grammar: + /// Matches the following grammar (per RFC 1559). /// - /// meta_item : IDENT ( '=' UNSUFFIXED_LIT | '(' meta_item_inner? ')' )? ; - /// meta_item_inner : (meta_item | UNSUFFIXED_LIT) (',' meta_item_inner)? ; + /// meta_item : IDENT ( '=' UNSUFFIXED_LIT | '(' meta_item_inner? ')' )? ; + /// meta_item_inner : (meta_item | UNSUFFIXED_LIT) (',' meta_item_inner)? ; pub fn parse_meta_item(&mut self) -> PResult<'a, ast::MetaItem> { let nt_meta = match self.token.kind { token::Interpolated(ref nt) => match **nt { @@ -303,7 +303,7 @@ impl<'a> Parser<'a> { }) } - /// matches meta_item_inner : (meta_item | UNSUFFIXED_LIT) ; + /// Matches `meta_item_inner : (meta_item | UNSUFFIXED_LIT) ;`. fn parse_meta_item_inner(&mut self) -> PResult<'a, ast::NestedMetaItem> { match self.parse_unsuffixed_lit() { Ok(lit) => { @@ -324,7 +324,7 @@ impl<'a> Parser<'a> { Err(self.diagnostic().struct_span_err(self.token.span, &msg)) } - /// matches meta_seq = ( COMMASEP(meta_item_inner) ) + /// Matches `meta_seq = ( COMMASEP(meta_item_inner) )`. fn parse_meta_seq(&mut self) -> PResult<'a, Vec> { self.parse_seq_to_end(&token::CloseDelim(token::Paren), SeqSep::trailing_allowed(token::Comma), diff --git a/src/libsyntax/parse/diagnostics.rs b/src/libsyntax/parse/diagnostics.rs index d050d4f4ce705..2890a8e721e65 100644 --- a/src/libsyntax/parse/diagnostics.rs +++ b/src/libsyntax/parse/diagnostics.rs @@ -240,7 +240,7 @@ impl<'a> Parser<'a> { ) -> PResult<'a, bool /* recovered */> { fn tokens_to_string(tokens: &[TokenType]) -> String { let mut i = tokens.iter(); - // This might be a sign we need a connect method on Iterator. + // This might be a sign we need a connect method on `Iterator`. let b = i.next() .map_or(String::new(), |t| t.to_string()); i.enumerate().fold(b, |mut b, (i, a)| { @@ -301,7 +301,7 @@ impl<'a> Parser<'a> { ); } let sp = if self.token == token::Eof { - // This is EOF, don't want to point at the following char, but rather the last token + // This is EOF; don't want to point at the following char, but rather the last token. self.prev_span } else { label_sp @@ -317,9 +317,9 @@ impl<'a> Parser<'a> { } let is_semi_suggestable = expected.iter().any(|t| match t { - TokenType::Token(token::Semi) => true, // we expect a `;` here + TokenType::Token(token::Semi) => true, // We expect a `;` here. _ => false, - }) && ( // a `;` would be expected before the current keyword + }) && ( // A `;` would be expected before the current keyword. self.token.is_keyword(kw::Break) || self.token.is_keyword(kw::Continue) || self.token.is_keyword(kw::For) || @@ -541,16 +541,16 @@ impl<'a> Parser<'a> { } } - /// Produce an error if comparison operators are chained (RFC #558). - /// We only need to check lhs, not rhs, because all comparison ops - /// have same precedence and are left-associative + /// Produces an error if comparison operators are chained (RFC #558). + /// We only need to check the LHS, not the RHS, because all comparison ops + /// have same precedence and are left-associative. crate fn check_no_chained_comparison(&self, lhs: &Expr, outer_op: &AssocOp) -> PResult<'a, ()> { debug_assert!(outer_op.is_comparison(), "check_no_chained_comparison: {:?} is not comparison", outer_op); match lhs.node { ExprKind::Binary(op, _, _) if op.node.is_comparison() => { - // respan to include both operators + // Respan to include both operators. let op_span = op.span.to(self.token.span); let mut err = self.struct_span_err( op_span, @@ -691,9 +691,9 @@ impl<'a> Parser<'a> { Ok(()) } - /// Try to recover from associated item paths like `[T]::AssocItem`/`(T, U)::AssocItem`. - /// Attempt to convert the base expression/pattern/type into a type, parse the `::AssocItem` - /// tail, and combine them into a `::AssocItem` expression/pattern/type. + /// Tries to recover from associated item paths like `[T]::AssocItem` / `(T, U)::AssocItem`. + /// Attempts to convert the base expression/pattern/type into a type, parses the `::AssocItem` + /// tail, and combines them into a `::AssocItem` expression/pattern/type. crate fn maybe_recover_from_bad_qpath( &mut self, base: P, @@ -708,8 +708,8 @@ impl<'a> Parser<'a> { Ok(base) } - /// Given an already parsed `Ty` parse the `::AssocItem` tail and - /// combine them into a `::AssocItem` expression/pattern/type. + /// Given an already parsed `Ty`, parses the `::AssocItem` tail and + /// combines them into a `::AssocItem` expression/pattern/type. crate fn maybe_recover_from_bad_qpath_stage_2( &mut self, ty_span: Span, @@ -730,7 +730,7 @@ impl<'a> Parser<'a> { self.diagnostic() .struct_span_err(path.span, "missing angle brackets in associated item path") .span_suggestion( - // this is a best-effort recovery + // This is a best-effort recovery. path.span, "try", format!("<{}>::{}", ty_str, path), @@ -738,7 +738,7 @@ impl<'a> Parser<'a> { ) .emit(); - let path_span = ty_span.shrink_to_hi(); // use an empty path since `position` == 0 + let path_span = ty_span.shrink_to_hi(); // Use an empty path since `position == 0`. Ok(P(T::recovered( Some(QSelf { ty, @@ -761,8 +761,8 @@ impl<'a> Parser<'a> { if !items.is_empty() { let previous_item = &items[items.len() - 1]; let previous_item_kind_name = match previous_item.node { - // say "braced struct" because tuple-structs and - // braceless-empty-struct declarations do take a semicolon + // Say "braced struct" because tuple-structs and + // braceless-empty-struct declarations do take a semicolon. ItemKind::Struct(..) => Some("braced struct"), ItemKind::Enum(..) => Some("enum"), ItemKind::Trait(..) => Some("trait"), @@ -783,7 +783,7 @@ impl<'a> Parser<'a> { } } - /// Create a `DiagnosticBuilder` for an unexpected token `t` and try to recover if it is a + /// Creates a `DiagnosticBuilder` for an unexpected token `t` and tries to recover if it is a /// closing delimiter. pub fn unexpected_try_recover( &mut self, @@ -841,7 +841,7 @@ impl<'a> Parser<'a> { extern_sp: Span, ) -> PResult<'a, ()> { if self.token != token::Semi { - // this might be an incorrect fn definition (#62109) + // This might be an incorrect fn definition (#62109). let parser_snapshot = self.clone(); match self.parse_inner_attrs_and_block() { Ok((_, body)) => { @@ -871,7 +871,7 @@ impl<'a> Parser<'a> { Ok(()) } - /// Consume alternative await syntaxes like `await!()`, `await `, + /// Consumes alternative await syntaxes like `await!()`, `await `, /// `await? `, `await()`, and `await { }`. crate fn parse_incorrect_await_syntax( &mut self, @@ -924,7 +924,7 @@ impl<'a> Parser<'a> { sp } - /// If encountering `future.await()`, consume and emit error. + /// If encountering `future.await()`, consumes and emits an error. crate fn recover_from_await_method_call(&mut self) { if self.token == token::OpenDelim(token::Paren) && self.look_ahead(1, |t| t == &token::CloseDelim(token::Paren)) @@ -944,7 +944,7 @@ impl<'a> Parser<'a> { } } - /// Recover a situation like `for ( $pat in $expr )` + /// Recovers a situation like `for ( $pat in $expr )` /// and suggest writing `for $pat in $expr` instead. /// /// This should be called before parsing the `$block`. @@ -1010,7 +1010,7 @@ impl<'a> Parser<'a> { Ok(x) => x, Err(mut err) => { err.emit(); - // recover from parse error + // Recover from parse error. self.consume_block(delim); self.mk_expr(lo.to(self.prev_span), ExprKind::Err, ThinVec::new()) } @@ -1023,7 +1023,7 @@ impl<'a> Parser<'a> { mut err: DiagnosticBuilder<'a>, ) -> PResult<'a, bool> { let mut pos = None; - // we want to use the last closing delim that would apply + // We want to use the last closing delim that would apply. for (i, unmatched) in self.unclosed_delims.iter().enumerate().rev() { if tokens.contains(&token::CloseDelim(unmatched.expected_delim)) && Some(self.token.span) > unmatched.unclosed_span @@ -1041,7 +1041,7 @@ impl<'a> Parser<'a> { let unmatched = self.unclosed_delims.remove(pos); let delim = TokenType::Token(token::CloseDelim(unmatched.expected_delim)); - // We want to suggest the inclusion of the closing delimiter where it makes + // We want to suggest the inclusion of the closing delimiter where it makes // the most sense, which is immediately after the last token: // // {foo(bar {}} @@ -1067,7 +1067,7 @@ impl<'a> Parser<'a> { } } - /// Recover from `pub` keyword in places where it seems _reasonable_ but isn't valid. + /// Recovers from `pub` keyword in places where it seems _reasonable_ but isn't valid. crate fn eat_bad_pub(&mut self) { if self.token.is_keyword(kw::Pub) { match self.parse_visibility(false) { @@ -1082,21 +1082,21 @@ impl<'a> Parser<'a> { } } - // Eat tokens until we can be relatively sure we reached the end of the - // statement. This is something of a best-effort heuristic. - // - // We terminate when we find an unmatched `}` (without consuming it). - crate fn recover_stmt(&mut self) { + /// Eats tokens until we can be relatively sure we reached the end of the + /// statement. This is something of a best-effort heuristic. + /// + /// We terminate when we find an unmatched `}` (without consuming it). + pub fn recover_stmt(&mut self) { self.recover_stmt_(SemiColonMode::Ignore, BlockMode::Ignore) } - // If `break_on_semi` is `Break`, then we will stop consuming tokens after - // finding (and consuming) a `;` outside of `{}` or `[]` (note that this is - // approximate - it can mean we break too early due to macros, but that - // should only lead to sub-optimal recovery, not inaccurate parsing). - // - // If `break_on_block` is `Break`, then we will stop consuming tokens - // after finding (and consuming) a brace-delimited block. + /// If `break_on_semi` is `Break`, then we will stop consuming tokens after + /// finding (and consuming) a `;` outside of `{}` or `[]` (note that this is + /// approximate -- it can mean we break too early due to macros, but that + /// should only lead to sub-optimal recovery, not inaccurate parsing). + /// + /// If `break_on_block` is `Break`, then we will stop consuming tokens + /// after finding (and consuming) a brace-delimited block. crate fn recover_stmt_(&mut self, break_on_semi: SemiColonMode, break_on_block: BlockMode) { let mut brace_depth = 0; let mut bracket_depth = 0; diff --git a/src/libsyntax/parse/lexer/tests.rs b/src/libsyntax/parse/lexer/tests.rs index 652ae95c85349..d965bf28ee7df 100644 --- a/src/libsyntax/parse/lexer/tests.rs +++ b/src/libsyntax/parse/lexer/tests.rs @@ -4,9 +4,10 @@ use crate::symbol::Symbol; use crate::source_map::{SourceMap, FilePathMapping}; use crate::parse::token; use crate::with_default_globals; + +use errors::{Handler, emitter::EmitterWriter}; use std::io; use std::path::PathBuf; -use errors::{Handler, emitter::EmitterWriter}; use syntax_pos::{BytePos, Span}; fn mk_sess(sm: Lrc) -> ParseSess { @@ -21,7 +22,7 @@ fn mk_sess(sm: Lrc) -> ParseSess { ParseSess::with_span_handler(Handler::with_emitter(true, None, Box::new(emitter)), sm) } -// open a string reader for the given string +// Creates a string reader for the given string. fn setup<'a>(sm: &SourceMap, sess: &'a ParseSess, teststr: String) @@ -38,7 +39,7 @@ fn t1() { let mut string_reader = setup( &sm, &sh, - "/* my source file */ fn main() { println!(\"zebra\"); }\n".to_string(), + "/* my source file */ fn main() { println!(\"zebra\"); }\n".to_owned(), ); assert_eq!(string_reader.next_token(), token::Comment); assert_eq!(string_reader.next_token(), token::Whitespace); @@ -50,7 +51,7 @@ fn t1() { assert_eq!(tok1.kind, tok2.kind); assert_eq!(tok1.span, tok2.span); assert_eq!(string_reader.next_token(), token::Whitespace); - // read another token: + // Read another token. let tok3 = string_reader.next_token(); assert_eq!(string_reader.pos.clone(), BytePos(28)); let tok4 = Token::new( @@ -65,15 +66,15 @@ fn t1() { }) } -// check that the given reader produces the desired stream -// of tokens (stop checking after exhausting the expected vec) +// Checks that the given reader produces the desired stream +// of tokens (stop checking after exhausting `expected`). fn check_tokenization(mut string_reader: StringReader<'_>, expected: Vec) { for expected_tok in &expected { assert_eq!(&string_reader.next_token(), expected_tok); } } -// make the identifier by looking up the string in the interner +// Makes the identifier by looking up the string in the interner. fn mk_ident(id: &str) -> TokenKind { token::Ident(Symbol::intern(id), false) } @@ -201,7 +202,7 @@ fn literal_suffixes() { setup(&sm, &sh, format!("{}suffix", $input)).next_token(), mk_lit(token::$tok_type, $tok_contents, Some("suffix")), ); - // with a whitespace separator: + // with a whitespace separator assert_eq!( setup(&sm, &sh, format!("{} suffix", $input)).next_token(), mk_lit(token::$tok_type, $tok_contents, None), diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index b1af4806e2d78..aa57c3954e352 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -13,12 +13,12 @@ use crate::print::pprust; use crate::symbol::Symbol; use errors::{Applicability, FatalError, Level, Handler, ColorConfig, Diagnostic, DiagnosticBuilder}; +use rustc_data_structures::fx::{FxHashSet, FxHashMap}; use rustc_data_structures::sync::{Lrc, Lock, Once}; use syntax_pos::{Span, SourceFile, FileName, MultiSpan}; use syntax_pos::edition::Edition; use syntax_pos::hygiene::ExpnId; -use rustc_data_structures::fx::{FxHashSet, FxHashMap}; use std::borrow::Cow; use std::path::{Path, PathBuf}; use std::str; @@ -81,25 +81,27 @@ pub struct ParseSess { impl ParseSess { pub fn new(file_path_mapping: FilePathMapping) -> Self { let cm = Lrc::new(SourceMap::new(file_path_mapping)); - let handler = Handler::with_tty_emitter(ColorConfig::Auto, - true, - None, - Some(cm.clone())); + let handler = Handler::with_tty_emitter( + ColorConfig::Auto, + true, + None, + Some(cm.clone()), + ); ParseSess::with_span_handler(handler, cm) } - pub fn with_span_handler(handler: Handler, source_map: Lrc) -> ParseSess { - ParseSess { + pub fn with_span_handler(handler: Handler, source_map: Lrc) -> Self { + Self { span_diagnostic: handler, unstable_features: UnstableFeatures::from_environment(), config: FxHashSet::default(), + edition: ExpnId::root().expn_data().edition, missing_fragment_specifiers: Lock::new(FxHashSet::default()), raw_identifier_spans: Lock::new(Vec::new()), registered_diagnostics: Lock::new(ErrorMap::new()), included_mod_stack: Lock::new(vec![]), source_map, buffered_lints: Lock::new(vec![]), - edition: ExpnId::root().expn_data().edition, ambiguous_block_expr_parse: Lock::new(FxHashMap::default()), injected_crate_name: Once::new(), gated_spans: GatedSpans::default(), @@ -155,17 +157,17 @@ pub struct Directory<'a> { #[derive(Copy, Clone)] pub enum DirectoryOwnership { Owned { - // None if `mod.rs`, `Some("foo")` if we're in `foo.rs` + // None if `mod.rs`, `Some("foo")` if we're in `foo.rs`. relative: Option, }, UnownedViaBlock, UnownedViaMod(bool /* legacy warnings? */), } -// a bunch of utility functions of the form parse__from_ +// A bunch of utility functions of the form `parse__from_` // where includes crate, expr, item, stmt, tts, and one that // uses a HOF to parse anything, and includes file and -// source_str. +// `source_str`. pub fn parse_crate_from_file<'a>(input: &Path, sess: &'a ParseSess) -> PResult<'a, ast::Crate> { let mut parser = new_parser_from_file(sess, input); @@ -219,14 +221,13 @@ pub fn maybe_new_parser_from_source_str(sess: &ParseSess, name: FileName, source Ok(parser) } -/// Creates a new parser, handling errors as appropriate -/// if the file doesn't exist +/// Creates a new parser, handling errors as appropriate if the file doesn't exist. pub fn new_parser_from_file<'a>(sess: &'a ParseSess, path: &Path) -> Parser<'a> { source_file_to_parser(sess, file_to_source_file(sess, path, None)) } -/// Creates a new parser, returning buffered diagnostics if the file doesn't -/// exist or from lexing the initial token stream. +/// Creates a new parser, returning buffered diagnostics if the file doesn't exist, +/// or from lexing the initial token stream. pub fn maybe_new_parser_from_file<'a>(sess: &'a ParseSess, path: &Path) -> Result, Vec> { let file = try_file_to_source_file(sess, path, None).map_err(|db| vec![db])?; @@ -234,8 +235,8 @@ pub fn maybe_new_parser_from_file<'a>(sess: &'a ParseSess, path: &Path) } /// Given a session, a crate config, a path, and a span, add -/// the file at the given path to the source_map, and return a parser. -/// On an error, use the given span as the source of the problem. +/// the file at the given path to the `source_map`, and returns a parser. +/// On an error, uses the given span as the source of the problem. pub fn new_sub_parser_from_file<'a>(sess: &'a ParseSess, path: &Path, directory_ownership: DirectoryOwnership, @@ -247,13 +248,13 @@ pub fn new_sub_parser_from_file<'a>(sess: &'a ParseSess, p } -/// Given a source_file and config, return a parser +/// Given a `source_file` and config, returns a parser. fn source_file_to_parser(sess: &ParseSess, source_file: Lrc) -> Parser<'_> { panictry_buffer!(&sess.span_diagnostic, maybe_source_file_to_parser(sess, source_file)) } -/// Given a source_file and config, return a parser. Returns any buffered errors from lexing the +/// Given a `source_file` and config, return a parser. Returns any buffered errors from lexing the /// initial token stream. fn maybe_source_file_to_parser( sess: &ParseSess, @@ -270,14 +271,14 @@ fn maybe_source_file_to_parser( Ok(parser) } -// must preserve old name for now, because quote! from the *existing* -// compiler expands into it +// Must preserve old name for now, because `quote!` from the *existing* +// compiler expands into it. pub fn new_parser_from_tts(sess: &ParseSess, tts: Vec) -> Parser<'_> { stream_to_parser(sess, tts.into_iter().collect(), crate::MACRO_ARGUMENTS) } -// base abstractions +// Base abstractions /// Given a session and a path and an optional span (for error reporting), /// add the path to the session's source_map and return the new source_file or @@ -296,7 +297,7 @@ fn try_file_to_source_file(sess: &ParseSess, path: &Path, spanopt: Option) } /// Given a session and a path and an optional span (for error reporting), -/// add the path to the session's `source_map` and return the new `source_file`. +/// adds the path to the session's `source_map` and returns the new `source_file`. fn file_to_source_file(sess: &ParseSess, path: &Path, spanopt: Option) -> Lrc { match try_file_to_source_file(sess, path, spanopt) { @@ -308,7 +309,7 @@ fn file_to_source_file(sess: &ParseSess, path: &Path, spanopt: Option) } } -/// Given a source_file, produces a sequence of token trees. +/// Given a `source_file`, produces a sequence of token trees. pub fn source_file_to_stream( sess: &ParseSess, source_file: Lrc, @@ -352,7 +353,7 @@ pub fn maybe_file_to_stream( } } -/// Given stream and the `ParseSess`, produces a parser. +/// Given a stream and the `ParseSess`, produces a parser. pub fn stream_to_parser<'a>( sess: &'a ParseSess, stream: TokenStream, @@ -361,7 +362,7 @@ pub fn stream_to_parser<'a>( Parser::new(sess, stream, None, true, false, subparser_name) } -/// Given stream, the `ParseSess` and the base directory, produces a parser. +/// Given a stream, the `ParseSess` and the base directory, produces a parser. /// /// Use this function when you are creating a parser from the token stream /// and also care about the current working directory of the parser (e.g., diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index ab5462baaf721..fcaf5065dac78 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -10,22 +10,22 @@ pub use path::PathStyle; mod stmt; mod generics; -use crate::ast::{self, AttrStyle, Attribute, Param, BindingMode, StrStyle, SelfKind}; -use crate::ast::{FnDecl, Ident, IsAsync, MacDelimiter, Mutability, TyKind}; -use crate::ast::{Visibility, VisibilityKind, Unsafety, CrateSugar}; -use crate::source_map::{self, respan}; -use crate::parse::{SeqSep, literal, token}; +use crate::ast::{ + self, DUMMY_NODE_ID, AttrStyle, Attribute, BindingMode, CrateSugar, FnDecl, Ident, + IsAsync, MacDelimiter, Mutability, Param, StrStyle, SelfKind, TyKind, Visibility, + VisibilityKind, Unsafety, +}; +use crate::parse::{ParseSess, PResult, Directory, DirectoryOwnership, SeqSep, literal, token}; +use crate::parse::diagnostics::{Error, dummy_arg}; use crate::parse::lexer::UnmatchedBrace; use crate::parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration}; use crate::parse::token::{Token, TokenKind, DelimToken}; -use crate::parse::{ParseSess, Directory, DirectoryOwnership}; use crate::print::pprust; use crate::ptr::P; -use crate::parse::PResult; -use crate::ThinVec; -use crate::tokenstream::{self, DelimSpan, TokenTree, TokenStream, TreeAndJoint}; +use crate::source_map::{self, respan}; use crate::symbol::{kw, sym, Symbol}; -use crate::parse::diagnostics::{Error, dummy_arg}; +use crate::tokenstream::{self, DelimSpan, TokenTree, TokenStream, TreeAndJoint}; +use crate::ThinVec; use errors::{Applicability, DiagnosticId, FatalError}; use rustc_target::spec::abi::{self, Abi}; @@ -56,7 +56,7 @@ crate enum BlockMode { Ignore, } -/// As maybe_whole_expr, but for things other than expressions +/// Like `maybe_whole_expr`, but for things other than expressions. #[macro_export] macro_rules! maybe_whole { ($p:expr, $constructor:ident, |$x:ident| $e:expr) => { @@ -116,11 +116,11 @@ pub struct Parser<'a> { /// with non-interpolated identifier and lifetime tokens they refer to. /// Perhaps the normalized / non-normalized setup can be simplified somehow. pub token: Token, - /// Span of the current non-normalized token. + /// The span of the current non-normalized token. meta_var_span: Option, - /// Span of the previous non-normalized token. + /// The span of the previous non-normalized token. pub prev_span: Span, - /// Kind of the previous normalized token (in simplified form). + /// The kind of the previous normalized token (in simplified form). prev_token_kind: PrevTokenKind, restrictions: Restrictions, /// Used to determine the path to externally loaded source files. @@ -143,7 +143,7 @@ pub struct Parser<'a> { /// See the comments in the `parse_path_segment` function for more details. crate unmatched_angle_bracket_count: u32, crate max_angle_bracket_count: u32, - /// List of all unclosed delimiters found by the lexer. If an entry is used for error recovery + /// 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, @@ -799,14 +799,14 @@ impl<'a> Parser<'a> { break; } Err(mut e) => { - // Attempt to keep parsing if it was a similar separator + // Attempt to keep parsing if it was a similar separator. if let Some(ref tokens) = t.similar_tokens() { if tokens.contains(&self.token.kind) { self.bump(); } } e.emit(); - // Attempt to keep parsing if it was an omitted separator + // Attempt to keep parsing if it was an omitted separator. match f(self) { Ok(t) => { v.push(t); @@ -871,7 +871,7 @@ impl<'a> Parser<'a> { self.parse_delim_comma_seq(token::Paren, f) } - /// Advance the parser by one token + /// Advance the parser by one token. pub fn bump(&mut self) { if self.prev_token_kind == PrevTokenKind::Eof { // Bumping after EOF is a bad sign, usually an infinite loop. @@ -894,17 +894,17 @@ impl<'a> Parser<'a> { self.token = self.next_tok(); self.expected_tokens.clear(); - // check after each token + // Check after each token. self.process_potential_macro_variable(); } - /// Advance the parser using provided token as a next one. Use this when + /// Advances the parser using provided token as a next one. Use this when /// consuming a part of a token. For example a single `<` from `<<`. fn bump_with(&mut self, next: TokenKind, span: Span) { self.prev_span = self.token.span.with_hi(span.lo()); // It would be incorrect to record the kind of the current token, but // fortunately for tokens currently using `bump_with`, the - // prev_token_kind will be of no use anyway. + // `prev_token_kind` will be of no use anyway. self.prev_token_kind = PrevTokenKind::Other; self.token = Token::new(next, span); self.expected_tokens.clear(); @@ -937,8 +937,8 @@ impl<'a> Parser<'a> { fn parse_asyncness(&mut self) -> IsAsync { if self.eat_keyword(kw::Async) { IsAsync::Async { - closure_id: ast::DUMMY_NODE_ID, - return_impl_trait_id: ast::DUMMY_NODE_ID, + closure_id: DUMMY_NODE_ID, + return_impl_trait_id: DUMMY_NODE_ID, } } else { IsAsync::NotAsync @@ -1040,7 +1040,7 @@ impl<'a> Parser<'a> { let span = lo.to(self.token.span); - Ok(Param { attrs: attrs.into(), id: ast::DUMMY_NODE_ID, pat, span, ty }) + Ok(Param { attrs: attrs.into(), id: DUMMY_NODE_ID, pat, span, ty }) } /// Parses mutability (`mut` or nothing). @@ -1497,7 +1497,7 @@ impl<'a> Parser<'a> { format!("in {}", path), Applicability::MachineApplicable, ) - .emit(); // emit diagnostic, but continue with public visibility + .emit(); // Emit diagnostic, but continue with public visibility. } } diff --git a/src/libsyntax/parse/parser/expr.rs b/src/libsyntax/parse/parser/expr.rs index e8c8e199fd06b..f70c607198fa9 100644 --- a/src/libsyntax/parse/parser/expr.rs +++ b/src/libsyntax/parse/parser/expr.rs @@ -1,26 +1,26 @@ -use super::{Parser, PResult, Restrictions, PrevTokenKind, TokenType, PathStyle}; -use super::{BlockMode, SemiColonMode}; -use super::{SeqSep, TokenExpectType}; +use super::{ + Parser, PResult, Restrictions, PrevTokenKind, TokenType, PathStyle, BlockMode, SemiColonMode, + SeqSep, TokenExpectType, +}; use super::pat::{GateOr, PARAM_EXPECTED}; +use crate::ast::{ + self, DUMMY_NODE_ID, Attribute, AttrStyle, Ident, CaptureBy, BlockCheckMode, + Expr, ExprKind, RangeLimits, Label, Movability, IsAsync, Arm, Ty, TyKind, + FunctionRetTy, Param, FnDecl, BinOpKind, BinOp, UnOp, Mac, AnonConst, Field, +}; use crate::maybe_recover_from_interpolated_ty_qpath; -use crate::ptr::P; -use crate::ast::{self, Attribute, AttrStyle, Ident, CaptureBy, BlockCheckMode}; -use crate::ast::{Expr, ExprKind, RangeLimits, Label, Movability, IsAsync, Arm}; -use crate::ast::{Ty, TyKind, FunctionRetTy, Param, FnDecl}; -use crate::ast::{BinOpKind, BinOp, UnOp}; -use crate::ast::{Mac, AnonConst, Field}; - use crate::parse::classify; use crate::parse::token::{self, Token}; -use crate::parse::diagnostics::{Error}; +use crate::parse::diagnostics::Error; use crate::print::pprust; +use crate::ptr::P; use crate::source_map::{self, Span}; use crate::symbol::{kw, sym}; use crate::util::parser::{AssocOp, Fixity, prec_let_scrutinee_needs_par}; -use std::mem; use errors::Applicability; +use std::mem; use rustc_data_structures::thin_vec::ThinVec; /// Possibly accepts an `token::Interpolated` expression (a pre-parsed expression @@ -51,7 +51,7 @@ macro_rules! maybe_whole_expr { $p.token.span, ExprKind::Block(block, None), ThinVec::new() )); } - // N.B: `NtIdent(ident)` is normalized to `Ident` in `fn bump`. + // N.B., `NtIdent(ident)` is normalized to `Ident` in `fn bump`. _ => {}, }; } @@ -340,7 +340,7 @@ impl<'a> Parser<'a> { fn is_at_start_of_range_notation_rhs(&self) -> bool { if self.token.can_begin_expr() { - // parse `for i in 1.. { }` as infinite loop, not as `for i in (1..{})`. + // Parse `for i in 1.. { }` as infinite loop, not as `for i in (1..{})`. if self.token == token::OpenDelim(token::Brace) { return !self.restrictions.contains(Restrictions::NO_STRUCT_LITERAL); } @@ -350,12 +350,12 @@ impl<'a> Parser<'a> { } } - /// Parse prefix-forms of range notation: `..expr`, `..`, `..=expr` + /// Parses prefix-forms of range notation: `..expr`, `..`, `..=expr`. fn parse_prefix_range_expr( &mut self, already_parsed_attrs: Option> ) -> PResult<'a, P> { - // Check for deprecated `...` syntax + // Check for deprecated `...` syntax. if self.token == token::DotDotDot { self.err_dotdotdot_syntax(self.token.span); } @@ -389,7 +389,7 @@ impl<'a> Parser<'a> { Ok(self.mk_expr(lo.to(hi), r, attrs)) } - /// Parse a prefix-unary-operator expr + /// Parses a prefix-unary-operator expr. fn parse_prefix_expr( &mut self, already_parsed_attrs: Option> @@ -549,7 +549,7 @@ impl<'a> Parser<'a> { let expr = mk_expr(self, P(Ty { span: path.span, node: TyKind::Path(None, path), - id: ast::DUMMY_NODE_ID + id: DUMMY_NODE_ID, })); let expr_str = self.span_to_snippet(expr.span) @@ -565,7 +565,7 @@ impl<'a> Parser<'a> { expr.span, &format!("try {} the cast value", op_verb), format!("({})", expr_str), - Applicability::MachineApplicable + Applicability::MachineApplicable, ) .emit(); @@ -741,7 +741,6 @@ impl<'a> Parser<'a> { }) } - /// At the bottom (top?) of the precedence hierarchy, /// Parses things like parenthesized exprs, macros, `return`, etc. /// @@ -755,7 +754,7 @@ impl<'a> Parser<'a> { // added to the return value after the fact. // // Therefore, prevent sub-parser from parsing - // attributes by giving them a empty "already parsed" list. + // attributes by giving them a empty "already-parsed" list. let mut attrs = ThinVec::new(); let lo = self.token.span; @@ -778,7 +777,7 @@ impl<'a> Parser<'a> { } } - // Note: when adding new syntax here, don't forget to adjust TokenKind::can_begin_expr(). + // Note: when adding new syntax here, don't forget to adjust `TokenKind::can_begin_expr()`. match self.token.kind { // This match arm is a special-case of the `_` match arm below and // could be removed without changing functionality, but it's faster @@ -791,8 +790,8 @@ impl<'a> Parser<'a> { attrs.extend(self.parse_inner_attributes()?); - // (e) is parenthesized e - // (e,) is a tuple with only one field, e + // `(e)` is parenthesized `e`. + // `(e,)` is a tuple with only one field, `e`. let mut es = vec![]; let mut trailing_comma = false; let mut recovered = false; @@ -800,7 +799,7 @@ impl<'a> Parser<'a> { es.push(match self.parse_expr() { Ok(es) => es, Err(mut err) => { - // recover from parse error in tuple list + // Recover from parse error in tuple list. match self.token.kind { token::Ident(name, false) if name == kw::Underscore && self.look_ahead(1, |t| { @@ -844,7 +843,7 @@ impl<'a> Parser<'a> { return self.parse_block_expr(None, lo, BlockCheckMode::Default, attrs); } token::BinOp(token::Or) | token::OrOr => { - return self.parse_lambda_expr(attrs); + return self.parse_closure(attrs); } token::OpenDelim(token::Bracket) => { self.bump(); @@ -852,21 +851,21 @@ impl<'a> Parser<'a> { attrs.extend(self.parse_inner_attributes()?); if self.eat(&token::CloseDelim(token::Bracket)) { - // Empty vector. + // Empty vector ex = ExprKind::Array(Vec::new()); } else { - // Nonempty vector. + // Non-empty vector let first_expr = self.parse_expr()?; if self.eat(&token::Semi) { - // Repeating array syntax: [ 0; 512 ] + // Repeating array syntax: `[ 0; 512 ]` let count = AnonConst { - id: ast::DUMMY_NODE_ID, + id: DUMMY_NODE_ID, value: self.parse_expr()?, }; self.expect(&token::CloseDelim(token::Bracket))?; ex = ExprKind::Repeat(first_expr, count); } else if self.eat(&token::Comma) { - // Vector with two or more elements. + // Vector with two or more elements let remaining_exprs = self.parse_seq_to_end( &token::CloseDelim(token::Bracket), SeqSep::trailing_allowed(token::Comma), @@ -876,7 +875,7 @@ impl<'a> Parser<'a> { exprs.extend(remaining_exprs); ex = ExprKind::Array(exprs); } else { - // Vector with one element. + // Vector with one element self.expect(&token::CloseDelim(token::Bracket))?; ex = ExprKind::Array(vec![first_expr]); } @@ -892,7 +891,7 @@ impl<'a> Parser<'a> { if self.token.is_path_start() { let path = self.parse_path(PathStyle::Expr)?; - // `!`, as an operator, is prefix, so we know this isn't that + // `!`, as an operator, is prefix, so we know this isn't that. if self.eat(&token::Not) { // MACRO INVOCATION expression let (delim, tts) = self.expect_delimited_token_tree()?; @@ -920,7 +919,7 @@ impl<'a> Parser<'a> { return self.maybe_recover_from_bad_qpath(expr, true); } if self.check_keyword(kw::Move) || self.check_keyword(kw::Static) { - return self.parse_lambda_expr(attrs); + return self.parse_closure(attrs); } if self.eat_keyword(kw::If) { return self.parse_if_expr(attrs); @@ -991,13 +990,13 @@ impl<'a> Parser<'a> { return self.parse_try_block(lo, attrs); } - // Span::rust_2018() is somewhat expensive; don't get it repeatedly. + // `Span::rust_2018()` is somewhat expensive; don't get it repeatedly. let is_span_rust_2018 = self.token.span.rust_2018(); if is_span_rust_2018 && self.check_keyword(kw::Async) { - return if self.is_async_block() { // check for `async {` and `async move {` + return if self.is_async_block() { // Check for `async {` and `async move {`. self.parse_async_block(attrs) } else { - self.parse_lambda_expr(attrs) + self.parse_closure(attrs) }; } if self.eat_keyword(kw::Return) { @@ -1043,13 +1042,12 @@ impl<'a> Parser<'a> { // recovery in order to keep the error count down. Fixing the // delimiters will possibly also fix the bare semicolon found in // expression context. For example, silence the following error: - // ``` - // error: expected expression, found `;` - // --> file.rs:2:13 - // | - // 2 | foo(bar(; - // | ^ expected expression - // ``` + // + // error: expected expression, found `;` + // --> file.rs:2:13 + // | + // 2 | foo(bar(; + // | ^ expected expression self.bump(); return Ok(self.mk_expr(self.token.span, ExprKind::Err, ThinVec::new())); } @@ -1096,11 +1094,11 @@ impl<'a> Parser<'a> { attrs.extend(self.parse_inner_attributes()?); let blk = self.parse_block_tail(lo, blk_mode)?; - return Ok(self.mk_expr(blk.span, ExprKind::Block(blk, opt_label), attrs)); + Ok(self.mk_expr(blk.span, ExprKind::Block(blk, opt_label), attrs)) } - /// Parses `move |args| expr`. - fn parse_lambda_expr(&mut self, attrs: ThinVec) -> PResult<'a, P> { + /// Parses a closure (e.g., `move |args| expr`). + fn parse_closure(&mut self, attrs: ThinVec) -> PResult<'a, P> { let lo = self.token.span; let movability = if self.eat_keyword(kw::Static) { @@ -1115,7 +1113,7 @@ impl<'a> Parser<'a> { IsAsync::NotAsync }; if asyncness.is_async() { - // Feature gate `async ||` closures. + // Feature-gate `async ||` closures. self.sess.gated_spans.async_closure.borrow_mut().push(self.prev_span); } @@ -1128,8 +1126,7 @@ impl<'a> Parser<'a> { self.parse_expr_res(restrictions, None)? }, _ => { - // If an explicit return type is given, require a - // block to appear (RFC 968). + // If an explicit return type is given, require a block to appear (RFC 968). let body_lo = self.token.span; self.parse_block_expr(None, body_lo, BlockCheckMode::Default, ThinVec::new())? } @@ -1141,7 +1138,7 @@ impl<'a> Parser<'a> { attrs)) } - /// Parse an optional `move` prefix to a closure lke construct. + /// Parses an optional `move` prefix to a closure lke construct. fn parse_capture_clause(&mut self) -> CaptureBy { if self.eat_keyword(kw::Move) { CaptureBy::Value @@ -1176,7 +1173,7 @@ impl<'a> Parser<'a> { })) } - /// Parses a parameter in a lambda header (e.g., `|arg, arg|`). + /// Parses a parameter in a closure header (e.g., `|arg, arg|`). fn parse_fn_block_param(&mut self) -> PResult<'a, Param> { let lo = self.token.span; let attrs = self.parse_param_attributes()?; @@ -1185,7 +1182,7 @@ impl<'a> Parser<'a> { self.parse_ty()? } else { P(Ty { - id: ast::DUMMY_NODE_ID, + id: DUMMY_NODE_ID, node: TyKind::Infer, span: self.prev_span, }) @@ -1196,7 +1193,7 @@ impl<'a> Parser<'a> { ty: t, pat, span, - id: ast::DUMMY_NODE_ID + id: DUMMY_NODE_ID }) } @@ -1233,7 +1230,7 @@ impl<'a> Parser<'a> { Ok(self.mk_expr(lo.to(hi), ExprKind::If(cond, thn, els), attrs)) } - /// Parse the condition of a `if`- or `while`-expression + /// Parses the condition of a `if` or `while` expression. fn parse_cond_expr(&mut self) -> PResult<'a, P> { let cond = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?; @@ -1261,7 +1258,7 @@ impl<'a> Parser<'a> { Ok(self.mk_expr(span, ExprKind::Let(pat, expr), attrs)) } - /// `else` token already eaten + /// Parses an `else { ... }` expression (`else` token already eaten). fn parse_else_expr(&mut self) -> PResult<'a, P> { if self.eat_keyword(kw::If) { return self.parse_if_expr(ThinVec::new()); @@ -1271,7 +1268,7 @@ impl<'a> Parser<'a> { } } - /// Parse a 'for' .. 'in' expression ('for' token already eaten) + /// Parses a `for ... in` expression (`for` token already eaten). fn parse_for_expr( &mut self, opt_label: Option