Skip to content

Commit c728bf3

Browse files
committed
Auto merge of #114656 - bossmc:rework-no-coverage-attr, r=oli-obk
Rework `no_coverage` to `coverage(off)` As discussed at the tail of #84605 this replaces the `no_coverage` attribute with a `coverage` attribute that takes sub-parameters (currently `off` and `on`) to control the coverage instrumentation. Allows future-proofing for things like `coverage(off, reason="Tested live", issue="#12345")` or similar.
2 parents 8142a31 + 0ca6c38 commit c728bf3

File tree

38 files changed

+253
-213
lines changed

38 files changed

+253
-213
lines changed

compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub fn expand_deriving_eq(
3434
attributes: thin_vec![
3535
cx.attr_word(sym::inline, span),
3636
cx.attr_nested_word(sym::doc, sym::hidden, span),
37-
cx.attr_word(sym::no_coverage, span)
37+
cx.attr_nested_word(sym::coverage, sym::off, span)
3838
],
3939
fieldless_variants_strategy: FieldlessVariantsStrategy::Unify,
4040
combine_substructure: combine_substructure(Box::new(|a, b, c| {

compiler/rustc_builtin_macros/src/test_harness.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ fn generate_test_harness(
254254
let expn_id = ext_cx.resolver.expansion_for_ast_pass(
255255
DUMMY_SP,
256256
AstPass::TestHarness,
257-
&[sym::test, sym::rustc_attrs, sym::no_coverage],
257+
&[sym::test, sym::rustc_attrs, sym::coverage_attribute],
258258
None,
259259
);
260260
let def_site = DUMMY_SP.with_def_site_ctxt(expn_id.to_expn_id());
@@ -335,8 +335,8 @@ fn mk_main(cx: &mut TestCtxt<'_>) -> P<ast::Item> {
335335

336336
// #[rustc_main]
337337
let main_attr = ecx.attr_word(sym::rustc_main, sp);
338-
// #[no_coverage]
339-
let no_coverage_attr = ecx.attr_word(sym::no_coverage, sp);
338+
// #[coverage(off)]
339+
let coverage_attr = ecx.attr_nested_word(sym::coverage, sym::off, sp);
340340

341341
// pub fn main() { ... }
342342
let main_ret_ty = ecx.ty(sp, ast::TyKind::Tup(ThinVec::new()));
@@ -366,7 +366,7 @@ fn mk_main(cx: &mut TestCtxt<'_>) -> P<ast::Item> {
366366

367367
let main = P(ast::Item {
368368
ident: main_id,
369-
attrs: thin_vec![main_attr, no_coverage_attr],
369+
attrs: thin_vec![main_attr, coverage_attr],
370370
id: ast::DUMMY_NODE_ID,
371371
kind: main,
372372
vis: ast::Visibility { span: sp, kind: ast::VisibilityKind::Public, tokens: None },

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,10 @@ fn add_unused_functions(cx: &CodegenCx<'_, '_>) {
330330
{
331331
let codegen_fn_attrs = tcx.codegen_fn_attrs(non_codegenned_def_id);
332332

333-
// If a function is marked `#[no_coverage]`, then skip generating a
333+
// If a function is marked `#[coverage(off)]`, then skip generating a
334334
// dead code stub for it.
335335
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NO_COVERAGE) {
336-
debug!("skipping unused fn marked #[no_coverage]: {:?}", non_codegenned_def_id);
336+
debug!("skipping unused fn marked #[coverage(off)]: {:?}", non_codegenned_def_id);
337337
continue;
338338
}
339339

compiler/rustc_codegen_ssa/messages.ftl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ codegen_ssa_erroneous_constant = erroneous constant encountered
2323
2424
codegen_ssa_error_creating_remark_dir = failed to create remark directory: {$error}
2525
26+
codegen_ssa_expected_coverage_symbol = expected `coverage(off)` or `coverage(on)`
27+
2628
codegen_ssa_expected_used_symbol = expected `used`, `used(compiler)` or `used(linker)`
2729
2830
codegen_ssa_extern_funcs_not_found = some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ use rustc_target::spec::{abi, SanitizerSet};
1616

1717
use crate::errors;
1818
use crate::target_features::from_target_feature;
19-
use crate::{errors::ExpectedUsedSymbol, target_features::check_target_feature_trait_unsafe};
19+
use crate::{
20+
errors::{ExpectedCoverageSymbol, ExpectedUsedSymbol},
21+
target_features::check_target_feature_trait_unsafe,
22+
};
2023

2124
fn linkage_by_name(tcx: TyCtxt<'_>, def_id: LocalDefId, name: &str) -> Linkage {
2225
use rustc_middle::mir::mono::Linkage::*;
@@ -128,7 +131,21 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
128131
.emit();
129132
}
130133
}
131-
sym::no_coverage => codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_COVERAGE,
134+
sym::coverage => {
135+
let inner = attr.meta_item_list();
136+
match inner.as_deref() {
137+
Some([item]) if item.has_name(sym::off) => {
138+
codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_COVERAGE;
139+
}
140+
Some([item]) if item.has_name(sym::on) => {
141+
// Allow #[coverage(on)] for being explicit, maybe also in future to enable
142+
// coverage on a smaller scope within an excluded larger scope.
143+
}
144+
Some(_) | None => {
145+
tcx.sess.emit_err(ExpectedCoverageSymbol { span: attr.span });
146+
}
147+
}
148+
}
132149
sym::rustc_std_internal_symbol => {
133150
codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL
134151
}

compiler/rustc_codegen_ssa/src/errors.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,13 @@ pub struct UnknownArchiveKind<'a> {
561561
pub kind: &'a str,
562562
}
563563

564+
#[derive(Diagnostic)]
565+
#[diag(codegen_ssa_expected_coverage_symbol)]
566+
pub struct ExpectedCoverageSymbol {
567+
#[primary_span]
568+
pub span: Span,
569+
}
570+
564571
#[derive(Diagnostic)]
565572
#[diag(codegen_ssa_expected_used_symbol)]
566573
pub struct ExpectedUsedSymbol {
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
A `#[no_coverage]` attribute was applied to something which does not show up
1+
A `#[coverage]` attribute was applied to something which does not show up
22
in code coverage, or is too granular to be excluded from the coverage report.
33

44
For now, this attribute can only be applied to function, method, and closure
@@ -9,18 +9,18 @@ will just emit an `unused_attributes` lint instead of this error.
99
Example of erroneous code:
1010

1111
```compile_fail,E0788
12-
#[no_coverage]
12+
#[coverage(off)]
1313
struct Foo;
1414
15-
#[no_coverage]
15+
#[coverage(on)]
1616
const FOO: Foo = Foo;
1717
```
1818

19-
`#[no_coverage]` tells the compiler to not generate coverage instrumentation for
20-
a piece of code when the `-C instrument-coverage` flag is passed. Things like
21-
structs and consts are not coverable code, and thus cannot do anything with this
22-
attribute.
19+
`#[coverage(off)]` tells the compiler to not generate coverage instrumentation
20+
for a piece of code when the `-C instrument-coverage` flag is passed. Things
21+
like structs and consts are not coverable code, and thus cannot do anything
22+
with this attribute.
2323

2424
If you wish to apply this attribute to all methods in an impl or module,
2525
manually annotate each method; it is not possible to annotate the entire impl
26-
with a `#[no_coverage]` attribute.
26+
with a `#[coverage]` attribute.

compiler/rustc_feature/src/active.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,9 @@ declare_features! (
398398
(active, const_trait_impl, "1.42.0", Some(67792), None),
399399
/// Allows the `?` operator in const contexts.
400400
(active, const_try, "1.56.0", Some(74935), None),
401+
/// Allows function attribute `#[coverage(on/off)]`, to control coverage
402+
/// instrumentation of that function.
403+
(active, coverage_attribute, "CURRENT_RUSTC_VERSION", Some(84605), None),
401404
/// Allows non-builtin attributes in inner attribute position.
402405
(active, custom_inner_attributes, "1.30.0", Some(54726), None),
403406
/// Allows custom test frameworks with `#![test_runner]` and `#[test_case]`.
@@ -509,9 +512,6 @@ declare_features! (
509512
(active, never_type_fallback, "1.41.0", Some(65992), None),
510513
/// Allows `#![no_core]`.
511514
(active, no_core, "1.3.0", Some(29639), None),
512-
/// Allows function attribute `#[no_coverage]`, to bypass coverage
513-
/// instrumentation of that function.
514-
(active, no_coverage, "1.53.0", Some(84605), None),
515515
/// Allows the use of `no_sanitize` attribute.
516516
(active, no_sanitize, "1.42.0", Some(39699), None),
517517
/// Allows using the `non_exhaustive_omitted_patterns` lint.

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
395395
template!(List: "address, kcfi, memory, thread"), DuplicatesOk,
396396
experimental!(no_sanitize)
397397
),
398-
gated!(no_coverage, Normal, template!(Word), WarnFollowing, experimental!(no_coverage)),
398+
gated!(coverage, Normal, template!(Word, List: "on|off"), WarnFollowing, coverage_attribute, experimental!(coverage)),
399399

400400
ungated!(
401401
doc, Normal, template!(List: "hidden|inline|...", NameValueStr: "string"), DuplicatesOk

compiler/rustc_feature/src/removed.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ declare_features! (
136136
Some("subsumed by `#![feature(allocator_internals)]`")),
137137
/// Allows use of unary negate on unsigned integers, e.g., -e for e: u8
138138
(removed, negate_unsigned, "1.0.0", Some(29645), None, None),
139+
/// Allows `#[no_coverage]` on functions.
140+
/// The feature was renamed to `coverage` and the attribute to `#[coverage(on|off)]`
141+
(removed, no_coverage, "CURRENT_RUSTC_VERSION", Some(84605), None, Some("renamed to `coverage`")),
139142
/// Allows `#[no_debug]`.
140143
(removed, no_debug, "1.43.0", Some(29721), None, Some("removed due to lack of demand")),
141144
/// Allows using `#[on_unimplemented(..)]` on traits.

0 commit comments

Comments
 (0)