fix: Skip cfg attributes in macro input attribute stripping #21205
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description:
This fixes a regression introduced in commit f0e372c ("Rewrite attribute handling"), where cfg attributes
were incorrectly counted during macro input stripping.
Problem
When using attribute macros like maybe-async-cfg, if cfg attributes are present on the item, rust-analyzer may
fail to strip the macro attribute itself from the input. This results in the macro being expanded recursively
(e.g.,
TestStruct->TestStructAsync(Correct) ->TestStructAsyncAsync(Wrong)).Cause
The
macro_input_callbackfunction incrates/hir-expand/src/cfg_process.rsuses a counter (item_tree_attr_id)to identify which attribute to strip. This counter was being incremented for cfg attributes.
However,
collect_item_tree_attrs(which assigns the attribute IDs) filters out cfg attributes. This caused thecounter in
macro_input_callback to desynchronize with the actual attribute IDs, leading to the macro attributenot being identified and stripped.
Fix
Explicitly skip cfg attributes when determining whether to call

should_strip_attr()inmacro_input_callback.This ensures the attribute counter remains in sync with the item tree.
Repro