Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,312 changes: 712 additions & 600 deletions Cargo.lock

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]

name = "rustfmt-nightly"
version = "1.4.20"
version = "1.4.21"
authors = ["Nicholas Cameron <[email protected]>", "The Rustfmt developers"]
description = "Tool to find and fix Rust formatting issues"
repository = "https://github.com/rust-lang/rustfmt"
Expand Down Expand Up @@ -66,36 +66,36 @@ rustc-workspace-hack = "1.0.0"

[dependencies.rustc_ast]
package = "rustc-ap-rustc_ast"
version = "671.0.0"
version = "677.0.0"

[dependencies.rustc_ast_pretty]
package = "rustc-ap-rustc_ast_pretty"
version = "671.0.0"
version = "677.0.0"

[dependencies.rustc_attr]
package = "rustc-ap-rustc_attr"
version = "671.0.0"
version = "677.0.0"

[dependencies.rustc_data_structures]
package = "rustc-ap-rustc_data_structures"
version = "671.0.0"
version = "677.0.0"

[dependencies.rustc_errors]
package = "rustc-ap-rustc_errors"
version = "671.0.0"
version = "677.0.0"

[dependencies.rustc_expand]
package = "rustc-ap-rustc_expand"
version = "671.0.0"
version = "677.0.0"

[dependencies.rustc_parse]
package = "rustc-ap-rustc_parse"
version = "671.0.0"
version = "677.0.0"

[dependencies.rustc_session]
package = "rustc-ap-rustc_session"
version = "671.0.0"
version = "677.0.0"

[dependencies.rustc_span]
package = "rustc-ap-rustc_span"
version = "671.0.0"
version = "677.0.0"
20 changes: 17 additions & 3 deletions src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use rustc_ast::ast;
use rustc_ast::attr::HasAttrs;
use rustc_span::{symbol::sym, BytePos, Span, DUMMY_SP};
use rustc_span::{symbol::sym, BytePos, Span, Symbol, DUMMY_SP};

use self::doc_comment::DocCommentFormatter;
use crate::comment::{contains_comment, rewrite_doc_comment, CommentStyle};
Expand All @@ -18,6 +18,20 @@ use crate::utils::{count_newlines, mk_sp};

mod doc_comment;

pub(crate) fn contains_name(attrs: &[ast::Attribute], name: Symbol) -> bool {
attrs.iter().any(|attr| attr.has_name(name))
}

pub(crate) fn first_attr_value_str_by_name(
attrs: &[ast::Attribute],
name: Symbol,
) -> Option<Symbol> {
attrs
.iter()
.find(|attr| attr.has_name(name))
.and_then(|attr| attr.value_str())
}

/// Returns attributes on the given statement.
pub(crate) fn get_attrs_from_stmt(stmt: &ast::Stmt) -> &[ast::Attribute] {
stmt.attrs()
Expand Down Expand Up @@ -49,7 +63,7 @@ pub(crate) fn filter_inline_attrs(
}

fn is_derive(attr: &ast::Attribute) -> bool {
attr.check_name(sym::derive)
attr.has_name(sym::derive)
}

/// Returns the arguments of `#[derive(...)]`.
Expand Down Expand Up @@ -327,7 +341,7 @@ impl Rewrite for ast::Attribute {

if let Some(ref meta) = self.meta() {
// This attribute is possibly a doc attribute needing normalization to a doc comment
if context.config.normalize_doc_attributes() && meta.check_name(sym::doc) {
if context.config.normalize_doc_attributes() && meta.has_name(sym::doc) {
if let Some(ref literal) = meta.value_str() {
let comment_style = match self.style {
ast::AttrStyle::Inner => CommentStyle::Doc,
Expand Down
31 changes: 8 additions & 23 deletions src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use self::newline_style::apply_newline_style;
use crate::comment::{CharClasses, FullCodeCharKind};
use crate::config::{Config, FileName, Verbosity};
use crate::issues::BadIssueSeeker;
use crate::modules::Module;
use crate::syntux::parser::{DirectoryOwnership, Parser, ParserError};
use crate::syntux::session::ParseSess;
use crate::utils::count_newlines;
Expand All @@ -29,7 +30,7 @@ impl<'b, T: Write + 'b> Session<'b, T> {
return Err(ErrorKind::VersionMismatch);
}

rustc_ast::with_session_globals(self.config.edition().to_libsyntax_pos_edition(), || {
rustc_span::with_session_globals(self.config.edition().to_libsyntax_pos_edition(), || {
if self.config.disable_all_formatting() {
// When the input is from stdin, echo back the input.
if let Input::Text(ref buf) = input {
Expand Down Expand Up @@ -102,8 +103,7 @@ fn format_project<T: FormatHandler>(
continue;
}
should_emit_verbose(input_is_stdin, config, || println!("Formatting {}", path));
let is_root = path == main_file;
context.format_file(path, &module, is_root)?;
context.format_file(path, &module)?;
}
timer = timer.done_formatting();

Expand Down Expand Up @@ -134,13 +134,8 @@ impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
}

// Formats a single file/module.
fn format_file(
&mut self,
path: FileName,
module: &ast::Mod,
is_root: bool,
) -> Result<(), ErrorKind> {
let snippet_provider = self.parse_session.snippet_provider(module.inner);
fn format_file(&mut self, path: FileName, module: &Module<'_>) -> Result<(), ErrorKind> {
let snippet_provider = self.parse_session.snippet_provider(module.as_ref().inner);
let mut visitor = FmtVisitor::from_parse_sess(
&self.parse_session,
&self.config,
Expand All @@ -149,19 +144,9 @@ impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
);
visitor.skip_context.update_with_attrs(&self.krate.attrs);

// Format inner attributes if available.
if !self.krate.attrs.is_empty() && is_root {
visitor.skip_empty_lines(snippet_provider.end_pos());
if visitor.visit_attrs(&self.krate.attrs, ast::AttrStyle::Inner) {
visitor.push_rewrite(module.inner, None);
} else {
visitor.format_separate_mod(module, snippet_provider.end_pos());
}
} else {
visitor.last_pos = snippet_provider.start_pos();
visitor.skip_empty_lines(snippet_provider.end_pos());
visitor.format_separate_mod(module, snippet_provider.end_pos());
};
visitor.last_pos = snippet_provider.start_pos();
visitor.skip_empty_lines(snippet_provider.end_pos());
visitor.format_separate_mod(module, snippet_provider.end_pos());

debug_assert_eq!(
visitor.line_number,
Expand Down
Loading