From 1e9af9fa2eb6ba4e0ad1e2e275003ada7e5a27e2 Mon Sep 17 00:00:00 2001 From: Caleb Cartwright Date: Fri, 10 Apr 2020 03:52:07 -0500 Subject: [PATCH] bump rustc-ap-* crates to v651 (#4106) --- Cargo.lock | 183 ++++++++++++------ rustfmt-core/rustfmt-config/Cargo.toml | 2 +- rustfmt-core/rustfmt-lib/Cargo.toml | 25 +-- rustfmt-core/rustfmt-lib/src/attr.rs | 13 +- rustfmt-core/rustfmt-lib/src/chains.rs | 14 +- rustfmt-core/rustfmt-lib/src/closures.rs | 8 +- rustfmt-core/rustfmt-lib/src/expr.rs | 22 ++- rustfmt-core/rustfmt-lib/src/formatting.rs | 4 +- rustfmt-core/rustfmt-lib/src/imports.rs | 2 +- rustfmt-core/rustfmt-lib/src/items.rs | 119 ++++++------ rustfmt-core/rustfmt-lib/src/lib.rs | 2 +- rustfmt-core/rustfmt-lib/src/macros.rs | 28 +-- rustfmt-core/rustfmt-lib/src/matches.rs | 4 +- rustfmt-core/rustfmt-lib/src/modules.rs | 123 ++++++++---- .../rustfmt-lib/src/modules/visitor.rs | 16 +- rustfmt-core/rustfmt-lib/src/overflow.rs | 4 +- rustfmt-core/rustfmt-lib/src/pairs.rs | 2 +- rustfmt-core/rustfmt-lib/src/patterns.rs | 10 +- rustfmt-core/rustfmt-lib/src/reorder.rs | 2 +- rustfmt-core/rustfmt-lib/src/rewrite.rs | 2 +- rustfmt-core/rustfmt-lib/src/skip.rs | 4 +- rustfmt-core/rustfmt-lib/src/spanned.rs | 11 +- rustfmt-core/rustfmt-lib/src/stmt.rs | 4 +- rustfmt-core/rustfmt-lib/src/syntux/parser.rs | 86 +++----- .../rustfmt-lib/src/syntux/session.rs | 9 +- rustfmt-core/rustfmt-lib/src/types.rs | 26 +-- rustfmt-core/rustfmt-lib/src/utils.rs | 34 ++-- rustfmt-core/rustfmt-lib/src/vertical.rs | 2 +- rustfmt-core/rustfmt-lib/src/visitor.rs | 63 +++--- .../rustfmt-lib/tests/source/imports.rs | 12 +- .../tests/source/imports_2015_edition.rs | 12 +- .../rustfmt-lib/tests/target/imports.rs | 10 +- .../tests/target/imports_2015_edition.rs | 10 +- 33 files changed, 482 insertions(+), 386 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b909e5949ee..b95d9516274 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -488,7 +488,7 @@ checksum = "fef709d3257013bba7cff14fc504e07e80631d3fe0f6d38ce63b8f6510ccb932" dependencies = [ "byteorder", "memmap", - "parking_lot", + "parking_lot 0.9.0", "rustc-hash", ] @@ -543,10 +543,20 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f842b1982eb6c2fe34036a4fbfb06dd185a3f5c8edfaacdf7d1ea10b07de6252" dependencies = [ "lock_api", - "parking_lot_core", + "parking_lot_core 0.6.2", "rustc_version", ] +[[package]] +name = "parking_lot" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92e98c49ab0b7ce5b222f2cc9193fc4efe11c6d0bd4f648e374684a6857b1cfc" +dependencies = [ + "lock_api", + "parking_lot_core 0.7.0", +] + [[package]] name = "parking_lot_core" version = "0.6.2" @@ -562,6 +572,20 @@ dependencies = [ "winapi", ] +[[package]] +name = "parking_lot_core" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7582838484df45743c8434fbff785e8edf260c28748353d44bc0da32e0ceabf1" +dependencies = [ + "cfg-if", + "cloudabi", + "libc", + "redox_syscall", + "smallvec 1.1.0", + "winapi", +] + [[package]] name = "proc-macro-error" version = "0.4.4" @@ -699,9 +723,9 @@ dependencies = [ [[package]] name = "rustc-ap-arena" -version = "642.0.0" +version = "651.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ea82fa3d9a8add7422228ca1a2cbba0784fa8861f56148ff64da08b3c7921b03" +checksum = "632704fb93ca8148957191e5d2d827082f93c4aa20cdd242fb46d8cca57029da" dependencies = [ "rustc-ap-rustc_data_structures", "smallvec 1.1.0", @@ -709,28 +733,64 @@ dependencies = [ [[package]] name = "rustc-ap-graphviz" -version = "642.0.0" +version = "651.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bdd4689b814859c9f1b3e314ed2bde596acac428a256a16894635f600bed46b4" + +[[package]] +name = "rustc-ap-rustc_ast" +version = "651.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "638d0b2b3bcf99824e0cb5a25dbc547b61dc20942e11daf6a97e981918aa18e5" +checksum = "101c1517d3fd19d083aaca5b113f9965e6ae353a0bb09c49959b0f62b95b75d9" +dependencies = [ + "log", + "rustc-ap-rustc_data_structures", + "rustc-ap-rustc_index", + "rustc-ap-rustc_lexer", + "rustc-ap-rustc_macros", + "rustc-ap-rustc_span", + "rustc-ap-serialize", + "scoped-tls", + "smallvec 1.1.0", +] + +[[package]] +name = "rustc-ap-rustc_ast_passes" +version = "651.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ab3f5a7e939b37c99d8ca371f09b10bb5b5c85ad5d5b8d1d736ce8248c71be0" +dependencies = [ + "log", + "rustc-ap-rustc_ast", + "rustc-ap-rustc_ast_pretty", + "rustc-ap-rustc_attr", + "rustc-ap-rustc_data_structures", + "rustc-ap-rustc_errors", + "rustc-ap-rustc_feature", + "rustc-ap-rustc_parse", + "rustc-ap-rustc_session", + "rustc-ap-rustc_span", +] [[package]] name = "rustc-ap-rustc_ast_pretty" -version = "642.0.0" +version = "651.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d38bab04dd676dee6d2f9670506a18c31bfce38bf7f8420aa83eb1140ecde049" +checksum = "05046d3a2b8de22b20bcda9a1c063dc5c1f2f721f042b6c2809df2d23c64a13e" dependencies = [ "log", + "rustc-ap-rustc_ast", "rustc-ap-rustc_data_structures", "rustc-ap-rustc_span", - "rustc-ap-syntax", ] [[package]] name = "rustc-ap-rustc_attr" -version = "642.0.0" +version = "651.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "10b843ba8b1ed43739133047673b9f6a54d3b3b4d328d69c6ea89ff971395f35" +checksum = "f00b7ccad6fc3628fb44950435772945a425575f9ea0b3708c536fe75623a6e8" dependencies = [ + "rustc-ap-rustc_ast", "rustc-ap-rustc_ast_pretty", "rustc-ap-rustc_data_structures", "rustc-ap-rustc_errors", @@ -739,26 +799,25 @@ dependencies = [ "rustc-ap-rustc_session", "rustc-ap-rustc_span", "rustc-ap-serialize", - "rustc-ap-syntax", "smallvec 1.1.0", ] [[package]] name = "rustc-ap-rustc_data_structures" -version = "642.0.0" +version = "651.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc3d1c6d0a80ab0c1df76405377cec0f3d5423fb5b0953a8eac70a2ad6c44df2" +checksum = "4c6121ab6766644fa76b711f65d4c39f2e335488ab768324567fed0ed191166e" dependencies = [ "bitflags", "cfg-if", - "crossbeam-utils 0.6.6", + "crossbeam-utils 0.7.0", "ena", "indexmap", "jobserver", "lazy_static", "log", "measureme", - "parking_lot", + "parking_lot 0.10.0", "rustc-ap-graphviz", "rustc-ap-rustc_index", "rustc-ap-serialize", @@ -772,9 +831,9 @@ dependencies = [ [[package]] name = "rustc-ap-rustc_errors" -version = "642.0.0" +version = "651.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4909a1eca29331332257230f29120a8ff68c9e37d868c564fcd599e430cf8914" +checksum = "adab84c842003ad1c8435fd71b8d0cc19bf0d702a8a2147d5be06e083db2d207" dependencies = [ "annotate-snippets", "atty", @@ -788,11 +847,33 @@ dependencies = [ "winapi", ] +[[package]] +name = "rustc-ap-rustc_expand" +version = "651.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb001df541ea02b65c8e294252530010c6f90e3c6a5716e8e24e58c12dd1cd86" +dependencies = [ + "log", + "rustc-ap-rustc_ast", + "rustc-ap-rustc_ast_passes", + "rustc-ap-rustc_ast_pretty", + "rustc-ap-rustc_attr", + "rustc-ap-rustc_data_structures", + "rustc-ap-rustc_errors", + "rustc-ap-rustc_feature", + "rustc-ap-rustc_lexer", + "rustc-ap-rustc_parse", + "rustc-ap-rustc_session", + "rustc-ap-rustc_span", + "rustc-ap-serialize", + "smallvec 1.1.0", +] + [[package]] name = "rustc-ap-rustc_feature" -version = "642.0.0" +version = "651.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "63ab887a181d795cf5fd3edadf367760deafb90aefb844f168ab5255266e3478" +checksum = "446cc60613cc3b05d0bdbcab7feb02305790b5617fa43c532d51ae3223d677a4" dependencies = [ "lazy_static", "rustc-ap-rustc_data_structures", @@ -801,15 +882,15 @@ dependencies = [ [[package]] name = "rustc-ap-rustc_fs_util" -version = "642.0.0" +version = "651.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70814116df3c5fbec8f06f6a1d013ca481f620fd22a9475754e9bf3ee9ba70d8" +checksum = "9ac99d6f67e7db3bb300895630e769ed41bd3e336c0e725870c70e676c1a5ff1" [[package]] name = "rustc-ap-rustc_index" -version = "642.0.0" +version = "651.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac1bf1d3cf3d119d41353d6fd229ef7272d5097bc0924de021c0294bf86d48bf" +checksum = "5608c1cf50d2251b7e10a138cf6dd388e97f139b21c00b06a22d06f89c6591f6" dependencies = [ "rustc-ap-serialize", "smallvec 1.1.0", @@ -817,20 +898,19 @@ dependencies = [ [[package]] name = "rustc-ap-rustc_lexer" -version = "642.0.0" +version = "651.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4cda21a32cebdc11ec4f5393aa2fcde5ed1b2f673a8571e5a4dcdf07e4ae9cac" +checksum = "74e9c1c6f5dc85977b3adb6fb556b2ff23354d1a12021da15eb1d36353458bde" dependencies = [ "unicode-xid", ] [[package]] name = "rustc-ap-rustc_macros" -version = "642.0.0" +version = "651.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75c47b48ea51910ecfd853c9248a9bf4c767bc823449ab6a1d864dff65fbae16" +checksum = "3226b5ec864312a5d23eb40a5d621ee06bdc0754228d20d6eb76d4ddc4f2d4a1" dependencies = [ - "itertools", "proc-macro2", "quote", "syn", @@ -839,33 +919,33 @@ dependencies = [ [[package]] name = "rustc-ap-rustc_parse" -version = "642.0.0" +version = "651.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "abd88e89cd5b5d28dcd3a347a3d534c08627d9455570dc1a2d402cb8437b9d30" +checksum = "ba3b042344c2280b50d5df0058d11379028a8f016a407e575bb3ea8b6c798049" dependencies = [ "bitflags", "log", + "rustc-ap-rustc_ast", "rustc-ap-rustc_ast_pretty", - "rustc-ap-rustc_attr", "rustc-ap-rustc_data_structures", "rustc-ap-rustc_errors", "rustc-ap-rustc_feature", "rustc-ap-rustc_lexer", "rustc-ap-rustc_session", "rustc-ap-rustc_span", - "rustc-ap-syntax", "smallvec 1.1.0", "unicode-normalization", ] [[package]] name = "rustc-ap-rustc_session" -version = "642.0.0" +version = "651.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b8487b4575fbb2d1fc6f1cd61225efd108a4d36817e6fb9b643d57fcae9cb12" +checksum = "ff35ef4b5d9fbcb2fd539c7c908eb3cdd1f68ddbccd042945ef50ae65564f941" dependencies = [ "log", "num_cpus", + "rustc-ap-rustc_ast", "rustc-ap-rustc_data_structures", "rustc-ap-rustc_errors", "rustc-ap-rustc_feature", @@ -874,14 +954,13 @@ dependencies = [ "rustc-ap-rustc_span", "rustc-ap-rustc_target", "rustc-ap-serialize", - "rustc-ap-syntax", ] [[package]] name = "rustc-ap-rustc_span" -version = "642.0.0" +version = "651.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f69746c0d4c21bf20a5bb2bd247261a1aa8631f04202d7303352942dde70d987" +checksum = "e323b1f4a824039886eed8e33cad20ea4f492a9f9b3c9441009797c91de3e87a" dependencies = [ "cfg-if", "log", @@ -896,9 +975,9 @@ dependencies = [ [[package]] name = "rustc-ap-rustc_target" -version = "642.0.0" +version = "651.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8bbc6ae09b5d42ec66edd520e8412e0615c53a7c93607fe33dc4abab60ba7c8b" +checksum = "e161eb7b3a5b7993c6b480135296dc61476db80041d49dd446422742426e390b" dependencies = [ "bitflags", "log", @@ -911,31 +990,14 @@ dependencies = [ [[package]] name = "rustc-ap-serialize" -version = "642.0.0" +version = "651.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e13a1ead0252fc3d96da4c336a95950be6795f2b00c84a67ccadf26142f8cb41" +checksum = "af510a659098d8c45a7303fb882fa780f4a87ec5f5d7a2053521e7d5d7f332c4" dependencies = [ "indexmap", "smallvec 1.1.0", ] -[[package]] -name = "rustc-ap-syntax" -version = "642.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e1f59f48ca3a2ec16a7e82e718ed5aadf9c9e08cf63015d28b4e774767524a6a" -dependencies = [ - "log", - "rustc-ap-rustc_data_structures", - "rustc-ap-rustc_index", - "rustc-ap-rustc_lexer", - "rustc-ap-rustc_macros", - "rustc-ap-rustc_span", - "rustc-ap-serialize", - "scoped-tls", - "smallvec 1.1.0", -] - [[package]] name = "rustc-demangle" version = "0.1.16" @@ -1056,13 +1118,14 @@ dependencies = [ "lazy_static", "log", "regex", + "rustc-ap-rustc_ast", "rustc-ap-rustc_ast_pretty", "rustc-ap-rustc_data_structures", "rustc-ap-rustc_errors", + "rustc-ap-rustc_expand", "rustc-ap-rustc_parse", "rustc-ap-rustc_session", "rustc-ap-rustc_span", - "rustc-ap-syntax", "rustfmt_configuration", "rustfmt_emitter", "term", diff --git a/rustfmt-core/rustfmt-config/Cargo.toml b/rustfmt-core/rustfmt-config/Cargo.toml index b4a8dc81deb..90b490f71f3 100644 --- a/rustfmt-core/rustfmt-config/Cargo.toml +++ b/rustfmt-core/rustfmt-config/Cargo.toml @@ -19,5 +19,5 @@ toml = "0.5" [dependencies.rustc_span] package = "rustc-ap-rustc_span" -version = "642.0.0" +version = "651.0.0" diff --git a/rustfmt-core/rustfmt-lib/Cargo.toml b/rustfmt-core/rustfmt-lib/Cargo.toml index 7ce58421738..09f62d67bf7 100644 --- a/rustfmt-core/rustfmt-lib/Cargo.toml +++ b/rustfmt-core/rustfmt-lib/Cargo.toml @@ -30,31 +30,34 @@ unicode-width = "0.1.5" [dev-dependencies] env_logger = "0.7" +[dependencies.rustc_ast] +package = "rustc-ap-rustc_ast" +version = "651.0.0" + [dependencies.rustc_ast_pretty] package = "rustc-ap-rustc_ast_pretty" -version = "642.0.0" +version = "651.0.0" [dependencies.rustc_data_structures] package = "rustc-ap-rustc_data_structures" -version = "642.0.0" +version = "651.0.0" [dependencies.rustc_errors] package = "rustc-ap-rustc_errors" -version = "642.0.0" +version = "651.0.0" + +[dependencies.rustc_expand] +package = "rustc-ap-rustc_expand" +version = "651.0.0" [dependencies.rustc_parse] package = "rustc-ap-rustc_parse" -version = "642.0.0" +version = "651.0.0" [dependencies.rustc_session] package = "rustc-ap-rustc_session" -version = "642.0.0" +version = "651.0.0" [dependencies.rustc_span] package = "rustc-ap-rustc_span" -version = "642.0.0" - -[dependencies.syntax] -package = "rustc-ap-syntax" -version = "642.0.0" - +version = "651.0.0" diff --git a/rustfmt-core/rustfmt-lib/src/attr.rs b/rustfmt-core/rustfmt-lib/src/attr.rs index 461f0ddf029..b9e160020fb 100644 --- a/rustfmt-core/rustfmt-lib/src/attr.rs +++ b/rustfmt-core/rustfmt-lib/src/attr.rs @@ -1,7 +1,8 @@ //! Format attributes and meta items. +use rustc_ast::ast; +use rustc_ast::attr::HasAttrs; use rustc_span::{symbol::sym, BytePos, Span, DUMMY_SP}; -use syntax::ast; use self::doc_comment::DocCommentFormatter; use crate::comment::{contains_comment, rewrite_doc_comment, CommentStyle}; @@ -19,12 +20,7 @@ mod doc_comment; /// Returns attributes on the given statement. pub(crate) fn get_attrs_from_stmt(stmt: &ast::Stmt) -> &[ast::Attribute] { - match stmt.kind { - ast::StmtKind::Local(ref local) => &local.attrs, - ast::StmtKind::Item(ref item) => &item.attrs, - ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => &expr.attrs, - ast::StmtKind::Mac(ref mac) => &mac.2, - } + stmt.attrs() } pub(crate) fn get_span_without_attrs(stmt: &ast::Stmt) -> Span { @@ -32,10 +28,11 @@ pub(crate) fn get_span_without_attrs(stmt: &ast::Stmt) -> Span { ast::StmtKind::Local(ref local) => local.span, ast::StmtKind::Item(ref item) => item.span, ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => expr.span, - ast::StmtKind::Mac(ref mac) => { + ast::StmtKind::MacCall(ref mac) => { let (ref mac, _, _) = **mac; mac.span() } + ast::StmtKind::Empty => stmt.span, } } diff --git a/rustfmt-core/rustfmt-lib/src/chains.rs b/rustfmt-core/rustfmt-lib/src/chains.rs index e4d65709ed6..ae7d04f7710 100644 --- a/rustfmt-core/rustfmt-lib/src/chains.rs +++ b/rustfmt-core/rustfmt-lib/src/chains.rs @@ -58,8 +58,8 @@ use std::borrow::Cow; use std::cmp::min; +use rustc_ast::{ast, ptr}; use rustc_span::{BytePos, Span}; -use syntax::{ast, ptr}; use crate::comment::{rewrite_comment, CharClasses, FullCodeCharKind, RichChar}; use crate::config::IndentStyle; @@ -148,7 +148,15 @@ impl ChainItemKind { ast::ExprKind::MethodCall(ref segment, ref expressions) => { let types = if let Some(ref generic_args) = segment.args { if let ast::GenericArgs::AngleBracketed(ref data) = **generic_args { - data.args.clone() + data.args + .iter() + .filter_map(|x| match x { + ast::AngleBracketedArg::Arg(ref generic_arg) => { + Some(generic_arg.clone()) + } + _ => None, + }) + .collect::>() } else { vec![] } @@ -396,7 +404,7 @@ impl Chain { fn convert_try(expr: &ast::Expr, context: &RewriteContext<'_>) -> ast::Expr { match expr.kind { - ast::ExprKind::Mac(ref mac) if context.config.use_try_shorthand() => { + ast::ExprKind::MacCall(ref mac) if context.config.use_try_shorthand() => { if let Some(subexpr) = convert_try_mac(mac, context) { subexpr } else { diff --git a/rustfmt-core/rustfmt-lib/src/closures.rs b/rustfmt-core/rustfmt-lib/src/closures.rs index 4c3c1c658ac..6b3140947b1 100644 --- a/rustfmt-core/rustfmt-lib/src/closures.rs +++ b/rustfmt-core/rustfmt-lib/src/closures.rs @@ -1,5 +1,5 @@ +use rustc_ast::{ast, ptr}; use rustc_span::Span; -use syntax::{ast, ptr}; use crate::attr::get_attrs_from_stmt; use crate::config::lists::*; @@ -25,7 +25,7 @@ use crate::utils::{last_line_width, left_most_sub_expr, stmt_expr, NodeIdExt}; pub(crate) fn rewrite_closure( capture: ast::CaptureBy, - is_async: &ast::IsAsync, + is_async: &ast::Async, movability: ast::Movability, fn_decl: &ast::FnDecl, body: &ast::Expr, @@ -50,7 +50,7 @@ pub(crate) fn rewrite_closure( } let result = match fn_decl.output { - ast::FunctionRetTy::Default(_) if !context.inside_macro() => { + ast::FnRetTy::Default(_) if !context.inside_macro() => { try_rewrite_without_block(body, &prefix, capture, context, shape, body_shape) } _ => None, @@ -222,7 +222,7 @@ fn rewrite_closure_block( // Return type is (prefix, extra_offset) fn rewrite_closure_fn_decl( capture: ast::CaptureBy, - asyncness: &ast::IsAsync, + asyncness: &ast::Async, movability: ast::Movability, fn_decl: &ast::FnDecl, body: &ast::Expr, diff --git a/rustfmt-core/rustfmt-lib/src/expr.rs b/rustfmt-core/rustfmt-lib/src/expr.rs index ea33af366d1..afdf4b0a5e3 100644 --- a/rustfmt-core/rustfmt-lib/src/expr.rs +++ b/rustfmt-core/rustfmt-lib/src/expr.rs @@ -2,9 +2,9 @@ use std::borrow::Cow; use std::cmp::min; use itertools::Itertools; +use rustc_ast::token::{DelimToken, LitKind}; +use rustc_ast::{ast, ptr}; use rustc_span::{BytePos, Span}; -use syntax::token::{DelimToken, LitKind}; -use syntax::{ast, ptr}; use crate::chains::rewrite_chain; use crate::closures; @@ -197,7 +197,7 @@ pub(crate) fn format_expr( ast::ExprKind::Try(..) | ast::ExprKind::Field(..) | ast::ExprKind::MethodCall(..) => { rewrite_chain(expr, context, shape) } - ast::ExprKind::Mac(ref mac) => { + ast::ExprKind::MacCall(ref mac) => { rewrite_macro(mac, None, context, shape, MacroPosition::Expression).or_else(|| { wrap_str( context.snippet(expr.span).to_owned(), @@ -320,7 +320,7 @@ pub(crate) fn format_expr( } // We do not format these expressions yet, but they should still // satisfy our width restrictions. - ast::ExprKind::InlineAsm(..) => Some(context.snippet(expr.span).to_owned()), + ast::ExprKind::LlvmInlineAsm(..) => Some(context.snippet(expr.span).to_owned()), ast::ExprKind::TryBlock(ref block) => { if let rw @ Some(_) = rewrite_single_line_block(context, "try ", block, Some(&expr.attrs), None, shape) @@ -1191,15 +1191,17 @@ pub(crate) fn is_simple_block_stmt( } fn block_has_statements(block: &ast::Block) -> bool { - block.stmts.iter().any(|stmt| { - if let ast::StmtKind::Semi(ref expr) = stmt.kind { + block.stmts.iter().any(|stmt| match stmt.kind { + ast::StmtKind::Semi(ref expr) => { if let ast::ExprKind::Tup(ref tup_exprs) = expr.kind { if tup_exprs.is_empty() { return false; } } + true } - true + ast::StmtKind::Empty => false, + _ => true, }) } @@ -1349,9 +1351,9 @@ pub(crate) fn can_be_overflowed_expr( context.config.overflow_delimited_expr() || (context.use_block_indent() && args_len == 1) } - ast::ExprKind::Mac(ref mac) => { + ast::ExprKind::MacCall(ref mac) => { match ( - syntax::ast::MacDelimiter::from_token(mac.args.delim()), + rustc_ast::ast::MacDelimiter::from_token(mac.args.delim()), context.config.overflow_delimited_expr(), ) { (Some(ast::MacDelimiter::Bracket), true) @@ -1377,7 +1379,7 @@ pub(crate) fn can_be_overflowed_expr( pub(crate) fn is_nested_call(expr: &ast::Expr) -> bool { match expr.kind { - ast::ExprKind::Call(..) | ast::ExprKind::Mac(..) => true, + ast::ExprKind::Call(..) | ast::ExprKind::MacCall(..) => true, ast::ExprKind::AddrOf(_, _, ref expr) | ast::ExprKind::Box(ref expr) | ast::ExprKind::Try(ref expr) diff --git a/rustfmt-core/rustfmt-lib/src/formatting.rs b/rustfmt-core/rustfmt-lib/src/formatting.rs index 14040a2c429..529e4004a56 100644 --- a/rustfmt-core/rustfmt-lib/src/formatting.rs +++ b/rustfmt-core/rustfmt-lib/src/formatting.rs @@ -4,8 +4,8 @@ use std::collections::HashMap; use std::io::{self, Write}; use std::time::{Duration, Instant}; +use rustc_ast::ast; use rustc_span::Span; -use syntax::ast; use self::newline_style::apply_newline_style; use crate::comment::{CharClasses, FullCodeCharKind}; @@ -29,7 +29,7 @@ impl<'b, T: Write + 'b> Session<'b, T> { return Err(ErrorKind::VersionMismatch); } - syntax::with_globals(self.config.edition().into(), || { + rustc_ast::with_globals(self.config.edition().into(), || { if self.config.disable_all_formatting() { // When the input is from stdin, echo back the input. if let Input::Text(ref buf) = input { diff --git a/rustfmt-core/rustfmt-lib/src/imports.rs b/rustfmt-core/rustfmt-lib/src/imports.rs index c9f545d693a..94cc419eee5 100644 --- a/rustfmt-core/rustfmt-lib/src/imports.rs +++ b/rustfmt-core/rustfmt-lib/src/imports.rs @@ -2,8 +2,8 @@ use std::borrow::Cow; use std::cmp::Ordering; use std::fmt; +use rustc_ast::ast::{self, UseTreeKind}; use rustc_span::{source_map, symbol::sym, BytePos, Span, DUMMY_SP}; -use syntax::ast::{self, UseTreeKind}; use crate::comment::combine_strs_with_missing_comments; use crate::config::lists::*; diff --git a/rustfmt-core/rustfmt-lib/src/items.rs b/rustfmt-core/rustfmt-lib/src/items.rs index c46e2659641..98554f15deb 100644 --- a/rustfmt-core/rustfmt-lib/src/items.rs +++ b/rustfmt-core/rustfmt-lib/src/items.rs @@ -4,9 +4,9 @@ use std::borrow::Cow; use std::cmp::{max, min, Ordering}; use regex::Regex; +use rustc_ast::visit; +use rustc_ast::{ast, ptr}; use rustc_span::{source_map, symbol, BytePos, Span, DUMMY_SP}; -use syntax::visit; -use syntax::{ast, ptr}; use crate::attr::filter_inline_attrs; use crate::comment::{ @@ -223,10 +223,10 @@ pub(crate) struct FnSig<'a> { decl: &'a ast::FnDecl, generics: &'a ast::Generics, ext: ast::Extern, - is_async: Cow<'a, ast::IsAsync>, - constness: ast::Constness, + is_async: Cow<'a, ast::Async>, + constness: ast::Const, defaultness: ast::Defaultness, - unsafety: ast::Unsafety, + unsafety: ast::Unsafe, visibility: ast::Visibility, } @@ -240,10 +240,10 @@ impl<'a> FnSig<'a> { decl, generics, ext: ast::Extern::None, - is_async: Cow::Owned(ast::IsAsync::NotAsync), - constness: ast::Constness::NotConst, + is_async: Cow::Owned(ast::Async::No), + constness: ast::Const::No, defaultness: ast::Defaultness::Final, - unsafety: ast::Unsafety::Normal, + unsafety: ast::Unsafe::No, visibility: vis, } } @@ -254,8 +254,8 @@ impl<'a> FnSig<'a> { ) -> FnSig<'a> { FnSig { unsafety: method_sig.header.unsafety, - is_async: Cow::Borrowed(&method_sig.header.asyncness.node), - constness: method_sig.header.constness.node, + is_async: Cow::Borrowed(&method_sig.header.asyncness), + constness: method_sig.header.constness, defaultness: ast::Defaultness::Final, ext: method_sig.header.ext, decl: &*method_sig.decl, @@ -282,8 +282,8 @@ impl<'a> FnSig<'a> { decl, generics, ext: fn_sig.header.ext, - constness: fn_sig.header.constness.node, - is_async: Cow::Borrowed(&fn_sig.header.asyncness.node), + constness: fn_sig.header.constness, + is_async: Cow::Borrowed(&fn_sig.header.asyncness), defaultness, unsafety: fn_sig.header.unsafety, visibility: vis.clone(), @@ -356,8 +356,14 @@ impl<'a> FmtVisitor<'a> { fn format_foreign_item(&mut self, item: &ast::ForeignItem) { let rewrite = item.rewrite(&self.get_context(), self.shape()); - self.push_rewrite(item.span(), rewrite); - self.last_pos = item.span.hi(); + let hi = item.span.hi(); + let span = if item.attrs.is_empty() { + item.span + } else { + mk_sp(item.attrs[0].span.lo(), hi) + }; + self.push_rewrite(span, rewrite); + self.last_pos = hi; } pub(crate) fn rewrite_fn_before_block( @@ -646,14 +652,14 @@ impl<'a> FmtVisitor<'a> { self.buffer.clear(); } - fn is_type(ty: &Option>) -> bool { + fn is_type(ty: &Option>) -> bool { match ty { None => true, Some(lty) => lty.kind.opaque_top_hack().is_none(), } } - fn is_opaque(ty: &Option>) -> bool { + fn is_opaque(ty: &Option>) -> bool { match ty { None => false, Some(lty) => lty.kind.opaque_top_hack().is_some(), @@ -661,15 +667,15 @@ impl<'a> FmtVisitor<'a> { } fn both_type( - a: &Option>, - b: &Option>, + a: &Option>, + b: &Option>, ) -> bool { is_type(a) && is_type(b) } fn both_opaque( - a: &Option>, - b: &Option>, + a: &Option>, + b: &Option>, ) -> bool { is_opaque(a) && is_opaque(b) } @@ -681,7 +687,7 @@ impl<'a> FmtVisitor<'a> { use crate::ast::AssocItemKind::*; fn need_empty_line(a: &ast::AssocItemKind, b: &ast::AssocItemKind) -> bool { match (a, b) { - (TyAlias(_, ref lty), TyAlias(_, ref rty)) + (TyAlias(_, _, _, ref lty), TyAlias(_, _, _, ref rty)) if both_type(lty, rty) || both_opaque(lty, rty) => { false @@ -692,23 +698,23 @@ impl<'a> FmtVisitor<'a> { } buffer.sort_by(|(_, a), (_, b)| match (&a.kind, &b.kind) { - (TyAlias(_, ref lty), TyAlias(_, ref rty)) + (TyAlias(_, _, _, ref lty), TyAlias(_, _, _, ref rty)) if both_type(lty, rty) || both_opaque(lty, rty) => { a.ident.as_str().cmp(&b.ident.as_str()) } - (Const(..), Const(..)) | (Macro(..), Macro(..)) => { + (Const(..), Const(..)) | (MacCall(..), MacCall(..)) => { a.ident.as_str().cmp(&b.ident.as_str()) } (Fn(..), Fn(..)) => a.span.lo().cmp(&b.span.lo()), - (TyAlias(_, ref ty), _) if is_type(ty) => Ordering::Less, - (_, TyAlias(_, ref ty)) if is_type(ty) => Ordering::Greater, + (TyAlias(_, _, _, ref ty), _) if is_type(ty) => Ordering::Less, + (_, TyAlias(_, _, _, ref ty)) if is_type(ty) => Ordering::Greater, (TyAlias(..), _) => Ordering::Less, (_, TyAlias(..)) => Ordering::Greater, (Const(..), _) => Ordering::Less, (_, Const(..)) => Ordering::Greater, - (Macro(..), _) => Ordering::Less, - (_, Macro(..)) => Ordering::Greater, + (MacCall(..), _) => Ordering::Less, + (_, MacCall(..)) => Ordering::Greater, }); let mut prev_kind = None; for (buf, item) in buffer { @@ -912,10 +918,9 @@ fn format_impl_ref_and_type( let generics_str = rewrite_generics(context, "impl", generics, shape)?; result.push_str(&generics_str); - let polarity_str = if polarity == ast::ImplPolarity::Negative { - "!" - } else { - "" + let polarity_str = match polarity { + ast::ImplPolarity::Negative(_) => "!", + ast::ImplPolarity::Positive => "", }; if let Some(ref trait_ref) = *trait_ref { @@ -1760,9 +1765,13 @@ pub(crate) struct StaticParts<'a> { impl<'a> StaticParts<'a> { pub(crate) fn from_item(item: &'a ast::Item) -> Self { - let (prefix, ty, mutability, expr) = match item.kind { - ast::ItemKind::Static(ref ty, mutability, ref expr) => ("static", ty, mutability, expr), - ast::ItemKind::Const(ref ty, ref expr) => ("const", ty, ast::Mutability::Not, expr), + let (defaultness, prefix, ty, mutability, expr) = match item.kind { + ast::ItemKind::Static(ref ty, mutability, ref expr) => { + (None, "static", ty, mutability, expr) + } + ast::ItemKind::Const(defaultness, ref ty, ref expr) => { + (Some(defaultness), "const", ty, ast::Mutability::Not, expr) + } _ => unreachable!(), }; StaticParts { @@ -1771,15 +1780,17 @@ impl<'a> StaticParts<'a> { ident: item.ident, ty, mutability, - expr_opt: Some(expr), - defaultness: None, + expr_opt: expr.as_ref(), + defaultness, span: item.span, } } pub(crate) fn from_trait_item(ti: &'a ast::AssocItem) -> Self { - let (ty, expr_opt) = match ti.kind { - ast::AssocItemKind::Const(ref ty, ref expr_opt) => (ty, expr_opt), + let (defaultness, ty, expr_opt) = match ti.kind { + ast::AssocItemKind::Const(defaultness, ref ty, ref expr_opt) => { + (defaultness, ty, expr_opt) + } _ => unreachable!(), }; StaticParts { @@ -1789,14 +1800,14 @@ impl<'a> StaticParts<'a> { ty, mutability: ast::Mutability::Not, expr_opt: expr_opt.as_ref(), - defaultness: None, + defaultness: Some(defaultness), span: ti.span, } } pub(crate) fn from_impl_item(ii: &'a ast::AssocItem) -> Self { - let (ty, expr) = match ii.kind { - ast::AssocItemKind::Const(ref ty, ref expr) => (ty, expr), + let (defaultness, ty, expr) = match ii.kind { + ast::AssocItemKind::Const(defaultness, ref ty, ref expr) => (defaultness, ty, expr), _ => unreachable!(), }; StaticParts { @@ -1806,7 +1817,7 @@ impl<'a> StaticParts<'a> { ty, mutability: ast::Mutability::Not, expr_opt: expr.as_ref(), - defaultness: Some(ii.defaultness), + defaultness: Some(defaultness), span: ii.span, } } @@ -1970,16 +1981,16 @@ pub(crate) fn rewrite_associated_impl_type( let result = rewrite_associated_type(ident, ty_opt, generics, None, context, indent)?; match defaultness { - ast::Defaultness::Default => Some(format!("default {}", result)), + ast::Defaultness::Default(..) => Some(format!("default {}", result)), _ => Some(result), } } -impl Rewrite for ast::FunctionRetTy { +impl Rewrite for ast::FnRetTy { fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option { match *self { - ast::FunctionRetTy::Default(_) => Some(String::new()), - ast::FunctionRetTy::Ty(ref ty) => { + ast::FnRetTy::Default(_) => Some(String::new()), + ast::FnRetTy::Ty(ref ty) => { if context.config.indent_style() == IndentStyle::Visual { let inner_width = shape.width.checked_sub(3)?; return ty @@ -2348,7 +2359,7 @@ fn rewrite_fn_base( } // Return type. - if let ast::FunctionRetTy::Ty(..) = fd.output { + if let ast::FnRetTy::Ty(..) = fd.output { let ret_should_indent = match context.config.indent_style() { // If our params are block layout then we surely must have space. IndentStyle::Block if put_params_in_block || fd.inputs.is_empty() => false, @@ -2448,8 +2459,8 @@ fn rewrite_fn_base( } let pos_before_where = match fd.output { - ast::FunctionRetTy::Default(..) => params_span.hi(), - ast::FunctionRetTy::Ty(ref ty) => ty.span.hi(), + ast::FnRetTy::Default(..) => params_span.hi(), + ast::FnRetTy::Ty(ref ty) => ty.span.hi(), }; let is_params_multi_lined = param_str.contains('\n'); @@ -2477,7 +2488,7 @@ fn rewrite_fn_base( // If there are neither where-clause nor return type, we may be missing comments between // params and `{`. if where_clause_str.is_empty() { - if let ast::FunctionRetTy::Default(ret_span) = fd.output { + if let ast::FnRetTy::Default(ret_span) = fd.output { match recover_missing_comment_in_span( mk_sp(params_span.hi(), ret_span.hi()), shape, @@ -3121,7 +3132,7 @@ impl Rewrite for ast::ForeignItem { let span = mk_sp(self.span.lo(), self.span.hi() - BytePos(1)); let item_str = match self.kind { - ast::ForeignItemKind::Fn(ref fn_sig, ref generics, _) => rewrite_fn_base( + ast::ForeignItemKind::Fn(_, ref fn_sig, ref generics, _) => rewrite_fn_base( context, shape.indent, self.ident, @@ -3130,7 +3141,7 @@ impl Rewrite for ast::ForeignItem { FnBraceStyle::None, ) .map(|(s, _)| format!("{};", s)), - ast::ForeignItemKind::Static(ref ty, mutability) => { + ast::ForeignItemKind::Static(ref ty, mutability, _) => { // FIXME(#21): we're dropping potential comments in between the // function kw here. let vis = format_visibility(context, &self.vis); @@ -3144,7 +3155,7 @@ impl Rewrite for ast::ForeignItem { // 1 = ; rewrite_assign_rhs(context, prefix, &**ty, shape.sub_width(1)?).map(|s| s + ";") } - ast::ForeignItemKind::Ty => { + ast::ForeignItemKind::TyAlias(..) => { let vis = format_visibility(context, &self.vis); Some(format!( "{}type {};", @@ -3152,7 +3163,7 @@ impl Rewrite for ast::ForeignItem { rewrite_ident(context, self.ident) )) } - ast::ForeignItemKind::Macro(ref mac) => { + ast::ForeignItemKind::MacCall(ref mac) => { rewrite_macro(mac, None, context, shape, MacroPosition::Item) } }?; diff --git a/rustfmt-core/rustfmt-lib/src/lib.rs b/rustfmt-core/rustfmt-lib/src/lib.rs index adbe23afbf0..a6fe9c1a8c0 100644 --- a/rustfmt-core/rustfmt-lib/src/lib.rs +++ b/rustfmt-core/rustfmt-lib/src/lib.rs @@ -15,8 +15,8 @@ use std::panic; use std::path::PathBuf; use std::rc::Rc; +use rustc_ast::ast; use rustfmt_configuration as config; -use syntax::ast; use thiserror::Error; use crate::comment::LineClasses; diff --git a/rustfmt-core/rustfmt-lib/src/macros.rs b/rustfmt-core/rustfmt-lib/src/macros.rs index 7637b7c5332..b1f2004f8be 100644 --- a/rustfmt-core/rustfmt-lib/src/macros.rs +++ b/rustfmt-core/rustfmt-lib/src/macros.rs @@ -12,12 +12,12 @@ use std::collections::HashMap; use std::panic::{catch_unwind, AssertUnwindSafe}; +use rustc_ast::token::{BinOpToken, DelimToken, Token, TokenKind}; +use rustc_ast::tokenstream::{Cursor, TokenStream, TokenTree}; +use rustc_ast::{ast, ptr}; use rustc_ast_pretty::pprust; use rustc_parse::{new_parser_from_tts, parser::Parser}; use rustc_span::{symbol::kw, BytePos, Span, Symbol, DUMMY_SP}; -use syntax::token::{BinOpToken, DelimToken, Token, TokenKind}; -use syntax::tokenstream::{Cursor, TokenStream, TokenTree}; -use syntax::{ast, ptr}; use crate::comment::{ contains_comment, CharClasses, FindUncommented, FullCodeCharKind, LineClasses, @@ -187,7 +187,7 @@ fn return_macro_parse_failure_fallback( } pub(crate) fn rewrite_macro( - mac: &ast::Mac, + mac: &ast::MacCall, extra_ident: Option, context: &RewriteContext<'_>, shape: Shape, @@ -230,7 +230,7 @@ fn check_keyword<'a, 'b: 'a>(parser: &'a mut Parser<'b>) -> Option { parser.bump(); return Some(MacroArg::Keyword( ast::Ident::with_dummy_span(keyword), - parser.prev_span, + parser.prev_token.span, )); } } @@ -238,7 +238,7 @@ fn check_keyword<'a, 'b: 'a>(parser: &'a mut Parser<'b>) -> Option { } fn rewrite_macro_inner( - mac: &ast::Mac, + mac: &ast::MacCall, extra_ident: Option, context: &RewriteContext<'_>, shape: Shape, @@ -493,7 +493,7 @@ pub(crate) fn rewrite_macro_def( None => return snippet, }; - let mut result = if def.legacy { + let mut result = if def.macro_rules { String::from("macro_rules!") } else { format!("{}macro", format_visibility(context, vis)) @@ -502,7 +502,7 @@ pub(crate) fn rewrite_macro_def( result += " "; result += rewrite_ident(context, ident); - let multi_branch_style = def.legacy || parsed_def.branches.len() != 1; + let multi_branch_style = def.macro_rules || parsed_def.branches.len() != 1; let arm_shape = if multi_branch_style { shape @@ -535,7 +535,7 @@ pub(crate) fn rewrite_macro_def( .collect::>(); let fmt = ListFormatting::new(arm_shape, context.config) - .separator(if def.legacy { ";" } else { "" }) + .separator(if def.macro_rules { ";" } else { "" }) .trailing_separator(SeparatorTactic::Always) .preserve_newline(true); @@ -1184,7 +1184,10 @@ fn next_space(tok: &TokenKind) -> SpaceState { /// Tries to convert a macro use into a short hand try expression. Returns `None` /// when the macro is not an instance of `try!` (or parsing the inner expression /// failed). -pub(crate) fn convert_try_mac(mac: &ast::Mac, context: &RewriteContext<'_>) -> Option { +pub(crate) fn convert_try_mac( + mac: &ast::MacCall, + context: &RewriteContext<'_>, +) -> Option { // The `try!` macro was deprecated in Rust 1.39.0 and `try` is a // reserved keyword in the 2018 Edition so the raw identifier // `r#try!` must be used in the 2018 Edition. @@ -1210,7 +1213,7 @@ pub(crate) fn convert_try_mac(mac: &ast::Mac, context: &RewriteContext<'_>) -> O } } -pub(crate) fn macro_style(mac: &ast::Mac, context: &RewriteContext<'_>) -> DelimToken { +pub(crate) fn macro_style(mac: &ast::MacCall, context: &RewriteContext<'_>) -> DelimToken { let snippet = context.snippet(mac.span()); let paren_pos = snippet.find_uncommented("(").unwrap_or(usize::max_value()); let bracket_pos = snippet.find_uncommented("[").unwrap_or(usize::max_value()); @@ -1492,7 +1495,8 @@ fn format_lazy_static( )?); result.push(';'); if parser.token.kind != TokenKind::Eof { - let snippet = context.snippet(mk_sp(parser.prev_span.hi(), parser.token.span.lo())); + let snippet = + context.snippet(mk_sp(parser.prev_token.span.hi(), parser.token.span.lo())); if count_newlines(snippet) >= 2 { result.push('\n'); } diff --git a/rustfmt-core/rustfmt-lib/src/matches.rs b/rustfmt-core/rustfmt-lib/src/matches.rs index d6072655d15..77cd3335953 100644 --- a/rustfmt-core/rustfmt-lib/src/matches.rs +++ b/rustfmt-core/rustfmt-lib/src/matches.rs @@ -2,8 +2,8 @@ use std::iter::repeat; +use rustc_ast::{ast, ptr}; use rustc_span::{BytePos, Span}; -use syntax::{ast, ptr}; use crate::comment::{combine_strs_with_missing_comments, rewrite_comment}; use crate::config::lists::*; @@ -572,7 +572,7 @@ fn can_flatten_block_around_this(body: &ast::Expr) -> bool { | ast::ExprKind::Array(..) | ast::ExprKind::Call(..) | ast::ExprKind::MethodCall(..) - | ast::ExprKind::Mac(..) + | ast::ExprKind::MacCall(..) | ast::ExprKind::Struct(..) | ast::ExprKind::Tup(..) => true, ast::ExprKind::AddrOf(_, _, ref expr) diff --git a/rustfmt-core/rustfmt-lib/src/modules.rs b/rustfmt-core/rustfmt-lib/src/modules.rs index c3ed27a35f1..0c932e78023 100644 --- a/rustfmt-core/rustfmt-lib/src/modules.rs +++ b/rustfmt-core/rustfmt-lib/src/modules.rs @@ -2,9 +2,9 @@ use std::borrow::Cow; use std::collections::BTreeMap; use std::path::{Path, PathBuf}; +use rustc_ast::ast; +use rustc_ast::visit::Visitor; use rustc_span::symbol::{sym, Symbol}; -use syntax::ast; -use syntax::visit::Visitor; use crate::attr::MetaVisitor; use crate::config::FileName; @@ -32,7 +32,7 @@ pub(crate) struct ModResolver<'ast, 'sess> { #[derive(Clone)] enum SubModKind<'a, 'ast> { /// `mod foo;` - External(PathBuf, DirectoryOwnership), + External(PathBuf, DirectoryOwnership, Cow<'ast, ast::Mod>), /// `mod foo;` with multiple sources. MultiExternal(Vec<(PathBuf, DirectoryOwnership, Cow<'ast, ast::Mod>)>), /// `#[path = "..."] mod foo {}` @@ -82,7 +82,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> { /// Visit `cfg_if` macro and look for module declarations. fn visit_cfg_if(&mut self, item: Cow<'ast, ast::Item>) -> Result<(), String> { - let mut visitor = visitor::CfgIfVisitor::new(self.parse_sess, &self.directory); + let mut visitor = visitor::CfgIfVisitor::new(self.parse_sess); visitor.visit_item(&item); for module_item in visitor.mods() { if let ast::ItemKind::Mod(ref sub_mod) = module_item.item.kind { @@ -150,7 +150,6 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> { // mod foo; // Look for an extern file. self.find_external_module(item.ident, &item.attrs, sub_mod) - .map(Some) } else { // An internal module (`mod foo { /* ... */ }`); if let Some(path) = find_path_value(&item.attrs) { @@ -165,15 +164,19 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> { fn insert_sub_mod( &mut self, sub_mod_kind: SubModKind<'c, 'ast>, - sub_mod: Cow<'ast, ast::Mod>, + _sub_mod: Cow<'ast, ast::Mod>, ) -> Result<(), String> { match sub_mod_kind { - SubModKind::External(mod_path, _) => { - self.file_map.insert(FileName::Real(mod_path), sub_mod); + SubModKind::External(mod_path, _, sub_mod) => { + self.file_map + .entry(FileName::Real(mod_path)) + .or_insert(sub_mod); } SubModKind::MultiExternal(mods) => { for (mod_path, _, sub_mod) in mods { - self.file_map.insert(FileName::Real(mod_path), sub_mod); + self.file_map + .entry(FileName::Real(mod_path)) + .or_insert(sub_mod); } } _ => (), @@ -187,7 +190,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> { sub_mod_kind: SubModKind<'c, 'ast>, ) -> Result<(), String> { match sub_mod_kind { - SubModKind::External(mod_path, directory_ownership) => { + SubModKind::External(mod_path, directory_ownership, sub_mod) => { let directory = Directory { path: mod_path.parent().unwrap().to_path_buf(), ownership: directory_ownership, @@ -239,50 +242,87 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> { mod_name: ast::Ident, attrs: &[ast::Attribute], sub_mod: &Cow<'ast, ast::Mod>, - ) -> Result, String> { + ) -> Result>, String> { + let relative = match self.directory.ownership { + DirectoryOwnership::Owned { relative } => relative, + DirectoryOwnership::UnownedViaBlock | DirectoryOwnership::UnownedViaMod => None, + }; if let Some(path) = Parser::submod_path_from_attr(attrs, &self.directory.path) { - return Ok(SubModKind::External( - path, - DirectoryOwnership::Owned { relative: None }, - )); + if self.parse_sess.is_file_parsed(&path) { + return Ok(None); + } + return match Parser::parse_file_as_module(self.parse_sess, &path, sub_mod.inner) { + Some(m) => Ok(Some(SubModKind::External( + path, + DirectoryOwnership::Owned { relative: None }, + Cow::Owned(m), + ))), + None => Err(format!( + "Failed to find module {} in {:?} {:?}", + mod_name, self.directory.path, relative, + )), + }; } // Look for nested path, like `#[cfg_attr(feature = "foo", path = "bar.rs")]`. let mut mods_outside_ast = self.find_mods_outside_of_ast(attrs, sub_mod); - let relative = match self.directory.ownership { - DirectoryOwnership::Owned { relative } => relative, - DirectoryOwnership::UnownedViaBlock | DirectoryOwnership::UnownedViaMod => None, - }; match self .parse_sess .default_submod_path(mod_name, relative, &self.directory.path) .result { Ok(ModulePathSuccess { - path, - directory_ownership, - .. + path, ownership, .. }) => { - if mods_outside_ast.is_empty() { - Ok(SubModKind::External(path, directory_ownership)) - } else { - let should_insert = !mods_outside_ast - .iter() - .any(|(outside_path, _, _)| outside_path == &path); - if should_insert { - mods_outside_ast.push((path, directory_ownership, sub_mod.clone())); + let outside_mods_empty = mods_outside_ast.is_empty(); + let should_insert = !mods_outside_ast + .iter() + .any(|(outside_path, _, _)| outside_path == &path); + if self.parse_sess.is_file_parsed(&path) { + if outside_mods_empty { + return Ok(None); + } else { + if should_insert { + mods_outside_ast.push((path, ownership, sub_mod.clone())); + } + return Ok(Some(SubModKind::MultiExternal(mods_outside_ast))); } - Ok(SubModKind::MultiExternal(mods_outside_ast)) } + match Parser::parse_file_as_module(self.parse_sess, &path, sub_mod.inner) { + Some(m) if outside_mods_empty => { + Ok(Some(SubModKind::External(path, ownership, Cow::Owned(m)))) + } + Some(m) => { + mods_outside_ast.push((path.clone(), ownership, Cow::Owned(m))); + if should_insert { + mods_outside_ast.push((path, ownership, sub_mod.clone())); + } + Ok(Some(SubModKind::MultiExternal(mods_outside_ast))) + } + None if outside_mods_empty => Err(format!( + "Failed to find module {} in {:?} {:?}", + mod_name, self.directory.path, relative, + )), + None => { + if should_insert { + mods_outside_ast.push((path, ownership, sub_mod.clone())); + } + Ok(Some(SubModKind::MultiExternal(mods_outside_ast))) + } + } + } + Err(mut e) if !mods_outside_ast.is_empty() => { + e.cancel(); + Ok(Some(SubModKind::MultiExternal(mods_outside_ast))) } - Err(_) if !mods_outside_ast.is_empty() => { - Ok(SubModKind::MultiExternal(mods_outside_ast)) + Err(mut e) => { + e.cancel(); + Err(format!( + "Failed to find module {} in {:?} {:?}", + mod_name, self.directory.path, relative, + )) } - Err(_) => Err(format!( - "Failed to find module {} in {:?} {:?}", - mod_name, self.directory.path, relative, - )), } } @@ -338,11 +378,8 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> { continue; } - let m = match Parser::parse_file_as_module( - self.directory.ownership, - self.parse_sess, - &actual_path, - ) { + let m = match Parser::parse_file_as_module(self.parse_sess, &actual_path, sub_mod.inner) + { Some(m) => m, None => continue, }; @@ -374,7 +411,7 @@ fn find_path_value(attrs: &[ast::Attribute]) -> Option { fn is_cfg_if(item: &ast::Item) -> bool { match item.kind { - ast::ItemKind::Mac(ref mac) => { + ast::ItemKind::MacCall(ref mac) => { if let Some(first_segment) = mac.path.segments.first() { if first_segment.ident.name == *CFG_IF { return true; diff --git a/rustfmt-core/rustfmt-lib/src/modules/visitor.rs b/rustfmt-core/rustfmt-lib/src/modules/visitor.rs index ad9111f7a9f..942e70d0e88 100644 --- a/rustfmt-core/rustfmt-lib/src/modules/visitor.rs +++ b/rustfmt-core/rustfmt-lib/src/modules/visitor.rs @@ -1,9 +1,9 @@ +use rustc_ast::ast; +use rustc_ast::visit::Visitor; use rustc_span::Symbol; -use syntax::ast; -use syntax::visit::Visitor; use crate::attr::MetaVisitor; -use crate::syntux::parser::{Directory, Parser}; +use crate::syntux::parser::Parser; use crate::syntux::session::ParseSess; pub(crate) struct ModItem { @@ -14,15 +14,13 @@ pub(crate) struct ModItem { pub(crate) struct CfgIfVisitor<'a> { parse_sess: &'a ParseSess, mods: Vec, - base_dir: &'a Directory, } impl<'a> CfgIfVisitor<'a> { - pub(crate) fn new(parse_sess: &'a ParseSess, base_dir: &'a Directory) -> CfgIfVisitor<'a> { + pub(crate) fn new(parse_sess: &'a ParseSess) -> CfgIfVisitor<'a> { CfgIfVisitor { mods: vec![], parse_sess, - base_dir, } } @@ -32,7 +30,7 @@ impl<'a> CfgIfVisitor<'a> { } impl<'a, 'ast: 'a> Visitor<'ast> for CfgIfVisitor<'a> { - fn visit_mac(&mut self, mac: &'ast ast::Mac) { + fn visit_mac(&mut self, mac: &'ast ast::MacCall) { match self.visit_mac_inner(mac) { Ok(()) => (), Err(e) => debug!("{}", e), @@ -41,7 +39,7 @@ impl<'a, 'ast: 'a> Visitor<'ast> for CfgIfVisitor<'a> { } impl<'a, 'ast: 'a> CfgIfVisitor<'a> { - fn visit_mac_inner(&mut self, mac: &'ast ast::Mac) -> Result<(), &'static str> { + fn visit_mac_inner(&mut self, mac: &'ast ast::MacCall) -> Result<(), &'static str> { // Support both: // ``` // extern crate cfg_if; @@ -64,7 +62,7 @@ impl<'a, 'ast: 'a> CfgIfVisitor<'a> { } }; - let items = Parser::parse_cfg_if(self.parse_sess, mac, &self.base_dir)?; + let items = Parser::parse_cfg_if(self.parse_sess, mac)?; self.mods .append(&mut items.into_iter().map(|item| ModItem { item }).collect()); diff --git a/rustfmt-core/rustfmt-lib/src/overflow.rs b/rustfmt-core/rustfmt-lib/src/overflow.rs index 6d90b065ea7..e52d5fa0889 100644 --- a/rustfmt-core/rustfmt-lib/src/overflow.rs +++ b/rustfmt-core/rustfmt-lib/src/overflow.rs @@ -3,9 +3,9 @@ use std::cmp::min; use itertools::Itertools; +use rustc_ast::token::DelimToken; +use rustc_ast::{ast, ptr}; use rustc_span::Span; -use syntax::token::DelimToken; -use syntax::{ast, ptr}; use crate::closures; use crate::config::lists::*; diff --git a/rustfmt-core/rustfmt-lib/src/pairs.rs b/rustfmt-core/rustfmt-lib/src/pairs.rs index 80393e851bc..354e7a4e1d3 100644 --- a/rustfmt-core/rustfmt-lib/src/pairs.rs +++ b/rustfmt-core/rustfmt-lib/src/pairs.rs @@ -1,4 +1,4 @@ -use syntax::ast; +use rustc_ast::ast; use crate::config::lists::*; use crate::config::IndentStyle; diff --git a/rustfmt-core/rustfmt-lib/src/patterns.rs b/rustfmt-core/rustfmt-lib/src/patterns.rs index 6cb62d732c6..ef4b00b73c0 100644 --- a/rustfmt-core/rustfmt-lib/src/patterns.rs +++ b/rustfmt-core/rustfmt-lib/src/patterns.rs @@ -1,6 +1,6 @@ +use rustc_ast::ast::{self, BindingMode, FieldPat, Pat, PatKind, RangeEnd, RangeSyntax}; +use rustc_ast::ptr; use rustc_span::{BytePos, Span}; -use syntax::ast::{self, BindingMode, FieldPat, Pat, PatKind, RangeEnd, RangeSyntax}; -use syntax::ptr; use crate::comment::{combine_strs_with_missing_comments, FindUncommented}; use crate::config::lists::*; @@ -40,7 +40,7 @@ fn is_short_pattern_inner(pat: &ast::Pat) -> bool { ast::PatKind::Rest | ast::PatKind::Wild | ast::PatKind::Lit(_) => true, ast::PatKind::Ident(_, _, ref pat) => pat.is_none(), ast::PatKind::Struct(..) - | ast::PatKind::Mac(..) + | ast::PatKind::MacCall(..) | ast::PatKind::Slice(..) | ast::PatKind::Path(..) | ast::PatKind::Range(..) => false, @@ -247,7 +247,9 @@ impl Rewrite for Pat { PatKind::Struct(ref path, ref fields, ellipsis) => { rewrite_struct_pat(path, fields, ellipsis, self.span, context, shape) } - PatKind::Mac(ref mac) => rewrite_macro(mac, None, context, shape, MacroPosition::Pat), + PatKind::MacCall(ref mac) => { + rewrite_macro(mac, None, context, shape, MacroPosition::Pat) + } PatKind::Paren(ref pat) => pat .rewrite(context, shape.offset_left(1)?.sub_width(1)?) .map(|inner_pat| format!("({})", inner_pat)), diff --git a/rustfmt-core/rustfmt-lib/src/reorder.rs b/rustfmt-core/rustfmt-lib/src/reorder.rs index 0235e552be7..3b9f2475acc 100644 --- a/rustfmt-core/rustfmt-lib/src/reorder.rs +++ b/rustfmt-core/rustfmt-lib/src/reorder.rs @@ -8,8 +8,8 @@ use std::cmp::{Ord, Ordering}; +use rustc_ast::{ast, attr}; use rustc_span::{symbol::sym, Span}; -use syntax::{ast, attr}; use crate::config::Config; use crate::imports::{merge_use_trees, UseTree}; diff --git a/rustfmt-core/rustfmt-lib/src/rewrite.rs b/rustfmt-core/rustfmt-lib/src/rewrite.rs index f73602f09a2..86b2e9ded1c 100644 --- a/rustfmt-core/rustfmt-lib/src/rewrite.rs +++ b/rustfmt-core/rustfmt-lib/src/rewrite.rs @@ -3,8 +3,8 @@ use std::cell::{Cell, RefCell}; use std::rc::Rc; +use rustc_ast::ptr; use rustc_span::Span; -use syntax::ptr; use crate::config::{Config, IndentStyle}; use crate::shape::Shape; diff --git a/rustfmt-core/rustfmt-lib/src/skip.rs b/rustfmt-core/rustfmt-lib/src/skip.rs index dcb4cf247d6..b187316fa33 100644 --- a/rustfmt-core/rustfmt-lib/src/skip.rs +++ b/rustfmt-core/rustfmt-lib/src/skip.rs @@ -1,7 +1,7 @@ //! Module that contains skip related stuffs. +use rustc_ast::ast::{Attribute, PathSegment}; use rustc_span::symbol::{sym, Symbol}; -use syntax::ast::{Attribute, PathSegment}; macro_rules! sym { ($tt:tt) => { @@ -29,7 +29,7 @@ impl SkipContext { } } for attr in attrs { - if let syntax::ast::AttrKind::Normal(ref attr_item) = &attr.kind { + if let rustc_ast::ast::AttrKind::Normal(ref attr_item) = &attr.kind { if is_skip_attr_with(&attr_item.path.segments, |s| s == sym!(macros)) { get_skip_names(&mut self.macros, attr) } else if is_skip_attr_with(&attr_item.path.segments, |s| s == sym::attributes) { diff --git a/rustfmt-core/rustfmt-lib/src/spanned.rs b/rustfmt-core/rustfmt-lib/src/spanned.rs index 19ed7137c85..976af78e3d1 100644 --- a/rustfmt-core/rustfmt-lib/src/spanned.rs +++ b/rustfmt-core/rustfmt-lib/src/spanned.rs @@ -1,7 +1,7 @@ use std::cmp::max; +use rustc_ast::{ast, ptr}; use rustc_span::{source_map, Span}; -use syntax::{ast, ptr}; use crate::macros::MacroArg; use crate::utils::{mk_sp, outer_attributes}; @@ -66,7 +66,7 @@ impl Spanned for ast::Stmt { ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => { mk_sp(expr.span().lo(), self.span.hi()) } - ast::StmtKind::Mac(ref mac) => { + ast::StmtKind::MacCall(ref mac) => { let (_, _, ref attrs) = **mac; if attrs.is_empty() { self.span @@ -74,6 +74,7 @@ impl Spanned for ast::Stmt { mk_sp(attrs[0].span.lo(), self.span.hi()) } } + ast::StmtKind::Empty => self.span, } } } @@ -151,11 +152,11 @@ impl Spanned for ast::WherePredicate { } } -impl Spanned for ast::FunctionRetTy { +impl Spanned for ast::FnRetTy { fn span(&self) -> Span { match *self { - ast::FunctionRetTy::Default(span) => span, - ast::FunctionRetTy::Ty(ref ty) => ty.span, + ast::FnRetTy::Default(span) => span, + ast::FnRetTy::Ty(ref ty) => ty.span, } } } diff --git a/rustfmt-core/rustfmt-lib/src/stmt.rs b/rustfmt-core/rustfmt-lib/src/stmt.rs index aebdd2b385c..0c289bb4f3e 100644 --- a/rustfmt-core/rustfmt-lib/src/stmt.rs +++ b/rustfmt-core/rustfmt-lib/src/stmt.rs @@ -1,5 +1,5 @@ +use rustc_ast::ast; use rustc_span::Span; -use syntax::ast; use crate::comment::recover_comment_removed; use crate::expr::{format_expr, ExprType}; @@ -105,7 +105,7 @@ fn format_stmt( let shape = shape.sub_width(suffix.len())?; format_expr(ex, expr_type, context, shape).map(|s| s + suffix) } - ast::StmtKind::Mac(..) | ast::StmtKind::Item(..) => None, + ast::StmtKind::MacCall(..) | ast::StmtKind::Item(..) | ast::StmtKind::Empty => None, }; result.and_then(|res| recover_comment_removed(res, stmt.span(), context)) } diff --git a/rustfmt-core/rustfmt-lib/src/syntux/parser.rs b/rustfmt-core/rustfmt-lib/src/syntux/parser.rs index 108a354a657..2964b530ad0 100644 --- a/rustfmt-core/rustfmt-lib/src/syntux/parser.rs +++ b/rustfmt-core/rustfmt-lib/src/syntux/parser.rs @@ -1,18 +1,17 @@ -use std::borrow::Cow; use std::panic::{catch_unwind, AssertUnwindSafe}; use std::path::{Path, PathBuf}; +use rustc_ast::ast; +use rustc_ast::token::{DelimToken, TokenKind}; use rustc_errors::{Diagnostic, PResult}; -use rustc_parse::{new_sub_parser_from_file, parser::Parser as RawParser}; -use rustc_span::{symbol::kw, Span, DUMMY_SP}; -use syntax::ast; -use syntax::token::{DelimToken, TokenKind}; +use rustc_parse::{new_parser_from_file, parser::Parser as RawParser}; +use rustc_span::{symbol::kw, Span}; use crate::syntux::session::ParseSess; use crate::{Config, Input}; -pub(crate) type DirectoryOwnership = rustc_parse::DirectoryOwnership; -pub(crate) type ModulePathSuccess = rustc_parse::parser::ModulePathSuccess; +pub(crate) type DirectoryOwnership = rustc_expand::module::DirectoryOwnership; +pub(crate) type ModulePathSuccess = rustc_expand::module::ModulePathSuccess; #[derive(Clone)] pub(crate) struct Directory { @@ -20,15 +19,6 @@ pub(crate) struct Directory { pub(crate) ownership: DirectoryOwnership, } -impl<'a> Directory { - fn to_syntax_directory(&'a self) -> rustc_parse::Directory<'a> { - rustc_parse::Directory { - path: Cow::Borrowed(&self.path), - ownership: self.ownership, - } - } -} - /// A parser for Rust source code. pub(crate) struct Parser<'a> { parser: RawParser<'a>, @@ -69,11 +59,10 @@ impl<'a> ParserBuilder<'a> { } pub(crate) fn build(self) -> Result, ParserError> { - let config = self.config.ok_or(ParserError::NoConfig)?; let sess = self.sess.ok_or(ParserError::NoParseSess)?; let input = self.input.ok_or(ParserError::NoInput)?; - let mut parser = match Self::parser(sess.inner(), input, self.directory_ownership) { + let parser = match Self::parser(sess.inner(), input) { Ok(p) => p, Err(db) => { sess.emit_diagnostics(db); @@ -81,45 +70,26 @@ impl<'a> ParserBuilder<'a> { } }; - parser.cfg_mods = false; - parser.recurse_into_file_modules = config.recursive(); - Ok(Parser { parser, sess }) } fn parser( sess: &'a rustc_session::parse::ParseSess, input: Input, - directory_ownership: Option, ) -> Result, Vec> { match input { - Input::File(ref file) => Ok(if let Some(directory_ownership) = directory_ownership { - rustc_parse::new_sub_parser_from_file( - sess, - file, - directory_ownership, - None, - DUMMY_SP, - ) - } else { - rustc_parse::new_parser_from_file(sess, file) - }), + Input::File(ref file) => Ok(new_parser_from_file(sess, file, None)), Input::Text(text) => rustc_parse::maybe_new_parser_from_source_str( sess, rustc_span::FileName::Custom("stdin".to_owned()), text, - ) - .map(|mut parser| { - parser.recurse_into_file_modules = false; - parser - }), + ), } } } #[derive(Debug, PartialEq)] pub(crate) enum ParserError { - NoConfig, NoParseSess, NoInput, ParserCreationError, @@ -129,7 +99,7 @@ pub(crate) enum ParserError { impl<'a> Parser<'a> { pub(crate) fn submod_path_from_attr(attrs: &[ast::Attribute], path: &Path) -> Option { - rustc_parse::parser::Parser::submod_path_from_attr(attrs, path) + rustc_expand::module::submod_path_from_attr(attrs, path) } // FIXME(topecongiro) Use the method from libsyntax[1] once it become public. @@ -151,8 +121,8 @@ impl<'a> Parser<'a> { } TokenKind::DocComment(s) => { // we need to get the position of this token before we bump. - let attr = syntax::attr::mk_doc_comment( - syntax::util::comments::doc_comment_style(&s.as_str()), + let attr = rustc_ast::attr::mk_doc_comment( + rustc_ast::util::comments::doc_comment_style(&s.as_str()), s, parser.token.span, ); @@ -175,10 +145,15 @@ impl<'a> Parser<'a> { items.push(item); } + // Handle extern mods that are empty files/files with only comments. + if items.is_empty() { + parser.parse_mod(&TokenKind::Eof)?; + } + let hi = if parser.token.span.is_dummy() { span } else { - parser.prev_span + parser.prev_token.span }; Ok(ast::Mod { @@ -189,15 +164,13 @@ impl<'a> Parser<'a> { } pub(crate) fn parse_file_as_module( - directory_ownership: DirectoryOwnership, sess: &'a ParseSess, path: &Path, + span: Span, ) -> Option { let result = catch_unwind(AssertUnwindSafe(|| { - let mut parser = - new_sub_parser_from_file(sess.inner(), &path, directory_ownership, None, DUMMY_SP); + let mut parser = new_parser_from_file(sess.inner(), &path, Some(span)); - parser.cfg_mods = false; let lo = parser.token.span; // FIXME(topecongiro) Format inner attributes (#3606). match Parser::parse_inner_attrs(&mut parser) { @@ -266,12 +239,9 @@ impl<'a> Parser<'a> { pub(crate) fn parse_cfg_if( sess: &'a ParseSess, - mac: &'a ast::Mac, - base_dir: &Directory, + mac: &'a ast::MacCall, ) -> Result, &'static str> { - match catch_unwind(AssertUnwindSafe(|| { - Parser::parse_cfg_if_inner(sess, mac, base_dir) - })) { + match catch_unwind(AssertUnwindSafe(|| Parser::parse_cfg_if_inner(sess, mac))) { Ok(Ok(items)) => Ok(items), Ok(err @ Err(_)) => err, Err(..) => Err("failed to parse cfg_if!"), @@ -280,17 +250,11 @@ impl<'a> Parser<'a> { fn parse_cfg_if_inner( sess: &'a ParseSess, - mac: &'a ast::Mac, - base_dir: &Directory, + mac: &'a ast::MacCall, ) -> Result, &'static str> { let token_stream = mac.args.inner_tokens(); - let mut parser = rustc_parse::stream_to_parser_with_base_dir( - sess.inner(), - token_stream, - base_dir.to_syntax_directory(), - ); - - parser.cfg_mods = false; + let mut parser = + rustc_parse::stream_to_parser(sess.inner(), token_stream.clone(), Some("")); let mut items = vec![]; let mut process_if_cfg = true; diff --git a/rustfmt-core/rustfmt-lib/src/syntux/session.rs b/rustfmt-core/rustfmt-lib/src/syntux/session.rs index 9ec85efb109..da8741c84e5 100644 --- a/rustfmt-core/rustfmt-lib/src/syntux/session.rs +++ b/rustfmt-core/rustfmt-lib/src/syntux/session.rs @@ -2,6 +2,7 @@ use std::cell::RefCell; use std::path::Path; use std::rc::Rc; +use rustc_ast::ast; use rustc_data_structures::sync::{Lrc, Send}; use rustc_errors::emitter::{Emitter, EmitterWriter}; use rustc_errors::{ColorConfig, Diagnostic, Handler, Level as DiagnosticLevel}; @@ -10,7 +11,6 @@ use rustc_span::{ source_map::{FilePathMapping, SourceMap}, BytePos, Span, }; -use syntax::ast; use crate::config::file_lines::LineRange; use crate::ignore_path::IgnorePathSet; @@ -150,12 +150,13 @@ impl ParseSess { id: ast::Ident, relative: Option, dir_path: &Path, - ) -> rustc_parse::parser::ModulePath { - rustc_parse::parser::Parser::default_submod_path( + ) -> rustc_expand::module::ModulePath<'_> { + rustc_expand::module::default_submod_path( + &self.parse_sess, id, + rustc_span::DUMMY_SP, relative, dir_path, - self.parse_sess.source_map(), ) } diff --git a/rustfmt-core/rustfmt-lib/src/types.rs b/rustfmt-core/rustfmt-lib/src/types.rs index 0cfe906a30b..ebf6d397ae7 100644 --- a/rustfmt-core/rustfmt-lib/src/types.rs +++ b/rustfmt-core/rustfmt-lib/src/types.rs @@ -1,8 +1,8 @@ use std::iter::ExactSizeIterator; use std::ops::Deref; +use rustc_ast::ast::{self, FnRetTy, Mutability}; use rustc_span::{symbol::kw, BytePos, Span}; -use syntax::ast::{self, FunctionRetTy, Mutability}; use crate::config::lists::*; use crate::config::{IndentStyle, TypeDensity}; @@ -233,14 +233,18 @@ fn rewrite_segment( if let Some(ref args) = segment.args { match **args { - ast::GenericArgs::AngleBracketed(ref data) - if !data.args.is_empty() || !data.constraints.is_empty() => - { + ast::GenericArgs::AngleBracketed(ref data) if !data.args.is_empty() => { let param_list = data .args .iter() - .map(SegmentParam::from_generic_arg) - .chain(data.constraints.iter().map(|x| SegmentParam::Binding(&*x))) + .map(|x| match x { + ast::AngleBracketedArg::Arg(generic_arg) => { + SegmentParam::from_generic_arg(generic_arg) + } + ast::AngleBracketedArg::Constraint(constraint) => { + SegmentParam::Binding(constraint) + } + }) .collect::>(); // HACK: squeeze out the span between the identifier and the parameters. @@ -292,7 +296,7 @@ fn rewrite_segment( fn format_function_type<'a, I>( inputs: I, - output: &FunctionRetTy, + output: &FnRetTy, variadic: bool, span: Span, context: &RewriteContext<'_>, @@ -311,11 +315,11 @@ where IndentStyle::Visual => shape.block_left(4)?, }; let output = match *output { - FunctionRetTy::Ty(ref ty) => { + FnRetTy::Ty(ref ty) => { let type_str = ty.rewrite(context, ty_shape)?; format!(" -> {}", type_str) } - FunctionRetTy::Default(..) => String::new(), + FnRetTy::Default(..) => String::new(), }; let list_shape = if context.use_block_indent() { @@ -556,7 +560,7 @@ impl Rewrite for ast::GenericParam { _ => (), } - if let syntax::ast::GenericParamKind::Const { ref ty } = &self.kind { + if let rustc_ast::ast::GenericParamKind::Const { ref ty } = &self.kind { result.push_str("const "); result.push_str(rewrite_ident(context, self.ident)); result.push_str(": "); @@ -735,7 +739,7 @@ impl Rewrite for ast::Ty { } ast::TyKind::BareFn(ref bare_fn) => rewrite_bare_fn(bare_fn, self.span, context, shape), ast::TyKind::Never => Some(String::from("!")), - ast::TyKind::Mac(ref mac) => { + ast::TyKind::MacCall(ref mac) => { rewrite_macro(mac, None, context, shape, MacroPosition::Expression) } ast::TyKind::ImplicitSelf => Some(String::from("")), diff --git a/rustfmt-core/rustfmt-lib/src/utils.rs b/rustfmt-core/rustfmt-lib/src/utils.rs index 63bbbbd4952..63b09537961 100644 --- a/rustfmt-core/rustfmt-lib/src/utils.rs +++ b/rustfmt-core/rustfmt-lib/src/utils.rs @@ -1,12 +1,12 @@ use std::borrow::Cow; -use rustc_ast_pretty::pprust; -use rustc_span::{sym, BytePos, ExpnId, Span, Symbol, SyntaxContext}; -use syntax::ast::{ +use rustc_ast::ast::{ self, Attribute, CrateSugar, MetaItem, MetaItemKind, NestedMetaItem, NodeId, Path, Visibility, VisibilityKind, }; -use syntax::ptr; +use rustc_ast::ptr; +use rustc_ast_pretty::pprust; +use rustc_span::{sym, BytePos, ExpnId, Span, Symbol, SyntaxContext}; use unicode_width::UnicodeWidthStr; use crate::comment::{filter_normal_code, CharClasses, FullCodeCharKind, LineClasses}; @@ -85,34 +85,34 @@ pub(crate) fn format_visibility( } #[inline] -pub(crate) fn format_async(is_async: ast::IsAsync) -> &'static str { +pub(crate) fn format_async(is_async: ast::Async) -> &'static str { match is_async { - ast::IsAsync::Async { .. } => "async ", - ast::IsAsync::NotAsync => "", + ast::Async::Yes { .. } => "async ", + ast::Async::No => "", } } #[inline] -pub(crate) fn format_constness(constness: ast::Constness) -> &'static str { +pub(crate) fn format_constness(constness: ast::Const) -> &'static str { match constness { - ast::Constness::Const => "const ", - ast::Constness::NotConst => "", + ast::Const::Yes(..) => "const ", + ast::Const::No => "", } } #[inline] pub(crate) fn format_defaultness(defaultness: ast::Defaultness) -> &'static str { match defaultness { - ast::Defaultness::Default => "default ", + ast::Defaultness::Default(..) => "default ", ast::Defaultness::Final => "", } } #[inline] -pub(crate) fn format_unsafety(unsafety: ast::Unsafety) -> &'static str { +pub(crate) fn format_unsafety(unsafety: ast::Unsafe) -> &'static str { match unsafety { - ast::Unsafety::Unsafe => "unsafe ", - ast::Unsafety::Normal => "", + ast::Unsafe::Yes(..) => "unsafe ", + ast::Unsafe::No => "", } } @@ -154,7 +154,7 @@ pub(crate) fn format_extern( } #[inline] -// Transform `Vec>` into `Vec<&T>` +// Transform `Vec>` into `Vec<&T>` pub(crate) fn ptr_vec_to_ref_vec(vec: &[ptr::P]) -> Vec<&T> { vec.iter().map(|x| &**x).collect::>() } @@ -452,7 +452,7 @@ pub(crate) fn first_line_ends_with(s: &str, c: char) -> bool { // parens, braces, and brackets in its idiomatic formatting. pub(crate) fn is_block_expr(context: &RewriteContext<'_>, expr: &ast::Expr, repr: &str) -> bool { match expr.kind { - ast::ExprKind::Mac(..) + ast::ExprKind::MacCall(..) | ast::ExprKind::Call(..) | ast::ExprKind::MethodCall(..) | ast::ExprKind::Array(..) @@ -486,7 +486,7 @@ pub(crate) fn is_block_expr(context: &RewriteContext<'_>, expr: &ast::Expr, repr | ast::ExprKind::Continue(..) | ast::ExprKind::Err | ast::ExprKind::Field(..) - | ast::ExprKind::InlineAsm(..) + | ast::ExprKind::LlvmInlineAsm(..) | ast::ExprKind::Let(..) | ast::ExprKind::Path(..) | ast::ExprKind::Range(..) diff --git a/rustfmt-core/rustfmt-lib/src/vertical.rs b/rustfmt-core/rustfmt-lib/src/vertical.rs index b932b0afa17..95d1d5c2d7e 100644 --- a/rustfmt-core/rustfmt-lib/src/vertical.rs +++ b/rustfmt-core/rustfmt-lib/src/vertical.rs @@ -3,8 +3,8 @@ use std::cmp; use itertools::Itertools; +use rustc_ast::ast; use rustc_span::{BytePos, Span}; -use syntax::ast; use crate::comment::combine_strs_with_missing_comments; use crate::config::lists::*; diff --git a/rustfmt-core/rustfmt-lib/src/visitor.rs b/rustfmt-core/rustfmt-lib/src/visitor.rs index 2f25e7b4df7..f39cf8c698d 100644 --- a/rustfmt-core/rustfmt-lib/src/visitor.rs +++ b/rustfmt-core/rustfmt-lib/src/visitor.rs @@ -1,8 +1,8 @@ use std::cell::{Cell, RefCell}; use std::rc::Rc; +use rustc_ast::{ast, token::DelimToken, visit}; use rustc_span::{BytePos, Pos, Span}; -use syntax::{ast, token::DelimToken, visit}; use crate::attr::*; use crate::comment::{rewrite_comment, CodeCharKind, CommentCodeSlices}; @@ -147,7 +147,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { self.push_rewrite(stmt.span(), rewrite) } } - ast::StmtKind::Mac(ref mac) => { + ast::StmtKind::MacCall(ref mac) => { let (ref mac, _macro_style, ref attrs) = **mac; if self.visit_attrs(attrs, ast::AttrStyle::Outer) { self.push_skipped_with_span( @@ -160,6 +160,9 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { } self.format_missing(stmt.span().hi()); } + ast::StmtKind::Empty => { + // println!("inside visitor with empty statement"); + } } } @@ -492,7 +495,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { self.format_missing_with_indent(source!(self, item.span).lo()); self.format_mod(module, &item.vis, item.span, item.ident, attrs, is_inline); } - ast::ItemKind::Mac(ref mac) => { + ast::ItemKind::MacCall(ref mac) => { self.visit_mac(mac, Some(item.ident), MacroPosition::Item); } ast::ItemKind::ForeignMod(ref foreign_mod) => { @@ -502,7 +505,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { ast::ItemKind::Static(..) | ast::ItemKind::Const(..) => { self.visit_static(&StaticParts::from_item(item)); } - ast::ItemKind::Fn(ref fn_signature, ref generics, Some(ref body)) => { + ast::ItemKind::Fn(defaultness, ref fn_signature, ref generics, Some(ref body)) => { let inner_attrs = inner_attributes(&item.attrs); let fn_ctxt = match fn_signature.header.ext { ast::Extern::None => visit::FnCtxt::Free, @@ -519,11 +522,11 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { generics, &fn_signature.decl, item.span, - ast::Defaultness::Final, + defaultness, Some(&inner_attrs), ) } - ast::ItemKind::Fn(ref fn_signature, ref generics, None) => { + ast::ItemKind::Fn(_, ref fn_signature, ref generics, None) => { let indent = self.block_indent; let rewrite = self.rewrite_required_fn( indent, @@ -534,19 +537,19 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { ); self.push_rewrite(item.span, rewrite); } - ast::ItemKind::TyAlias(ref ty, ref generics) => match ty.kind.opaque_top_hack() { - None => { + ast::ItemKind::TyAlias(_, ref generics, ref generic_bounds, ref ty) => match ty { + Some(ty) => { let rewrite = rewrite_type_alias( &self.get_context(), self.block_indent, item.ident, - ty, + &*ty, generics, &item.vis, ); self.push_rewrite(item.span, rewrite); } - Some(generic_bounds) => { + None => { let rewrite = rewrite_opaque_type( &self.get_context(), self.block_indent, @@ -589,37 +592,36 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { match ti.kind { ast::AssocItemKind::Const(..) => self.visit_static(&StaticParts::from_trait_item(ti)), - ast::AssocItemKind::Fn(ref sig, None) => { + ast::AssocItemKind::Fn(_, ref sig, ref generics, None) => { let indent = self.block_indent; - let rewrite = - self.rewrite_required_fn(indent, ti.ident, sig, &ti.generics, ti.span); + let rewrite = self.rewrite_required_fn(indent, ti.ident, sig, generics, ti.span); self.push_rewrite(ti.span, rewrite); } - ast::AssocItemKind::Fn(ref sig, Some(ref body)) => { + ast::AssocItemKind::Fn(defaultness, ref sig, ref generics, Some(ref body)) => { let inner_attrs = inner_attributes(&ti.attrs); let vis = rustc_span::source_map::dummy_spanned(ast::VisibilityKind::Inherited); let fn_ctxt = visit::FnCtxt::Assoc(visit::AssocCtxt::Trait); self.visit_fn( visit::FnKind::Fn(fn_ctxt, ti.ident, sig, &vis, Some(body)), - &ti.generics, + generics, &sig.decl, ti.span, - ast::Defaultness::Final, + defaultness, Some(&inner_attrs), ); } - ast::AssocItemKind::TyAlias(ref generic_bounds, ref type_default) => { + ast::AssocItemKind::TyAlias(_, ref generics, ref generic_bounds, ref type_default) => { let rewrite = rewrite_associated_type( ti.ident, type_default.as_ref(), - &ti.generics, + generics, Some(generic_bounds), &self.get_context(), self.block_indent, ); self.push_rewrite(ti.span, rewrite); } - ast::AssocItemKind::Macro(ref mac) => { + ast::AssocItemKind::MacCall(ref mac) => { self.visit_mac(mac, Some(ti.ident), MacroPosition::Item); } } @@ -634,32 +636,31 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { } match ii.kind { - ast::AssocItemKind::Fn(ref sig, Some(ref body)) => { + ast::AssocItemKind::Fn(defaultness, ref sig, ref generics, Some(ref body)) => { let inner_attrs = inner_attributes(&ii.attrs); let fn_ctxt = visit::FnCtxt::Assoc(visit::AssocCtxt::Impl); self.visit_fn( visit::FnKind::Fn(fn_ctxt, ii.ident, sig, &ii.vis, Some(body)), - &ii.generics, + generics, &sig.decl, ii.span, - ii.defaultness, + defaultness, Some(&inner_attrs), ); } - ast::AssocItemKind::Fn(ref sig, None) => { + ast::AssocItemKind::Fn(_, ref sig, ref generics, None) => { let indent = self.block_indent; - let rewrite = - self.rewrite_required_fn(indent, ii.ident, sig, &ii.generics, ii.span); + let rewrite = self.rewrite_required_fn(indent, ii.ident, sig, generics, ii.span); self.push_rewrite(ii.span, rewrite); } ast::AssocItemKind::Const(..) => self.visit_static(&StaticParts::from_impl_item(ii)), - ast::AssocItemKind::TyAlias(_, ref ty) => { + ast::AssocItemKind::TyAlias(defaultness, ref generics, _, ref ty) => { let rewrite_associated = || { rewrite_associated_impl_type( ii.ident, - ii.defaultness, + defaultness, ty.as_ref(), - &ii.generics, + generics, &self.get_context(), self.block_indent, ) @@ -670,7 +671,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { Some(generic_bounds) => rewrite_opaque_impl_type( &self.get_context(), ii.ident, - &ii.generics, + generics, generic_bounds, self.block_indent, ), @@ -679,13 +680,13 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { }; self.push_rewrite(ii.span, rewrite); } - ast::AssocItemKind::Macro(ref mac) => { + ast::AssocItemKind::MacCall(ref mac) => { self.visit_mac(mac, Some(ii.ident), MacroPosition::Item); } } } - fn visit_mac(&mut self, mac: &ast::Mac, ident: Option, pos: MacroPosition) { + fn visit_mac(&mut self, mac: &ast::MacCall, ident: Option, pos: MacroPosition) { skip_out_of_file_lines_range_visitor!(self, mac.span()); // 1 = ; diff --git a/rustfmt-core/rustfmt-lib/tests/source/imports.rs b/rustfmt-core/rustfmt-lib/tests/source/imports.rs index c95415e9ce2..baafddc6686 100644 --- a/rustfmt-core/rustfmt-lib/tests/source/imports.rs +++ b/rustfmt-core/rustfmt-lib/tests/source/imports.rs @@ -3,7 +3,7 @@ // Imports. // Long import. -use syntax::ast::{ItemForeignMod, ItemImpl, ItemMac, ItemMod, ItemStatic, ItemDefaultImpl}; +use rustc_ast::ast::{ItemForeignMod, ItemImpl, ItemMac, ItemMod, ItemStatic, ItemDefaultImpl}; use exceedingly::looooooooooooooooooooooooooooooooooooooooooooooooooooooooooong::import::path::{ItemA, ItemB}; use exceedingly::loooooooooooooooooooooooooooooooooooooooooooooooooooooooong::import::path::{ItemA, ItemB}; @@ -15,13 +15,13 @@ use list::{ use test::{ Other /* C */ , /* A */ self /* B */ }; -use syntax::{self}; +use rustc_ast::{self}; use {/* Pre-comment! */ Foo, Bar /* comment */}; use Foo::{Bar, Baz}; -pub use syntax::ast::{Expr_, Expr, ExprAssign, ExprCall, ExprMethodCall, ExprPath}; +pub use rustc_ast::ast::{Expr_, Expr, ExprAssign, ExprCall, ExprMethodCall, ExprPath}; -use syntax::some::{}; +use rustc_ast::some::{}; use self; use std::io::{self}; @@ -31,7 +31,7 @@ use a::{/* comment */ item}; use a::{item /* comment */}; mod Foo { - pub use syntax::ast::{ + pub use rustc_ast::ast::{ ItemForeignMod, ItemImpl, ItemMac, @@ -41,7 +41,7 @@ mod Foo { }; mod Foo2 { - pub use syntax::ast::{ItemForeignMod, ItemImpl, ItemMac, ItemMod, ItemStatic, self, ItemDefaultImpl}; + pub use rustc_ast::ast::{ItemForeignMod, ItemImpl, ItemMac, ItemMod, ItemStatic, self, ItemDefaultImpl}; } } diff --git a/rustfmt-core/rustfmt-lib/tests/source/imports_2015_edition.rs b/rustfmt-core/rustfmt-lib/tests/source/imports_2015_edition.rs index 9292660e1d2..c6d14e31af9 100644 --- a/rustfmt-core/rustfmt-lib/tests/source/imports_2015_edition.rs +++ b/rustfmt-core/rustfmt-lib/tests/source/imports_2015_edition.rs @@ -4,7 +4,7 @@ // Imports. // Long import. -use syntax::ast::{ItemForeignMod, ItemImpl, ItemMac, ItemMod, ItemStatic, ItemDefaultImpl}; +use rustc_ast::ast::{ItemForeignMod, ItemImpl, ItemMac, ItemMod, ItemStatic, ItemDefaultImpl}; use exceedingly::looooooooooooooooooooooooooooooooooooooooooooooooooooooooooong::import::path::{ItemA, ItemB}; use exceedingly::loooooooooooooooooooooooooooooooooooooooooooooooooooooooong::import::path::{ItemA, ItemB}; @@ -16,20 +16,20 @@ use list::{ use test::{ Other /* C */ , /* A */ self /* B */ }; -use syntax::{self}; +use rustc_ast::{self}; use {/* Pre-comment! */ Foo, Bar /* comment */}; use Foo::{Bar, Baz}; -pub use syntax::ast::{Expr_, Expr, ExprAssign, ExprCall, ExprMethodCall, ExprPath}; +pub use rustc_ast::ast::{Expr_, Expr, ExprAssign, ExprCall, ExprMethodCall, ExprPath}; -use syntax::some::{}; +use rustc_ast::some::{}; use self; use std::io::{self}; use std::io::self; mod Foo { - pub use syntax::ast::{ + pub use rustc_ast::ast::{ ItemForeignMod, ItemImpl, ItemMac, @@ -39,7 +39,7 @@ mod Foo { }; mod Foo2 { - pub use syntax::ast::{ItemForeignMod, ItemImpl, ItemMac, ItemMod, ItemStatic, self, ItemDefaultImpl}; + pub use rustc_ast::ast::{ItemForeignMod, ItemImpl, ItemMac, ItemMod, ItemStatic, self, ItemDefaultImpl}; } } diff --git a/rustfmt-core/rustfmt-lib/tests/target/imports.rs b/rustfmt-core/rustfmt-lib/tests/target/imports.rs index d18fd4a3dd4..12c83a0f47b 100644 --- a/rustfmt-core/rustfmt-lib/tests/target/imports.rs +++ b/rustfmt-core/rustfmt-lib/tests/target/imports.rs @@ -9,7 +9,7 @@ use exceedingly::loooooooooooooooooooooooooooooooooooooooooooooooooooooooong::im use exceedingly::looooooooooooooooooooooooooooooooooooooooooooooooooooooooooong::import::path::{ ItemA, ItemB, }; -use syntax::ast::{ItemDefaultImpl, ItemForeignMod, ItemImpl, ItemMac, ItemMod, ItemStatic}; +use rustc_ast::ast::{ItemDefaultImpl, ItemForeignMod, ItemImpl, ItemMac, ItemMod, ItemStatic}; use list::{ // Another item @@ -22,8 +22,8 @@ use list::{ use test::{/* A */ self /* B */, Other /* C */}; -pub use syntax::ast::{Expr, ExprAssign, ExprCall, ExprMethodCall, ExprPath, Expr_}; -use syntax::{self}; +pub use rustc_ast::ast::{Expr, ExprAssign, ExprCall, ExprMethodCall, ExprPath, Expr_}; +use rustc_ast::{self}; use Foo::{Bar, Baz}; use {Bar /* comment */, /* Pre-comment! */ Foo}; @@ -34,12 +34,12 @@ use a::{/* comment */ item}; use a::{item /* comment */}; mod Foo { - pub use syntax::ast::{ + pub use rustc_ast::ast::{ ItemDefaultImpl, ItemForeignMod, ItemImpl, ItemMac, ItemMod, ItemStatic, }; mod Foo2 { - pub use syntax::ast::{ + pub use rustc_ast::ast::{ self, ItemDefaultImpl, ItemForeignMod, ItemImpl, ItemMac, ItemMod, ItemStatic, }; } diff --git a/rustfmt-core/rustfmt-lib/tests/target/imports_2015_edition.rs b/rustfmt-core/rustfmt-lib/tests/target/imports_2015_edition.rs index 474ecdb8bcf..e5100e1d34a 100644 --- a/rustfmt-core/rustfmt-lib/tests/target/imports_2015_edition.rs +++ b/rustfmt-core/rustfmt-lib/tests/target/imports_2015_edition.rs @@ -10,7 +10,7 @@ use exceedingly::loooooooooooooooooooooooooooooooooooooooooooooooooooooooong::im use exceedingly::looooooooooooooooooooooooooooooooooooooooooooooooooooooooooong::import::path::{ ItemA, ItemB, }; -use syntax::ast::{ItemDefaultImpl, ItemForeignMod, ItemImpl, ItemMac, ItemMod, ItemStatic}; +use rustc_ast::ast::{ItemDefaultImpl, ItemForeignMod, ItemImpl, ItemMac, ItemMod, ItemStatic}; use list::{ // Another item @@ -23,8 +23,8 @@ use list::{ use test::{/* A */ self /* B */, Other /* C */}; -pub use syntax::ast::{Expr, ExprAssign, ExprCall, ExprMethodCall, ExprPath, Expr_}; -use syntax::{self}; +pub use rustc_ast::ast::{Expr, ExprAssign, ExprCall, ExprMethodCall, ExprPath, Expr_}; +use rustc_ast::{self}; use Foo::{Bar, Baz}; use {Bar /* comment */, /* Pre-comment! */ Foo}; @@ -32,12 +32,12 @@ use std::io; use std::io::{self}; mod Foo { - pub use syntax::ast::{ + pub use rustc_ast::ast::{ ItemDefaultImpl, ItemForeignMod, ItemImpl, ItemMac, ItemMod, ItemStatic, }; mod Foo2 { - pub use syntax::ast::{ + pub use rustc_ast::ast::{ self, ItemDefaultImpl, ItemForeignMod, ItemImpl, ItemMac, ItemMod, ItemStatic, }; }