Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix span of unsafe attribute diagnostic #133270

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ehuss
Copy link
Contributor

@ehuss ehuss commented Nov 21, 2024

This fixes the span of the unsafe_attr_outside_unsafe diagnostic when the attribute uses cfg_attr and comes from a macro. Previously the span it was pointing to was in the wrong place (offset by 2 bytes in the start, and 1 byte in the end), causing a corrupt suggestion.

The problem is that the lint was trying to do manual byte manipulation of the Attribute span to get within the #[ and ] tokens. However, when the attribute comes from cfg_attr, that span starts from the attribute path (like no_mangle), not the #[ of the cfg_attr.

The solution here is to store the span of the AttrItem while parsing, so that we know for sure that it covers the correct range (the path and all args). We could not use AttrItem::span() (which is removed in this PR), because that function did not correctly account for the path and arguments coming from separate expansion contexts. For example, in the macro expansion of #[$p = $a], the span would be $p = because you are not allowed to generate a span across expansion contexts.

Fixes #132908

@rustbot
Copy link
Collaborator

rustbot commented Nov 21, 2024

r? @petrochenkov

rustbot has assigned @petrochenkov.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added 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 Nov 21, 2024
@@ -241,10 +241,6 @@ impl Attribute {
}

impl AttrItem {
pub fn span(&self) -> Span {
self.args.span().map_or(self.path.span, |args_span| self.path.span.to(args_span))
Copy link
Member

@compiler-errors compiler-errors Nov 21, 2024

Choose a reason for hiding this comment

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

Are you sure we can't save this by using one of the span ancestor functions like https://doc.rust-lang.org/nightly/nightly-rustc/rustc_span/span_encoding/struct.Span.html#method.find_ancestor_in_same_ctxt?

Copy link
Contributor

Choose a reason for hiding this comment

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

This is another instance of the span combining problem really.
The x.to(y) operation should just work for things like #[$a = $b] if all metavar spans are recorded.
In fact the parser does exactly the same a.to(b) operation to save the span into the AST, it just may see different spans if nonterminals are involved.

The logic in validate_attrs may work or not work depending on how exactly parts of the attribute was passed (as tt, ident, meta or expr), previously it was fine-tuned for one scenarios, now it's fine tuned for others.

The only certain thing here is that the manual span arithmetic (+/- BytePos(1/2)) should not be performed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe just remove the span arithmetic and not try to improve spans for macro scenarios?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't see a way to get find_ancestor_in_same_ctxt to work. The issue is that in the case of #[link_section = $s], the span for $s is not stored anywhere. The AttrItem stores the path as link_section (or $e in the other example, it is before expansion). The AttrArgs stores the eq_span for the = in the macro, and the value as "foo" from the invocation.

I think supporting proper suggestions in macros is pretty important, as this pattern comes up quite frequently. I realize this can only be tuned to support certain scenarios, and not be perfect in all situations. However, I think recording the span as implemented in this PR covers the vast majority of situations well.

Are you still concerned with the regressions here?

/// The span of the contents of the attribute.
///
/// This is the span starting from the path and ending at the end of the args.
pub span: Span,
Copy link
Contributor

Choose a reason for hiding this comment

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

Increasing the size of AttrItem may be a performance sensitive change.

@petrochenkov
Copy link
Contributor

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 22, 2024
@petrochenkov petrochenkov removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 22, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 22, 2024
Fix span of unsafe attribute diagnostic

This fixes the span of the `unsafe_attr_outside_unsafe` diagnostic when the attribute uses `cfg_attr` and comes from a macro. Previously the span it was pointing to was in the wrong place (offset by 2 bytes in the start, and 1 byte in the end), causing a corrupt suggestion.

The problem is that the lint was trying to do manual byte manipulation of the `Attribute` span to get within the `#[` and `]` tokens. However, when the attribute comes from `cfg_attr`, that span starts from the attribute path (like `no_mangle`), not the `#[` of the `cfg_attr`.

The solution here is to store the span of the `AttrItem` while parsing, so that we know for sure that it covers the correct range (the path and all args). We could not use `AttrItem::span()` (which is removed in this PR), because that function did not correctly account for the path and arguments coming from separate expansion contexts. For example, in the macro expansion of `#[$p = $a]`, the span would be `$p = ` because you are not allowed to generate a span across expansion contexts.

Fixes rust-lang#132908
@bors
Copy link
Contributor

bors commented Nov 22, 2024

⌛ Trying commit 92fe744 with merge 46c9e3a...

@bors
Copy link
Contributor

bors commented Nov 22, 2024

☀️ Try build successful - checks-actions
Build commit: 46c9e3a (46c9e3a092ea03e1b0a4f75f57a4c0997f490346)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (46c9e3a): comparison URL.

Overall result: ❌ regressions - please read the text below

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please fix the regressions and do another perf run. If the next run shows neutral or positive results, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

This is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.

mean range count
Regressions ❌
(primary)
0.3% [0.2%, 0.4%] 5
Regressions ❌
(secondary)
0.3% [0.2%, 0.3%] 13
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.3% [0.2%, 0.4%] 5

Max RSS (memory usage)

Results (primary 2.1%, secondary 1.5%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
2.1% [1.2%, 3.0%] 2
Regressions ❌
(secondary)
1.5% [1.5%, 1.5%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 2.1% [1.2%, 3.0%] 2

Cycles

Results (secondary -2.2%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.2% [-2.4%, -2.0%] 3
All ❌✅ (primary) - - 0

Binary size

Results (primary 0.2%, secondary 0.2%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.2% [0.0%, 0.7%] 49
Regressions ❌
(secondary)
0.2% [0.1%, 0.6%] 18
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.2% [0.0%, 0.7%] 49

Bootstrap: 796.689s -> 796.521s (-0.02%)
Artifact size: 335.98 MiB -> 336.01 MiB (0.01%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Nov 22, 2024
@petrochenkov petrochenkov added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Nov 23, 2024
@ehuss ehuss added the A-edition-2024 Area: The 2024 edition label Dec 16, 2024
@compiler-errors
Copy link
Member

I believe the correct fix for this is #134478

bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 18, 2024
Properly record metavar spans for other expansions other than TT

This properly records metavar spans for nonterminals other than tokentree. This means that we operations like `span.to(other_span)` work correctly for macros. As you can see, other diagnostics involving metavars have improved as a result.

Fixes rust-lang#132908
Alternative to rust-lang#133270

cc `@ehuss`
cc `@petrochenkov`
bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 18, 2024
Properly record metavar spans for other expansions other than TT

This properly records metavar spans for nonterminals other than tokentree. This means that we operations like `span.to(other_span)` work correctly for macros. As you can see, other diagnostics involving metavars have improved as a result.

Fixes rust-lang#132908
Alternative to rust-lang#133270

cc `@ehuss`
cc `@petrochenkov`
bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 18, 2024
Properly record metavar spans for other expansions other than TT

This properly records metavar spans for nonterminals other than tokentree. This means that we operations like `span.to(other_span)` work correctly for macros. As you can see, other diagnostics involving metavars have improved as a result.

Fixes rust-lang#132908
Alternative to rust-lang#133270

cc `@ehuss`
cc `@petrochenkov`
bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 18, 2024
Properly record metavar spans for other expansions other than TT

This properly records metavar spans for nonterminals other than tokentree. This means that we operations like `span.to(other_span)` work correctly for macros. As you can see, other diagnostics involving metavars have improved as a result.

Fixes rust-lang#132908
Alternative to rust-lang#133270

cc `@ehuss`
cc `@petrochenkov`
bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 18, 2024
Properly record metavar spans for other expansions other than TT

This properly records metavar spans for nonterminals other than tokentree. This means that we operations like `span.to(other_span)` work correctly for macros. As you can see, other diagnostics involving metavars have improved as a result.

Fixes rust-lang#132908
Alternative to rust-lang#133270

cc `@ehuss`
cc `@petrochenkov`
bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 21, 2024
Properly record metavar spans for other expansions other than TT

This properly records metavar spans for nonterminals other than tokentree. This means that we operations like `span.to(other_span)` work correctly for macros. As you can see, other diagnostics involving metavars have improved as a result.

Fixes rust-lang#132908
Alternative to rust-lang#133270

cc `@ehuss`
cc `@petrochenkov`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-edition-2024 Area: The 2024 edition perf-regression Performance regression. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

Bad span in suggestion for unsafe_attr_outside_unsafe in macro expansion with cfg_attr
6 participants