diff options
| author | XAMPPRocky <4464295+XAMPPRocky@users.noreply.github.com> | 2020-02-26 21:39:30 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-02-26 21:39:30 +0100 |
| commit | 526280a853f31bbc3120334dfe46e19ea4dbaa25 (patch) | |
| tree | cd35561be4cdcd7591d98bae715726c0e40995b7 /src/libsyntax | |
| parent | e7a344fb745a0a663e21be947b2619df05df6d31 (diff) | |
| parent | abc3073c92df034636a823c5382ece2186d22b9e (diff) | |
| download | rust-526280a853f31bbc3120334dfe46e19ea4dbaa25.tar.gz rust-526280a853f31bbc3120334dfe46e19ea4dbaa25.zip | |
Merge branch 'master' into relnotes-1.42.0
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/ast.rs | 266 | ||||
| -rw-r--r-- | src/libsyntax/attr/mod.rs | 2 | ||||
| -rw-r--r-- | src/libsyntax/mut_visit.rs | 96 | ||||
| -rw-r--r-- | src/libsyntax/token.rs | 50 | ||||
| -rw-r--r-- | src/libsyntax/util/comments.rs | 4 | ||||
| -rw-r--r-- | src/libsyntax/util/literal.rs | 2 | ||||
| -rw-r--r-- | src/libsyntax/visit.rs | 73 |
7 files changed, 243 insertions, 250 deletions
diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index b22406124e0..62ff4f5183a 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -34,7 +34,7 @@ use rustc_data_structures::thin_vec::ThinVec; use rustc_index::vec::Idx; use rustc_macros::HashStable_Generic; use rustc_serialize::{self, Decoder, Encoder}; -use rustc_span::source_map::{dummy_spanned, respan, Spanned}; +use rustc_span::source_map::{respan, Spanned}; use rustc_span::symbol::{kw, sym, Symbol}; use rustc_span::{Span, DUMMY_SP}; @@ -243,7 +243,7 @@ pub struct ParenthesizedArgs { pub inputs: Vec<P<Ty>>, /// `C` - pub output: FunctionRetTy, + pub output: FnRetTy, } impl ParenthesizedArgs { @@ -429,6 +429,13 @@ pub struct Crate { pub module: Mod, pub attrs: Vec<Attribute>, pub span: Span, + /// The order of items in the HIR is unrelated to the order of + /// items in the AST. However, we generate proc macro harnesses + /// based on the AST order, and later refer to these harnesses + /// from the HIR. This field keeps track of the order in which + /// we generated proc macros harnesses, so that we can map + /// HIR proc macros items back to their harness items. + pub proc_macros: Vec<NodeId>, } /// Possible values inside of compile-time attribute lists. @@ -1198,14 +1205,14 @@ pub enum ExprKind { /// A closure (e.g., `move |a, b, c| a + b + c`). /// /// The final span is the span of the argument block `|...|`. - Closure(CaptureBy, IsAsync, Movability, P<FnDecl>, P<Expr>, Span), + Closure(CaptureBy, Async, Movability, P<FnDecl>, P<Expr>, Span), /// A block (`'label: { ... }`). Block(P<Block>, Option<Label>), /// An async block (`async move { ... }`). /// /// The `NodeId` is the `NodeId` for the closure that results from /// desugaring an async block, just like the NodeId field in the - /// `IsAsync` enum. This is necessary in order to create a def for the + /// `Async::Yes` variant. This is necessary in order to create a def for the /// closure which can be used as a parent of any child defs. Defs /// created during lowering cannot be made the parent of any other /// preexisting defs. @@ -1605,46 +1612,6 @@ pub struct FnSig { pub decl: P<FnDecl>, } -/// Represents associated items. -/// These include items in `impl` and `trait` definitions. -#[derive(Clone, RustcEncodable, RustcDecodable, Debug)] -pub struct AssocItem { - pub attrs: Vec<Attribute>, - pub id: NodeId, - pub span: Span, - pub vis: Visibility, - pub ident: Ident, - - pub defaultness: Defaultness, - pub generics: Generics, - pub kind: AssocItemKind, - /// See `Item::tokens` for what this is. - pub tokens: Option<TokenStream>, -} - -/// Represents various kinds of content within an `impl`. -/// -/// The term "provided" in the variants below refers to the item having a default -/// definition / body. Meanwhile, a "required" item lacks a definition / body. -/// In an implementation, all items must be provided. -/// The `Option`s below denote the bodies, where `Some(_)` -/// means "provided" and conversely `None` means "required". -#[derive(Clone, RustcEncodable, RustcDecodable, Debug)] -pub enum AssocItemKind { - /// An associated constant, `const $ident: $ty $def?;` where `def ::= "=" $expr? ;`. - /// If `def` is parsed, then the associated constant is provided, and otherwise required. - Const(P<Ty>, Option<P<Expr>>), - - /// An associated function. - Fn(FnSig, Option<P<Block>>), - - /// An associated type. - TyAlias(GenericBounds, Option<P<Ty>>), - - /// A macro expanding to an associated item. - Macro(Mac), -} - #[derive( Clone, Copy, @@ -1863,7 +1830,7 @@ pub struct Ty { #[derive(Clone, RustcEncodable, RustcDecodable, Debug)] pub struct BareFnTy { - pub unsafety: Unsafety, + pub unsafety: Unsafe, pub ext: Extern, pub generic_params: Vec<GenericParam>, pub decl: P<FnDecl>, @@ -2076,7 +2043,7 @@ impl Param { #[derive(Clone, RustcEncodable, RustcDecodable, Debug)] pub struct FnDecl { pub inputs: Vec<Param>, - pub output: FunctionRetTy, + pub output: FnRetTy, } impl FnDecl { @@ -2101,77 +2068,45 @@ pub enum IsAuto { No, } -#[derive( - Copy, - Clone, - PartialEq, - Eq, - PartialOrd, - Ord, - Hash, - RustcEncodable, - RustcDecodable, - Debug, - HashStable_Generic -)] -pub enum Unsafety { - Unsafe, - Normal, -} - -impl Unsafety { - pub fn prefix_str(&self) -> &'static str { - match self { - Unsafety::Unsafe => "unsafe ", - Unsafety::Normal => "", - } - } -} - -impl fmt::Display for Unsafety { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Display::fmt( - match *self { - Unsafety::Normal => "normal", - Unsafety::Unsafe => "unsafe", - }, - f, - ) - } +#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable, Debug)] +#[derive(HashStable_Generic)] +pub enum Unsafe { + Yes(Span), + No, } #[derive(Copy, Clone, RustcEncodable, RustcDecodable, Debug)] -pub enum IsAsync { - Async { closure_id: NodeId, return_impl_trait_id: NodeId }, - NotAsync, +pub enum Async { + Yes { span: Span, closure_id: NodeId, return_impl_trait_id: NodeId }, + No, } -impl IsAsync { +impl Async { pub fn is_async(self) -> bool { - if let IsAsync::Async { .. } = self { true } else { false } + if let Async::Yes { .. } = self { true } else { false } } /// In ths case this is an `async` return, the `NodeId` for the generated `impl Trait` item. pub fn opt_return_id(self) -> Option<NodeId> { match self { - IsAsync::Async { return_impl_trait_id, .. } => Some(return_impl_trait_id), - IsAsync::NotAsync => None, + Async::Yes { return_impl_trait_id, .. } => Some(return_impl_trait_id), + Async::No => None, } } } #[derive(Copy, Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, Debug)] #[derive(HashStable_Generic)] -pub enum Constness { - Const, - NotConst, +pub enum Const { + Yes(Span), + No, } /// Item defaultness. /// For details see the [RFC #2532](https://github.com/rust-lang/rfcs/pull/2532). #[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable, Debug, HashStable_Generic)] pub enum Defaultness { - Default, + Default(Span), Final, } @@ -2193,8 +2128,7 @@ impl fmt::Debug for ImplPolarity { } #[derive(Clone, RustcEncodable, RustcDecodable, Debug)] -pub enum FunctionRetTy { - // FIXME(Centril): Rename to `FnRetTy` and in HIR also. +pub enum FnRetTy { /// Returns type is not specified. /// /// Functions default to `()` and closures default to inference. @@ -2204,11 +2138,11 @@ pub enum FunctionRetTy { Ty(P<Ty>), } -impl FunctionRetTy { +impl FnRetTy { pub fn span(&self) -> Span { match *self { - FunctionRetTy::Default(span) => span, - FunctionRetTy::Ty(ref ty) => ty.span, + FnRetTy::Default(span) => span, + FnRetTy::Ty(ref ty) => ty.span, } } } @@ -2477,15 +2411,15 @@ impl VariantData { } } -/// An item. -/// -/// The name might be a dummy name in case of anonymous items. +/// An item definition. #[derive(Clone, RustcEncodable, RustcDecodable, Debug)] pub struct Item<K = ItemKind> { pub attrs: Vec<Attribute>, pub id: NodeId, pub span: Span, pub vis: Visibility, + /// The name of the item. + /// It might be a dummy name in case of anonymous items. pub ident: Ident, pub kind: K, @@ -2507,6 +2441,13 @@ impl Item { } } +impl<K: IntoItemKind> Item<K> { + pub fn into_item(self) -> Item { + let Item { attrs, id, span, vis, ident, kind, tokens } = self; + Item { attrs, id, span, vis, ident, kind: kind.into_item_kind(), tokens } + } +} + /// `extern` qualifier on a function item or function type. #[derive(Clone, Copy, RustcEncodable, RustcDecodable, Debug)] pub enum Extern { @@ -2527,9 +2468,9 @@ impl Extern { /// included in this struct (e.g., `async unsafe fn` or `const extern "C" fn`). #[derive(Clone, Copy, RustcEncodable, RustcDecodable, Debug)] pub struct FnHeader { - pub unsafety: Unsafety, - pub asyncness: Spanned<IsAsync>, - pub constness: Spanned<Constness>, + pub unsafety: Unsafe, + pub asyncness: Async, + pub constness: Const, pub ext: Extern, } @@ -2537,9 +2478,9 @@ impl FnHeader { /// Does this function header have any qualifiers or is it empty? pub fn has_qualifiers(&self) -> bool { let Self { unsafety, asyncness, constness, ext } = self; - matches!(unsafety, Unsafety::Unsafe) - || asyncness.node.is_async() - || matches!(constness.node, Constness::Const) + matches!(unsafety, Unsafe::Yes(_)) + || asyncness.is_async() + || matches!(constness, Const::Yes(_)) || !matches!(ext, Extern::None) } } @@ -2547,9 +2488,9 @@ impl FnHeader { impl Default for FnHeader { fn default() -> FnHeader { FnHeader { - unsafety: Unsafety::Normal, - asyncness: dummy_spanned(IsAsync::NotAsync), - constness: dummy_spanned(Constness::NotConst), + unsafety: Unsafe::No, + asyncness: Async::No, + constness: Const::No, ext: Extern::None, } } @@ -2568,15 +2509,15 @@ pub enum ItemKind { /// A static item (`static`). /// /// E.g., `static FOO: i32 = 42;` or `static FOO: &'static str = "bar";`. - Static(P<Ty>, Mutability, P<Expr>), + Static(P<Ty>, Mutability, Option<P<Expr>>), /// A constant item (`const`). /// /// E.g., `const FOO: i32 = 42;`. - Const(P<Ty>, P<Expr>), + Const(Defaultness, P<Ty>, Option<P<Expr>>), /// A function declaration (`fn`). /// /// E.g., `fn foo(bar: usize) -> usize { .. }`. - Fn(FnSig, Generics, Option<P<Block>>), + Fn(Defaultness, FnSig, Generics, Option<P<Block>>), /// A module declaration (`mod`). /// /// E.g., `mod foo;` or `mod foo { .. }`. @@ -2590,7 +2531,7 @@ pub enum ItemKind { /// A type alias (`type`). /// /// E.g., `type Foo = Bar<u8>;`. - TyAlias(P<Ty>, Generics), + TyAlias(Defaultness, Generics, GenericBounds, Option<P<Ty>>), /// An enum definition (`enum`). /// /// E.g., `enum Foo<A, B> { C<A>, D<B> }`. @@ -2606,7 +2547,7 @@ pub enum ItemKind { /// A trait declaration (`trait`). /// /// E.g., `trait Foo { .. }`, `trait Foo<T> { .. }` or `auto trait Foo {}`. - Trait(IsAuto, Unsafety, Generics, GenericBounds, Vec<P<AssocItem>>), + Trait(IsAuto, Unsafe, Generics, GenericBounds, Vec<P<AssocItem>>), /// Trait alias /// /// E.g., `trait Foo = Bar + Quux;`. @@ -2615,10 +2556,10 @@ pub enum ItemKind { /// /// E.g., `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`. Impl { - unsafety: Unsafety, + unsafety: Unsafe, polarity: ImplPolarity, defaultness: Defaultness, - constness: Constness, + constness: Const, generics: Generics, /// The trait being implemented, if any. @@ -2637,30 +2578,41 @@ pub enum ItemKind { } impl ItemKind { - pub fn descriptive_variant(&self) -> &str { - match *self { + pub fn article(&self) -> &str { + use ItemKind::*; + match self { + Use(..) | Static(..) | Const(..) | Fn(..) | Mod(..) | GlobalAsm(..) | TyAlias(..) + | Struct(..) | Union(..) | Trait(..) | TraitAlias(..) | MacroDef(..) => "a", + ExternCrate(..) | ForeignMod(..) | Mac(..) | Enum(..) | Impl { .. } => "an", + } + } + + pub fn descr(&self) -> &str { + match self { ItemKind::ExternCrate(..) => "extern crate", - ItemKind::Use(..) => "use", + ItemKind::Use(..) => "`use` import", ItemKind::Static(..) => "static item", ItemKind::Const(..) => "constant item", ItemKind::Fn(..) => "function", ItemKind::Mod(..) => "module", - ItemKind::ForeignMod(..) => "foreign module", - ItemKind::GlobalAsm(..) => "global asm", + ItemKind::ForeignMod(..) => "extern block", + ItemKind::GlobalAsm(..) => "global asm item", ItemKind::TyAlias(..) => "type alias", ItemKind::Enum(..) => "enum", ItemKind::Struct(..) => "struct", ItemKind::Union(..) => "union", ItemKind::Trait(..) => "trait", ItemKind::TraitAlias(..) => "trait alias", - ItemKind::Mac(..) | ItemKind::MacroDef(..) | ItemKind::Impl { .. } => "item", + ItemKind::Mac(..) => "item macro invocation", + ItemKind::MacroDef(..) => "macro definition", + ItemKind::Impl { .. } => "implementation", } } pub fn generics(&self) -> Option<&Generics> { match self { - Self::Fn(_, generics, _) - | Self::TyAlias(_, generics) + Self::Fn(_, _, generics, _) + | Self::TyAlias(_, generics, ..) | Self::Enum(_, generics) | Self::Struct(_, generics) | Self::Union(_, generics) @@ -2672,28 +2624,58 @@ impl ItemKind { } } -pub type ForeignItem = Item<ForeignItemKind>; +pub trait IntoItemKind { + fn into_item_kind(self) -> ItemKind; +} + +// FIXME(Centril): These definitions should be unmerged; +// see https://github.com/rust-lang/rust/pull/69194#discussion_r379899975 +pub type ForeignItem = Item<AssocItemKind>; +pub type ForeignItemKind = AssocItemKind; -/// An item within an `extern` block. +/// Represents associated items. +/// These include items in `impl` and `trait` definitions. +pub type AssocItem = Item<AssocItemKind>; + +/// Represents non-free item kinds. +/// +/// The term "provided" in the variants below refers to the item having a default +/// definition / body. Meanwhile, a "required" item lacks a definition / body. +/// In an implementation, all items must be provided. +/// The `Option`s below denote the bodies, where `Some(_)` +/// means "provided" and conversely `None` means "required". #[derive(Clone, RustcEncodable, RustcDecodable, Debug)] -pub enum ForeignItemKind { - /// A foreign function. - Fn(FnSig, Generics, Option<P<Block>>), - /// A foreign static item (`static ext: u8`). - Static(P<Ty>, Mutability), - /// A foreign type. - Ty, - /// A macro invocation. +pub enum AssocItemKind { + /// A constant, `const $ident: $ty $def?;` where `def ::= "=" $expr? ;`. + /// If `def` is parsed, then the constant is provided, and otherwise required. + Const(Defaultness, P<Ty>, Option<P<Expr>>), + /// A static item (`static FOO: u8`). + Static(P<Ty>, Mutability, Option<P<Expr>>), + /// A function. + Fn(Defaultness, FnSig, Generics, Option<P<Block>>), + /// A type. + TyAlias(Defaultness, Generics, GenericBounds, Option<P<Ty>>), + /// A macro expanding to items. Macro(Mac), } -impl ForeignItemKind { - pub fn descriptive_variant(&self) -> &str { +impl AssocItemKind { + pub fn defaultness(&self) -> Defaultness { match *self { - ForeignItemKind::Fn(..) => "foreign function", - ForeignItemKind::Static(..) => "foreign static item", - ForeignItemKind::Ty => "foreign type", - ForeignItemKind::Macro(..) => "macro in foreign module", + Self::Const(def, ..) | Self::Fn(def, ..) | Self::TyAlias(def, ..) => def, + Self::Macro(..) | Self::Static(..) => Defaultness::Final, + } + } +} + +impl IntoItemKind for AssocItemKind { + fn into_item_kind(self) -> ItemKind { + match self { + AssocItemKind::Const(a, b, c) => ItemKind::Const(a, b, c), + AssocItemKind::Static(a, b, c) => ItemKind::Static(a, b, c), + AssocItemKind::Fn(a, b, c, d) => ItemKind::Fn(a, b, c, d), + AssocItemKind::TyAlias(a, b, c, d) => ItemKind::TyAlias(a, b, c, d), + AssocItemKind::Macro(a) => ItemKind::Mac(a), } } } diff --git a/src/libsyntax/attr/mod.rs b/src/libsyntax/attr/mod.rs index 313f5269235..cd485e71378 100644 --- a/src/libsyntax/attr/mod.rs +++ b/src/libsyntax/attr/mod.rs @@ -722,6 +722,6 @@ macro_rules! derive_has_attrs { } derive_has_attrs! { - Item, Expr, Local, ast::ForeignItem, ast::StructField, ast::AssocItem, ast::Arm, + Item, Expr, Local, ast::ForeignItem, ast::StructField, ast::Arm, ast::Field, ast::FieldPat, ast::Variant, ast::Param, GenericParam } diff --git a/src/libsyntax/mut_visit.rs b/src/libsyntax/mut_visit.rs index 8517f223f92..b3abd4fc755 100644 --- a/src/libsyntax/mut_visit.rs +++ b/src/libsyntax/mut_visit.rs @@ -114,7 +114,7 @@ pub trait MutVisitor: Sized { noop_visit_fn_decl(d, self); } - fn visit_asyncness(&mut self, a: &mut IsAsync) { + fn visit_asyncness(&mut self, a: &mut Async) { noop_visit_asyncness(a, self); } @@ -711,30 +711,17 @@ pub fn noop_visit_interpolated<T: MutVisitor>(nt: &mut token::Nonterminal, vis: } token::NtPath(path) => vis.visit_path(path), token::NtTT(tt) => vis.visit_tt(tt), - token::NtImplItem(item) => visit_clobber(item, |item| { - // See reasoning above. - vis.flat_map_impl_item(item).expect_one("expected visitor to produce exactly one item") - }), - token::NtTraitItem(item) => visit_clobber(item, |item| { - // See reasoning above. - vis.flat_map_trait_item(item).expect_one("expected visitor to produce exactly one item") - }), token::NtVis(visib) => vis.visit_vis(visib), - token::NtForeignItem(item) => visit_clobber(item, |item| { - // See reasoning above. - vis.flat_map_foreign_item(item) - .expect_one("expected visitor to produce exactly one item") - }), } } -pub fn noop_visit_asyncness<T: MutVisitor>(asyncness: &mut IsAsync, vis: &mut T) { +pub fn noop_visit_asyncness<T: MutVisitor>(asyncness: &mut Async, vis: &mut T) { match asyncness { - IsAsync::Async { closure_id, return_impl_trait_id } => { + Async::Yes { span: _, closure_id, return_impl_trait_id } => { vis.visit_id(closure_id); vis.visit_id(return_impl_trait_id); } - IsAsync::NotAsync => {} + Async::No => {} } } @@ -744,10 +731,10 @@ pub fn noop_visit_fn_decl<T: MutVisitor>(decl: &mut P<FnDecl>, vis: &mut T) { noop_visit_fn_ret_ty(output, vis); } -pub fn noop_visit_fn_ret_ty<T: MutVisitor>(fn_ret_ty: &mut FunctionRetTy, vis: &mut T) { +pub fn noop_visit_fn_ret_ty<T: MutVisitor>(fn_ret_ty: &mut FnRetTy, vis: &mut T) { match fn_ret_ty { - FunctionRetTy::Default(span) => vis.visit_span(span), - FunctionRetTy::Ty(ty) => vis.visit_ty(ty), + FnRetTy::Default(span) => vis.visit_span(span), + FnRetTy::Ty(ty) => vis.visit_ty(ty), } } @@ -890,15 +877,11 @@ pub fn noop_visit_item_kind<T: MutVisitor>(kind: &mut ItemKind, vis: &mut T) { match kind { ItemKind::ExternCrate(_orig_name) => {} ItemKind::Use(use_tree) => vis.visit_use_tree(use_tree), - ItemKind::Static(ty, _mut, expr) => { - vis.visit_ty(ty); - vis.visit_expr(expr); - } - ItemKind::Const(ty, expr) => { + ItemKind::Static(ty, _, expr) | ItemKind::Const(_, ty, expr) => { vis.visit_ty(ty); - vis.visit_expr(expr); + visit_opt(expr, |expr| vis.visit_expr(expr)); } - ItemKind::Fn(sig, generics, body) => { + ItemKind::Fn(_, sig, generics, body) => { visit_fn_sig(sig, vis); vis.visit_generics(generics); visit_opt(body, |body| vis.visit_block(body)); @@ -906,9 +889,10 @@ pub fn noop_visit_item_kind<T: MutVisitor>(kind: &mut ItemKind, vis: &mut T) { ItemKind::Mod(m) => vis.visit_mod(m), ItemKind::ForeignMod(nm) => vis.visit_foreign_mod(nm), ItemKind::GlobalAsm(_ga) => {} - ItemKind::TyAlias(ty, generics) => { - vis.visit_ty(ty); + ItemKind::TyAlias(_, generics, bounds, ty) => { vis.visit_generics(generics); + visit_bounds(bounds, vis); + visit_opt(ty, |ty| vis.visit_ty(ty)); } ItemKind::Enum(EnumDef { variants }, generics) => { variants.flat_map_in_place(|variant| vis.flat_map_variant(variant)); @@ -951,36 +935,47 @@ pub fn noop_flat_map_assoc_item<T: MutVisitor>( mut item: P<AssocItem>, visitor: &mut T, ) -> SmallVec<[P<AssocItem>; 1]> { - let AssocItem { id, ident, vis, defaultness: _, attrs, generics, kind, span, tokens: _ } = - item.deref_mut(); + let Item { id, ident, vis, attrs, kind, span, tokens: _ } = item.deref_mut(); + walk_nested_item(visitor, id, span, ident, vis, attrs, kind); + smallvec![item] +} + +pub fn walk_nested_item( + visitor: &mut impl MutVisitor, + id: &mut NodeId, + span: &mut Span, + ident: &mut Ident, + vis: &mut Visibility, + attrs: &mut Vec<Attribute>, + kind: &mut AssocItemKind, +) { visitor.visit_id(id); visitor.visit_ident(ident); visitor.visit_vis(vis); visit_attrs(attrs, visitor); - visitor.visit_generics(generics); match kind { - AssocItemKind::Const(ty, expr) => { + AssocItemKind::Const(_, ty, expr) | AssocItemKind::Static(ty, _, expr) => { visitor.visit_ty(ty); visit_opt(expr, |expr| visitor.visit_expr(expr)); } - AssocItemKind::Fn(sig, body) => { + AssocItemKind::Fn(_, sig, generics, body) => { + visitor.visit_generics(generics); visit_fn_sig(sig, visitor); visit_opt(body, |body| visitor.visit_block(body)); } - AssocItemKind::TyAlias(bounds, ty) => { + AssocItemKind::TyAlias(_, generics, bounds, ty) => { + visitor.visit_generics(generics); visit_bounds(bounds, visitor); visit_opt(ty, |ty| visitor.visit_ty(ty)); } AssocItemKind::Macro(mac) => visitor.visit_mac(mac), } visitor.visit_span(span); - - smallvec![item] } pub fn noop_visit_fn_header<T: MutVisitor>(header: &mut FnHeader, vis: &mut T) { let FnHeader { unsafety: _, asyncness, constness: _, ext: _ } = header; - vis.visit_asyncness(&mut asyncness.node); + vis.visit_asyncness(asyncness); } pub fn noop_visit_mod<T: MutVisitor>(Mod { inner, items, inline: _ }: &mut Mod, vis: &mut T) { @@ -989,7 +984,7 @@ pub fn noop_visit_mod<T: MutVisitor>(Mod { inner, items, inline: _ }: &mut Mod, } pub fn noop_visit_crate<T: MutVisitor>(krate: &mut Crate, vis: &mut T) { - visit_clobber(krate, |Crate { module, attrs, span }| { + visit_clobber(krate, |Crate { module, attrs, span, proc_macros }| { let item = P(Item { ident: Ident::invalid(), attrs, @@ -1004,11 +999,11 @@ pub fn noop_visit_crate<T: MutVisitor>(krate: &mut Crate, vis: &mut T) { let len = items.len(); if len == 0 { let module = Mod { inner: span, items: vec![], inline: true }; - Crate { module, attrs: vec![], span } + Crate { module, attrs: vec![], span, proc_macros } } else if len == 1 { let Item { attrs, span, kind, .. } = items.into_iter().next().unwrap().into_inner(); match kind { - ItemKind::Mod(module) => Crate { module, attrs, span }, + ItemKind::Mod(module) => Crate { module, attrs, span, proc_macros }, _ => panic!("visitor converted a module to not a module"), } } else { @@ -1040,23 +1035,8 @@ pub fn noop_flat_map_foreign_item<T: MutVisitor>( mut item: P<ForeignItem>, visitor: &mut T, ) -> SmallVec<[P<ForeignItem>; 1]> { - let ForeignItem { ident, attrs, id, kind, vis, span, tokens: _ } = item.deref_mut(); - visitor.visit_ident(ident); - visit_attrs(attrs, visitor); - match kind { - ForeignItemKind::Fn(sig, generics, body) => { - visit_fn_sig(sig, visitor); - visitor.visit_generics(generics); - visit_opt(body, |body| visitor.visit_block(body)); - } - ForeignItemKind::Static(t, _m) => visitor.visit_ty(t), - ForeignItemKind::Ty => {} - ForeignItemKind::Macro(mac) => visitor.visit_mac(mac), - } - visitor.visit_id(id); - visitor.visit_span(span); - visitor.visit_vis(vis); - + let Item { ident, attrs, id, kind, vis, span, tokens: _ } = item.deref_mut(); + walk_nested_item(visitor, id, span, ident, vis, attrs, kind); smallvec![item] } diff --git a/src/libsyntax/token.rs b/src/libsyntax/token.rs index 3045f147698..52bf50604fb 100644 --- a/src/libsyntax/token.rs +++ b/src/libsyntax/token.rs @@ -270,6 +270,39 @@ impl TokenKind { Literal(Lit::new(kind, symbol, suffix)) } + // An approximation to proc-macro-style single-character operators used by rustc parser. + // If the operator token can be broken into two tokens, the first of which is single-character, + // then this function performs that operation, otherwise it returns `None`. + pub fn break_two_token_op(&self) -> Option<(TokenKind, TokenKind)> { + Some(match *self { + Le => (Lt, Eq), + EqEq => (Eq, Eq), + Ne => (Not, Eq), + Ge => (Gt, Eq), + AndAnd => (BinOp(And), BinOp(And)), + OrOr => (BinOp(Or), BinOp(Or)), + BinOp(Shl) => (Lt, Lt), + BinOp(Shr) => (Gt, Gt), + BinOpEq(Plus) => (BinOp(Plus), Eq), + BinOpEq(Minus) => (BinOp(Minus), Eq), + BinOpEq(Star) => (BinOp(Star), Eq), + BinOpEq(Slash) => (BinOp(Slash), Eq), + BinOpEq(Percent) => (BinOp(Percent), Eq), + BinOpEq(Caret) => (BinOp(Caret), Eq), + BinOpEq(And) => (BinOp(And), Eq), + BinOpEq(Or) => (BinOp(Or), Eq), + BinOpEq(Shl) => (Lt, Le), + BinOpEq(Shr) => (Gt, Ge), + DotDot => (Dot, Dot), + DotDotDot => (Dot, DotDot), + ModSep => (Colon, Colon), + RArrow => (BinOp(Minus), Gt), + LArrow => (Lt, BinOp(Minus)), + FatArrow => (Eq, Gt), + _ => return None, + }) + } + /// Returns tokens that are likely to be typed accidentally instead of the current token. /// Enables better error recovery when the wrong token is found. pub fn similar_tokens(&self) -> Option<Vec<TokenKind>> { @@ -402,12 +435,14 @@ impl Token { /// Returns `true` if the token is any literal, a minus (which can prefix a literal, /// for example a '-42', or one of the boolean idents). + /// + /// Keep this in sync with `Lit::from_token`. pub fn can_begin_literal_or_bool(&self) -> bool { match self.kind { Literal(..) | BinOp(Minus) => true, Ident(name, false) if name.is_bool_lit() => true, - Interpolated(ref nt) => match **nt { - NtLiteral(..) => true, + Interpolated(ref nt) => match &**nt { + NtExpr(e) | NtLiteral(e) => matches!(e.kind, ast::ExprKind::Lit(_)), _ => false, }, _ => false, @@ -530,7 +565,7 @@ impl Token { } /// Returns `true` if the token is a non-raw identifier for which `pred` holds. - fn is_non_raw_ident_where(&self, pred: impl FnOnce(ast::Ident) -> bool) -> bool { + pub fn is_non_raw_ident_where(&self, pred: impl FnOnce(ast::Ident) -> bool) -> bool { match self.ident() { Some((id, false)) => pred(id), _ => false, @@ -677,12 +712,6 @@ pub enum Nonterminal { NtPath(ast::Path), NtVis(ast::Visibility), NtTT(TokenTree), - // Used only for passing items to proc macro attributes (they are not - // strictly necessary for that, `Annotatable` can be converted into - // tokens directly, but doing that naively regresses pretty-printing). - NtTraitItem(P<ast::AssocItem>), - NtImplItem(P<ast::AssocItem>), - NtForeignItem(P<ast::ForeignItem>), } // `Nonterminal` is used a lot. Make sure it doesn't unintentionally get bigger. @@ -720,9 +749,6 @@ impl fmt::Debug for Nonterminal { NtMeta(..) => f.pad("NtMeta(..)"), NtPath(..) => f.pad("NtPath(..)"), NtTT(..) => f.pad("NtTT(..)"), - NtImplItem(..) => f.pad("NtImplItem(..)"), - NtTraitItem(..) => f.pad("NtTraitItem(..)"), - NtForeignItem(..) => f.pad("NtForeignItem(..)"), NtVis(..) => f.pad("NtVis(..)"), NtLifetime(..) => f.pad("NtLifetime(..)"), } diff --git a/src/libsyntax/util/comments.rs b/src/libsyntax/util/comments.rs index 5a67531624d..0e42ae11fa2 100644 --- a/src/libsyntax/util/comments.rs +++ b/src/libsyntax/util/comments.rs @@ -189,8 +189,8 @@ fn split_block_comment_into_lines(text: &str, col: CharPos) -> Vec<String> { // it appears this function is called only from pprust... that's // probably not a good thing. pub fn gather_comments(sm: &SourceMap, path: FileName, src: String) -> Vec<Comment> { - let cm = SourceMap::new(sm.path_mapping().clone()); - let source_file = cm.new_source_file(path, src); + let sm = SourceMap::new(sm.path_mapping().clone()); + let source_file = sm.new_source_file(path, src); let text = (*source_file.src.as_ref().unwrap()).clone(); let text: &str = text.as_str(); diff --git a/src/libsyntax/util/literal.rs b/src/libsyntax/util/literal.rs index dd06c25b4de..0c611adc06b 100644 --- a/src/libsyntax/util/literal.rs +++ b/src/libsyntax/util/literal.rs @@ -188,6 +188,8 @@ impl Lit { } /// Converts arbitrary token into an AST literal. + /// + /// Keep this in sync with `Token::can_begin_literal_or_bool`. pub fn from_token(token: &Token) -> Result<Lit, LitError> { let lit = match token.kind { token::Ident(name, false) if name.is_bool_lit() => { diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs index 73e731397c3..96149ad7947 100644 --- a/src/libsyntax/visit.rs +++ b/src/libsyntax/visit.rs @@ -215,7 +215,7 @@ pub trait Visitor<'ast>: Sized { fn visit_vis(&mut self, vis: &'ast Visibility) { walk_vis(self, vis) } - fn visit_fn_ret_ty(&mut self, ret_ty: &'ast FunctionRetTy) { + fn visit_fn_ret_ty(&mut self, ret_ty: &'ast FnRetTy) { walk_fn_ret_ty(self, ret_ty) } fn visit_fn_header(&mut self, _header: &'ast FnHeader) { @@ -298,11 +298,11 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) { } } ItemKind::Use(ref use_tree) => visitor.visit_use_tree(use_tree, item.id, false), - ItemKind::Static(ref typ, _, ref expr) | ItemKind::Const(ref typ, ref expr) => { + ItemKind::Static(ref typ, _, ref expr) | ItemKind::Const(_, ref typ, ref expr) => { visitor.visit_ty(typ); - visitor.visit_expr(expr); + walk_list!(visitor, visit_expr, expr); } - ItemKind::Fn(ref sig, ref generics, ref body) => { + ItemKind::Fn(_, ref sig, ref generics, ref body) => { visitor.visit_generics(generics); let kind = FnKind::Fn(FnCtxt::Free, item.ident, sig, &item.vis, body.as_deref()); visitor.visit_fn(kind, item.span, item.id) @@ -312,9 +312,10 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) { walk_list!(visitor, visit_foreign_item, &foreign_module.items); } ItemKind::GlobalAsm(ref ga) => visitor.visit_global_asm(ga), - ItemKind::TyAlias(ref typ, ref generics) => { - visitor.visit_ty(typ); - visitor.visit_generics(generics) + ItemKind::TyAlias(_, ref generics, ref bounds, ref ty) => { + visitor.visit_generics(generics); + walk_list!(visitor, visit_param_bound, bounds); + walk_list!(visitor, visit_ty, ty); } ItemKind::Enum(ref enum_definition, ref generics) => { visitor.visit_generics(generics); @@ -525,21 +526,8 @@ pub fn walk_pat<'a, V: Visitor<'a>>(visitor: &mut V, pattern: &'a Pat) { } pub fn walk_foreign_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a ForeignItem) { - visitor.visit_vis(&item.vis); - visitor.visit_ident(item.ident); - - match item.kind { - ForeignItemKind::Fn(ref sig, ref generics, ref body) => { - visitor.visit_generics(generics); - let kind = FnKind::Fn(FnCtxt::Foreign, item.ident, sig, &item.vis, body.as_deref()); - visitor.visit_fn(kind, item.span, item.id); - } - ForeignItemKind::Static(ref typ, _) => visitor.visit_ty(typ), - ForeignItemKind::Ty => (), - ForeignItemKind::Macro(ref mac) => visitor.visit_mac(mac), - } - - walk_list!(visitor, visit_attribute, &item.attrs); + let Item { id, span, ident, vis, attrs, kind, tokens: _ } = item; + walk_nested_item(visitor, *id, *span, *ident, vis, attrs, kind, FnCtxt::Foreign); } pub fn walk_global_asm<'a, V: Visitor<'a>>(_: &mut V, _: &'a GlobalAsm) { @@ -594,8 +582,8 @@ pub fn walk_where_predicate<'a, V: Visitor<'a>>(visitor: &mut V, predicate: &'a } } -pub fn walk_fn_ret_ty<'a, V: Visitor<'a>>(visitor: &mut V, ret_ty: &'a FunctionRetTy) { - if let FunctionRetTy::Ty(ref output_ty) = *ret_ty { +pub fn walk_fn_ret_ty<'a, V: Visitor<'a>>(visitor: &mut V, ret_ty: &'a FnRetTy) { + if let FnRetTy::Ty(ref output_ty) = *ret_ty { visitor.visit_ty(output_ty) } } @@ -622,24 +610,39 @@ pub fn walk_fn<'a, V: Visitor<'a>>(visitor: &mut V, kind: FnKind<'a>, _span: Spa } pub fn walk_assoc_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem, ctxt: AssocCtxt) { - visitor.visit_vis(&item.vis); - visitor.visit_ident(item.ident); - walk_list!(visitor, visit_attribute, &item.attrs); - visitor.visit_generics(&item.generics); - match item.kind { - AssocItemKind::Const(ref ty, ref expr) => { + let Item { id, span, ident, vis, attrs, kind, tokens: _ } = item; + walk_nested_item(visitor, *id, *span, *ident, vis, attrs, kind, FnCtxt::Assoc(ctxt)); +} + +fn walk_nested_item<'a, V: Visitor<'a>>( + visitor: &mut V, + id: NodeId, + span: Span, + ident: Ident, + vis: &'a Visibility, + attrs: &'a [Attribute], + kind: &'a AssocItemKind, + ctxt: FnCtxt, +) { + visitor.visit_vis(vis); + visitor.visit_ident(ident); + walk_list!(visitor, visit_attribute, attrs); + match kind { + AssocItemKind::Const(_, ty, expr) | AssocItemKind::Static(ty, _, expr) => { visitor.visit_ty(ty); walk_list!(visitor, visit_expr, expr); } - AssocItemKind::Fn(ref sig, ref body) => { - let kind = FnKind::Fn(FnCtxt::Assoc(ctxt), item.ident, sig, &item.vis, body.as_deref()); - visitor.visit_fn(kind, item.span, item.id); + AssocItemKind::Fn(_, sig, generics, body) => { + visitor.visit_generics(generics); + let kind = FnKind::Fn(ctxt, ident, sig, vis, body.as_deref()); + visitor.visit_fn(kind, span, id); } - AssocItemKind::TyAlias(ref bounds, ref ty) => { + AssocItemKind::TyAlias(_, generics, bounds, ty) => { + visitor.visit_generics(generics); walk_list!(visitor, visit_param_bound, bounds); walk_list!(visitor, visit_ty, ty); } - AssocItemKind::Macro(ref mac) => { + AssocItemKind::Macro(mac) => { visitor.visit_mac(mac); } } |
