rustfmt fix: allow file not found errors for external mods annotated with #[my_macro]#159737
rustfmt fix: allow file not found errors for external mods annotated with #[my_macro]#159737ytmimi wants to merge 1 commit into
#[my_macro]#159737Conversation
|
cc @rust-lang/rustfmt |
#[my_macro]#[my_macro]
e679de0 to
0a54058
Compare
| .filter(|attr| matches!(attr.style(), AttrStyle::Outer)) | ||
| .any(|attr| { | ||
| attr.meta().is_some_and(|meta_item| match meta_item.kind { | ||
| MetaItemKind::Word => meta_item.has_name(Symbol::intern("my_macro")), |
There was a problem hiding this comment.
"my_macro" was only an example. This could be any custom attribute macro.
There was a problem hiding this comment.
Okay, in that case if there are any outer attributes then we could ignore the file not found error. I don't think there's a reliable way for rustfmt to know if this is a custom vs built-in attribute macro.
There was a problem hiding this comment.
it doesn't even have to be a macro, necessarily:
#[cfg(false)]
mod x;so this was a problem long before macros on modules.
There was a problem hiding this comment.
it doesn't even have to be a macro, necessarily:
#[cfg(false)] mod x;so this was a problem long before macros on modules.
rustfmt has always assumed that it could find the source file for externally defined modules. rustfmt only operates on the AST of your program so it just sees an external module declaration that has some outer attributes. Just because something isn't going to be compiled doesn't mean that we shouldn't format the content.
As mentioned in rust-lang/rustfmt#6959 (comment), the only time rustfmt won't try to resolve external modules is when it's running on input from stdin or if you're running rustfmt with the --skip-children command line flag.
0a54058 to
3eb2b02
Compare
| ModError::FileNotFound(_, default_path, _secondary_path) => { | ||
| if attrs | ||
| .iter() | ||
| .any(|a| matches!(a.style(), ast::AttrStyle::Outer)) |
There was a problem hiding this comment.
This condition is always true if the module is not loaded.
rustfmt has access to compiler crates, so it can use is_builtin_attr_name or is_builtin_attr to detect non-built-in attributes.
There was a problem hiding this comment.
Thank you for pointing that out. I think that would help narrow the scope in which we ignore the error.
…om outer attributes 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. Fixes rustfmt issue 6959
3eb2b02 to
baffab0
Compare
|
@petrochenkov thanks for pointing out that I could use @rustbot ready |
Tracking issue: #54727
#[my_macro]was stabilized for macro hygiene 2.0 in #157857. There isn't a guarantee that the external module exists on the file system so ignore any file not found errors encountered when trying to resolve the module's file.Fixes rust-lang/rustfmt#6959
r? @petrochenkov
cc: @TimNN