From c0c998a0e7aeb8d6aea55a75247bbf98b6a7f9dc Mon Sep 17 00:00:00 2001 From: Caleb Cartwright Date: Tue, 27 Oct 2020 20:58:52 -0500 Subject: [PATCH 1/9] deps: bump rustc-ap to v686 --- Cargo.lock | 158 +++++++++++++++++--------------------------------- Cargo.toml | 18 +++--- src/macros.rs | 6 +- 3 files changed, 66 insertions(+), 116 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ea03d6dbede..df5d17c6b32 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -576,15 +576,6 @@ version = "0.2.77" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f2f96b10ec2560088a8e76961b00d47107b3a625fecb76dedb29ee7ccbf98235" -[[package]] -name = "lock_api" -version = "0.3.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4da24a77a3d8a6d4862d95f72e6fdb9c09a643ecdb402d754004a557f2bec75" -dependencies = [ - "scopeguard", -] - [[package]] name = "lock_api" version = "0.4.1" @@ -616,14 +607,13 @@ dependencies = [ [[package]] name = "measureme" -version = "0.7.1" +version = "9.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fef709d3257013bba7cff14fc504e07e80631d3fe0f6d38ce63b8f6510ccb932" +checksum = "22bf8d885d073610aee20e7fa205c4341ed32a761dbde96da5fd96301a8d3e82" dependencies = [ - "byteorder", - "memmap", - "parking_lot 0.9.0", + "parking_lot", "rustc-hash", + "smallvec", ] [[package]] @@ -632,16 +622,6 @@ version = "2.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "88579771288728879b57485cc7d6b07d648c9f0141eb955f8ab7f9d45394468e" -[[package]] -name = "memmap" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6585fd95e7bb50d6cc31e20d4cf9afb4e2ba16c5846fc76793f11218da9c475b" -dependencies = [ - "libc", - "winapi", -] - [[package]] name = "memoffset" version = "0.5.1" @@ -681,17 +661,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "parking_lot" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f842b1982eb6c2fe34036a4fbfb06dd185a3f5c8edfaacdf7d1ea10b07de6252" -dependencies = [ - "lock_api 0.3.4", - "parking_lot_core 0.6.2", - "rustc_version", -] - [[package]] name = "parking_lot" version = "0.11.0" @@ -699,23 +668,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4893845fa2ca272e647da5d0e46660a314ead9c2fdd9a883aabc32e481a8733" dependencies = [ "instant", - "lock_api 0.4.1", - "parking_lot_core 0.8.0", -] - -[[package]] -name = "parking_lot_core" -version = "0.6.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b876b1b9e7ac6e1a74a6da34d25c42e17e8862aa409cbbbdcfc8d86c6f3bc62b" -dependencies = [ - "cfg-if", - "cloudabi 0.0.3", - "libc", - "redox_syscall", - "rustc_version", - "smallvec 0.6.10", - "winapi", + "lock_api", + "parking_lot_core", ] [[package]] @@ -729,7 +683,7 @@ dependencies = [ "instant", "libc", "redox_syscall", - "smallvec 1.4.2", + "smallvec", "winapi", ] @@ -935,18 +889,18 @@ dependencies = [ [[package]] name = "rustc-ap-rustc_arena" -version = "684.0.0" +version = "686.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d77be159a7d411e02ddf51a97cc2f98e88b93bf2856ac203dea737b8041ed23c" +checksum = "477085eefed2f12085c68577cc3827c8c39a31a4a750978aacb9af10f7903174" dependencies = [ - "smallvec 1.4.2", + "smallvec", ] [[package]] name = "rustc-ap-rustc_ast" -version = "684.0.0" +version = "686.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "584ac06879fc46441feb444b6b6f55bf71489e3dc10a6a94222513f118468526" +checksum = "4d4ad5ec25f6b3d122354595be0d1b513f37fca3299d9b448b1db28f4a9e4b12" dependencies = [ "bitflags", "rustc-ap-rustc_data_structures", @@ -955,15 +909,15 @@ dependencies = [ "rustc-ap-rustc_macros", "rustc-ap-rustc_serialize", "rustc-ap-rustc_span", - "smallvec 1.4.2", + "smallvec", "tracing", ] [[package]] name = "rustc-ap-rustc_ast_passes" -version = "684.0.0" +version = "686.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "155c1e648133f0df2848d899564aade8bd58ce2c82b03727e8437ec2523f420b" +checksum = "0c6d8635298d7736decdb3c6e92e784d3eccde557462a9c10ac11a34fec3d756" dependencies = [ "itertools 0.9.0", "rustc-ap-rustc_ast", @@ -980,9 +934,9 @@ dependencies = [ [[package]] name = "rustc-ap-rustc_ast_pretty" -version = "684.0.0" +version = "686.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3786235c5f1466ffd0f00cb1851e7cd8637f07d12efdeb206de3aa8cc285cf7" +checksum = "7a61bdb5252e1a95b7715038949e10f07ce770a436fcd497cdd9bc7255471de9" dependencies = [ "rustc-ap-rustc_ast", "rustc-ap-rustc_span", @@ -992,9 +946,9 @@ dependencies = [ [[package]] name = "rustc-ap-rustc_attr" -version = "684.0.0" +version = "686.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e02580ccbaf8d417c59d97a946b446b2a5332d68a0cf1eab74bb5dd33100e99" +checksum = "84520a16cb61bd31e9c27e87eca5d933a9c94ac84f25649bddcc19989275ab2a" dependencies = [ "rustc-ap-rustc_ast", "rustc-ap-rustc_ast_pretty", @@ -1011,9 +965,9 @@ dependencies = [ [[package]] name = "rustc-ap-rustc_data_structures" -version = "684.0.0" +version = "686.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be2a1c1a22021daef8354fb7504da4ed6df6e0aaf345af32c51e4ccf8afc07c0" +checksum = "b1cb2b6a38759cf7c0c1434c8b4cbfcab9cd24970d05f960f2ca01226ddb4d68" dependencies = [ "arrayvec 0.5.1", "bitflags", @@ -1024,7 +978,7 @@ dependencies = [ "jobserver", "libc", "measureme", - "parking_lot 0.11.0", + "parking_lot", "rustc-ap-rustc_graphviz", "rustc-ap-rustc_index", "rustc-ap-rustc_macros", @@ -1032,7 +986,7 @@ dependencies = [ "rustc-hash", "rustc-rayon", "rustc-rayon-core", - "smallvec 1.4.2", + "smallvec", "stable_deref_trait", "stacker", "tempfile", @@ -1042,9 +996,9 @@ dependencies = [ [[package]] name = "rustc-ap-rustc_errors" -version = "684.0.0" +version = "686.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f81730d3ab322d96265163f9ab2ced6815d336a0441a85641d134c30d69d6660" +checksum = "46cfb19536426bf9252827a78552d635be207a4be74f4e92832aad82d7f2135c" dependencies = [ "annotate-snippets 0.8.0", "atty", @@ -1061,9 +1015,9 @@ dependencies = [ [[package]] name = "rustc-ap-rustc_expand" -version = "684.0.0" +version = "686.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "325f40a43586a4fbd1d3837c3bb024db3729193146201d13f59b637d28e73ffe" +checksum = "6273e60042a0ef31f6cfe783c519873993eb426f055be2bc058a48b6ca3934d0" dependencies = [ "rustc-ap-rustc_ast", "rustc-ap-rustc_ast_passes", @@ -1078,15 +1032,15 @@ dependencies = [ "rustc-ap-rustc_serialize", "rustc-ap-rustc_session", "rustc-ap-rustc_span", - "smallvec 1.4.2", + "smallvec", "tracing", ] [[package]] name = "rustc-ap-rustc_feature" -version = "684.0.0" +version = "686.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e53e214c56bd3db863bf87e23a68cc9c7b2ccf1796dc63b5c795f75e1457654b" +checksum = "2936e8346157e2848305e509f38aa3ed4e97697975ef68027587f5db6a38703f" dependencies = [ "rustc-ap-rustc_data_structures", "rustc-ap-rustc_span", @@ -1094,21 +1048,21 @@ dependencies = [ [[package]] name = "rustc-ap-rustc_fs_util" -version = "684.0.0" +version = "686.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9783efc336cdb51ca8ed281040bf72bcb178a2a29867aade35893e4335f7ac01" +checksum = "9b4c3ae17776b5a5aa441ca510a650f75805e1f5569edd231caa8378552195a4" [[package]] name = "rustc-ap-rustc_graphviz" -version = "684.0.0" +version = "686.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb71e52620bcba8695271b90cb1d294ed16f1dcc81e85327e6dc67e6b5c3ef1d" +checksum = "5611bf0ac0ac49c2a22c959c7d8b17f85f69959293f0e8c4f753eca832fe7ad0" [[package]] name = "rustc-ap-rustc_index" -version = "684.0.0" +version = "686.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1e73d13c4839873924d0c04fb885a4e3980b5c8416f8a96f925a8e5a4eb9c8e" +checksum = "ca67cf37c427057192e451c7f912e94ae9a8ca5ad69fd481c011fad3f86982cb" dependencies = [ "arrayvec 0.5.1", "rustc-ap-rustc_macros", @@ -1117,18 +1071,18 @@ dependencies = [ [[package]] name = "rustc-ap-rustc_lexer" -version = "684.0.0" +version = "686.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "de5fb086575a381760a993126795222a49f3bab9faeb91dc67268dd19eab5c13" +checksum = "a5b04cd2159495584d976d501c5394498470c2e94e4f0cebb8186562d407a678" dependencies = [ "unicode-xid", ] [[package]] name = "rustc-ap-rustc_macros" -version = "684.0.0" +version = "686.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1cb3c133f618b5296c2ba57f05ee7b4c3fbec09d2779922083c2d16c5674f57c" +checksum = "61ec6d623853449acd3c65050d249d3674edab5f6e4d9f074c7bac183269f9c8" dependencies = [ "proc-macro2", "quote", @@ -1138,9 +1092,9 @@ dependencies = [ [[package]] name = "rustc-ap-rustc_parse" -version = "684.0.0" +version = "686.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9cc8e42993c1ff89f56e2e00240a186c6eaaad6839a271ee3d534240a624ea5a" +checksum = "ca524bafce4b04d2b49fee2d40b4b26c3ebab9f1a4f731fdf561f00617862f02" dependencies = [ "bitflags", "rustc-ap-rustc_ast", @@ -1151,26 +1105,26 @@ dependencies = [ "rustc-ap-rustc_lexer", "rustc-ap-rustc_session", "rustc-ap-rustc_span", - "smallvec 1.4.2", + "smallvec", "tracing", "unicode-normalization", ] [[package]] name = "rustc-ap-rustc_serialize" -version = "684.0.0" +version = "686.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1bb88c78d4551e0243518d05ea3ab16388af97ec839e60de71c39147154a0303" +checksum = "c67920561e58f98c4de864407c92b2dd05ace5d5e5301e17444f10f742c005b7" dependencies = [ "indexmap", - "smallvec 1.4.2", + "smallvec", ] [[package]] name = "rustc-ap-rustc_session" -version = "684.0.0" +version = "686.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1362a5e57049bc7d722b06f5a8886fbf169e3a892ac793aedd8529ef60a8750" +checksum = "0762fd855792e06ef639327237898e4e092ad68150e6a8e19aeb7dc06276ad7a" dependencies = [ "bitflags", "getopts", @@ -1189,9 +1143,9 @@ dependencies = [ [[package]] name = "rustc-ap-rustc_span" -version = "684.0.0" +version = "686.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b67a086e3f81d54cb9d83974b11961ca8c4111a0955173124314e1ae99a1c2ad" +checksum = "0bf3db7b4ca5d21c14c45475df155e5e020c9a3760346945a662c9a9053b49c8" dependencies = [ "cfg-if", "md-5", @@ -1208,9 +1162,9 @@ dependencies = [ [[package]] name = "rustc-ap-rustc_target" -version = "684.0.0" +version = "686.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "574c6a3e4495dfc522918103bd2cc77148a4716a58583ebed1a764963769151f" +checksum = "3aa6560bb9742b276064d67ab9edb5766ecb303f8ae3854835ad3fad4b432188" dependencies = [ "bitflags", "rustc-ap-rustc_data_structures", @@ -1407,12 +1361,6 @@ dependencies = [ "opaque-debug", ] -[[package]] -name = "smallvec" -version = "0.6.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab606a9c5e214920bb66c458cd7be8ef094f813f20fe77a54cc7dbfff220d4b7" - [[package]] name = "smallvec" version = "1.4.2" @@ -1644,7 +1592,7 @@ version = "0.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5479532badd04e128284890390c1e876ef7a993d0570b3597ae43dfa1d59afa4" dependencies = [ - "smallvec 1.4.2", + "smallvec", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 58801ae281b..855f321a918 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -66,36 +66,36 @@ rustc-workspace-hack = "1.0.0" [dependencies.rustc_ast] package = "rustc-ap-rustc_ast" -version = "684.0.0" +version = "686.0.0" [dependencies.rustc_ast_pretty] package = "rustc-ap-rustc_ast_pretty" -version = "684.0.0" +version = "686.0.0" [dependencies.rustc_attr] package = "rustc-ap-rustc_attr" -version = "684.0.0" +version = "686.0.0" [dependencies.rustc_data_structures] package = "rustc-ap-rustc_data_structures" -version = "684.0.0" +version = "686.0.0" [dependencies.rustc_errors] package = "rustc-ap-rustc_errors" -version = "684.0.0" +version = "686.0.0" [dependencies.rustc_expand] package = "rustc-ap-rustc_expand" -version = "684.0.0" +version = "686.0.0" [dependencies.rustc_parse] package = "rustc-ap-rustc_parse" -version = "684.0.0" +version = "686.0.0" [dependencies.rustc_session] package = "rustc-ap-rustc_session" -version = "684.0.0" +version = "686.0.0" [dependencies.rustc_span] package = "rustc-ap-rustc_span" -version = "684.0.0" +version = "686.0.0" diff --git a/src/macros.rs b/src/macros.rs index 62cc5f9dcc5..c4e2b6ac748 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -13,7 +13,9 @@ 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::tokenstream::{ + Cursor, LazyTokenStream, LazyTokenStreamInner, TokenStream, TokenTree, +}; use rustc_ast::{ast, ptr}; use rustc_ast_pretty::pprust; use rustc_parse::parser::Parser; @@ -1212,7 +1214,7 @@ pub(crate) fn convert_try_mac( kind: ast::ExprKind::Try(parser.parse_expr().ok()?), span: mac.span(), // incorrect span, but shouldn't matter too much attrs: ast::AttrVec::new(), - tokens: Some(ts), + tokens: Some(LazyTokenStream::new(LazyTokenStreamInner::Ready(ts))), }) } else { None From 82d4fb4ebe54cab7f6a45d52bf8a4a1b0ae5fe76 Mon Sep 17 00:00:00 2001 From: Caleb Cartwright Date: Thu, 29 Oct 2020 22:50:25 -0500 Subject: [PATCH 2/9] fix(parser): better unclosed delims handling --- src/syntux/parser.rs | 40 +++++++++-------- src/test/mod.rs | 34 +-------------- src/test/parser.rs | 50 ++++++++++++++++++++++ tests/parser/unclosed-delims/issue_4466.rs | 11 +++++ 4 files changed, 86 insertions(+), 49 deletions(-) create mode 100644 src/test/parser.rs create mode 100644 tests/parser/unclosed-delims/issue_4466.rs diff --git a/src/syntux/parser.rs b/src/syntux/parser.rs index 711dfc80c2c..78de08b993e 100644 --- a/src/syntux/parser.rs +++ b/src/syntux/parser.rs @@ -23,7 +23,6 @@ pub(crate) struct Directory { /// A parser for Rust source code. pub(crate) struct Parser<'a> { parser: RawParser<'a>, - sess: &'a ParseSess, } /// A builder for the `Parser`. @@ -71,7 +70,7 @@ impl<'a> ParserBuilder<'a> { } }; - Ok(Parser { parser, sess }) + Ok(Parser { parser }) } fn parser( @@ -150,6 +149,25 @@ impl<'a> Parser<'a> { input: Input, directory_ownership: Option, sess: &'a ParseSess, + ) -> Result { + let krate = Parser::parse_crate_inner(config, input, directory_ownership, sess)?; + if !sess.has_errors() { + return Ok(krate); + } + + if sess.can_reset_errors() { + sess.reset_errors(); + return Ok(krate); + } + + Err(ParserError::ParseError) + } + + fn parse_crate_inner( + config: &'a Config, + input: Input, + directory_ownership: Option, + sess: &'a ParseSess, ) -> Result { let mut parser = ParserBuilder::default() .config(config) @@ -157,26 +175,14 @@ impl<'a> Parser<'a> { .directory_ownership(directory_ownership) .sess(sess) .build()?; - - parser.parse_crate_inner() + parser.parse_crate_mod() } - fn parse_crate_inner(&mut self) -> Result { + fn parse_crate_mod(&mut self) -> Result { let mut parser = AssertUnwindSafe(&mut self.parser); match catch_unwind(move || parser.parse_crate_mod()) { - Ok(Ok(krate)) => { - if !self.sess.has_errors() { - return Ok(krate); - } - - if self.sess.can_reset_errors() { - self.sess.reset_errors(); - return Ok(krate); - } - - Err(ParserError::ParseError) - } + Ok(Ok(k)) => Ok(k), Ok(Err(mut db)) => { db.emit(); Err(ParserError::ParseError) diff --git a/src/test/mod.rs b/src/test/mod.rs index 57b5f2a78cd..9b3ca717152 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -11,14 +11,12 @@ use std::thread; use crate::config::{Color, Config, EmitMode, FileName, NewlineStyle, ReportTactic}; use crate::formatting::{ReportedErrors, SourceFile}; -use crate::modules::{ModuleResolutionError, ModuleResolutionErrorKind}; use crate::rustfmt_diff::{make_diff, print_diff, DiffLine, Mismatch, ModifiedChunk, OutputWriter}; use crate::source_file; -use crate::{ - is_nightly_channel, ErrorKind, FormatReport, FormatReportFormatterBuilder, Input, Session, -}; +use crate::{is_nightly_channel, FormatReport, FormatReportFormatterBuilder, Input, Session}; mod configuration_snippet; +mod parser; const DIFF_CONTEXT_SIZE: usize = 3; @@ -485,34 +483,6 @@ fn format_lines_errors_are_reported_with_tabs() { assert!(session.has_formatting_errors()); } -#[test] -fn parser_errors_in_submods_are_surfaced() { - // See also https://github.com/rust-lang/rustfmt/issues/4126 - let filename = "tests/parser/issue-4126/lib.rs"; - let input_file = PathBuf::from(filename); - let exp_mod_name = "invalid"; - let config = read_config(&input_file); - let mut session = Session::::new(config, None); - if let Err(ErrorKind::ModuleResolutionError(ModuleResolutionError { module, kind })) = - session.format(Input::File(filename.into())) - { - assert_eq!(&module, exp_mod_name); - if let ModuleResolutionErrorKind::ParseError { - file: unparseable_file, - } = kind - { - assert_eq!( - unparseable_file, - PathBuf::from("tests/parser/issue-4126/invalid.rs"), - ); - } else { - panic!("Expected parser error"); - } - } else { - panic!("Expected ModuleResolution operation error"); - } -} - // For each file, run rustfmt and collect the output. // Returns the number of files checked and the number of failures. fn check_files(files: Vec, opt_config: &Option) -> (Vec, u32, u32) { diff --git a/src/test/parser.rs b/src/test/parser.rs new file mode 100644 index 00000000000..75aed32e68b --- /dev/null +++ b/src/test/parser.rs @@ -0,0 +1,50 @@ +use std::io; +use std::path::PathBuf; + +use super::read_config; + +use crate::modules::{ModuleResolutionError, ModuleResolutionErrorKind}; +use crate::{ErrorKind, Input, Session}; + +#[test] +fn parser_errors_in_submods_are_surfaced() { + // See also https://github.com/rust-lang/rustfmt/issues/4126 + let filename = "tests/parser/issue-4126/lib.rs"; + let input_file = PathBuf::from(filename); + let exp_mod_name = "invalid"; + let config = read_config(&input_file); + let mut session = Session::::new(config, None); + if let Err(ErrorKind::ModuleResolutionError(ModuleResolutionError { module, kind })) = + session.format(Input::File(filename.into())) + { + assert_eq!(&module, exp_mod_name); + if let ModuleResolutionErrorKind::ParseError { + file: unparseable_file, + } = kind + { + assert_eq!( + unparseable_file, + PathBuf::from("tests/parser/issue-4126/invalid.rs"), + ); + } else { + panic!("Expected parser error"); + } + } else { + panic!("Expected ModuleResolution operation error"); + } +} + +fn assert_parser_error(filename: &str) { + let file = PathBuf::from(filename); + let config = read_config(&file); + let mut session = Session::::new(config, None); + let _ = session.format(Input::File(filename.into())).unwrap(); + assert!(session.has_parsing_errors()); +} + +#[test] +fn crate_parsing_errors_on_unclosed_delims() { + // See also https://github.com/rust-lang/rustfmt/issues/4466 + let filename = "tests/parser/unclosed-delims/issue_4466.rs"; + assert_parser_error(filename); +} diff --git a/tests/parser/unclosed-delims/issue_4466.rs b/tests/parser/unclosed-delims/issue_4466.rs new file mode 100644 index 00000000000..2c2c81c91d1 --- /dev/null +++ b/tests/parser/unclosed-delims/issue_4466.rs @@ -0,0 +1,11 @@ +fn main() { + if true { + println!("answer: {}", a_func(); + } else { + println!("don't think so."); + } +} + +fn a_func() -> i32 { + 42 +} \ No newline at end of file From 2fc074a95a3c5f81eef41127b3475f906438f436 Mon Sep 17 00:00:00 2001 From: Caleb Cartwright Date: Sat, 26 Sep 2020 00:01:21 -0500 Subject: [PATCH 3/9] tests: add test for panic on new_parser_from_file --- src/test/mod.rs | 16 ++++++++++++++++ tests/parser/issue_4418.rs | 1 + 2 files changed, 17 insertions(+) create mode 100644 tests/parser/issue_4418.rs diff --git a/src/test/mod.rs b/src/test/mod.rs index 9b3ca717152..7d2e8c18e9c 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -483,6 +483,22 @@ fn format_lines_errors_are_reported_with_tabs() { assert!(session.has_formatting_errors()); } +#[test] +fn parser_creation_errors_on_entry_new_parser_from_file_panic() { + // See also https://github.com/rust-lang/rustfmt/issues/4418 + let filename = "tests/parser/issue_4418.rs"; + let file = PathBuf::from(filename); + let (config, operation, _) = read_config(&file); + if let Err(OperationError::ParseError { input, is_panic }) = + format_file(&file, operation, config) + { + assert_eq!(input.as_path().unwrap(), file); + assert!(is_panic); + } else { + panic!("Expected ParseError operation error"); + } +} + // For each file, run rustfmt and collect the output. // Returns the number of files checked and the number of failures. fn check_files(files: Vec, opt_config: &Option) -> (Vec, u32, u32) { diff --git a/tests/parser/issue_4418.rs b/tests/parser/issue_4418.rs new file mode 100644 index 00000000000..ff30235f076 --- /dev/null +++ b/tests/parser/issue_4418.rs @@ -0,0 +1 @@ +} \ No newline at end of file From 4740158871f60a4a5a392fbb1fe06f5bad591361 Mon Sep 17 00:00:00 2001 From: Caleb Cartwright Date: Sat, 26 Sep 2020 00:00:49 -0500 Subject: [PATCH 4/9] fix: resolve some parser related bugs --- src/syntux/parser.rs | 23 ++++++++++++++++------- src/syntux/session.rs | 11 +++++++++++ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/syntux/parser.rs b/src/syntux/parser.rs index 78de08b993e..3a72fb6be88 100644 --- a/src/syntux/parser.rs +++ b/src/syntux/parser.rs @@ -65,8 +65,11 @@ impl<'a> ParserBuilder<'a> { let parser = match Self::parser(sess.inner(), input) { Ok(p) => p, Err(db) => { - sess.emit_diagnostics(db); - return Err(ParserError::ParserCreationError); + if let Some(diagnostics) = db { + sess.emit_diagnostics(diagnostics); + return Err(ParserError::ParserCreationError); + } + return Err(ParserError::ParsePanicError); } }; @@ -76,14 +79,18 @@ impl<'a> ParserBuilder<'a> { fn parser( sess: &'a rustc_session::parse::ParseSess, input: Input, - ) -> Result, Vec> { + ) -> Result, Option>> { match input { - Input::File(ref file) => Ok(new_parser_from_file(sess, file, None)), + Input::File(ref file) => catch_unwind(AssertUnwindSafe(move || { + new_parser_from_file(sess, file, None) + })) + .map_err(|_| None), Input::Text(text) => rustc_parse::maybe_new_parser_from_source_str( sess, rustc_span::FileName::Custom("stdin".to_owned()), text, - ), + ) + .map_err(|db| Some(db)), } } } @@ -120,8 +127,10 @@ impl<'a> Parser<'a> { match parser.parse_mod(&TokenKind::Eof, ast::Unsafe::No) { Ok(result) => Some(result), Err(mut e) => { - e.cancel(); - sess.reset_errors(); + sess.emit_or_cancel_diagnostic(&mut e); + if sess.can_reset_errors() { + sess.reset_errors(); + } None } } diff --git a/src/syntux/session.rs b/src/syntux/session.rs index 5045eab29dd..ef5ad62674e 100644 --- a/src/syntux/session.rs +++ b/src/syntux/session.rs @@ -230,6 +230,17 @@ impl ParseSess { } } + pub(crate) fn emit_or_cancel_diagnostic(&self, diagnostic: &mut Diagnostic) { + self.parse_sess.span_diagnostic.emit_diagnostic(diagnostic); + // The Handler will check whether the diagnostic should be emitted + // based on the user's rustfmt configuration and the originating file + // that caused the parser error. If the Handler determined it should skip + // emission then we need to ensure the diagnostic is cancelled. + if !diagnostic.cancelled() { + diagnostic.cancel(); + } + } + pub(super) fn can_reset_errors(&self) -> bool { *self.can_reset_errors.borrow() } From bb0f0ecf7ec7efdaa5ab246cbb0a4ba5da09654e Mon Sep 17 00:00:00 2001 From: Caleb Cartwright Date: Thu, 29 Oct 2020 22:59:45 -0500 Subject: [PATCH 5/9] tests: adjust some cherry-picekd tests --- src/test/mod.rs | 16 ---------------- src/test/parser.rs | 7 +++++++ 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/src/test/mod.rs b/src/test/mod.rs index 7d2e8c18e9c..9b3ca717152 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -483,22 +483,6 @@ fn format_lines_errors_are_reported_with_tabs() { assert!(session.has_formatting_errors()); } -#[test] -fn parser_creation_errors_on_entry_new_parser_from_file_panic() { - // See also https://github.com/rust-lang/rustfmt/issues/4418 - let filename = "tests/parser/issue_4418.rs"; - let file = PathBuf::from(filename); - let (config, operation, _) = read_config(&file); - if let Err(OperationError::ParseError { input, is_panic }) = - format_file(&file, operation, config) - { - assert_eq!(input.as_path().unwrap(), file); - assert!(is_panic); - } else { - panic!("Expected ParseError operation error"); - } -} - // For each file, run rustfmt and collect the output. // Returns the number of files checked and the number of failures. fn check_files(files: Vec, opt_config: &Option) -> (Vec, u32, u32) { diff --git a/src/test/parser.rs b/src/test/parser.rs index 75aed32e68b..ae4a4f94d92 100644 --- a/src/test/parser.rs +++ b/src/test/parser.rs @@ -42,6 +42,13 @@ fn assert_parser_error(filename: &str) { assert!(session.has_parsing_errors()); } +#[test] +fn parser_creation_errors_on_entry_new_parser_from_file_panic() { + // See also https://github.com/rust-lang/rustfmt/issues/4418 + let filename = "tests/parser/issue_4418.rs"; + assert_parser_error(filename); +} + #[test] fn crate_parsing_errors_on_unclosed_delims() { // See also https://github.com/rust-lang/rustfmt/issues/4466 From bc755f31912edcc6b9a7994a26b02e983b2fe233 Mon Sep 17 00:00:00 2001 From: Scott McMurray Date: Mon, 12 Oct 2020 21:43:04 -0700 Subject: [PATCH 6/9] Add some basic tests for `try{}` expressions They failed to parse in rustfmt on me in https://github.com/rust-lang/rust/pull/77877, which looks like it's since been fixed, but I figured I'd send in some tests anyway. --- tests/source/try_block.rs | 30 ++++++++++++++++++++++++++++++ tests/target/try_block.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 tests/source/try_block.rs create mode 100644 tests/target/try_block.rs diff --git a/tests/source/try_block.rs b/tests/source/try_block.rs new file mode 100644 index 00000000000..2e8d61f7e66 --- /dev/null +++ b/tests/source/try_block.rs @@ -0,0 +1,30 @@ +// rustfmt-edition: 2018 + +fn main() -> Result<(), !> { + let _x: Option<_> = try { + 4 + }; + + try {} +} + +fn baz() -> Option { + if (1 == 1) { + return try { + 5 + }; + } + + // test + let x: Option<()> = try { + // try blocks are great + }; + + let y: Option = try { + 6 + }; // comment + + let x: Option = try { baz()?; baz()?; baz()?; 7 }; + + return None; +} diff --git a/tests/target/try_block.rs b/tests/target/try_block.rs new file mode 100644 index 00000000000..19a3f3e1487 --- /dev/null +++ b/tests/target/try_block.rs @@ -0,0 +1,29 @@ +// rustfmt-edition: 2018 + +fn main() -> Result<(), !> { + let _x: Option<_> = try { 4 }; + + try {} +} + +fn baz() -> Option { + if (1 == 1) { + return try { 5 }; + } + + // test + let x: Option<()> = try { + // try blocks are great + }; + + let y: Option = try { 6 }; // comment + + let x: Option = try { + baz()?; + baz()?; + baz()?; + 7 + }; + + return None; +} From 63f172d995e4419575907e84a9bd529d200d7b6c Mon Sep 17 00:00:00 2001 From: Caleb Cartwright Date: Fri, 30 Oct 2020 00:03:03 -0500 Subject: [PATCH 7/9] feat: v2 support for nested tuples w/o spaces --- src/chains.rs | 8 ++++++-- tests/source/tuple.rs | 11 +++++++++++ tests/source/tuple_v2.rs | 5 +++++ tests/target/tuple.rs | 11 +++++++++++ tests/target/tuple_v2.rs | 5 +++++ 5 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 tests/source/tuple_v2.rs create mode 100644 tests/target/tuple_v2.rs diff --git a/src/chains.rs b/src/chains.rs index 8a0b416632b..ac14ec9e110 100644 --- a/src/chains.rs +++ b/src/chains.rs @@ -62,7 +62,7 @@ use rustc_ast::{ast, ptr}; use rustc_span::{symbol, BytePos, Span}; use crate::comment::{rewrite_comment, CharClasses, FullCodeCharKind, RichChar}; -use crate::config::IndentStyle; +use crate::config::{IndentStyle, Version}; use crate::expr::rewrite_call; use crate::lists::extract_pre_comment; use crate::macros::convert_try_mac; @@ -200,7 +200,11 @@ impl Rewrite for ChainItem { ChainItemKind::StructField(ident) => format!(".{}", rewrite_ident(context, ident)), ChainItemKind::TupleField(ident, nested) => format!( "{}.{}", - if nested { " " } else { "" }, + if nested && context.config.version() == Version::One { + " " + } else { + "" + }, rewrite_ident(context, ident) ), ChainItemKind::Await => ".await".to_owned(), diff --git a/tests/source/tuple.rs b/tests/source/tuple.rs index 6c3ec8f1115..9a0f979fbca 100644 --- a/tests/source/tuple.rs +++ b/tests/source/tuple.rs @@ -50,3 +50,14 @@ fn issue1725() { bench_antialiased_lines!(bench_draw_antialiased_line_segment_diagonal, (10, 10), (450, 450)); bench_antialiased_lines!(bench_draw_antialiased_line_segment_shallow, (10, 10), (450, 80)); } + +fn issue_4355() { + let _ = ((1,),).0.0; +} + +// https://github.com/rust-lang/rustfmt/issues/4410 +impl Drop for LockGuard { + fn drop(&mut self) { + LockMap::unlock(&self.0.0, &self.0.1); + } +} diff --git a/tests/source/tuple_v2.rs b/tests/source/tuple_v2.rs new file mode 100644 index 00000000000..9223033832b --- /dev/null +++ b/tests/source/tuple_v2.rs @@ -0,0 +1,5 @@ +// rustfmt-version: Two + +fn issue_4355() { + let _ = ((1,),).0 .0; +} diff --git a/tests/target/tuple.rs b/tests/target/tuple.rs index a22586284dc..68bb2f3bc28 100644 --- a/tests/target/tuple.rs +++ b/tests/target/tuple.rs @@ -87,3 +87,14 @@ fn issue1725() { (450, 80) ); } + +fn issue_4355() { + let _ = ((1,),).0 .0; +} + +// https://github.com/rust-lang/rustfmt/issues/4410 +impl Drop for LockGuard { + fn drop(&mut self) { + LockMap::unlock(&self.0 .0, &self.0 .1); + } +} diff --git a/tests/target/tuple_v2.rs b/tests/target/tuple_v2.rs new file mode 100644 index 00000000000..ba653291c2f --- /dev/null +++ b/tests/target/tuple_v2.rs @@ -0,0 +1,5 @@ +// rustfmt-version: Two + +fn issue_4355() { + let _ = ((1,),).0.0; +} From 494b23b901470903447ab7e98176777b37336f87 Mon Sep 17 00:00:00 2001 From: Caleb Cartwright Date: Fri, 30 Oct 2020 00:20:23 -0500 Subject: [PATCH 8/9] ci: fix asset upload workflow --- .github/workflows/upload-assets.yml | 4 +- Makefile.toml | 71 +++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 Makefile.toml diff --git a/.github/workflows/upload-assets.yml b/.github/workflows/upload-assets.yml index 7401d45d6d5..092dcffe698 100644 --- a/.github/workflows/upload-assets.yml +++ b/.github/workflows/upload-assets.yml @@ -37,8 +37,8 @@ jobs: - name: Install cargo-make uses: actions-rs/cargo@v1 with: - command: build - args: --release + command: install + args: --force cargo-make - name: Build release binaries uses: actions-rs/cargo@v1 diff --git a/Makefile.toml b/Makefile.toml new file mode 100644 index 00000000000..597dd120564 --- /dev/null +++ b/Makefile.toml @@ -0,0 +1,71 @@ +[env] +CFG_RELEASE = { value = "${CARGO_MAKE_RUST_VERSION}", condition = { env_not_set = ["CFG_RELEASE"] } } +CFG_RELEASE_CHANNEL = { value = "${CARGO_MAKE_RUST_CHANNEL}", condition = { env_not_set = ["CFG_RELEASE_CHANNEL"] } } + +[tasks.build-bin] +command = "cargo" +args = [ + "build", + "--bin", + "rustfmt", + "--bin", + "cargo-fmt", +] + +[tasks.build-bins] +command = "cargo" +args = [ + "build", + "--bins", +] + +[tasks.install] +command = "cargo" +args = [ + "install", + "--path", + ".", + "--force", + "--locked", # Respect Cargo.lock +] + +[tasks.release] +command = "cargo" +args = [ + "build", + "--release", +] + +[tasks.test] +command = "cargo" +args = [ + "test", +] + +[tasks.test-all] +dependencies = ["build-bin"] +run_task = { name = ["test", "test-ignored"] } + +[tasks.test-ignored] +command = "cargo" +args = [ + "test", + "--", + "--ignored", +] + +[tasks.b] +alias = "build" + +[tasks.bb] +alias = "build-bin" + +[tasks.bins] +alias = "build-bins" + +[tasks.c] +alias = "check" + +[tasks.t] +alias = "test" + From 42d389fcd785bf686f10bccbd26384f2c867d107 Mon Sep 17 00:00:00 2001 From: Caleb Cartwright Date: Fri, 30 Oct 2020 00:37:08 -0500 Subject: [PATCH 9/9] meta: bump rustfmt version to 1.4.23 --- CHANGELOG.md | 19 ++++++++++++++++++- Cargo.lock | 2 +- Cargo.toml | 2 +- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 903b6f121e6..1c07e15f4c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,24 @@ ## [Unreleased] +## [1.4.22] 2020-10-30 + +### Changed + +- Update `rustc-ap-*` crates to v686.0.0 + +### Added +- Initial support for formatting new ConstBlock syntax ([#4478](https://github.com/rust-lang/rustfmt/pull/4478)) + +### Fixed +- Handling of unclosed delimiter-only parsing errors in input files ([#4466](https://github.com/rust-lang/rustfmt/issues/4466)) +- Misc. minor parser bugs ([#4418](https://github.com/rust-lang/rustfmt/issues/4418) and [#4431](https://github.com/rust-lang/rustfmt/issues/4431)) +- Panic on nested tuple access ([#4355](https://github.com/rust-lang/rustfmt/issues/4355)) +- Unable to disable license template path via cli override ([#4487](https://github.com/rust-lang/rustfmt/issues/4487)) +- Preserve comments in empty statements [#4018](https://github.com/rust-lang/rustfmt/issues/4018)) +- Indentation on skipped code [#4398](https://github.com/rust-lang/rustfmt/issues/4398)) + + ## [1.4.22] 2020-10-04 ### Changed @@ -10,7 +28,6 @@ - Add config option to allow control of leading match arm pipes - Support `RUSTFMT` environment variable in `cargo fmt` to run specified `rustfmt` instance - ### Fixed - Fix preservation of type aliases within extern blocks diff --git a/Cargo.lock b/Cargo.lock index df5d17c6b32..7f3536cbe3a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1237,7 +1237,7 @@ dependencies = [ [[package]] name = "rustfmt-nightly" -version = "1.4.22" +version = "1.4.23" dependencies = [ "annotate-snippets 0.6.1", "anyhow", diff --git a/Cargo.toml b/Cargo.toml index 855f321a918..c22e1b3d12c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "rustfmt-nightly" -version = "1.4.22" +version = "1.4.23" authors = ["Nicholas Cameron ", "The Rustfmt developers"] description = "Tool to find and fix Rust formatting issues" repository = "https://github.com/rust-lang/rustfmt"