-
Notifications
You must be signed in to change notification settings - Fork 13k
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
base: master
Are you sure you want to change the base?
Conversation
rustbot has assigned @petrochenkov. Use |
@@ -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)) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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.
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
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
☀️ Try build successful - checks-actions |
This comment has been minimized.
This comment has been minimized.
Finished benchmarking commit (46c9e3a): comparison URL. Overall result: ❌ regressions - please read the text belowBenchmarking 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 @bors rollup=never Instruction countThis 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.
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.
CyclesResults (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.
Binary sizeResults (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.
Bootstrap: 796.689s -> 796.521s (-0.02%) |
I believe the correct fix for this is #134478 |
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`
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`
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`
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`
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`
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`
This fixes the span of the
unsafe_attr_outside_unsafe
diagnostic when the attribute usescfg_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 fromcfg_attr
, that span starts from the attribute path (likeno_mangle
), not the#[
of thecfg_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 useAttrItem::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