Merge pull request #6193 from ytmimi/subtree-push-nightly-2024-06-13

subtree-push nightly-2024-06-13
This commit is contained in:
Caleb Cartwright 2024-06-17 15:47:07 -05:00 committed by GitHub
commit 3ffd7d46a9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
52 changed files with 598 additions and 419 deletions

View File

@ -19,7 +19,6 @@ jobs:
matrix: matrix:
integration: [ integration: [
bitflags, bitflags,
error-chain,
log, log,
mdbook, mdbook,
packed_simd, packed_simd,

View File

@ -3,6 +3,17 @@
## [Unreleased] ## [Unreleased]
- Updating `dirs 4.0.0 -> 5.0.1` and `cargo_metadata 0.15.4 -> 0.18.0` [#6033] (https://github.com/rust-lang/rustfmt/issues/6033) - Updating `dirs 4.0.0 -> 5.0.1` and `cargo_metadata 0.15.4 -> 0.18.0` [#6033] (https://github.com/rust-lang/rustfmt/issues/6033)
- Bumped bytecount `0.6.4` -> `0.6.8` to fix compilation issues with the `generic-simd` feature. See [bytecount#92] and [bytecount#93]
[bytecount#92]: https://github.com/llogiq/bytecount/pull/92
[bytecount#93]: https://github.com/llogiq/bytecount/pull/93
- Output correct syntax for type ascription builtin [#6159](https://github.com/rust-lang/rustfmt/issues/6159)
```rust
fn main() {
builtin # type_ascribe(10, usize)
}
```
## [1.7.0] 2023-10-22 ## [1.7.0] 2023-10-22

33
Cargo.lock generated
View File

@ -98,12 +98,9 @@ dependencies = [
[[package]] [[package]]
name = "bytecount" name = "bytecount"
version = "0.6.4" version = "0.6.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad152d03a2c813c80bb94fedbf3a3f02b28f793e39e7c214c8a0bcc196343de7" checksum = "5ce89b21cab1437276d2650d57e971f9d548a2d9037cc231abdc0562b97498ce"
dependencies = [
"packed_simd",
]
[[package]] [[package]]
name = "camino" name = "camino"
@ -369,12 +366,6 @@ version = "0.2.141"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3304a64d199bb964be99741b7a14d26972741915b3649639149b2479bb46f4b5" checksum = "3304a64d199bb964be99741b7a14d26972741915b3649639149b2479bb46f4b5"
[[package]]
name = "libm"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058"
[[package]] [[package]]
name = "log" name = "log"
version = "0.4.16" version = "0.4.16"
@ -409,16 +400,6 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "num-traits"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c"
dependencies = [
"autocfg",
"libm",
]
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.17.1" version = "1.17.1"
@ -437,16 +418,6 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39"
[[package]]
name = "packed_simd"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f9f08af0c877571712e2e3e686ad79efad9657dbf0f7c3c8ba943ff6c38932d"
dependencies = [
"cfg-if",
"num-traits",
]
[[package]] [[package]]
name = "pin-project-lite" name = "pin-project-lite"
version = "0.2.9" version = "0.2.9"

View File

@ -35,7 +35,7 @@ generic-simd = ["bytecount/generic-simd"]
[dependencies] [dependencies]
annotate-snippets = { version = "0.9", features = ["color"] } annotate-snippets = { version = "0.9", features = ["color"] }
anyhow = "1.0" anyhow = "1.0"
bytecount = "0.6.4" bytecount = "0.6.8"
cargo_metadata = "0.18" cargo_metadata = "0.18"
clap = { version = "4.4.2", features = ["derive"] } clap = { version = "4.4.2", features = ["derive"] }
clap-cargo = "0.12.0" clap-cargo = "0.12.0"

View File

@ -104,7 +104,7 @@ case ${INTEGRATION} in
check_fmt_with_all_tests check_fmt_with_all_tests
cd - cd -
;; ;;
error-chain | tempdir) tempdir)
git clone --depth=1 https://github.com/rust-lang-deprecated/${INTEGRATION}.git git clone --depth=1 https://github.com/rust-lang-deprecated/${INTEGRATION}.git
cd ${INTEGRATION} cd ${INTEGRATION}
show_head show_head

View File

@ -68,7 +68,11 @@ fn get_name_value_str_lit(attr: &syn::Attribute, name: &str) -> Option<String> {
match &attr.meta { match &attr.meta {
syn::Meta::NameValue(syn::MetaNameValue { syn::Meta::NameValue(syn::MetaNameValue {
path, path,
value: syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Str(lit_str), .. }), value:
syn::Expr::Lit(syn::ExprLit {
lit: syn::Lit::Str(lit_str),
..
}),
.. ..
}) if path.is_ident(name) => Some(lit_str.value()), }) if path.is_ident(name) => Some(lit_str.value()),
_ => None, _ => None,

View File

@ -1,3 +1,3 @@
[toolchain] [toolchain]
channel = "nightly-2023-12-28" channel = "nightly-2024-06-13"
components = ["llvm-tools", "rustc-dev"] components = ["llvm-tools", "rustc-dev"]

View File

@ -26,7 +26,7 @@ pub(crate) fn get_attrs_from_stmt(stmt: &ast::Stmt) -> &[ast::Attribute] {
pub(crate) fn get_span_without_attrs(stmt: &ast::Stmt) -> Span { pub(crate) fn get_span_without_attrs(stmt: &ast::Stmt) -> Span {
match stmt.kind { match stmt.kind {
ast::StmtKind::Local(ref local) => local.span, ast::StmtKind::Let(ref local) => local.span,
ast::StmtKind::Item(ref item) => item.span, ast::StmtKind::Item(ref item) => item.span,
ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => expr.span, ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => expr.span,
ast::StmtKind::MacCall(ref mac_stmt) => mac_stmt.mac.span(), ast::StmtKind::MacCall(ref mac_stmt) => mac_stmt.mac.span(),
@ -353,10 +353,18 @@ impl Rewrite for ast::Attribute {
// 1 = `[` // 1 = `[`
let shape = shape.offset_left(prefix.len() + 1)?; let shape = shape.offset_left(prefix.len() + 1)?;
Some( Some(meta.rewrite(context, shape).map_or_else(
meta.rewrite(context, shape) || snippet.to_owned(),
.map_or_else(|| snippet.to_owned(), |rw| format!("{}[{}]", prefix, rw)), |rw| match &self.kind {
) ast::AttrKind::Normal(normal_attr) => match normal_attr.item.unsafety {
// For #![feature(unsafe_attributes)]
// See https://github.com/rust-lang/rust/issues/123757
ast::Safety::Unsafe(_) => format!("{}[unsafe({})]", prefix, rw),
_ => format!("{}[{}]", prefix, rw),
},
_ => format!("{}[{}]", prefix, rw),
},
))
} else { } else {
Some(snippet.to_owned()) Some(snippet.to_owned())
} }

View File

@ -1714,10 +1714,10 @@ pub(crate) fn recover_comment_removed(
// We missed some comments. Warn and keep the original text. // We missed some comments. Warn and keep the original text.
if context.config.error_on_unformatted() { if context.config.error_on_unformatted() {
context.report.append( context.report.append(
context.parse_sess.span_to_filename(span), context.psess.span_to_filename(span),
vec![FormattingError::from_span( vec![FormattingError::from_span(
span, span,
context.parse_sess, context.psess,
ErrorKind::LostComment, ErrorKind::LostComment,
)], )],
); );

View File

@ -1,6 +1,7 @@
use crate::config::StyleEdition; use crate::config::StyleEdition;
/// Defines the default value for the given style edition /// Defines the default value for the given style edition
#[allow(dead_code)]
pub(crate) trait StyleEditionDefault { pub(crate) trait StyleEditionDefault {
type ConfigType; type ConfigType;
fn style_edition_default(style_edition: StyleEdition) -> Self::ConfigType; fn style_edition_default(style_edition: StyleEdition) -> Self::ConfigType;

View File

@ -3,7 +3,7 @@ use std::cmp::min;
use itertools::Itertools; use itertools::Itertools;
use rustc_ast::token::{Delimiter, Lit, LitKind}; use rustc_ast::token::{Delimiter, Lit, LitKind};
use rustc_ast::{ast, ptr, token, ForLoopKind}; use rustc_ast::{ast, ptr, token, ForLoopKind, MatchKind};
use rustc_span::{BytePos, Span}; use rustc_span::{BytePos, Span};
use crate::chains::rewrite_chain; use crate::chains::rewrite_chain;
@ -180,8 +180,8 @@ pub(crate) fn format_expr(
} }
} }
} }
ast::ExprKind::Match(ref cond, ref arms) => { ast::ExprKind::Match(ref cond, ref arms, kind) => {
rewrite_match(context, cond, arms, shape, expr.span, &expr.attrs) rewrite_match(context, cond, arms, shape, expr.span, &expr.attrs, kind)
} }
ast::ExprKind::Path(ref qself, ref path) => { ast::ExprKind::Path(ref qself, ref path) => {
rewrite_path(context, PathContext::Expr, qself, path, shape) rewrite_path(context, PathContext::Expr, qself, path, shape)
@ -263,14 +263,6 @@ pub(crate) fn format_expr(
shape, shape,
SeparatorPlace::Front, SeparatorPlace::Front,
), ),
ast::ExprKind::Type(ref expr, ref ty) => rewrite_pair(
&**expr,
&**ty,
PairParts::infix(": "),
context,
shape,
SeparatorPlace::Back,
),
ast::ExprKind::Index(ref expr, ref index, _) => { ast::ExprKind::Index(ref expr, ref index, _) => {
rewrite_index(&**expr, &**index, context, shape) rewrite_index(&**expr, &**index, context, shape)
} }
@ -412,6 +404,7 @@ pub(crate) fn format_expr(
} }
ast::ExprKind::Underscore => Some("_".to_owned()), ast::ExprKind::Underscore => Some("_".to_owned()),
ast::ExprKind::FormatArgs(..) ast::ExprKind::FormatArgs(..)
| ast::ExprKind::Type(..)
| ast::ExprKind::IncludedBytes(..) | ast::ExprKind::IncludedBytes(..)
| ast::ExprKind::OffsetOf(..) => { | ast::ExprKind::OffsetOf(..) => {
// These don't normally occur in the AST because macros aren't expanded. However, // These don't normally occur in the AST because macros aren't expanded. However,
@ -420,7 +413,7 @@ pub(crate) fn format_expr(
// Also, rustfmt might get passed the output from `-Zunpretty=expanded`. // Also, rustfmt might get passed the output from `-Zunpretty=expanded`.
None None
} }
ast::ExprKind::Err => None, ast::ExprKind::Err(_) | ast::ExprKind::Dummy => None,
}; };
expr_rw expr_rw
@ -641,7 +634,7 @@ pub(crate) fn rewrite_cond(
shape: Shape, shape: Shape,
) -> Option<String> { ) -> Option<String> {
match expr.kind { match expr.kind {
ast::ExprKind::Match(ref cond, _) => { ast::ExprKind::Match(ref cond, _, MatchKind::Prefix) => {
// `match `cond` {` // `match `cond` {`
let cond_shape = match context.config.indent_style() { let cond_shape = match context.config.indent_style() {
IndentStyle::Visual => shape.shrink_left(6).and_then(|s| s.sub_width(2))?, IndentStyle::Visual => shape.shrink_left(6).and_then(|s| s.sub_width(2))?,
@ -1963,7 +1956,7 @@ fn rewrite_unary_op(
} }
pub(crate) enum RhsAssignKind<'ast> { pub(crate) enum RhsAssignKind<'ast> {
Expr(&'ast ast::ExprKind, Span), Expr(&'ast ast::ExprKind, #[allow(dead_code)] Span),
Bounds, Bounds,
Ty, Ty,
} }

View File

@ -79,7 +79,7 @@ fn should_skip_module<T: FormatHandler>(
// FIXME(calebcartwright) - we need to determine how we'll handle the // FIXME(calebcartwright) - we need to determine how we'll handle the
// `format_generated_files` option with stdin based input. // `format_generated_files` option with stdin based input.
if !input_is_stdin && !config.format_generated_files() { if !input_is_stdin && !config.format_generated_files() {
let source_file = context.parse_session.span_to_file_contents(module.span); let source_file = context.psess.span_to_file_contents(module.span);
let src = source_file.src.as_ref().expect("SourceFile without src"); let src = source_file.src.as_ref().expect("SourceFile without src");
if is_generated_file(src, config) { if is_generated_file(src, config) {
@ -109,15 +109,16 @@ fn format_project<T: FormatHandler>(
let main_file = input.file_name(); let main_file = input.file_name();
let input_is_stdin = main_file == FileName::Stdin; let input_is_stdin = main_file == FileName::Stdin;
let parse_session = ParseSess::new(config)?; let psess = ParseSess::new(config)?;
if config.skip_children() && parse_session.ignore_file(&main_file) { if config.skip_children() && psess.ignore_file(&main_file) {
return Ok(FormatReport::new()); return Ok(FormatReport::new());
} }
// Parse the crate. // Parse the crate.
let mut report = FormatReport::new(); let mut report = FormatReport::new();
let directory_ownership = input.to_directory_ownership(); let directory_ownership = input.to_directory_ownership();
let krate = match Parser::parse_crate(input, &parse_session) {
let krate = match Parser::parse_crate(input, &psess) {
Ok(krate) => krate, Ok(krate) => krate,
// Surface parse error via Session (errors are merged there from report) // Surface parse error via Session (errors are merged there from report)
Err(e) => { Err(e) => {
@ -130,9 +131,9 @@ fn format_project<T: FormatHandler>(
} }
}; };
let mut context = FormatContext::new(&krate, report, parse_session, config, handler); let mut context = FormatContext::new(&krate, report, psess, config, handler);
let files = modules::ModResolver::new( let files = modules::ModResolver::new(
&context.parse_session, &context.psess,
directory_ownership.unwrap_or(DirectoryOwnership::UnownedViaBlock), directory_ownership.unwrap_or(DirectoryOwnership::UnownedViaBlock),
!input_is_stdin && !config.skip_children(), !input_is_stdin && !config.skip_children(),
) )
@ -147,16 +148,11 @@ fn format_project<T: FormatHandler>(
timer = timer.done_parsing(); timer = timer.done_parsing();
// Suppress error output if we have to do any further parsing. // Suppress error output if we have to do any further parsing.
context.parse_session.set_silent_emitter(); context.psess.set_silent_emitter();
for (path, module) in files { for (path, module) in files {
if input_is_stdin && contains_skip(module.attrs()) { if input_is_stdin && contains_skip(module.attrs()) {
return echo_back_stdin( return echo_back_stdin(context.psess.snippet_provider(module.span).entire_snippet());
context
.parse_session
.snippet_provider(module.span)
.entire_snippet(),
);
} }
should_emit_verbose(input_is_stdin, config, || println!("Formatting {}", path)); should_emit_verbose(input_is_stdin, config, || println!("Formatting {}", path));
context.format_file(path, &module, is_macro_def)?; context.format_file(path, &module, is_macro_def)?;
@ -178,7 +174,7 @@ fn format_project<T: FormatHandler>(
struct FormatContext<'a, T: FormatHandler> { struct FormatContext<'a, T: FormatHandler> {
krate: &'a ast::Crate, krate: &'a ast::Crate,
report: FormatReport, report: FormatReport,
parse_session: ParseSess, psess: ParseSess,
config: &'a Config, config: &'a Config,
handler: &'a mut T, handler: &'a mut T,
} }
@ -187,21 +183,21 @@ impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
fn new( fn new(
krate: &'a ast::Crate, krate: &'a ast::Crate,
report: FormatReport, report: FormatReport,
parse_session: ParseSess, psess: ParseSess,
config: &'a Config, config: &'a Config,
handler: &'a mut T, handler: &'a mut T,
) -> Self { ) -> Self {
FormatContext { FormatContext {
krate, krate,
report, report,
parse_session, psess,
config, config,
handler, handler,
} }
} }
fn ignore_file(&self, path: &FileName) -> bool { fn ignore_file(&self, path: &FileName) -> bool {
self.parse_session.ignore_file(path) self.psess.ignore_file(path)
} }
// Formats a single file/module. // Formats a single file/module.
@ -211,9 +207,9 @@ impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
module: &Module<'_>, module: &Module<'_>,
is_macro_def: bool, is_macro_def: bool,
) -> Result<(), ErrorKind> { ) -> Result<(), ErrorKind> {
let snippet_provider = self.parse_session.snippet_provider(module.span); let snippet_provider = self.psess.snippet_provider(module.span);
let mut visitor = FmtVisitor::from_parse_sess( let mut visitor = FmtVisitor::from_psess(
&self.parse_session, &self.psess,
self.config, self.config,
&snippet_provider, &snippet_provider,
self.report.clone(), self.report.clone(),
@ -256,7 +252,7 @@ impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
.add_non_formatted_ranges(visitor.skipped_range.borrow().clone()); .add_non_formatted_ranges(visitor.skipped_range.borrow().clone());
self.handler.handle_formatted_file( self.handler.handle_formatted_file(
&self.parse_session, &self.psess,
path, path,
visitor.buffer.to_owned(), visitor.buffer.to_owned(),
&mut self.report, &mut self.report,
@ -268,7 +264,7 @@ impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
trait FormatHandler { trait FormatHandler {
fn handle_formatted_file( fn handle_formatted_file(
&mut self, &mut self,
parse_session: &ParseSess, psess: &ParseSess,
path: FileName, path: FileName,
result: String, result: String,
report: &mut FormatReport, report: &mut FormatReport,
@ -279,14 +275,14 @@ impl<'b, T: Write + 'b> FormatHandler for Session<'b, T> {
// Called for each formatted file. // Called for each formatted file.
fn handle_formatted_file( fn handle_formatted_file(
&mut self, &mut self,
parse_session: &ParseSess, psess: &ParseSess,
path: FileName, path: FileName,
result: String, result: String,
report: &mut FormatReport, report: &mut FormatReport,
) -> Result<(), ErrorKind> { ) -> Result<(), ErrorKind> {
if let Some(ref mut out) = self.out { if let Some(ref mut out) = self.out {
match source_file::write_file( match source_file::write_file(
Some(parse_session), Some(psess),
&path, &path,
&result, &result,
out, out,
@ -317,17 +313,13 @@ pub(crate) struct FormattingError {
} }
impl FormattingError { impl FormattingError {
pub(crate) fn from_span( pub(crate) fn from_span(span: Span, psess: &ParseSess, kind: ErrorKind) -> FormattingError {
span: Span,
parse_sess: &ParseSess,
kind: ErrorKind,
) -> FormattingError {
FormattingError { FormattingError {
line: parse_sess.line_of_byte_pos(span.lo()), line: psess.line_of_byte_pos(span.lo()),
is_comment: kind.is_comment(), is_comment: kind.is_comment(),
kind, kind,
is_string: false, is_string: false,
line_buffer: parse_sess.span_to_first_line_string(span), line_buffer: psess.span_to_first_line_string(span),
} }
} }

View File

@ -458,7 +458,9 @@ impl UseTree {
version, version,
}); });
} }
UseTreeKind::Nested(ref list) => { UseTreeKind::Nested {
items: ref list, ..
} => {
// Extract comments between nested use items. // Extract comments between nested use items.
// This needs to be done before sorting use items. // This needs to be done before sorting use items.
let items = itemize_list( let items = itemize_list(

View File

@ -247,7 +247,7 @@ fn allow_single_line_let_else_block(result: &str, block: &ast::Block) -> bool {
#[allow(dead_code)] #[allow(dead_code)]
#[derive(Debug)] #[derive(Debug)]
struct Item<'a> { struct Item<'a> {
unsafety: ast::Unsafe, safety: ast::Safety,
abi: Cow<'static, str>, abi: Cow<'static, str>,
vis: Option<&'a ast::Visibility>, vis: Option<&'a ast::Visibility>,
body: Vec<BodyElement<'a>>, body: Vec<BodyElement<'a>>,
@ -257,7 +257,7 @@ struct Item<'a> {
impl<'a> Item<'a> { impl<'a> Item<'a> {
fn from_foreign_mod(fm: &'a ast::ForeignMod, span: Span, config: &Config) -> Item<'a> { fn from_foreign_mod(fm: &'a ast::ForeignMod, span: Span, config: &Config) -> Item<'a> {
Item { Item {
unsafety: fm.unsafety, safety: fm.safety,
abi: format_extern( abi: format_extern(
ast::Extern::from_abi(fm.abi, DUMMY_SP), ast::Extern::from_abi(fm.abi, DUMMY_SP),
config.force_explicit_abi(), config.force_explicit_abi(),
@ -290,7 +290,7 @@ pub(crate) struct FnSig<'a> {
coroutine_kind: Cow<'a, Option<ast::CoroutineKind>>, coroutine_kind: Cow<'a, Option<ast::CoroutineKind>>,
constness: ast::Const, constness: ast::Const,
defaultness: ast::Defaultness, defaultness: ast::Defaultness,
unsafety: ast::Unsafe, safety: ast::Safety,
visibility: &'a ast::Visibility, visibility: &'a ast::Visibility,
} }
@ -301,7 +301,7 @@ impl<'a> FnSig<'a> {
visibility: &'a ast::Visibility, visibility: &'a ast::Visibility,
) -> FnSig<'a> { ) -> FnSig<'a> {
FnSig { FnSig {
unsafety: method_sig.header.unsafety, safety: method_sig.header.safety,
coroutine_kind: Cow::Borrowed(&method_sig.header.coroutine_kind), coroutine_kind: Cow::Borrowed(&method_sig.header.coroutine_kind),
constness: method_sig.header.constness, constness: method_sig.header.constness,
defaultness: ast::Defaultness::Final, defaultness: ast::Defaultness::Final,
@ -330,7 +330,7 @@ impl<'a> FnSig<'a> {
constness: fn_sig.header.constness, constness: fn_sig.header.constness,
coroutine_kind: Cow::Borrowed(&fn_sig.header.coroutine_kind), coroutine_kind: Cow::Borrowed(&fn_sig.header.coroutine_kind),
defaultness, defaultness,
unsafety: fn_sig.header.unsafety, safety: fn_sig.header.safety,
visibility: vis, visibility: vis,
}, },
_ => unreachable!(), _ => unreachable!(),
@ -345,7 +345,7 @@ impl<'a> FnSig<'a> {
result.push_str(format_constness(self.constness)); result.push_str(format_constness(self.constness));
self.coroutine_kind self.coroutine_kind
.map(|coroutine_kind| result.push_str(format_coro(&coroutine_kind))); .map(|coroutine_kind| result.push_str(format_coro(&coroutine_kind)));
result.push_str(format_unsafety(self.unsafety)); result.push_str(format_safety(self.safety));
result.push_str(&format_extern( result.push_str(&format_extern(
self.ext, self.ext,
context.config.force_explicit_abi(), context.config.force_explicit_abi(),
@ -356,7 +356,7 @@ impl<'a> FnSig<'a> {
impl<'a> FmtVisitor<'a> { impl<'a> FmtVisitor<'a> {
fn format_item(&mut self, item: &Item<'_>) { fn format_item(&mut self, item: &Item<'_>) {
self.buffer.push_str(format_unsafety(item.unsafety)); self.buffer.push_str(format_safety(item.safety));
self.buffer.push_str(&item.abi); self.buffer.push_str(&item.abi);
let snippet = self.snippet(item.span); let snippet = self.snippet(item.span);
@ -735,7 +735,9 @@ impl<'a> FmtVisitor<'a> {
(Const(..), Const(..)) | (MacCall(..), MacCall(..)) => { (Const(..), Const(..)) | (MacCall(..), MacCall(..)) => {
a.ident.as_str().cmp(b.ident.as_str()) a.ident.as_str().cmp(b.ident.as_str())
} }
(Fn(..), Fn(..)) => a.span.lo().cmp(&b.span.lo()), (Fn(..), Fn(..)) | (Delegation(..), Delegation(..)) => {
a.span.lo().cmp(&b.span.lo())
}
(Type(ty), _) if is_type(&ty.ty) => Ordering::Less, (Type(ty), _) if is_type(&ty.ty) => Ordering::Less,
(_, Type(ty)) if is_type(&ty.ty) => Ordering::Greater, (_, Type(ty)) if is_type(&ty.ty) => Ordering::Greater,
(Type(..), _) => Ordering::Less, (Type(..), _) => Ordering::Less,
@ -744,6 +746,8 @@ impl<'a> FmtVisitor<'a> {
(_, Const(..)) => Ordering::Greater, (_, Const(..)) => Ordering::Greater,
(MacCall(..), _) => Ordering::Less, (MacCall(..), _) => Ordering::Less,
(_, MacCall(..)) => Ordering::Greater, (_, MacCall(..)) => Ordering::Greater,
(Delegation(..), _) | (DelegationMac(..), _) => Ordering::Less,
(_, Delegation(..)) | (_, DelegationMac(..)) => Ordering::Greater,
}); });
let mut prev_kind = None; let mut prev_kind = None;
for (buf, item) in buffer { for (buf, item) in buffer {
@ -929,7 +933,7 @@ fn format_impl_ref_and_type(
offset: Indent, offset: Indent,
) -> Option<String> { ) -> Option<String> {
let ast::Impl { let ast::Impl {
unsafety, safety,
polarity, polarity,
defaultness, defaultness,
constness, constness,
@ -942,7 +946,7 @@ fn format_impl_ref_and_type(
result.push_str(&format_visibility(context, &item.vis)); result.push_str(&format_visibility(context, &item.vis));
result.push_str(format_defaultness(defaultness)); result.push_str(format_defaultness(defaultness));
result.push_str(format_unsafety(unsafety)); result.push_str(format_safety(safety));
let shape = if context.config.version() == Version::Two { let shape = if context.config.version() == Version::Two {
Shape::indented(offset + last_line_width(&result), context.config) Shape::indented(offset + last_line_width(&result), context.config)
@ -1142,7 +1146,7 @@ pub(crate) fn format_trait(
}; };
let ast::Trait { let ast::Trait {
is_auto, is_auto,
unsafety, safety,
ref generics, ref generics,
ref bounds, ref bounds,
ref items, ref items,
@ -1152,7 +1156,7 @@ pub(crate) fn format_trait(
let header = format!( let header = format!(
"{}{}{}trait ", "{}{}{}trait ",
format_visibility(context, &item.vis), format_visibility(context, &item.vis),
format_unsafety(unsafety), format_safety(safety),
format_auto(is_auto), format_auto(is_auto),
); );
result.push_str(&header); result.push_str(&header);
@ -1656,8 +1660,7 @@ struct TyAliasRewriteInfo<'c, 'g>(
&'c RewriteContext<'c>, &'c RewriteContext<'c>,
Indent, Indent,
&'g ast::Generics, &'g ast::Generics,
(ast::TyAliasWhereClause, ast::TyAliasWhereClause), ast::TyAliasWhereClauses,
usize,
symbol::Ident, symbol::Ident,
Span, Span,
); );
@ -1677,7 +1680,6 @@ pub(crate) fn rewrite_type_alias<'a, 'b>(
ref bounds, ref bounds,
ref ty, ref ty,
where_clauses, where_clauses,
where_predicates_split,
} = *ty_alias_kind; } = *ty_alias_kind;
let ty_opt = ty.as_ref(); let ty_opt = ty.as_ref();
let (ident, vis) = match visitor_kind { let (ident, vis) = match visitor_kind {
@ -1685,15 +1687,7 @@ pub(crate) fn rewrite_type_alias<'a, 'b>(
AssocTraitItem(i) | AssocImplItem(i) => (i.ident, &i.vis), AssocTraitItem(i) | AssocImplItem(i) => (i.ident, &i.vis),
ForeignItem(i) => (i.ident, &i.vis), ForeignItem(i) => (i.ident, &i.vis),
}; };
let rw_info = &TyAliasRewriteInfo( let rw_info = &TyAliasRewriteInfo(context, indent, generics, where_clauses, ident, span);
context,
indent,
generics,
where_clauses,
where_predicates_split,
ident,
span,
);
let op_ty = opaque_ty(ty); let op_ty = opaque_ty(ty);
// Type Aliases are formatted slightly differently depending on the context // Type Aliases are formatted slightly differently depending on the context
// in which they appear, whether they are opaque, and whether they are associated. // in which they appear, whether they are opaque, and whether they are associated.
@ -1729,19 +1723,11 @@ fn rewrite_ty<R: Rewrite>(
vis: &ast::Visibility, vis: &ast::Visibility,
) -> Option<String> { ) -> Option<String> {
let mut result = String::with_capacity(128); let mut result = String::with_capacity(128);
let TyAliasRewriteInfo( let TyAliasRewriteInfo(context, indent, generics, where_clauses, ident, span) = *rw_info;
context,
indent,
generics,
where_clauses,
where_predicates_split,
ident,
span,
) = *rw_info;
let (before_where_predicates, after_where_predicates) = generics let (before_where_predicates, after_where_predicates) = generics
.where_clause .where_clause
.predicates .predicates
.split_at(where_predicates_split); .split_at(where_clauses.split);
if !after_where_predicates.is_empty() { if !after_where_predicates.is_empty() {
return None; return None;
} }
@ -1776,7 +1762,7 @@ fn rewrite_ty<R: Rewrite>(
let where_clause_str = rewrite_where_clause( let where_clause_str = rewrite_where_clause(
context, context,
before_where_predicates, before_where_predicates,
where_clauses.0.1, where_clauses.before.span,
context.config.brace_style(), context.config.brace_style(),
Shape::legacy(where_budget, indent), Shape::legacy(where_budget, indent),
false, false,
@ -1800,7 +1786,7 @@ fn rewrite_ty<R: Rewrite>(
let comment_span = context let comment_span = context
.snippet_provider .snippet_provider
.opt_span_before(span, "=") .opt_span_before(span, "=")
.map(|op_lo| mk_sp(where_clauses.0.1.hi(), op_lo)); .map(|op_lo| mk_sp(where_clauses.before.span.hi(), op_lo));
let lhs = match comment_span { let lhs = match comment_span {
Some(comment_span) Some(comment_span)
@ -3348,11 +3334,11 @@ impl Rewrite for ast::ForeignItem {
.map(|(s, _, _)| format!("{};", s)) .map(|(s, _, _)| format!("{};", s))
} }
} }
ast::ForeignItemKind::Static(ref ty, mutability, _) => { ast::ForeignItemKind::Static(ref static_foreign_item) => {
// FIXME(#21): we're dropping potential comments in between the // FIXME(#21): we're dropping potential comments in between the
// function kw here. // function kw here.
let vis = format_visibility(context, &self.vis); let vis = format_visibility(context, &self.vis);
let mut_str = format_mutability(mutability); let mut_str = format_mutability(static_foreign_item.mutability);
let prefix = format!( let prefix = format!(
"{}static {}{}:", "{}static {}{}:",
vis, vis,
@ -3363,7 +3349,7 @@ impl Rewrite for ast::ForeignItem {
rewrite_assign_rhs( rewrite_assign_rhs(
context, context,
prefix, prefix,
&**ty, &static_foreign_item.ty,
&RhsAssignKind::Ty, &RhsAssignKind::Ty,
shape.sub_width(1)?, shape.sub_width(1)?,
) )

View File

@ -137,8 +137,8 @@ fn return_macro_parse_failure_fallback(
} }
context.skipped_range.borrow_mut().push(( context.skipped_range.borrow_mut().push((
context.parse_sess.line_of_byte_pos(span.lo()), context.psess.line_of_byte_pos(span.lo()),
context.parse_sess.line_of_byte_pos(span.hi()), context.psess.line_of_byte_pos(span.hi()),
)); ));
// Return the snippet unmodified if the macro is not block-like // Return the snippet unmodified if the macro is not block-like
@ -1092,7 +1092,7 @@ fn next_space(tok: &TokenKind) -> SpaceState {
| TokenKind::DotDotEq | TokenKind::DotDotEq
| TokenKind::Question => SpaceState::Punctuation, | TokenKind::Question => SpaceState::Punctuation,
TokenKind::ModSep TokenKind::PathSep
| TokenKind::Pound | TokenKind::Pound
| TokenKind::Dollar | TokenKind::Dollar
| TokenKind::OpenDelim(_) | TokenKind::OpenDelim(_)

View File

@ -2,7 +2,7 @@
use std::iter::repeat; use std::iter::repeat;
use rustc_ast::{ast, ptr}; use rustc_ast::{ast, ptr, MatchKind};
use rustc_span::{BytePos, Span}; use rustc_span::{BytePos, Span};
use crate::comment::{combine_strs_with_missing_comments, rewrite_comment, FindUncommented}; use crate::comment::{combine_strs_with_missing_comments, rewrite_comment, FindUncommented};
@ -72,6 +72,7 @@ pub(crate) fn rewrite_match(
shape: Shape, shape: Shape,
span: Span, span: Span,
attrs: &[ast::Attribute], attrs: &[ast::Attribute],
match_kind: MatchKind,
) -> Option<String> { ) -> Option<String> {
// Do not take the rhs overhead from the upper expressions into account // Do not take the rhs overhead from the upper expressions into account
// when rewriting match condition. // when rewriting match condition.
@ -135,15 +136,27 @@ pub(crate) fn rewrite_match(
} }
} else { } else {
let span_after_cond = mk_sp(cond.span.hi(), span.hi()); let span_after_cond = mk_sp(cond.span.hi(), span.hi());
Some(format!(
"match {}{}{{\n{}{}{}\n{}}}", match match_kind {
cond_str, MatchKind::Prefix => Some(format!(
block_sep, "match {}{}{{\n{}{}{}\n{}}}",
inner_attrs_str, cond_str,
nested_indent_str, block_sep,
rewrite_match_arms(context, arms, shape, span_after_cond, open_brace_pos)?, inner_attrs_str,
shape.indent.to_string(context.config), nested_indent_str,
)) rewrite_match_arms(context, arms, shape, span_after_cond, open_brace_pos)?,
shape.indent.to_string(context.config),
)),
MatchKind::Postfix => Some(format!(
"{}.match{}{{\n{}{}{}\n{}}}",
cond_str,
block_sep,
inner_attrs_str,
nested_indent_str,
rewrite_match_arms(context, arms, shape, span_after_cond, open_brace_pos)?,
shape.indent.to_string(context.config),
)),
}
} }
} }

View File

@ -91,7 +91,7 @@ impl<'a> FmtVisitor<'a> {
assert!( assert!(
start < end, start < end,
"Request to format inverted span: {}", "Request to format inverted span: {}",
self.parse_sess.span_to_debug_info(mk_sp(start, end)), self.psess.span_to_debug_info(mk_sp(start, end)),
); );
self.last_pos = end; self.last_pos = end;
@ -166,8 +166,8 @@ impl<'a> FmtVisitor<'a> {
// Trim whitespace from the right hand side of each line. // Trim whitespace from the right hand side of each line.
// Annoyingly, the library functions for splitting by lines etc. are not // Annoyingly, the library functions for splitting by lines etc. are not
// quite right, so we must do it ourselves. // quite right, so we must do it ourselves.
let line = self.parse_sess.line_of_byte_pos(span.lo()); let line = self.psess.line_of_byte_pos(span.lo());
let file_name = &self.parse_sess.span_to_filename(span); let file_name = &self.psess.span_to_filename(span);
let mut status = SnippetStatus::new(line); let mut status = SnippetStatus::new(line);
let snippet = &*transform_missing_snippet(self.config, old_snippet); let snippet = &*transform_missing_snippet(self.config, old_snippet);

View File

@ -57,8 +57,8 @@ impl<'a> Module<'a> {
} }
/// Maps each module to the corresponding file. /// Maps each module to the corresponding file.
pub(crate) struct ModResolver<'ast, 'sess> { pub(crate) struct ModResolver<'ast, 'psess> {
parse_sess: &'sess ParseSess, psess: &'psess ParseSess,
directory: Directory, directory: Directory,
file_map: FileModMap<'ast>, file_map: FileModMap<'ast>,
recursive: bool, recursive: bool,
@ -99,10 +99,10 @@ enum SubModKind<'a, 'ast> {
Internal(&'a ast::Item), Internal(&'a ast::Item),
} }
impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> { impl<'ast, 'psess, 'c> ModResolver<'ast, 'psess> {
/// Creates a new `ModResolver`. /// Creates a new `ModResolver`.
pub(crate) fn new( pub(crate) fn new(
parse_sess: &'sess ParseSess, psess: &'psess ParseSess,
directory_ownership: DirectoryOwnership, directory_ownership: DirectoryOwnership,
recursive: bool, recursive: bool,
) -> Self { ) -> Self {
@ -112,7 +112,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
ownership: directory_ownership, ownership: directory_ownership,
}, },
file_map: BTreeMap::new(), file_map: BTreeMap::new(),
parse_sess, psess,
recursive, recursive,
} }
} }
@ -122,7 +122,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
mut self, mut self,
krate: &'ast ast::Crate, krate: &'ast ast::Crate,
) -> Result<FileModMap<'ast>, ModuleResolutionError> { ) -> Result<FileModMap<'ast>, ModuleResolutionError> {
let root_filename = self.parse_sess.span_to_filename(krate.spans.inner_span); let root_filename = self.psess.span_to_filename(krate.spans.inner_span);
self.directory.path = match root_filename { self.directory.path = match root_filename {
FileName::Real(ref p) => p.parent().unwrap_or(Path::new("")).to_path_buf(), FileName::Real(ref p) => p.parent().unwrap_or(Path::new("")).to_path_buf(),
_ => PathBuf::new(), _ => PathBuf::new(),
@ -133,7 +133,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
self.visit_mod_from_ast(&krate.items)?; self.visit_mod_from_ast(&krate.items)?;
} }
let snippet_provider = self.parse_sess.snippet_provider(krate.spans.inner_span); let snippet_provider = self.psess.snippet_provider(krate.spans.inner_span);
self.file_map.insert( self.file_map.insert(
root_filename, root_filename,
@ -149,7 +149,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
/// Visit `cfg_if` macro and look for module declarations. /// Visit `cfg_if` macro and look for module declarations.
fn visit_cfg_if(&mut self, item: Cow<'ast, ast::Item>) -> Result<(), ModuleResolutionError> { fn visit_cfg_if(&mut self, item: Cow<'ast, ast::Item>) -> Result<(), ModuleResolutionError> {
let mut visitor = visitor::CfgIfVisitor::new(self.parse_sess); let mut visitor = visitor::CfgIfVisitor::new(self.psess);
visitor.visit_item(&item); visitor.visit_item(&item);
for module_item in visitor.mods() { for module_item in visitor.mods() {
if let ast::ItemKind::Mod(_, ref sub_mod_kind) = module_item.item.kind { if let ast::ItemKind::Mod(_, ref sub_mod_kind) = module_item.item.kind {
@ -338,10 +338,10 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
DirectoryOwnership::UnownedViaBlock => None, DirectoryOwnership::UnownedViaBlock => None,
}; };
if let Some(path) = Parser::submod_path_from_attr(attrs, &self.directory.path) { if let Some(path) = Parser::submod_path_from_attr(attrs, &self.directory.path) {
if self.parse_sess.is_file_parsed(&path) { if self.psess.is_file_parsed(&path) {
return Ok(None); return Ok(None);
} }
return match Parser::parse_file_as_module(self.parse_sess, &path, sub_mod.span) { return match Parser::parse_file_as_module(self.psess, &path, sub_mod.span) {
Ok((ref attrs, _, _)) if contains_skip(attrs) => Ok(None), Ok((ref attrs, _, _)) if contains_skip(attrs) => Ok(None),
Ok((attrs, items, span)) => Ok(Some(SubModKind::External( Ok((attrs, items, span)) => Ok(Some(SubModKind::External(
path, path,
@ -368,7 +368,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
let mut mods_outside_ast = self.find_mods_outside_of_ast(attrs, sub_mod); let mut mods_outside_ast = self.find_mods_outside_of_ast(attrs, sub_mod);
match self match self
.parse_sess .psess
.default_submod_path(mod_name, relative, &self.directory.path) .default_submod_path(mod_name, relative, &self.directory.path)
{ {
Ok(ModulePathSuccess { Ok(ModulePathSuccess {
@ -380,7 +380,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
let should_insert = !mods_outside_ast let should_insert = !mods_outside_ast
.iter() .iter()
.any(|(outside_path, _, _)| outside_path == &file_path); .any(|(outside_path, _, _)| outside_path == &file_path);
if self.parse_sess.is_file_parsed(&file_path) { if self.psess.is_file_parsed(&file_path) {
if outside_mods_empty { if outside_mods_empty {
return Ok(None); return Ok(None);
} else { } else {
@ -390,7 +390,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
return Ok(Some(SubModKind::MultiExternal(mods_outside_ast))); return Ok(Some(SubModKind::MultiExternal(mods_outside_ast)));
} }
} }
match Parser::parse_file_as_module(self.parse_sess, &file_path, sub_mod.span) { match Parser::parse_file_as_module(self.psess, &file_path, sub_mod.span) {
Ok((ref attrs, _, _)) if contains_skip(attrs) => Ok(None), Ok((ref attrs, _, _)) if contains_skip(attrs) => Ok(None),
Ok((attrs, items, span)) if outside_mods_empty => { Ok((attrs, items, span)) if outside_mods_empty => {
Ok(Some(SubModKind::External( Ok(Some(SubModKind::External(
@ -517,7 +517,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
if !actual_path.exists() { if !actual_path.exists() {
continue; continue;
} }
if self.parse_sess.is_file_parsed(&actual_path) { if self.psess.is_file_parsed(&actual_path) {
// If the specified file is already parsed, then we just use that. // If the specified file is already parsed, then we just use that.
result.push(( result.push((
actual_path, actual_path,
@ -527,7 +527,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
continue; continue;
} }
let (attrs, items, span) = let (attrs, items, span) =
match Parser::parse_file_as_module(self.parse_sess, &actual_path, sub_mod.span) { match Parser::parse_file_as_module(self.psess, &actual_path, sub_mod.span) {
Ok((ref attrs, _, _)) if contains_skip(attrs) => continue, Ok((ref attrs, _, _)) if contains_skip(attrs) => continue,
Ok(m) => m, Ok(m) => m,
Err(..) => continue, Err(..) => continue,

View File

@ -12,15 +12,15 @@ pub(crate) struct ModItem {
/// Traverse `cfg_if!` macro and fetch modules. /// Traverse `cfg_if!` macro and fetch modules.
pub(crate) struct CfgIfVisitor<'a> { pub(crate) struct CfgIfVisitor<'a> {
parse_sess: &'a ParseSess, psess: &'a ParseSess,
mods: Vec<ModItem>, mods: Vec<ModItem>,
} }
impl<'a> CfgIfVisitor<'a> { impl<'a> CfgIfVisitor<'a> {
pub(crate) fn new(parse_sess: &'a ParseSess) -> CfgIfVisitor<'a> { pub(crate) fn new(psess: &'a ParseSess) -> CfgIfVisitor<'a> {
CfgIfVisitor { CfgIfVisitor {
mods: vec![], mods: vec![],
parse_sess, psess,
} }
} }
@ -62,7 +62,7 @@ impl<'a, 'ast: 'a> CfgIfVisitor<'a> {
} }
}; };
let items = parse_cfg_if(self.parse_sess, mac)?; let items = parse_cfg_if(self.psess, mac)?;
self.mods self.mods
.append(&mut items.into_iter().map(|item| ModItem { item }).collect()); .append(&mut items.into_iter().map(|item| ModItem { item }).collect());

View File

@ -7,5 +7,5 @@ use crate::rewrite::RewriteContext;
pub(crate) fn parse_asm(context: &RewriteContext<'_>, mac: &ast::MacCall) -> Option<AsmArgs> { pub(crate) fn parse_asm(context: &RewriteContext<'_>, mac: &ast::MacCall) -> Option<AsmArgs> {
let ts = mac.args.tokens.clone(); let ts = mac.args.tokens.clone();
let mut parser = super::build_parser(context, ts); let mut parser = super::build_parser(context, ts);
parse_asm_args(&mut parser, context.parse_sess.inner(), mac.span(), false).ok() parse_asm_args(&mut parser, mac.span(), false).ok()
} }

View File

@ -9,10 +9,10 @@ use crate::parse::macros::build_stream_parser;
use crate::parse::session::ParseSess; use crate::parse::session::ParseSess;
pub(crate) fn parse_cfg_if<'a>( pub(crate) fn parse_cfg_if<'a>(
sess: &'a ParseSess, psess: &'a ParseSess,
mac: &'a ast::MacCall, mac: &'a ast::MacCall,
) -> Result<Vec<ast::Item>, &'static str> { ) -> Result<Vec<ast::Item>, &'static str> {
match catch_unwind(AssertUnwindSafe(|| parse_cfg_if_inner(sess, mac))) { match catch_unwind(AssertUnwindSafe(|| parse_cfg_if_inner(psess, mac))) {
Ok(Ok(items)) => Ok(items), Ok(Ok(items)) => Ok(items),
Ok(err @ Err(_)) => err, Ok(err @ Err(_)) => err,
Err(..) => Err("failed to parse cfg_if!"), Err(..) => Err("failed to parse cfg_if!"),
@ -20,11 +20,11 @@ pub(crate) fn parse_cfg_if<'a>(
} }
fn parse_cfg_if_inner<'a>( fn parse_cfg_if_inner<'a>(
sess: &'a ParseSess, psess: &'a ParseSess,
mac: &'a ast::MacCall, mac: &'a ast::MacCall,
) -> Result<Vec<ast::Item>, &'static str> { ) -> Result<Vec<ast::Item>, &'static str> {
let ts = mac.args.tokens.clone(); let ts = mac.args.tokens.clone();
let mut parser = build_stream_parser(sess.inner(), ts); let mut parser = build_stream_parser(psess.inner(), ts);
let mut items = vec![]; let mut items = vec![];
let mut process_if_cfg = true; let mut process_if_cfg = true;
@ -67,7 +67,7 @@ fn parse_cfg_if_inner<'a>(
Ok(None) => continue, Ok(None) => continue,
Err(err) => { Err(err) => {
err.cancel(); err.cancel();
parser.sess.dcx.reset_err_count(); parser.psess.dcx.reset_err_count();
return Err( return Err(
"Expected item inside cfg_if block, but failed to parse it as an item", "Expected item inside cfg_if block, but failed to parse it as an item",
); );

View File

@ -16,8 +16,8 @@ pub(crate) fn parse_lazy_static(
($method:ident $(,)* $($arg:expr),* $(,)*) => { ($method:ident $(,)* $($arg:expr),* $(,)*) => {
match parser.$method($($arg,)*) { match parser.$method($($arg,)*) {
Ok(val) => { Ok(val) => {
if parser.sess.dcx.has_errors().is_some() { if parser.psess.dcx.has_errors().is_some() {
parser.sess.dcx.reset_err_count(); parser.psess.dcx.reset_err_count();
return None; return None;
} else { } else {
val val
@ -25,7 +25,7 @@ pub(crate) fn parse_lazy_static(
} }
Err(err) => { Err(err) => {
err.cancel(); err.cancel();
parser.sess.dcx.reset_err_count(); parser.psess.dcx.reset_err_count();
return None; return None;
} }
} }

View File

@ -1,8 +1,8 @@
use rustc_ast::token::{Delimiter, TokenKind}; use rustc_ast::token::{Delimiter, NonterminalKind, TokenKind};
use rustc_ast::tokenstream::TokenStream; use rustc_ast::tokenstream::TokenStream;
use rustc_ast::{ast, ptr}; use rustc_ast::{ast, ptr};
use rustc_parse::parser::{ForceCollect, Parser}; use rustc_parse::parser::{ForceCollect, Parser, Recovery};
use rustc_parse::{stream_to_parser, MACRO_ARGUMENTS}; use rustc_parse::MACRO_ARGUMENTS;
use rustc_session::parse::ParseSess; use rustc_session::parse::ParseSess;
use rustc_span::symbol::{self, kw}; use rustc_span::symbol::{self, kw};
use rustc_span::Symbol; use rustc_span::Symbol;
@ -14,31 +14,33 @@ pub(crate) mod asm;
pub(crate) mod cfg_if; pub(crate) mod cfg_if;
pub(crate) mod lazy_static; pub(crate) mod lazy_static;
fn build_stream_parser<'a>(sess: &'a ParseSess, tokens: TokenStream) -> Parser<'a> { fn build_stream_parser<'a>(psess: &'a ParseSess, tokens: TokenStream) -> Parser<'a> {
stream_to_parser(sess, tokens, MACRO_ARGUMENTS) Parser::new(psess, tokens, MACRO_ARGUMENTS).recovery(Recovery::Forbidden)
} }
fn build_parser<'a>(context: &RewriteContext<'a>, tokens: TokenStream) -> Parser<'a> { fn build_parser<'a>(context: &RewriteContext<'a>, tokens: TokenStream) -> Parser<'a> {
build_stream_parser(context.parse_sess.inner(), tokens) build_stream_parser(context.psess.inner(), tokens)
} }
fn parse_macro_arg<'a, 'b: 'a>(parser: &'a mut Parser<'b>) -> Option<MacroArg> { fn parse_macro_arg<'a, 'b: 'a>(parser: &'a mut Parser<'b>) -> Option<MacroArg> {
macro_rules! parse_macro_arg { macro_rules! parse_macro_arg {
($macro_arg:ident, $parser:expr, $f:expr) => { ($macro_arg:ident, $nt_kind:expr, $try_parse:expr, $then:expr) => {
let mut cloned_parser = (*parser).clone(); let mut cloned_parser = (*parser).clone();
match $parser(&mut cloned_parser) { if Parser::nonterminal_may_begin_with($nt_kind, &cloned_parser.token) {
Ok(x) => { match $try_parse(&mut cloned_parser) {
if parser.sess.dcx.has_errors().is_some() { Ok(x) => {
parser.sess.dcx.reset_err_count(); if parser.psess.dcx.has_errors().is_some() {
} else { parser.psess.dcx.reset_err_count();
// Parsing succeeded. } else {
*parser = cloned_parser; // Parsing succeeded.
return Some(MacroArg::$macro_arg($f(x)?)); *parser = cloned_parser;
return Some(MacroArg::$macro_arg($then(x)?));
}
}
Err(e) => {
e.cancel();
parser.psess.dcx.reset_err_count();
} }
}
Err(e) => {
e.cancel();
parser.sess.dcx.reset_err_count();
} }
} }
}; };
@ -46,23 +48,27 @@ fn parse_macro_arg<'a, 'b: 'a>(parser: &'a mut Parser<'b>) -> Option<MacroArg> {
parse_macro_arg!( parse_macro_arg!(
Expr, Expr,
|parser: &mut rustc_parse::parser::Parser<'b>| parser.parse_expr(), NonterminalKind::Expr,
|parser: &mut Parser<'b>| parser.parse_expr(),
|x: ptr::P<ast::Expr>| Some(x) |x: ptr::P<ast::Expr>| Some(x)
); );
parse_macro_arg!( parse_macro_arg!(
Ty, Ty,
|parser: &mut rustc_parse::parser::Parser<'b>| parser.parse_ty(), NonterminalKind::Ty,
|parser: &mut Parser<'b>| parser.parse_ty(),
|x: ptr::P<ast::Ty>| Some(x) |x: ptr::P<ast::Ty>| Some(x)
); );
parse_macro_arg!( parse_macro_arg!(
Pat, Pat,
|parser: &mut rustc_parse::parser::Parser<'b>| parser.parse_pat_no_top_alt(None, None), NonterminalKind::PatParam { inferred: false },
|parser: &mut Parser<'b>| parser.parse_pat_no_top_alt(None, None),
|x: ptr::P<ast::Pat>| Some(x) |x: ptr::P<ast::Pat>| Some(x)
); );
// `parse_item` returns `Option<ptr::P<ast::Item>>`. // `parse_item` returns `Option<ptr::P<ast::Item>>`.
parse_macro_arg!( parse_macro_arg!(
Item, Item,
|parser: &mut rustc_parse::parser::Parser<'b>| parser.parse_item(ForceCollect::No), NonterminalKind::Item,
|parser: &mut Parser<'b>| parser.parse_item(ForceCollect::No),
|x: Option<ptr::P<ast::Item>>| x |x: Option<ptr::P<ast::Item>>| x
); );

View File

@ -3,8 +3,9 @@ use std::path::{Path, PathBuf};
use rustc_ast::token::TokenKind; use rustc_ast::token::TokenKind;
use rustc_ast::{ast, attr, ptr}; use rustc_ast::{ast, attr, ptr};
use rustc_errors::Diagnostic; use rustc_errors::Diag;
use rustc_parse::{new_parser_from_file, parser::Parser as RawParser}; use rustc_parse::parser::Parser as RawParser;
use rustc_parse::{new_parser_from_file, new_parser_from_source_str, unwrap_or_emit_fatal};
use rustc_span::{sym, Span}; use rustc_span::{sym, Span};
use thin_vec::ThinVec; use thin_vec::ThinVec;
@ -29,7 +30,7 @@ pub(crate) struct Parser<'a> {
/// A builder for the `Parser`. /// A builder for the `Parser`.
#[derive(Default)] #[derive(Default)]
pub(crate) struct ParserBuilder<'a> { pub(crate) struct ParserBuilder<'a> {
sess: Option<&'a ParseSess>, psess: Option<&'a ParseSess>,
input: Option<Input>, input: Option<Input>,
} }
@ -39,23 +40,20 @@ impl<'a> ParserBuilder<'a> {
self self
} }
pub(crate) fn sess(mut self, sess: &'a ParseSess) -> ParserBuilder<'a> { pub(crate) fn psess(mut self, psess: &'a ParseSess) -> ParserBuilder<'a> {
self.sess = Some(sess); self.psess = Some(psess);
self self
} }
pub(crate) fn build(self) -> Result<Parser<'a>, ParserError> { pub(crate) fn build(self) -> Result<Parser<'a>, ParserError> {
let sess = self.sess.ok_or(ParserError::NoParseSess)?; let psess = self.psess.ok_or(ParserError::NoParseSess)?;
let input = self.input.ok_or(ParserError::NoInput)?; let input = self.input.ok_or(ParserError::NoInput)?;
let parser = match Self::parser(sess.inner(), input) { let parser = match Self::parser(psess.inner(), input) {
Ok(p) => p, Ok(p) => p,
Err(db) => { Err(diagnostics) => {
if let Some(diagnostics) = db { psess.emit_diagnostics(diagnostics);
sess.emit_diagnostics(diagnostics); return Err(ParserError::ParserCreationError);
return Err(ParserError::ParserCreationError);
}
return Err(ParserError::ParsePanicError);
} }
}; };
@ -63,20 +61,16 @@ impl<'a> ParserBuilder<'a> {
} }
fn parser( fn parser(
sess: &'a rustc_session::parse::ParseSess, psess: &'a rustc_session::parse::ParseSess,
input: Input, input: Input,
) -> Result<rustc_parse::parser::Parser<'a>, Option<Vec<Diagnostic>>> { ) -> Result<RawParser<'a>, Vec<Diag<'a>>> {
match input { match input {
Input::File(ref file) => catch_unwind(AssertUnwindSafe(move || { Input::File(ref file) => new_parser_from_file(psess, file, None),
new_parser_from_file(sess, file, None) Input::Text(text) => new_parser_from_source_str(
})) psess,
.map_err(|_| None),
Input::Text(text) => rustc_parse::maybe_new_parser_from_source_str(
sess,
rustc_span::FileName::Custom("stdin".to_owned()), rustc_span::FileName::Custom("stdin".to_owned()),
text, text,
) ),
.map_err(Some),
} }
} }
} }
@ -106,27 +100,28 @@ impl<'a> Parser<'a> {
} }
pub(crate) fn parse_file_as_module( pub(crate) fn parse_file_as_module(
sess: &'a ParseSess, psess: &'a ParseSess,
path: &Path, path: &Path,
span: Span, span: Span,
) -> Result<(ast::AttrVec, ThinVec<ptr::P<ast::Item>>, Span), ParserError> { ) -> Result<(ast::AttrVec, ThinVec<ptr::P<ast::Item>>, Span), ParserError> {
let result = catch_unwind(AssertUnwindSafe(|| { let result = catch_unwind(AssertUnwindSafe(|| {
let mut parser = new_parser_from_file(sess.inner(), path, Some(span)); let mut parser =
unwrap_or_emit_fatal(new_parser_from_file(psess.inner(), path, Some(span)));
match parser.parse_mod(&TokenKind::Eof) { match parser.parse_mod(&TokenKind::Eof) {
Ok((a, i, spans)) => Some((a, i, spans.inner_span)), Ok((a, i, spans)) => Some((a, i, spans.inner_span)),
Err(mut e) => { Err(e) => {
e.emit(); e.emit();
if sess.can_reset_errors() { if psess.can_reset_errors() {
sess.reset_errors(); psess.reset_errors();
} }
None None
} }
} }
})); }));
match result { match result {
Ok(Some(m)) if !sess.has_errors() => Ok(m), Ok(Some(m)) if !psess.has_errors() => Ok(m),
Ok(Some(m)) if sess.can_reset_errors() => { Ok(Some(m)) if psess.can_reset_errors() => {
sess.reset_errors(); psess.reset_errors();
Ok(m) Ok(m)
} }
Ok(_) => Err(ParserError::ParseError), Ok(_) => Err(ParserError::ParseError),
@ -137,39 +132,39 @@ impl<'a> Parser<'a> {
pub(crate) fn parse_crate( pub(crate) fn parse_crate(
input: Input, input: Input,
sess: &'a ParseSess, psess: &'a ParseSess,
) -> Result<ast::Crate, ParserError> { ) -> Result<ast::Crate, ParserError> {
let krate = Parser::parse_crate_inner(input, sess)?; let krate = Parser::parse_crate_inner(input, psess)?;
if !sess.has_errors() { if !psess.has_errors() {
return Ok(krate); return Ok(krate);
} }
if sess.can_reset_errors() { if psess.can_reset_errors() {
sess.reset_errors(); psess.reset_errors();
return Ok(krate); return Ok(krate);
} }
Err(ParserError::ParseError) Err(ParserError::ParseError)
} }
fn parse_crate_inner(input: Input, sess: &'a ParseSess) -> Result<ast::Crate, ParserError> { fn parse_crate_inner(input: Input, psess: &'a ParseSess) -> Result<ast::Crate, ParserError> {
ParserBuilder::default() ParserBuilder::default()
.input(input) .input(input)
.sess(sess) .psess(psess)
.build()? .build()?
.parse_crate_mod() .parse_crate_mod()
} }
fn parse_crate_mod(&mut self) -> Result<ast::Crate, ParserError> { fn parse_crate_mod(&mut self) -> Result<ast::Crate, ParserError> {
let mut parser = AssertUnwindSafe(&mut self.parser); let mut parser = AssertUnwindSafe(&mut self.parser);
let err = Err(ParserError::ParsePanicError);
match catch_unwind(move || parser.parse_crate_mod()) { match catch_unwind(move || parser.parse_crate_mod()) {
Ok(Ok(k)) => Ok(k), Ok(Ok(k)) => Ok(k),
Ok(Err(mut db)) => { Ok(Err(db)) => {
db.emit(); db.emit();
Err(ParserError::ParseError) err
} }
Err(_) => Err(ParserError::ParsePanicError), Err(_) => err,
} }
} }
} }

View File

@ -2,9 +2,9 @@ use std::path::Path;
use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::atomic::{AtomicBool, Ordering};
use rustc_data_structures::sync::{IntoDynSyncSend, Lrc}; use rustc_data_structures::sync::{IntoDynSyncSend, Lrc};
use rustc_errors::emitter::{DynEmitter, Emitter, EmitterWriter}; use rustc_errors::emitter::{stderr_destination, DynEmitter, Emitter, HumanEmitter, SilentEmitter};
use rustc_errors::translation::Translate; use rustc_errors::translation::Translate;
use rustc_errors::{ColorConfig, DiagCtxt, Diagnostic, Level as DiagnosticLevel}; use rustc_errors::{ColorConfig, Diag, DiagCtxt, DiagInner, Level as DiagnosticLevel};
use rustc_session::parse::ParseSess as RawParseSess; use rustc_session::parse::ParseSess as RawParseSess;
use rustc_span::{ use rustc_span::{
source_map::{FilePathMapping, SourceMap}, source_map::{FilePathMapping, SourceMap},
@ -22,36 +22,11 @@ use crate::{Config, ErrorKind, FileName};
/// ParseSess holds structs necessary for constructing a parser. /// ParseSess holds structs necessary for constructing a parser.
pub(crate) struct ParseSess { pub(crate) struct ParseSess {
parse_sess: RawParseSess, raw_psess: RawParseSess,
ignore_path_set: Lrc<IgnorePathSet>, ignore_path_set: Lrc<IgnorePathSet>,
can_reset_errors: Lrc<AtomicBool>, can_reset_errors: Lrc<AtomicBool>,
} }
/// Emitter which discards every error.
struct SilentEmitter;
impl Translate for SilentEmitter {
fn fluent_bundle(&self) -> Option<&Lrc<rustc_errors::FluentBundle>> {
None
}
fn fallback_fluent_bundle(&self) -> &rustc_errors::FluentBundle {
panic!("silent emitter attempted to translate a diagnostic");
}
}
impl Emitter for SilentEmitter {
fn source_map(&self) -> Option<&Lrc<SourceMap>> {
None
}
fn emit_diagnostic(&mut self, _db: &Diagnostic) {}
}
fn silent_emitter() -> Box<DynEmitter> {
Box::new(SilentEmitter {})
}
/// Emit errors against every files expect ones specified in the `ignore_path_set`. /// Emit errors against every files expect ones specified in the `ignore_path_set`.
struct SilentOnIgnoredFilesEmitter { struct SilentOnIgnoredFilesEmitter {
ignore_path_set: IntoDynSyncSend<Lrc<IgnorePathSet>>, ignore_path_set: IntoDynSyncSend<Lrc<IgnorePathSet>>,
@ -62,10 +37,10 @@ struct SilentOnIgnoredFilesEmitter {
} }
impl SilentOnIgnoredFilesEmitter { impl SilentOnIgnoredFilesEmitter {
fn handle_non_ignorable_error(&mut self, db: &Diagnostic) { fn handle_non_ignoreable_error(&mut self, diag: DiagInner) {
self.has_non_ignorable_parser_errors = true; self.has_non_ignorable_parser_errors = true;
self.can_reset.store(false, Ordering::Release); self.can_reset.store(false, Ordering::Release);
self.emitter.emit_diagnostic(db); self.emitter.emit_diagnostic(diag);
} }
} }
@ -84,11 +59,11 @@ impl Emitter for SilentOnIgnoredFilesEmitter {
None None
} }
fn emit_diagnostic(&mut self, db: &Diagnostic) { fn emit_diagnostic(&mut self, diag: DiagInner) {
if db.level() == DiagnosticLevel::Fatal { if diag.level() == DiagnosticLevel::Fatal {
return self.handle_non_ignorable_error(db); return self.handle_non_ignoreable_error(diag);
} }
if let Some(primary_span) = &db.span.primary_span() { if let Some(primary_span) = &diag.span.primary_span() {
let file_name = self.source_map.span_to_filename(*primary_span); let file_name = self.source_map.span_to_filename(*primary_span);
if let rustc_span::FileName::Real(rustc_span::RealFileName::LocalPath(ref path)) = if let rustc_span::FileName::Real(rustc_span::RealFileName::LocalPath(ref path)) =
file_name file_name
@ -104,7 +79,7 @@ impl Emitter for SilentOnIgnoredFilesEmitter {
} }
}; };
} }
self.handle_non_ignorable_error(db); self.handle_non_ignoreable_error(diag);
} }
} }
@ -132,16 +107,26 @@ fn default_dcx(
ColorConfig::Never ColorConfig::Never
}; };
let emitter = if !show_parse_errors { let fallback_bundle = rustc_errors::fallback_fluent_bundle(
silent_emitter() rustc_driver::DEFAULT_LOCALE_RESOURCES.to_vec(),
false,
);
let emitter = Box::new(
HumanEmitter::new(stderr_destination(emit_color), fallback_bundle.clone())
.sm(Some(source_map.clone())),
);
let emitter: Box<DynEmitter> = if !show_parse_errors {
Box::new(SilentEmitter {
fallback_bundle,
fatal_dcx: DiagCtxt::new(emitter),
fatal_note: None,
emit_fatal_diagnostic: false,
})
} else { } else {
let fallback_bundle = rustc_errors::fallback_fluent_bundle( emitter
rustc_driver::DEFAULT_LOCALE_RESOURCES.to_vec(),
false,
);
Box::new(EmitterWriter::stderr(emit_color, fallback_bundle).sm(Some(source_map.clone())))
}; };
DiagCtxt::with_emitter(Box::new(SilentOnIgnoredFilesEmitter { DiagCtxt::new(Box::new(SilentOnIgnoredFilesEmitter {
has_non_ignorable_parser_errors: false, has_non_ignorable_parser_errors: false,
source_map, source_map,
emitter, emitter,
@ -166,10 +151,10 @@ impl ParseSess {
config.show_parse_errors(), config.show_parse_errors(),
config.color(), config.color(),
); );
let parse_sess = RawParseSess::with_dcx(dcx, source_map); let raw_psess = RawParseSess::with_dcx(dcx, source_map);
Ok(ParseSess { Ok(ParseSess {
parse_sess, raw_psess,
ignore_path_set, ignore_path_set,
can_reset_errors, can_reset_errors,
}) })
@ -188,14 +173,14 @@ impl ParseSess {
relative: Option<symbol::Ident>, relative: Option<symbol::Ident>,
dir_path: &Path, dir_path: &Path,
) -> Result<ModulePathSuccess, ModError<'_>> { ) -> Result<ModulePathSuccess, ModError<'_>> {
rustc_expand::module::default_submod_path(&self.parse_sess, id, relative, dir_path).or_else( rustc_expand::module::default_submod_path(&self.raw_psess, id, relative, dir_path).or_else(
|e| { |e| {
// If resolving a module relative to {dir_path}/{symbol} fails because a file // If resolving a module relative to {dir_path}/{symbol} fails because a file
// could not be found, then try to resolve the module relative to {dir_path}. // could not be found, then try to resolve the module relative to {dir_path}.
// If we still can't find the module after searching for it in {dir_path}, // If we still can't find the module after searching for it in {dir_path},
// surface the original error. // surface the original error.
if matches!(e, ModError::FileNotFound(..)) && relative.is_some() { if matches!(e, ModError::FileNotFound(..)) && relative.is_some() {
rustc_expand::module::default_submod_path(&self.parse_sess, id, None, dir_path) rustc_expand::module::default_submod_path(&self.raw_psess, id, None, dir_path)
.map_err(|_| e) .map_err(|_| e)
} else { } else {
Err(e) Err(e)
@ -205,7 +190,7 @@ impl ParseSess {
} }
pub(crate) fn is_file_parsed(&self, path: &Path) -> bool { pub(crate) fn is_file_parsed(&self, path: &Path) -> bool {
self.parse_sess self.raw_psess
.source_map() .source_map()
.get_source_file(&rustc_span::FileName::Real( .get_source_file(&rustc_span::FileName::Real(
rustc_span::RealFileName::LocalPath(path.to_path_buf()), rustc_span::RealFileName::LocalPath(path.to_path_buf()),
@ -218,21 +203,28 @@ impl ParseSess {
} }
pub(crate) fn set_silent_emitter(&mut self) { pub(crate) fn set_silent_emitter(&mut self) {
self.parse_sess.dcx = DiagCtxt::with_emitter(silent_emitter()); // Ideally this invocation wouldn't be necessary and the fallback bundle in
// `self.parse_sess.dcx` could be used, but the lock in `DiagCtxt` prevents this.
// See `<rustc_errors::SilentEmitter as Translate>::fallback_fluent_bundle`.
let fallback_bundle = rustc_errors::fallback_fluent_bundle(
rustc_driver::DEFAULT_LOCALE_RESOURCES.to_vec(),
false,
);
self.raw_psess.dcx.make_silent(fallback_bundle, None, false);
} }
pub(crate) fn span_to_filename(&self, span: Span) -> FileName { pub(crate) fn span_to_filename(&self, span: Span) -> FileName {
self.parse_sess.source_map().span_to_filename(span).into() self.raw_psess.source_map().span_to_filename(span).into()
} }
pub(crate) fn span_to_file_contents(&self, span: Span) -> Lrc<rustc_span::SourceFile> { pub(crate) fn span_to_file_contents(&self, span: Span) -> Lrc<rustc_span::SourceFile> {
self.parse_sess self.raw_psess
.source_map() .source_map()
.lookup_source_file(span.data().lo) .lookup_source_file(span.data().lo)
} }
pub(crate) fn span_to_first_line_string(&self, span: Span) -> String { pub(crate) fn span_to_first_line_string(&self, span: Span) -> String {
let file_lines = self.parse_sess.source_map().span_to_lines(span).ok(); let file_lines = self.raw_psess.source_map().span_to_lines(span).ok();
match file_lines { match file_lines {
Some(fl) => fl Some(fl) => fl
@ -244,7 +236,7 @@ impl ParseSess {
} }
pub(crate) fn line_of_byte_pos(&self, pos: BytePos) -> usize { pub(crate) fn line_of_byte_pos(&self, pos: BytePos) -> usize {
self.parse_sess.source_map().lookup_char_pos(pos).line self.raw_psess.source_map().lookup_char_pos(pos).line
} }
// TODO(calebcartwright): Preemptive, currently unused addition // TODO(calebcartwright): Preemptive, currently unused addition
@ -257,15 +249,15 @@ impl ParseSess {
} }
pub(crate) fn span_to_debug_info(&self, span: Span) -> String { pub(crate) fn span_to_debug_info(&self, span: Span) -> String {
self.parse_sess.source_map().span_to_diagnostic_string(span) self.raw_psess.source_map().span_to_diagnostic_string(span)
} }
pub(crate) fn inner(&self) -> &RawParseSess { pub(crate) fn inner(&self) -> &RawParseSess {
&self.parse_sess &self.raw_psess
} }
pub(crate) fn snippet_provider(&self, span: Span) -> SnippetProvider { pub(crate) fn snippet_provider(&self, span: Span) -> SnippetProvider {
let source_file = self.parse_sess.source_map().lookup_char_pos(span.lo()).file; let source_file = self.raw_psess.source_map().lookup_char_pos(span.lo()).file;
SnippetProvider::new( SnippetProvider::new(
source_file.start_pos, source_file.start_pos,
source_file.end_position(), source_file.end_position(),
@ -274,7 +266,7 @@ impl ParseSess {
} }
pub(crate) fn get_original_snippet(&self, file_name: &FileName) -> Option<Lrc<String>> { pub(crate) fn get_original_snippet(&self, file_name: &FileName) -> Option<Lrc<String>> {
self.parse_sess self.raw_psess
.source_map() .source_map()
.get_source_file(&file_name.into()) .get_source_file(&file_name.into())
.and_then(|source_file| source_file.src.clone()) .and_then(|source_file| source_file.src.clone())
@ -283,9 +275,9 @@ impl ParseSess {
// Methods that should be restricted within the parse module. // Methods that should be restricted within the parse module.
impl ParseSess { impl ParseSess {
pub(super) fn emit_diagnostics(&self, diagnostics: Vec<Diagnostic>) { pub(super) fn emit_diagnostics(&self, diagnostics: Vec<Diag<'_>>) {
for diagnostic in diagnostics { for diagnostic in diagnostics {
self.parse_sess.dcx.emit_diagnostic(diagnostic); diagnostic.emit();
} }
} }
@ -294,23 +286,23 @@ impl ParseSess {
} }
pub(super) fn has_errors(&self) -> bool { pub(super) fn has_errors(&self) -> bool {
self.parse_sess.dcx.has_errors().is_some() self.raw_psess.dcx.has_errors().is_some()
} }
pub(super) fn reset_errors(&self) { pub(super) fn reset_errors(&self) {
self.parse_sess.dcx.reset_err_count(); self.raw_psess.dcx.reset_err_count();
} }
} }
impl LineRangeUtils for ParseSess { impl LineRangeUtils for ParseSess {
fn lookup_line_range(&self, span: Span) -> LineRange { fn lookup_line_range(&self, span: Span) -> LineRange {
let snippet = self let snippet = self
.parse_sess .raw_psess
.source_map() .source_map()
.span_to_snippet(span) .span_to_snippet(span)
.unwrap_or_default(); .unwrap_or_default();
let lo = self.parse_sess.source_map().lookup_line(span.lo()).unwrap(); let lo = self.raw_psess.source_map().lookup_line(span.lo()).unwrap();
let hi = self.parse_sess.source_map().lookup_line(span.hi()).unwrap(); let hi = self.raw_psess.source_map().lookup_line(span.hi()).unwrap();
debug_assert_eq!( debug_assert_eq!(
lo.sf.name, hi.sf.name, lo.sf.name, hi.sf.name,
@ -363,13 +355,14 @@ mod tests {
None None
} }
fn emit_diagnostic(&mut self, _db: &Diagnostic) { fn emit_diagnostic(&mut self, _diag: DiagInner) {
self.num_emitted_errors.fetch_add(1, Ordering::Release); self.num_emitted_errors.fetch_add(1, Ordering::Release);
} }
} }
fn build_diagnostic(level: DiagnosticLevel, span: Option<MultiSpan>) -> Diagnostic { fn build_diagnostic(level: DiagnosticLevel, span: Option<MultiSpan>) -> DiagInner {
let mut diag = Diagnostic::new(level, ""); #[allow(rustc::untranslatable_diagnostic)] // no translation needed for empty string
let mut diag = DiagInner::new(level, "");
diag.messages.clear(); diag.messages.clear();
if let Some(span) = span { if let Some(span) = span {
diag.span = span; diag.span = span;
@ -422,7 +415,7 @@ mod tests {
); );
let span = MultiSpan::from_span(mk_sp(BytePos(0), BytePos(1))); let span = MultiSpan::from_span(mk_sp(BytePos(0), BytePos(1)));
let fatal_diagnostic = build_diagnostic(DiagnosticLevel::Fatal, Some(span)); let fatal_diagnostic = build_diagnostic(DiagnosticLevel::Fatal, Some(span));
emitter.emit_diagnostic(&fatal_diagnostic); emitter.emit_diagnostic(fatal_diagnostic);
assert_eq!(num_emitted_errors.load(Ordering::Acquire), 1); assert_eq!(num_emitted_errors.load(Ordering::Acquire), 1);
assert_eq!(can_reset_errors.load(Ordering::Acquire), false); assert_eq!(can_reset_errors.load(Ordering::Acquire), false);
} }
@ -446,8 +439,8 @@ mod tests {
Some(ignore_list), Some(ignore_list),
); );
let span = MultiSpan::from_span(mk_sp(BytePos(0), BytePos(1))); let span = MultiSpan::from_span(mk_sp(BytePos(0), BytePos(1)));
let non_fatal_diagnostic = build_diagnostic(DiagnosticLevel::Warning(None), Some(span)); let non_fatal_diagnostic = build_diagnostic(DiagnosticLevel::Warning, Some(span));
emitter.emit_diagnostic(&non_fatal_diagnostic); emitter.emit_diagnostic(non_fatal_diagnostic);
assert_eq!(num_emitted_errors.load(Ordering::Acquire), 0); assert_eq!(num_emitted_errors.load(Ordering::Acquire), 0);
assert_eq!(can_reset_errors.load(Ordering::Acquire), true); assert_eq!(can_reset_errors.load(Ordering::Acquire), true);
} }
@ -470,8 +463,8 @@ mod tests {
None, None,
); );
let span = MultiSpan::from_span(mk_sp(BytePos(0), BytePos(1))); let span = MultiSpan::from_span(mk_sp(BytePos(0), BytePos(1)));
let non_fatal_diagnostic = build_diagnostic(DiagnosticLevel::Warning(None), Some(span)); let non_fatal_diagnostic = build_diagnostic(DiagnosticLevel::Warning, Some(span));
emitter.emit_diagnostic(&non_fatal_diagnostic); emitter.emit_diagnostic(non_fatal_diagnostic);
assert_eq!(num_emitted_errors.load(Ordering::Acquire), 1); assert_eq!(num_emitted_errors.load(Ordering::Acquire), 1);
assert_eq!(can_reset_errors.load(Ordering::Acquire), false); assert_eq!(can_reset_errors.load(Ordering::Acquire), false);
} }
@ -507,12 +500,12 @@ mod tests {
); );
let bar_span = MultiSpan::from_span(mk_sp(BytePos(0), BytePos(1))); let bar_span = MultiSpan::from_span(mk_sp(BytePos(0), BytePos(1)));
let foo_span = MultiSpan::from_span(mk_sp(BytePos(21), BytePos(22))); let foo_span = MultiSpan::from_span(mk_sp(BytePos(21), BytePos(22)));
let bar_diagnostic = build_diagnostic(DiagnosticLevel::Warning(None), Some(bar_span)); let bar_diagnostic = build_diagnostic(DiagnosticLevel::Warning, Some(bar_span));
let foo_diagnostic = build_diagnostic(DiagnosticLevel::Warning(None), Some(foo_span)); let foo_diagnostic = build_diagnostic(DiagnosticLevel::Warning, Some(foo_span));
let fatal_diagnostic = build_diagnostic(DiagnosticLevel::Fatal, None); let fatal_diagnostic = build_diagnostic(DiagnosticLevel::Fatal, None);
emitter.emit_diagnostic(&bar_diagnostic); emitter.emit_diagnostic(bar_diagnostic);
emitter.emit_diagnostic(&foo_diagnostic); emitter.emit_diagnostic(foo_diagnostic);
emitter.emit_diagnostic(&fatal_diagnostic); emitter.emit_diagnostic(fatal_diagnostic);
assert_eq!(num_emitted_errors.load(Ordering::Acquire), 2); assert_eq!(num_emitted_errors.load(Ordering::Acquire), 2);
assert_eq!(can_reset_errors.load(Ordering::Acquire), false); assert_eq!(can_reset_errors.load(Ordering::Acquire), false);
} }

View File

@ -1,6 +1,4 @@
use rustc_ast::ast::{ use rustc_ast::ast::{self, BindingMode, ByRef, Pat, PatField, PatKind, RangeEnd, RangeSyntax};
self, BindingAnnotation, ByRef, Pat, PatField, PatKind, RangeEnd, RangeSyntax,
};
use rustc_ast::ptr; use rustc_ast::ptr;
use rustc_span::{BytePos, Span}; use rustc_span::{BytePos, Span};
@ -40,9 +38,11 @@ pub(crate) fn is_short_pattern(pat: &ast::Pat, pat_str: &str) -> bool {
fn is_short_pattern_inner(pat: &ast::Pat) -> bool { fn is_short_pattern_inner(pat: &ast::Pat) -> bool {
match pat.kind { match pat.kind {
ast::PatKind::Rest | ast::PatKind::Never | ast::PatKind::Wild | ast::PatKind::Lit(_) => { ast::PatKind::Rest
true | ast::PatKind::Never
} | ast::PatKind::Wild
| ast::PatKind::Err(_)
| ast::PatKind::Lit(_) => true,
ast::PatKind::Ident(_, _, ref pat) => pat.is_none(), ast::PatKind::Ident(_, _, ref pat) => pat.is_none(),
ast::PatKind::Struct(..) ast::PatKind::Struct(..)
| ast::PatKind::MacCall(..) | ast::PatKind::MacCall(..)
@ -53,9 +53,10 @@ fn is_short_pattern_inner(pat: &ast::Pat) -> bool {
ast::PatKind::TupleStruct(_, ref path, ref subpats) => { ast::PatKind::TupleStruct(_, ref path, ref subpats) => {
path.segments.len() <= 1 && subpats.len() <= 1 path.segments.len() <= 1 && subpats.len() <= 1
} }
ast::PatKind::Box(ref p) | ast::PatKind::Ref(ref p, _) | ast::PatKind::Paren(ref p) => { ast::PatKind::Box(ref p)
is_short_pattern_inner(&*p) | PatKind::Deref(ref p)
} | ast::PatKind::Ref(ref p, _)
| ast::PatKind::Paren(ref p) => is_short_pattern_inner(&*p),
PatKind::Or(ref pats) => pats.iter().all(|p| is_short_pattern_inner(p)), PatKind::Or(ref pats) => pats.iter().all(|p| is_short_pattern_inner(p)),
} }
} }
@ -103,19 +104,20 @@ impl Rewrite for Pat {
write_list(&items, &fmt) write_list(&items, &fmt)
} }
PatKind::Box(ref pat) => rewrite_unary_prefix(context, "box ", &**pat, shape), PatKind::Box(ref pat) => rewrite_unary_prefix(context, "box ", &**pat, shape),
PatKind::Ident(BindingAnnotation(by_ref, mutability), ident, ref sub_pat) => { PatKind::Ident(BindingMode(by_ref, mutability), ident, ref sub_pat) => {
let prefix = match by_ref { let mut_prefix = format_mutability(mutability).trim();
ByRef::Yes => "ref",
ByRef::No => "", let (ref_kw, mut_infix) = match by_ref {
ByRef::Yes(rmutbl) => ("ref", format_mutability(rmutbl).trim()),
ByRef::No => ("", ""),
}; };
let mut_infix = format_mutability(mutability).trim();
let id_str = rewrite_ident(context, ident); let id_str = rewrite_ident(context, ident);
let sub_pat = match *sub_pat { let sub_pat = match *sub_pat {
Some(ref p) => { Some(ref p) => {
// 2 - `@ `. // 2 - `@ `.
let width = shape let width = shape.width.checked_sub(
.width mut_prefix.len() + ref_kw.len() + mut_infix.len() + id_str.len() + 2,
.checked_sub(prefix.len() + mut_infix.len() + id_str.len() + 2)?; )?;
let lo = context.snippet_provider.span_after(self.span, "@"); let lo = context.snippet_provider.span_after(self.span, "@");
combine_strs_with_missing_comments( combine_strs_with_missing_comments(
context, context,
@ -129,33 +131,55 @@ impl Rewrite for Pat {
None => "".to_owned(), None => "".to_owned(),
}; };
// combine prefix and mut // combine prefix and ref
let (first_lo, first) = if !prefix.is_empty() && !mut_infix.is_empty() { let (first_lo, first) = match (mut_prefix.is_empty(), ref_kw.is_empty()) {
let hi = context.snippet_provider.span_before(self.span, "mut"); (false, false) => {
let lo = context.snippet_provider.span_after(self.span, "ref"); let lo = context.snippet_provider.span_after(self.span, "mut");
( let hi = context.snippet_provider.span_before(self.span, "ref");
(
context.snippet_provider.span_after(self.span, "ref"),
combine_strs_with_missing_comments(
context,
mut_prefix,
ref_kw,
mk_sp(lo, hi),
shape,
true,
)?,
)
}
(false, true) => (
context.snippet_provider.span_after(self.span, "mut"), context.snippet_provider.span_after(self.span, "mut"),
combine_strs_with_missing_comments( mut_prefix.to_owned(),
context, ),
prefix, (true, false) => (
mut_infix,
mk_sp(lo, hi),
shape,
true,
)?,
)
} else if !prefix.is_empty() {
(
context.snippet_provider.span_after(self.span, "ref"), context.snippet_provider.span_after(self.span, "ref"),
prefix.to_owned(), ref_kw.to_owned(),
) ),
} else if !mut_infix.is_empty() { (true, true) => (self.span.lo(), "".to_owned()),
( };
context.snippet_provider.span_after(self.span, "mut"),
mut_infix.to_owned(), // combine result of above and mut
) let (second_lo, second) = match (first.is_empty(), mut_infix.is_empty()) {
} else { (false, false) => {
(self.span.lo(), "".to_owned()) let lo = context.snippet_provider.span_after(self.span, "ref");
let end_span = mk_sp(first_lo, self.span.hi());
let hi = context.snippet_provider.span_before(end_span, "mut");
(
context.snippet_provider.span_after(end_span, "mut"),
combine_strs_with_missing_comments(
context,
&first,
mut_infix,
mk_sp(lo, hi),
shape,
true,
)?,
)
}
(false, true) => (first_lo, first),
(true, false) => unreachable!("mut_infix necessarily follows a ref"),
(true, true) => (self.span.lo(), "".to_owned()),
}; };
let next = if !sub_pat.is_empty() { let next = if !sub_pat.is_empty() {
@ -174,9 +198,9 @@ impl Rewrite for Pat {
combine_strs_with_missing_comments( combine_strs_with_missing_comments(
context, context,
&first, &second,
&next, &next,
mk_sp(first_lo, ident.span.lo()), mk_sp(second_lo, ident.span.lo()),
shape, shape,
true, true,
) )
@ -274,6 +298,8 @@ impl Rewrite for Pat {
PatKind::Paren(ref pat) => pat PatKind::Paren(ref pat) => pat
.rewrite(context, shape.offset_left(1)?.sub_width(1)?) .rewrite(context, shape.offset_left(1)?.sub_width(1)?)
.map(|inner_pat| format!("({})", inner_pat)), .map(|inner_pat| format!("({})", inner_pat)),
PatKind::Err(_) => None,
PatKind::Deref(_) => None,
} }
} }
} }

View File

@ -263,13 +263,13 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
item_kind: ReorderableItemKind, item_kind: ReorderableItemKind,
in_group: bool, in_group: bool,
) -> usize { ) -> usize {
let mut last = self.parse_sess.lookup_line_range(items[0].span()); let mut last = self.psess.lookup_line_range(items[0].span());
let item_length = items let item_length = items
.iter() .iter()
.take_while(|ppi| { .take_while(|ppi| {
item_kind.is_same_item_kind(&***ppi) item_kind.is_same_item_kind(&***ppi)
&& (!in_group || { && (!in_group || {
let current = self.parse_sess.lookup_line_range(ppi.span()); let current = self.psess.lookup_line_range(ppi.span());
let in_same_group = current.lo < last.hi + 2; let in_same_group = current.lo < last.hi + 2;
last = current; last = current;
in_same_group in_same_group

View File

@ -26,7 +26,7 @@ impl<T: Rewrite> Rewrite for ptr::P<T> {
#[derive(Clone)] #[derive(Clone)]
pub(crate) struct RewriteContext<'a> { pub(crate) struct RewriteContext<'a> {
pub(crate) parse_sess: &'a ParseSess, pub(crate) psess: &'a ParseSess,
pub(crate) config: &'a Config, pub(crate) config: &'a Config,
pub(crate) inside_macro: Rc<Cell<bool>>, pub(crate) inside_macro: Rc<Cell<bool>>,
// Force block indent style even if we are using visual indent style. // Force block indent style even if we are using visual indent style.

View File

@ -49,7 +49,7 @@ where
} }
pub(crate) fn write_file<T>( pub(crate) fn write_file<T>(
parse_sess: Option<&ParseSess>, psess: Option<&ParseSess>,
filename: &FileName, filename: &FileName,
formatted_text: &str, formatted_text: &str,
out: &mut T, out: &mut T,
@ -66,6 +66,7 @@ where
} }
} }
#[allow(non_local_definitions)]
impl From<&FileName> for rustc_span::FileName { impl From<&FileName> for rustc_span::FileName {
fn from(filename: &FileName) -> rustc_span::FileName { fn from(filename: &FileName) -> rustc_span::FileName {
match filename { match filename {
@ -89,7 +90,7 @@ where
let original_text = if newline_style != NewlineStyle::Auto && *filename != FileName::Stdin { let original_text = if newline_style != NewlineStyle::Auto && *filename != FileName::Stdin {
Lrc::new(fs::read_to_string(ensure_real_path(filename))?) Lrc::new(fs::read_to_string(ensure_real_path(filename))?)
} else { } else {
match parse_sess.and_then(|sess| sess.get_original_snippet(filename)) { match psess.and_then(|psess| psess.get_original_snippet(filename)) {
Some(ori) => ori, Some(ori) => ori,
None => Lrc::new(fs::read_to_string(ensure_real_path(filename))?), None => Lrc::new(fs::read_to_string(ensure_real_path(filename))?),
} }

View File

@ -61,7 +61,7 @@ implement_spanned!(ast::Local);
impl Spanned for ast::Stmt { impl Spanned for ast::Stmt {
fn span(&self) -> Span { fn span(&self) -> Span {
match self.kind { match self.kind {
ast::StmtKind::Local(ref local) => mk_sp(local.span().lo(), self.span.hi()), ast::StmtKind::Let(ref local) => mk_sp(local.span().lo(), self.span.hi()),
ast::StmtKind::Item(ref item) => mk_sp(item.span().lo(), self.span.hi()), ast::StmtKind::Item(ref item) => mk_sp(item.span().lo(), self.span.hi()),
ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => { ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => {
mk_sp(expr.span().lo(), self.span.hi()) mk_sp(expr.span().lo(), self.span.hi())

View File

@ -115,7 +115,7 @@ fn format_stmt(
skip_out_of_file_lines_range!(context, stmt.span()); skip_out_of_file_lines_range!(context, stmt.span());
let result = match stmt.kind { let result = match stmt.kind {
ast::StmtKind::Local(ref local) => local.rewrite(context, shape), ast::StmtKind::Let(ref local) => local.rewrite(context, shape),
ast::StmtKind::Expr(ref ex) | ast::StmtKind::Semi(ref ex) => { ast::StmtKind::Expr(ref ex) | ast::StmtKind::Semi(ref ex) => {
let suffix = if semicolon_for_stmt(context, stmt, is_last_expr) { let suffix = if semicolon_for_stmt(context, stmt, is_last_expr) {
";" ";"

View File

@ -391,12 +391,24 @@ fn self_tests() {
files.push(path); files.push(path);
} }
// for crates that need to be included but lies outside src // for crates that need to be included but lies outside src
let external_crates = vec!["check_diff"]; let external_crates = vec!["check_diff", "config_proc_macro"];
for external_crate in external_crates { for external_crate in external_crates {
let mut path = PathBuf::from(external_crate); let mut path = PathBuf::from(external_crate);
path.push("src"); path.push("src");
path.push("main.rs"); let directory = fs::read_dir(&path).unwrap();
files.push(path); let search_files = directory.filter_map(|file| {
file.ok().and_then(|f| {
let name = f.file_name();
if matches!(name.as_os_str().to_str(), Some("main.rs" | "lib.rs")) {
Some(f.path())
} else {
None
}
})
});
for file in search_files {
files.push(file);
}
} }
files.push(PathBuf::from("src/lib.rs")); files.push(PathBuf::from("src/lib.rs"));

View File

@ -55,3 +55,17 @@ fn crate_parsing_errors_on_unclosed_delims() {
let filename = "tests/parser/unclosed-delims/issue_4466.rs"; let filename = "tests/parser/unclosed-delims/issue_4466.rs";
assert_parser_error(filename); assert_parser_error(filename);
} }
#[test]
fn crate_parsing_stashed_diag() {
// See also https://github.com/rust-lang/rust/issues/121450
let filename = "tests/parser/stashed-diag.rs";
assert_parser_error(filename);
}
#[test]
fn crate_parsing_stashed_diag2() {
// See also https://github.com/rust-lang/rust/issues/121517
let filename = "tests/parser/stashed-diag2.rs";
assert_parser_error(filename);
}

View File

@ -142,7 +142,7 @@ pub(crate) enum SegmentParam<'a> {
Const(&'a ast::AnonConst), Const(&'a ast::AnonConst),
LifeTime(&'a ast::Lifetime), LifeTime(&'a ast::Lifetime),
Type(&'a ast::Ty), Type(&'a ast::Ty),
Binding(&'a ast::AssocConstraint), Binding(&'a ast::AssocItemConstraint),
} }
impl<'a> SegmentParam<'a> { impl<'a> SegmentParam<'a> {
@ -177,9 +177,9 @@ impl<'a> Rewrite for SegmentParam<'a> {
} }
} }
impl Rewrite for ast::AssocConstraint { impl Rewrite for ast::AssocItemConstraint {
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> { fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
use ast::AssocConstraintKind::{Bound, Equality}; use ast::AssocItemConstraintKind::{Bound, Equality};
let mut result = String::with_capacity(128); let mut result = String::with_capacity(128);
result.push_str(rewrite_ident(context, self.ident)); result.push_str(rewrite_ident(context, self.ident));
@ -207,14 +207,14 @@ impl Rewrite for ast::AssocConstraint {
} }
} }
impl Rewrite for ast::AssocConstraintKind { impl Rewrite for ast::AssocItemConstraintKind {
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> { fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
match self { match self {
ast::AssocConstraintKind::Equality { term } => match term { ast::AssocItemConstraintKind::Equality { term } => match term {
Term::Ty(ty) => ty.rewrite(context, shape), Term::Ty(ty) => ty.rewrite(context, shape),
Term::Const(c) => c.rewrite(context, shape), Term::Const(c) => c.rewrite(context, shape),
}, },
ast::AssocConstraintKind::Bound { bounds } => bounds.rewrite(context, shape), ast::AssocItemConstraintKind::Bound { bounds } => bounds.rewrite(context, shape),
} }
} }
} }
@ -539,18 +539,29 @@ impl Rewrite for ast::Lifetime {
impl Rewrite for ast::GenericBound { impl Rewrite for ast::GenericBound {
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> { fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
match *self { match *self {
ast::GenericBound::Trait(ref poly_trait_ref, modifiers) => { ast::GenericBound::Trait(
ref poly_trait_ref,
ast::TraitBoundModifiers {
constness,
asyncness,
polarity,
},
) => {
let snippet = context.snippet(self.span()); let snippet = context.snippet(self.span());
let has_paren = snippet.starts_with('(') && snippet.ends_with(')'); let has_paren = snippet.starts_with('(') && snippet.ends_with(')');
let mut constness = modifiers.constness.as_str().to_string(); let mut constness = constness.as_str().to_string();
if !constness.is_empty() { if !constness.is_empty() {
constness.push(' '); constness.push(' ');
} }
let polarity = modifiers.polarity.as_str(); let mut asyncness = asyncness.as_str().to_string();
if !asyncness.is_empty() {
asyncness.push(' ');
}
let polarity = polarity.as_str();
let shape = shape.offset_left(constness.len() + polarity.len())?; let shape = shape.offset_left(constness.len() + polarity.len())?;
poly_trait_ref poly_trait_ref
.rewrite(context, shape) .rewrite(context, shape)
.map(|s| format!("{constness}{polarity}{s}")) .map(|s| format!("{constness}{asyncness}{polarity}{s}"))
.map(|s| if has_paren { format!("({})", s) } else { s }) .map(|s| if has_paren { format!("({})", s) } else { s })
} }
ast::GenericBound::Outlives(ref lifetime) => lifetime.rewrite(context, shape), ast::GenericBound::Outlives(ref lifetime) => lifetime.rewrite(context, shape),
@ -810,8 +821,8 @@ impl Rewrite for ast::Ty {
ast::TyKind::Tup(ref items) => { ast::TyKind::Tup(ref items) => {
rewrite_tuple(context, items.iter(), self.span, shape, items.len() == 1) rewrite_tuple(context, items.iter(), self.span, shape, items.len() == 1)
} }
ast::TyKind::AnonStruct(_) => Some(context.snippet(self.span).to_owned()), ast::TyKind::AnonStruct(..) => Some(context.snippet(self.span).to_owned()),
ast::TyKind::AnonUnion(_) => Some(context.snippet(self.span).to_owned()), ast::TyKind::AnonUnion(..) => Some(context.snippet(self.span).to_owned()),
ast::TyKind::Path(ref q_self, ref path) => { ast::TyKind::Path(ref q_self, ref path) => {
rewrite_path(context, PathContext::Type, q_self, path, shape) rewrite_path(context, PathContext::Type, q_self, path, shape)
} }
@ -836,7 +847,11 @@ impl Rewrite for ast::Ty {
rewrite_macro(mac, None, context, shape, MacroPosition::Expression) rewrite_macro(mac, None, context, shape, MacroPosition::Expression)
} }
ast::TyKind::ImplicitSelf => Some(String::from("")), ast::TyKind::ImplicitSelf => Some(String::from("")),
ast::TyKind::ImplTrait(_, ref it) => { ast::TyKind::ImplTrait(_, ref it, ref captures) => {
// FIXME(precise_capturing): Implement formatting.
if captures.is_some() {
return None;
}
// Empty trait is not a parser error. // Empty trait is not a parser error.
if it.is_empty() { if it.is_empty() {
return Some("impl".to_owned()); return Some("impl".to_owned());
@ -852,7 +867,7 @@ impl Rewrite for ast::Ty {
}) })
} }
ast::TyKind::CVarArgs => Some("...".to_owned()), ast::TyKind::CVarArgs => Some("...".to_owned()),
ast::TyKind::Err => Some(context.snippet(self.span).to_owned()), ast::TyKind::Dummy | ast::TyKind::Err(_) => Some(context.snippet(self.span).to_owned()),
ast::TyKind::Typeof(ref anon_const) => rewrite_call( ast::TyKind::Typeof(ref anon_const) => rewrite_call(
context, context,
"typeof", "typeof",
@ -860,6 +875,11 @@ impl Rewrite for ast::Ty {
self.span, self.span,
shape, shape,
), ),
ast::TyKind::Pat(ref ty, ref pat) => {
let ty = ty.rewrite(context, shape)?;
let pat = pat.rewrite(context, shape)?;
Some(format!("{ty} is {pat}"))
}
} }
} }
} }
@ -883,7 +903,7 @@ fn rewrite_bare_fn(
result.push_str("> "); result.push_str("> ");
} }
result.push_str(crate::utils::format_unsafety(bare_fn.unsafety)); result.push_str(crate::utils::format_safety(bare_fn.safety));
result.push_str(&format_extern( result.push_str(&format_extern(
bare_fn.ext, bare_fn.ext,
@ -1094,7 +1114,8 @@ fn join_bounds_inner(
pub(crate) fn opaque_ty(ty: &Option<ptr::P<ast::Ty>>) -> Option<&ast::GenericBounds> { pub(crate) fn opaque_ty(ty: &Option<ptr::P<ast::Ty>>) -> Option<&ast::GenericBounds> {
ty.as_ref().and_then(|t| match &t.kind { ty.as_ref().and_then(|t| match &t.kind {
ast::TyKind::ImplTrait(_, bounds) => Some(bounds), // FIXME(precise_capturing): Implement support here
ast::TyKind::ImplTrait(_, bounds, _) => Some(bounds),
_ => None, _ => None,
}) })
} }

View File

@ -108,10 +108,11 @@ pub(crate) fn format_defaultness(defaultness: ast::Defaultness) -> &'static str
} }
#[inline] #[inline]
pub(crate) fn format_unsafety(unsafety: ast::Unsafe) -> &'static str { pub(crate) fn format_safety(unsafety: ast::Safety) -> &'static str {
match unsafety { match unsafety {
ast::Unsafe::Yes(..) => "unsafe ", ast::Safety::Unsafe(..) => "unsafe ",
ast::Unsafe::No => "", ast::Safety::Safe(..) => "safe ",
ast::Safety::Default => "",
} }
} }
@ -362,7 +363,7 @@ macro_rules! out_of_file_lines_range {
&& !$self && !$self
.config .config
.file_lines() .file_lines()
.intersects(&$self.parse_sess.lookup_line_range($span)) .intersects(&$self.psess.lookup_line_range($span))
}; };
} }
@ -497,7 +498,8 @@ pub(crate) fn is_block_expr(context: &RewriteContext<'_>, expr: &ast::Expr, repr
| ast::ExprKind::Break(..) | ast::ExprKind::Break(..)
| ast::ExprKind::Cast(..) | ast::ExprKind::Cast(..)
| ast::ExprKind::Continue(..) | ast::ExprKind::Continue(..)
| ast::ExprKind::Err | ast::ExprKind::Dummy
| ast::ExprKind::Err(_)
| ast::ExprKind::Field(..) | ast::ExprKind::Field(..)
| ast::ExprKind::IncludedBytes(..) | ast::ExprKind::IncludedBytes(..)
| ast::ExprKind::InlineAsm(..) | ast::ExprKind::InlineAsm(..)

View File

@ -24,7 +24,7 @@ use crate::source_map::{LineRangeUtils, SpanUtils};
use crate::spanned::Spanned; use crate::spanned::Spanned;
use crate::stmt::Stmt; use crate::stmt::Stmt;
use crate::utils::{ use crate::utils::{
self, contains_skip, count_newlines, depr_skip_annotation, format_unsafety, inner_attributes, self, contains_skip, count_newlines, depr_skip_annotation, format_safety, inner_attributes,
last_line_width, mk_sp, ptr_vec_to_ref_vec, rewrite_ident, starts_with_newline, stmt_expr, last_line_width, mk_sp, ptr_vec_to_ref_vec, rewrite_ident, starts_with_newline, stmt_expr,
}; };
use crate::{ErrorKind, FormatReport, FormattingError}; use crate::{ErrorKind, FormatReport, FormattingError};
@ -71,7 +71,7 @@ impl SnippetProvider {
pub(crate) struct FmtVisitor<'a> { pub(crate) struct FmtVisitor<'a> {
parent_context: Option<&'a RewriteContext<'a>>, parent_context: Option<&'a RewriteContext<'a>>,
pub(crate) parse_sess: &'a ParseSess, pub(crate) psess: &'a ParseSess,
pub(crate) buffer: String, pub(crate) buffer: String,
pub(crate) last_pos: BytePos, pub(crate) last_pos: BytePos,
// FIXME: use an RAII util or closure for indenting // FIXME: use an RAII util or closure for indenting
@ -113,10 +113,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
} }
fn visit_stmt(&mut self, stmt: &Stmt<'_>, include_empty_semi: bool) { fn visit_stmt(&mut self, stmt: &Stmt<'_>, include_empty_semi: bool) {
debug!( debug!("visit_stmt: {}", self.psess.span_to_debug_info(stmt.span()));
"visit_stmt: {}",
self.parse_sess.span_to_debug_info(stmt.span())
);
if stmt.is_empty() { if stmt.is_empty() {
// If the statement is empty, just skip over it. Before that, make sure any comment // If the statement is empty, just skip over it. Before that, make sure any comment
@ -153,7 +150,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
self.visit_item(item); self.visit_item(item);
self.last_pos = stmt.span().hi(); self.last_pos = stmt.span().hi();
} }
ast::StmtKind::Local(..) | ast::StmtKind::Expr(..) | ast::StmtKind::Semi(..) => { ast::StmtKind::Let(..) | ast::StmtKind::Expr(..) | ast::StmtKind::Semi(..) => {
let attrs = get_attrs_from_stmt(stmt.as_ast_node()); let attrs = get_attrs_from_stmt(stmt.as_ast_node());
if contains_skip(attrs) { if contains_skip(attrs) {
self.push_skipped_with_span( self.push_skipped_with_span(
@ -217,10 +214,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
inner_attrs: Option<&[ast::Attribute]>, inner_attrs: Option<&[ast::Attribute]>,
has_braces: bool, has_braces: bool,
) { ) {
debug!( debug!("visit_block: {}", self.psess.span_to_debug_info(b.span));
"visit_block: {}",
self.parse_sess.span_to_debug_info(b.span),
);
// Check if this block has braces. // Check if this block has braces.
let brace_compensation = BytePos(if has_braces { 1 } else { 0 }); let brace_compensation = BytePos(if has_braces { 1 } else { 0 });
@ -523,9 +517,9 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
self.visit_enum(item.ident, &item.vis, def, generics, item.span); self.visit_enum(item.ident, &item.vis, def, generics, item.span);
self.last_pos = source!(self, item.span).hi(); self.last_pos = source!(self, item.span).hi();
} }
ast::ItemKind::Mod(unsafety, ref mod_kind) => { ast::ItemKind::Mod(safety, ref mod_kind) => {
self.format_missing_with_indent(source!(self, item.span).lo()); self.format_missing_with_indent(source!(self, item.span).lo());
self.format_mod(mod_kind, unsafety, &item.vis, item.span, item.ident, attrs); self.format_mod(mod_kind, safety, &item.vis, item.span, item.ident, attrs);
} }
ast::ItemKind::MacCall(ref mac) => { ast::ItemKind::MacCall(ref mac) => {
self.visit_mac(mac, Some(item.ident), MacroPosition::Item); self.visit_mac(mac, Some(item.ident), MacroPosition::Item);
@ -592,6 +586,11 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
); );
self.push_rewrite(item.span, rewrite); self.push_rewrite(item.span, rewrite);
} }
ast::ItemKind::Delegation(..) | ast::ItemKind::DelegationMac(..) => {
// TODO: rewrite delegation items once syntax is established.
// For now, leave the contents of the Span unformatted.
self.push_rewrite(item.span, None)
}
}; };
} }
self.skip_context = skip_context_saved; self.skip_context = skip_context_saved;
@ -739,10 +738,10 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
// do not take into account the lines with attributes as part of the skipped range // do not take into account the lines with attributes as part of the skipped range
let attrs_end = attrs let attrs_end = attrs
.iter() .iter()
.map(|attr| self.parse_sess.line_of_byte_pos(attr.span.hi())) .map(|attr| self.psess.line_of_byte_pos(attr.span.hi()))
.max() .max()
.unwrap_or(1); .unwrap_or(1);
let first_line = self.parse_sess.line_of_byte_pos(main_span.lo()); let first_line = self.psess.line_of_byte_pos(main_span.lo());
// Statement can start after some newlines and/or spaces // Statement can start after some newlines and/or spaces
// or it can be on the same line as the last attribute. // or it can be on the same line as the last attribute.
// So here we need to take a minimum between the two. // So here we need to take a minimum between the two.
@ -753,8 +752,8 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
} }
pub(crate) fn from_context(ctx: &'a RewriteContext<'_>) -> FmtVisitor<'a> { pub(crate) fn from_context(ctx: &'a RewriteContext<'_>) -> FmtVisitor<'a> {
let mut visitor = FmtVisitor::from_parse_sess( let mut visitor = FmtVisitor::from_psess(
ctx.parse_sess, ctx.psess,
ctx.config, ctx.config,
ctx.snippet_provider, ctx.snippet_provider,
ctx.report.clone(), ctx.report.clone(),
@ -764,8 +763,8 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
visitor visitor
} }
pub(crate) fn from_parse_sess( pub(crate) fn from_psess(
parse_session: &'a ParseSess, psess: &'a ParseSess,
config: &'a Config, config: &'a Config,
snippet_provider: &'a SnippetProvider, snippet_provider: &'a SnippetProvider,
report: FormatReport, report: FormatReport,
@ -781,7 +780,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
skip_context.macros.extend(macro_names); skip_context.macros.extend(macro_names);
FmtVisitor { FmtVisitor {
parent_context: None, parent_context: None,
parse_sess: parse_session, psess,
buffer: String::with_capacity(snippet_provider.big_snippet.len() * 2), buffer: String::with_capacity(snippet_provider.big_snippet.len() * 2),
last_pos: BytePos(0), last_pos: BytePos(0),
block_indent: Indent::empty(), block_indent: Indent::empty(),
@ -809,12 +808,12 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
pub(crate) fn visit_attrs(&mut self, attrs: &[ast::Attribute], style: ast::AttrStyle) -> bool { pub(crate) fn visit_attrs(&mut self, attrs: &[ast::Attribute], style: ast::AttrStyle) -> bool {
for attr in attrs { for attr in attrs {
if attr.has_name(depr_skip_annotation()) { if attr.has_name(depr_skip_annotation()) {
let file_name = self.parse_sess.span_to_filename(attr.span); let file_name = self.psess.span_to_filename(attr.span);
self.report.append( self.report.append(
file_name, file_name,
vec![FormattingError::from_span( vec![FormattingError::from_span(
attr.span, attr.span,
self.parse_sess, self.psess,
ErrorKind::DeprecatedAttr, ErrorKind::DeprecatedAttr,
)], )],
); );
@ -823,12 +822,12 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
ast::AttrKind::Normal(ref normal) ast::AttrKind::Normal(ref normal)
if self.is_unknown_rustfmt_attr(&normal.item.path.segments) => if self.is_unknown_rustfmt_attr(&normal.item.path.segments) =>
{ {
let file_name = self.parse_sess.span_to_filename(attr.span); let file_name = self.psess.span_to_filename(attr.span);
self.report.append( self.report.append(
file_name, file_name,
vec![FormattingError::from_span( vec![FormattingError::from_span(
attr.span, attr.span,
self.parse_sess, self.psess,
ErrorKind::BadAttr, ErrorKind::BadAttr,
)], )],
); );
@ -914,7 +913,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
fn format_mod( fn format_mod(
&mut self, &mut self,
mod_kind: &ast::ModKind, mod_kind: &ast::ModKind,
unsafety: ast::Unsafe, safety: ast::Safety,
vis: &ast::Visibility, vis: &ast::Visibility,
s: Span, s: Span,
ident: symbol::Ident, ident: symbol::Ident,
@ -922,7 +921,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
) { ) {
let vis_str = utils::format_visibility(&self.get_context(), vis); let vis_str = utils::format_visibility(&self.get_context(), vis);
self.push_str(&*vis_str); self.push_str(&*vis_str);
self.push_str(format_unsafety(unsafety)); self.push_str(format_safety(safety));
self.push_str("mod "); self.push_str("mod ");
// Calling `to_owned()` to work around borrow checker. // Calling `to_owned()` to work around borrow checker.
let ident_str = rewrite_ident(&self.get_context(), ident).to_owned(); let ident_str = rewrite_ident(&self.get_context(), ident).to_owned();
@ -1002,7 +1001,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
pub(crate) fn get_context(&self) -> RewriteContext<'_> { pub(crate) fn get_context(&self) -> RewriteContext<'_> {
RewriteContext { RewriteContext {
parse_sess: self.parse_sess, psess: self.psess,
config: self.config, config: self.config,
inside_macro: Rc::new(Cell::new(false)), inside_macro: Rc::new(Cell::new(false)),
use_block: Cell::new(false), use_block: Cell::new(false),

View File

@ -0,0 +1,3 @@
#![u={static N;}]
fn main() {}

View File

@ -0,0 +1,3 @@
trait Trait<'1> { s> {}
fn main() {}

View File

@ -180,10 +180,11 @@ fn dont_emit_ICE() {
"tests/target/issue_5728.rs", "tests/target/issue_5728.rs",
"tests/target/issue_5729.rs", "tests/target/issue_5729.rs",
"tests/target/issue-5885.rs", "tests/target/issue-5885.rs",
"tests/target/issue_6082.rs",
"tests/target/issue_6069.rs", "tests/target/issue_6069.rs",
"tests/target/issue-6105.rs", "tests/target/issue-6105.rs",
]; ];
for file in files { for file in files {
let args = [file]; let args = [file];
let (_stdout, stderr) = rustfmt(&args); let (_stdout, stderr) = rustfmt(&args);

View File

@ -1,7 +1,8 @@
#![feature(coroutines)] #![feature(coroutines)]
unsafe fn foo() { unsafe fn foo() {
let mut ga = static || { let mut ga = #[coroutine]
static || {
yield 1; yield 1;
}; };
} }

View File

@ -0,0 +1 @@
m!(const N: usize = 0;);

10
tests/source/mut_ref.rs Normal file
View File

@ -0,0 +1,10 @@
#![feature(mut_ref)]
fn mut_ref() {
if let Some(mut /*a*/ ref /*def*/ mut /*abc*/ state)= /*abc*/foo{
println!(
"asdfasdfasdf"); }
if let Some(mut /*a*/ ref /*def*/ /*mut*/ state)= /*abc*/foo{
println!(
"asdfasdfasdf"); }
}

View File

@ -0,0 +1,20 @@
#![feature(postfix_match)]
fn main() {
let val = Some(42);
val.match {
Some(_) => 2,
_ => 1
};
Some(2).match {
Some(_) => true,
None => false
}.match {
false => "ferris is cute",
true => "I turn cats in to petted cats",
}.match {
_ => (),
}
}

View File

@ -16,4 +16,16 @@ struct Foo {
e: f32, e: f32,
} }
// Test for https://github.com/rust-lang/rust/issues/117942
struct Foo {
_: union {
#[rustfmt::skip]
f: String,
},
#[rustfmt::skip]
_: struct {
g: i32,
},
}
fn main() {} fn main() {}

View File

@ -0,0 +1,3 @@
// rustfmt-edition: 2018
fn foo() -> impl async Fn() {}

View File

@ -1,7 +1,8 @@
#![feature(coroutines)] #![feature(coroutines)]
unsafe fn foo() { unsafe fn foo() {
let mut ga = static || { let mut ga = #[coroutine]
static || {
yield 1; yield 1;
}; };
} }

View File

@ -0,0 +1,5 @@
macro_rules! test {
($T:ident, $b:lifetime) => {
Box<$T<$b>>
};
}

View File

@ -0,0 +1,3 @@
fn main() {
builtin # type_ascribe(10, usize)
}

View File

@ -0,0 +1,3 @@
m!(
const N: usize = 0;
);

10
tests/target/mut_ref.rs Normal file
View File

@ -0,0 +1,10 @@
#![feature(mut_ref)]
fn mut_ref() {
if let Some(mut /*a*/ ref /*def*/ mut /*abc*/ state) = /*abc*/ foo {
println!("asdfasdfasdf");
}
if let Some(mut /*a*/ ref /*def*/ /*mut*/ state) = /*abc*/ foo {
println!("asdfasdfasdf");
}
}

View File

@ -0,0 +1,20 @@
#![feature(postfix_match)]
fn main() {
let val = Some(42);
val.match {
Some(_) => 2,
_ => 1,
};
Some(2).match {
Some(_) => true,
None => false,
}.match {
false => "ferris is cute",
true => "I turn cats in to petted cats",
}.match {
_ => (),
}
}

View File

@ -0,0 +1,34 @@
#![feature(unsafe_attributes)]
// https://github.com/rust-lang/rust/issues/123757
//
#![simple_ident]
#![simple::path]
#![simple_ident_expr = ""]
#![simple::path::Expr = ""]
#![simple_ident_tt(a b c)]
#![simple_ident_tt[a b c]]
#![simple_ident_tt{a b c}]
#![simple::path::tt(a b c)]
#![simple::path::tt[a b c]]
#![simple::path::tt{a b c}]
#![unsafe(simple_ident)]
#![unsafe(simple::path)]
#![unsafe(simple_ident_expr = "")]
#![unsafe(simple::path::Expr = "")]
#![unsafe(simple_ident_tt(a b c))]
#![unsafe(simple_ident_tt[a b c])]
#![unsafe(simple_ident_tt{a b c})]
#![unsafe(simple::path::tt(a b c))]
#![unsafe(simple::path::tt[a b c])]
#![unsafe(simple::path::tt{a b c})]
// I don't think `safe` attributes are a thing, but adding these formatting cases here just in case
#![safe(simple_ident)]
#![safe(simple::path)]
#![safe(simple_ident_expr = "")]
#![safe(simple::path::Expr = "")]
#![safe(simple_ident_tt(a b c))]
#![safe(simple_ident_tt[a b c])]
#![safe(simple_ident_tt{a b c})]
#![safe(simple::path::tt(a b c))]
#![safe(simple::path::tt[a b c])]
#![safe(simple::path::tt{a b c})]