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 change: 1 addition & 0 deletions src/tools/rustfmt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ extern crate rustc_ast_pretty;
extern crate rustc_data_structures;
extern crate rustc_errors;
extern crate rustc_expand;
extern crate rustc_feature;
extern crate rustc_parse;
extern crate rustc_session;
extern crate rustc_span;
Expand Down
12 changes: 11 additions & 1 deletion src/tools/rustfmt/src/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::parse::parser::{
Directory, DirectoryOwnership, ModError, ModulePathSuccess, Parser, ParserError,
};
use crate::parse::session::ParseSess;
use crate::utils::{contains_skip, mk_sp};
use crate::utils::{contains_custom_attributes, contains_skip, mk_sp};

mod visitor;

Expand Down Expand Up @@ -472,6 +472,16 @@ impl<'ast, 'psess, 'c> ModResolver<'ast, 'psess> {
}
Err(e) => match e {
ModError::FileNotFound(_, default_path, _secondary_path) => {
if contains_custom_attributes(attrs) {
// It's possible that at least one of the attributes is a custom proc macro
// that takes the module tokens as an input. It's hard to know for sure
// since rustfmt only operates on the AST pre-expansion. In this case we'll
// be overly permissive and just ignore the file not found error so rustfmt
// can still try formatting the input.
tracing::warn!("Couldn't find file for mod {};`", mod_name.to_string());
return Ok(None);
}

Err(ModuleResolutionError {
module: mod_name.to_string(),
kind: ModuleResolutionErrorKind::NotFound { file: default_path },
Expand Down
8 changes: 8 additions & 0 deletions src/tools/rustfmt/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use rustc_ast::ast::{
NodeId, Path, RestrictionKind, Visibility, VisibilityKind,
};
use rustc_ast_pretty::pprust;
use rustc_feature::is_builtin_attr_name;
use rustc_span::{BytePos, LocalExpnId, Span, Symbol, SyntaxContext, sym, symbol};
use unicode_width::UnicodeWidthStr;

Expand Down Expand Up @@ -327,6 +328,13 @@ pub(crate) fn contains_skip(attrs: &[Attribute]) -> bool {
.any(|a| a.meta().map_or(false, |a| is_skip(&a)))
}

#[inline]
pub(crate) fn contains_custom_attributes(attrs: &[Attribute]) -> bool {
attrs
.iter()
.any(|a| a.name().is_some_and(|name| !is_builtin_attr_name(name)))
}

#[inline]
pub(crate) fn semicolon_for_expr(context: &RewriteContext<'_>, expr: &ast::Expr) -> bool {
// Never try to insert semicolons on expressions when we're inside
Expand Down
2 changes: 2 additions & 0 deletions src/tools/rustfmt/tests/target/issue_6959.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[my_macro]
mod foo;
Loading