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

coverage: Make #[coverage(..)] apply recursively to nested functions #126721

Merged
merged 3 commits into from
Jun 27, 2024

Conversation

Zalathar
Copy link
Contributor

@Zalathar Zalathar commented Jun 20, 2024

This PR makes the (currently-unstable) #[coverage(off)] and #[coverage(on)] attributes apply recursively to all nested functions/closures, instead of just the function they are directly attached to.

Those attributes can now also be applied to modules and to impl/impl-trait blocks, where they have no direct effect, but will be inherited by all enclosed functions/closures/methods that don't override the inherited value.


Fixes #126625.

@rustbot
Copy link
Collaborator

rustbot commented Jun 20, 2024

r? @lcnr

rustbot has assigned @lcnr.
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. A-code-coverage Area: Source-based code coverage (-Cinstrument-coverage) labels Jun 20, 2024
@Zalathar
Copy link
Contributor Author

Zalathar commented Jun 20, 2024

Based on this implementation, it should be fairly simple to permit coverage attributes on things like mod and impl, and have them apply to nested functions.

EDIT: It was easy enough that I just did it, so this implementation now also permits coverage attributes on mod and impl.

@bors
Copy link
Contributor

bors commented Jun 24, 2024

☔ The latest upstream changes (presumably #126891) made this pull request unmergeable. Please resolve the merge conflicts.

@Zalathar Zalathar force-pushed the nested-cov-attr branch 2 times, most recently from 522b0bb to f0f220b Compare June 25, 2024 01:34
@Zalathar Zalathar marked this pull request as ready for review June 25, 2024 01:44
@rustbot
Copy link
Collaborator

rustbot commented Jun 25, 2024

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@Zalathar
Copy link
Contributor Author

#126682 has merged, so I've rebased this and marked it as ready.

Comment on lines +71 to +73
// Check the parent def (and so on recursively) until we find an
// enclosing attribute or reach the crate root.
Some(parent) => tcx.coverage_attr_on(parent),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is it fine for this query to just call itself recursively (via the query system) to walk up the def tree, or is there something special I have to do here?

Copy link
Contributor

@clarfonthey clarfonthey Jun 25, 2024

Choose a reason for hiding this comment

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

Presumably it'd make sense to do some sort of path compression/memoisation so that the query is only linear in the number of items, rather than quadratic. But not quite sure what exists to do that in the compiler.

(Each item performs a number of queries equivalent to their number of parents, rather than just one to check their immediate parent which is cached.)

Copy link
Contributor Author

@Zalathar Zalathar Jun 25, 2024

Choose a reason for hiding this comment

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

Given that I'm calling tcx.coverage_attr_on (and not recursing on fn coverage_attr_on directly), my understanding is that the query system will memoize the result.

So individual functions will only recurse until they hit something (typically the enclosing mod/impl) for which there is a previously-memoized result.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yea this is totally fine. We do this for a lot of queries (e.g. generics_of)

@oli-obk
Copy link
Contributor

oli-obk commented Jun 26, 2024

r? @oli-obk

so the intent is for nested modules also to use the attribute from the outer module? This is a new concept in Rust iirc, so maybe add it as an important point on the tracking issue for T-lang to talk about during stabilization

@rustbot rustbot assigned oli-obk and unassigned lcnr Jun 26, 2024
@Zalathar
Copy link
Contributor Author

The current behaviour (scanning all the way up to the crate root) wasn't my original intention, but as I implemented the feature it ended up being (a) easier, and (b) plausibly the right design, so I decided to stick with it until/unless the team decides otherwise.

@oli-obk
Copy link
Contributor

oli-obk commented Jun 26, 2024

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Jun 26, 2024

📌 Commit 7f37f8a has been approved by oli-obk

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-review Status: Awaiting review from the assignee but also interested parties. labels Jun 26, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jun 26, 2024
coverage: Make `#[coverage(..)]` apply recursively to nested functions

This PR makes the (currently-unstable) `#[coverage(off)]` and `#[coverage(on)]` attributes apply recursively to all nested functions/closures, instead of just the function they are directly attached to.

Those attributes can now also be applied to modules and to impl/impl-trait blocks, where they have no direct effect, but will be inherited by all enclosed functions/closures/methods that don't override the inherited value.

---

Fixes rust-lang#126625.
bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 26, 2024
…iaskrgr

Rollup of 9 pull requests

Successful merges:

 - rust-lang#125016 (Update compiler_builtins to 0.1.113)
 - rust-lang#126571 (Less `maybe_whole_expr`, take 2)
 - rust-lang#126721 (coverage: Make `#[coverage(..)]` apply recursively to nested functions)
 - rust-lang#126928 (Some `Nonterminal` removal precursors)
 - rust-lang#126929 (Remove `__rust_force_expr`.)
 - rust-lang#126941 (Migrate `run-make/llvm-ident` to `rmake.rs`)
 - rust-lang#126970 (Simplify `str::clone_into`)
 - rust-lang#126980 (set self.is_known_utf8 to false in extend_from_slice)
 - rust-lang#126983 (Remove `f16` and `f128` ICE paths from smir)

r? `@ghost`
`@rustbot` modify labels: rollup
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jun 26, 2024
coverage: Make `#[coverage(..)]` apply recursively to nested functions

This PR makes the (currently-unstable) `#[coverage(off)]` and `#[coverage(on)]` attributes apply recursively to all nested functions/closures, instead of just the function they are directly attached to.

Those attributes can now also be applied to modules and to impl/impl-trait blocks, where they have no direct effect, but will be inherited by all enclosed functions/closures/methods that don't override the inherited value.

---

Fixes rust-lang#126625.
bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 26, 2024
…iaskrgr

Rollup of 9 pull requests

Successful merges:

 - rust-lang#125016 (Update compiler_builtins to 0.1.113)
 - rust-lang#126571 (Less `maybe_whole_expr`, take 2)
 - rust-lang#126692 (patch `rust-lld` and `ld.lld` on NixOS)
 - rust-lang#126721 (coverage: Make `#[coverage(..)]` apply recursively to nested functions)
 - rust-lang#126928 (Some `Nonterminal` removal precursors)
 - rust-lang#126929 (Remove `__rust_force_expr`.)
 - rust-lang#126970 (Simplify `str::clone_into`)
 - rust-lang#126980 (set self.is_known_utf8 to false in extend_from_slice)
 - rust-lang#126983 (Remove `f16` and `f128` ICE paths from smir)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 27, 2024
Rollup of 6 pull requests

Successful merges:

 - rust-lang#126571 (Less `maybe_whole_expr`, take 2)
 - rust-lang#126721 (coverage: Make `#[coverage(..)]` apply recursively to nested functions)
 - rust-lang#126928 (Some `Nonterminal` removal precursors)
 - rust-lang#126929 (Remove `__rust_force_expr`.)
 - rust-lang#126980 (set self.is_known_utf8 to false in extend_from_slice)
 - rust-lang#126983 (Remove `f16` and `f128` ICE paths from smir)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 70b69a2 into rust-lang:master Jun 27, 2024
6 checks passed
@rustbot rustbot added this to the 1.81.0 milestone Jun 27, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Jun 27, 2024
Rollup merge of rust-lang#126721 - Zalathar:nested-cov-attr, r=oli-obk

coverage: Make `#[coverage(..)]` apply recursively to nested functions

This PR makes the (currently-unstable) `#[coverage(off)]` and `#[coverage(on)]` attributes apply recursively to all nested functions/closures, instead of just the function they are directly attached to.

Those attributes can now also be applied to modules and to impl/impl-trait blocks, where they have no direct effect, but will be inherited by all enclosed functions/closures/methods that don't override the inherited value.

---

Fixes rust-lang#126625.
@Zalathar Zalathar deleted the nested-cov-attr branch June 27, 2024 10:12
Coekjan added a commit to Coekjan/typst-upgrade that referenced this pull request Jul 3, 2024
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.

#[coverage(..)] attribute should apply recursively to nested functions/methods/closures
6 participants