Skip to content

coverage: Restrict empty-span expansion to only cover { and } - #132675

Merged
bors merged 5 commits into
rust-lang:masterfrom
Zalathar:empty-spans
Nov 10, 2024
Merged

coverage: Restrict empty-span expansion to only cover { and }#132675
bors merged 5 commits into
rust-lang:masterfrom
Zalathar:empty-spans

Conversation

@Zalathar

@Zalathar Zalathar commented Nov 6, 2024

Copy link
Copy Markdown
Member

Coverage instrumentation has some tricky code for converting a coverage-relevant Span into a set of start/end line/byte-column coordinates that will be embedded in the CGU's coverage metadata.

A big part of this complexity is special code for handling empty spans, which are expanded into non-empty spans (if possible) because LLVM's coverage reporter does not handle empty spans well.

This PR simplifies that code by restricting it to only apply in two specific situations: when the character after the empty span is {, or the character before the empty span is }.

(As an added benefit, this means that the expanded spans no longer extend awkwardly beyond the end of a physical line, which was common under the previous implementation.)

Along the way, this PR also removes some unhelpful code for dealing with function source code spread across multiple files. Functions currently can't have coverage spans in multiple files, and if that ever changes (e.g. to properly support expansion regions) then this code will need to be completely overhauled anyway.

@Zalathar Zalathar added the A-code-coverage Area: Source-based code coverage (-Cinstrument-coverage) label Nov 6, 2024
@rustbot

rustbot commented Nov 6, 2024

Copy link
Copy Markdown
Collaborator

r? @fee1-dead

rustbot has assigned @fee1-dead.
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 6, 2024
@rustbot

rustbot commented Nov 6, 2024

Copy link
Copy Markdown
Collaborator

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@fee1-dead

Copy link
Copy Markdown
Member

r? compiler

@rustbot rustbot assigned pnkfelix and unassigned fee1-dead Nov 6, 2024
@jieyouxu

jieyouxu commented Nov 7, 2024

Copy link
Copy Markdown
Member

r? jieyouxu

@rustbot rustbot assigned jieyouxu and unassigned pnkfelix Nov 7, 2024
Comment thread compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs Outdated
This isn't strictly necessary, but makes it easier to distinguish `body_span`
from the specific `span` being processed.
We already had a dedicated `LocalFileId` index type, but previously we used a
raw `u32` for global file IDs, because index types were harder to pass through
FFI.
Functions currently can't have mappings in multiple files, and if that ever
changes (e.g. to properly support expansion regions), this code will need to be
completely overhauled anyway.

@jieyouxu jieyouxu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, this looks good to me for the most part, two nits.

Comment thread compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs
Comment thread compiler/rustc_codegen_llvm/src/coverageinfo/ffi.rs
@jieyouxu

jieyouxu commented Nov 9, 2024

Copy link
Copy Markdown
Member

@rustbot author

@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 Nov 9, 2024
@Zalathar

Copy link
Copy Markdown
Member Author

Pass a LocalFileId to CoverageSpan::from_source_region (diff).

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 10, 2024
@jieyouxu

Copy link
Copy Markdown
Member

Thanks!

@bors r+

@bors

bors commented Nov 10, 2024

Copy link
Copy Markdown
Collaborator

📌 Commit 925dfc8 has been approved by jieyouxu

It is now in the queue for this repository.

@bors bors 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Nov 10, 2024
@jieyouxu

Copy link
Copy Markdown
Member

@bors r+

@bors

bors commented Nov 10, 2024

Copy link
Copy Markdown
Collaborator

📌 Commit 925dfc8 has been approved by jieyouxu

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 10, 2024
@jieyouxu

Copy link
Copy Markdown
Member

