Skip to content

Fix ICE from cfg_attr_trace #154803

Open
chenyukang wants to merge 1 commit intorust-lang:mainfrom
chenyukang:yukang-fix-154801-cfg-attr-span
Open

Fix ICE from cfg_attr_trace #154803
chenyukang wants to merge 1 commit intorust-lang:mainfrom
chenyukang:yukang-fix-154801-cfg-attr-span

Conversation

@chenyukang
Copy link
Copy Markdown
Member

@chenyukang chenyukang commented Apr 4, 2026

Fixes #154801

r? @JonathanBrouwer

The root cause is we recovery from parsing attribute error here:


while the later suggestion code from type checking try to inspect the attr span of the expr in the second error, keep the span seems reasonable.

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 4, 2026

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann, @JonathanBrouwer

Some changes occurred in compiler/rustc_passes/src/check_attr.rs

cc @jdonszelmann, @JonathanBrouwer

Some changes occurred in compiler/rustc_hir/src/attrs

cc @jdonszelmann, @JonathanBrouwer

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 4, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 4, 2026

JonathanBrouwer is currently at their maximum review capacity.
They may take a while to respond.

@rust-log-analyzer

This comment has been minimized.

@chenyukang chenyukang force-pushed the yukang-fix-154801-cfg-attr-span branch from ed6f9af to 952c5ba Compare April 4, 2026 09:44
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 4, 2026

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

@rustbot rustbot added the T-clippy Relevant to the Clippy team. label Apr 4, 2026

/// Represents the trace attribute of `#[cfg_attr]`
CfgAttrTrace,
CfgAttrTrace(Span),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this behave with multiple cfg_attr attributes? Since this attribute can exist multiple times, ideally we should have a ThinVec<Span> here?

@@ -0,0 +1,5 @@
fn main() {
let _x = 30;
#[cfg_attr(, (cc))] //~ ERROR expected a literal (`1u8`, `1.0f32`, `"string"`, etc.) here, found `,`
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This issue is not specific to cfg_attr, this will ICE with all attributes.
For example, this also ICES:

fn main() {
    let _x = 30;
    #[inline]
    _x
}

I think @jdonszelmann had an idea involving MultiSpans at some point to properly fix this

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the code.

The reason is that Attribute::Parsed(AttributeKind::...) does not seem to have a uniform “source span” model, a lot of them don't have span.

The current callers of has_span_without_desugaring_kind only need the narrower diagnostics question: whether an attribute should make suggestion rendering treat the expression like a prefixed form. I think a proper name is is_prefix_attr_for_suggestions: the goal is to reflect that this is not really a generic span-recovery API.

The new fix's behavior is intentionally conservative. We still filter out desugaring-generated lint attributes precisely where we can identify them, but otherwise prefer treating parsed expression attributes as affecting suggestion precedence.

a more concrete way is maybe add a API such as get_span_from_attr(attr_kind) -> Vec<Span> and then check desugaring_kind of spans, I'm hesitant whether it's necessary.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 4, 2026
@chenyukang chenyukang changed the title Fix ICE from cfg_attr_trace by storing span Fix ICE from cfg_attr_trace Apr 4, 2026
@chenyukang chenyukang force-pushed the yukang-fix-154801-cfg-attr-span branch from 952c5ba to 46e2396 Compare April 4, 2026 22:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ICE]: can't get span of parsed attr

4 participants