rustfmt fix: allow file not found errors for external mods annotated with #[my_macro]#159737
Conversation
|
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
…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 |
|
@bors r+ rollup |
…chenkov rustfmt fix: allow file not found errors for external mods annotated with `#[my_macro]` Tracking issue: rust-lang#54727 `#[my_macro]` was stabilized for macro hygiene 2.0 in rust-lang#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
Rollup of 20 pull requests Successful merges: - #150161 (Remove 'static requirement on try_as_dyn) - #159412 (Reorganize `tests/ui/issues` [26/N]) - #159466 (cmse: clear variant-dependent padding in `enum`s) - #157905 (Update comments and add tests for `-Zrandomize-layout` for some guaranteed ZSTs) - #158479 (Reject static item as direct const generic arg) - #159041 (Reorganize `tests/ui/issues` [22/N]) - #159108 (Reorganize `tests/ui/issues` [23/N]) - #159138 (doc: document wasm import symbol mangling) - #159531 (Reorganize `tests/ui/issues` [28/N]) - #159608 (early_otherwise: Don't hoist dereferences when the otherwise branch is reachable) - #159612 (Reorganize `tests/ui/issues` [29/N]) - #159653 (run `tests/assembly-llvm/asm/aarch64-types.rs` for `aarch64_be`) - #159719 (tests: add UI test for `.swap()` suggestion) - #159737 (rustfmt fix: allow file not found errors for external mods annotated with `#[my_macro]`) - #159759 (rustc-dev-guide subtree update) - #159761 (Remove outdated comment for resolve_vars_with_obligations) - #159764 (use `minicore::simd::Simd` more) - #159773 (Remove outdated workaround for `P` type) - #159778 (use lowest edition the test accepts) - #159788 (Avoid ICEing in RPIT method chain suggestion)
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