(Yielding to #132846)

bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 10, 2024
coverage: Restrict empty-span expansion to only cover `{` and `}`

Coverage instrumentation has some tricky code for converting a coverage-relevant `Span` into a set of start/end line/byte-column coordinates that will be embedded in the CGU's coverage metadata.

A big part of this complexity is special code for handling empty spans, which are expanded into non-empty spans (if possible) because LLVM's coverage reporter does not handle empty spans well.

This PR simplifies that code by restricting it to only apply in two specific situations: when the character after the empty span is `{`, or the character before the empty span is `}`.

(As an added benefit, this means that the expanded spans no longer extend awkwardly beyond the end of a physical line, which was common under the previous implementation.)

Along the way, this PR also removes some unhelpful code for dealing with function source code spread across multiple files. Functions currently can't have coverage spans in multiple files, and if that ever changes (e.g. to properly support expansion regions) then this code will need to be completely overhauled anyway.
@bors

bors commented Nov 10, 2024

Copy link
Copy Markdown
Collaborator

⌛ Testing commit 925dfc8 with merge 772f79d...

@jieyouxu

Copy link
Copy Markdown
Member

@bors r- retry

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 10, 2024
@jieyouxu

Copy link
Copy Markdown
Member

@bors r+

@bors

bors commented Nov 10, 2024

Copy link
Copy Markdown
Collaborator

📌 Commit 925dfc8 has been approved by jieyouxu

It is now in the queue for this repository.

@bors

bors commented Nov 10, 2024

Copy link
Copy Markdown
Collaborator

🌲 The tree is currently closed for pull requests below priority 1000. This pull request will be tested once the tree is reopened.

bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 10, 2024
…iaskrgr

Rollup of 3 pull requests

Successful merges:

 - rust-lang#132675 (coverage: Restrict empty-span expansion to only cover `{` and `}`)
 - rust-lang#132849 (Miri subtree update)
 - rust-lang#132858 (Update minifer version to `0.3.2`)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors
bors merged commit b95232d into rust-lang:master Nov 10, 2024
@rustbot rustbot added this to the 1.84.0 milestone Nov 10, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Nov 10, 2024
Rollup merge of rust-lang#132675 - Zalathar:empty-spans, r=jieyouxu

coverage: Restrict empty-span expansion to only cover `{` and `}`

Coverage instrumentation has some tricky code for converting a coverage-relevant `Span` into a set of start/end line/byte-column coordinates that will be embedded in the CGU's coverage metadata.

A big part of this complexity is special code for handling empty spans, which are expanded into non-empty spans (if possible) because LLVM's coverage reporter does not handle empty spans well.

This PR simplifies that code by restricting it to only apply in two specific situations: when the character after the empty span is `{`, or the character before the empty span is `}`.

(As an added benefit, this means that the expanded spans no longer extend awkwardly beyond the end of a physical line, which was common under the previous implementation.)

Along the way, this PR also removes some unhelpful code for dealing with function source code spread across multiple files. Functions currently can't have coverage spans in multiple files, and if that ever changes (e.g. to properly support expansion regions) then this code will need to be completely overhauled anyway.
@Zalathar
Zalathar deleted the empty-spans branch November 10, 2024 22:13
mati865 pushed a commit to mati865/rust that referenced this pull request Nov 12, 2024
coverage: Restrict empty-span expansion to only cover `{` and `}`

Coverage instrumentation has some tricky code for converting a coverage-relevant `Span` into a set of start/end line/byte-column coordinates that will be embedded in the CGU's coverage metadata.

A big part of this complexity is special code for handling empty spans, which are expanded into non-empty spans (if possible) because LLVM's coverage reporter does not handle empty spans well.

This PR simplifies that code by restricting it to only apply in two specific situations: when the character after the empty span is `{`, or the character before the empty span is `}`.

(As an added benefit, this means that the expanded spans no longer extend awkwardly beyond the end of a physical line, which was common under the previous implementation.)

Along the way, this PR also removes some unhelpful code for dealing with function source code spread across multiple files. Functions currently can't have coverage spans in multiple files, and if that ever changes (e.g. to properly support expansion regions) then this code will need to be completely overhauled anyway.
mati865 pushed a commit to mati865/rust that referenced this pull request Nov 12, 2024
…iaskrgr

Rollup of 3 pull requests

Successful merges:

 - rust-lang#132675 (coverage: Restrict empty-span expansion to only cover `{` and `}`)
 - rust-lang#132849 (Miri subtree update)
 - rust-lang#132858 (Update minifer version to `0.3.2`)

r? `@ghost`
`@rustbot` modify labels: rollup
github-merge-queue Bot pushed a commit to model-checking/kani that referenced this pull request Nov 13, 2024
Changes required due to
- rust-lang/rust#132675 (coverage: Restrict
empty-span expansion to only cover { and })

By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 and MIT licenses.
alex added a commit to alex/rust that referenced this pull request Jul 6, 2026
…on site

Functions whose bodies are entirely proc-macro-generated (e.g. the impls
from a `#[derive(...)]` macro) have every statement span pointing at the
macro invocation site, because proc-macro output tokens get call-site spans.

Before Rust 1.84, the empty invocation-site spans that survived span
refinement were widened by one column, so the invocation line reported the
generated code's execution count via the entry counter. PR rust-lang#132675 (1.84)
restricted that widening to spans adjacent to '{' or '}', which as a side
effect left such functions either completely uninstrumented, or with a
single leftover sliver region wired to an arbitrary non-entry counter that
reads 0 even when the function runs.

Restore the pre-1.84 semantics deliberately: when a function body is
entirely macro-expanded and its spans point back into the invocation site
(distinguishing proc-macro output from `macro_rules!` definition-site
tokens, which normal refinement still handles), emit a single code region
over the visible invocation site - the derive-macro path, the attribute, or
`name!` - wired to the function's entry counter.

Never-called generated functions get the same region as an unused-function
mapping (count 0); executed siblings from the same invocation share the
span, so llvm-cov's aggregation covers the line. `#[automatically_derived]`
impls remain uninstrumented, as before.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-code-coverage Area: Source-based code coverage (-Cinstrument-coverage) S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

7 participants