Skip to content

Commit a000fa8

Browse files
committed
coverage: Tighten validation of #[coverage(off)] and #[coverage(on)]
1 parent b5dfeba commit a000fa8

File tree

5 files changed

+28
-29
lines changed

5 files changed

+28
-29
lines changed

compiler/rustc_codegen_ssa/messages.ftl

-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ codegen_ssa_create_temp_dir = couldn't create a temp dir: {$error}
2727
2828
codegen_ssa_error_creating_remark_dir = failed to create remark directory: {$error}
2929
30-
codegen_ssa_expected_coverage_symbol = expected `coverage(off)` or `coverage(on)`
31-
3230
codegen_ssa_expected_used_symbol = expected `used`, `used(compiler)` or `used(linker)`
3331
3432
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

+4-7
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@ use rustc_span::{sym, Span};
1515
use rustc_target::spec::{abi, SanitizerSet};
1616

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

2420
fn linkage_by_name(tcx: TyCtxt<'_>, def_id: LocalDefId, name: &str) -> Linkage {
2521
use rustc_middle::mir::mono::Linkage::*;
@@ -139,7 +135,8 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
139135
// coverage on a smaller scope within an excluded larger scope.
140136
}
141137
Some(_) | None => {
142-
tcx.dcx().emit_err(ExpectedCoverageSymbol { span: attr.span });
138+
tcx.dcx()
139+
.span_delayed_bug(attr.span, "unexpected value of coverage attribute");
143140
}
144141
}
145142
}
@@ -174,7 +171,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
174171
codegen_fn_attrs.flags |= CodegenFnAttrFlags::USED;
175172
}
176173
Some(_) => {
177-
tcx.dcx().emit_err(ExpectedUsedSymbol { span: attr.span });
174+
tcx.dcx().emit_err(errors::ExpectedUsedSymbol { span: attr.span });
178175
}
179176
None => {
180177
// Unfortunately, unconditionally using `llvm.used` causes

compiler/rustc_codegen_ssa/src/errors.rs

-7
Original file line numberDiff line numberDiff line change
@@ -564,13 +564,6 @@ pub struct UnknownArchiveKind<'a> {
564564
pub kind: &'a str,
565565
}
566566

567-
#[derive(Diagnostic)]
568-
#[diag(codegen_ssa_expected_coverage_symbol)]
569-
pub struct ExpectedCoverageSymbol {
570-
#[primary_span]
571-
pub span: Span,
572-
}
573-
574567
#[derive(Diagnostic)]
575568
#[diag(codegen_ssa_expected_used_symbol)]
576569
pub struct ExpectedUsedSymbol {

compiler/rustc_feature/src/builtin_attrs.rs

+14-10
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ pub struct AttributeTemplate {
105105
pub word: bool,
106106
/// If `Some`, the attribute is allowed to take a list of items like `#[allow(..)]`.
107107
pub list: Option<&'static str>,
108+
/// If non-empty, the attribute is allowed to take a list containing exactly
109+
/// one of the listed words, like `#[coverage(off)]`.
110+
pub one_of: &'static [Symbol],
108111
/// If `Some`, the attribute is allowed to be a name/value pair where the
109112
/// value is a string, like `#[must_use = "reason"]`.
110113
pub name_value_str: Option<&'static str>,
@@ -165,19 +168,20 @@ pub enum AttributeDuplicates {
165168
/// E.g., `template!(Word, List: "description")` means that the attribute
166169
/// supports forms `#[attr]` and `#[attr(description)]`.
167170
macro_rules! template {
168-
(Word) => { template!(@ true, None, None) };
169-
(List: $descr: expr) => { template!(@ false, Some($descr), None) };
170-
(NameValueStr: $descr: expr) => { template!(@ false, None, Some($descr)) };
171-
(Word, List: $descr: expr) => { template!(@ true, Some($descr), None) };
172-
(Word, NameValueStr: $descr: expr) => { template!(@ true, None, Some($descr)) };
171+
(Word) => { template!(@ true, None, &[], None) };
172+
(List: $descr: expr) => { template!(@ false, Some($descr), &[], None) };
173+
(OneOf: $one_of: expr) => { template!(@ false, None, $one_of, None) };
174+
(NameValueStr: $descr: expr) => { template!(@ false, None, &[], Some($descr)) };
175+
(Word, List: $descr: expr) => { template!(@ true, Some($descr), &[], None) };
176+
(Word, NameValueStr: $descr: expr) => { template!(@ true, None, &[], Some($descr)) };
173177
(List: $descr1: expr, NameValueStr: $descr2: expr) => {
174-
template!(@ false, Some($descr1), Some($descr2))
178+
template!(@ false, Some($descr1), &[], Some($descr2))
175179
};
176180
(Word, List: $descr1: expr, NameValueStr: $descr2: expr) => {
177-
template!(@ true, Some($descr1), Some($descr2))
181+
template!(@ true, Some($descr1), &[], Some($descr2))
178182
};
179-
(@ $word: expr, $list: expr, $name_value_str: expr) => { AttributeTemplate {
180-
word: $word, list: $list, name_value_str: $name_value_str
183+
(@ $word: expr, $list: expr, $one_of: expr, $name_value_str: expr) => { AttributeTemplate {
184+
word: $word, list: $list, one_of: $one_of, name_value_str: $name_value_str
181185
} };
182186
}
183187

@@ -478,7 +482,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
478482
EncodeCrossCrate::No, experimental!(no_sanitize)
479483
),
480484
gated!(
481-
coverage, Normal, template!(Word, List: "on|off"),
485+
coverage, Normal, template!(OneOf: &[sym::off, sym::on]),
482486
ErrorPreceding, EncodeCrossCrate::No,
483487
coverage_attribute, experimental!(coverage)
484488
),

compiler/rustc_parse/src/validate_attr.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ use crate::{errors, parse_in};
44

55
use rustc_ast::token::Delimiter;
66
use rustc_ast::tokenstream::DelimSpan;
7-
use rustc_ast::MetaItemKind;
8-
use rustc_ast::{self as ast, AttrArgs, AttrArgsEq, Attribute, DelimArgs, MetaItem, Safety};
7+
use rustc_ast::{
8+
self as ast, AttrArgs, AttrArgsEq, Attribute, DelimArgs, MetaItem, MetaItemKind,
9+
NestedMetaItem, Safety,
10+
};
911
use rustc_errors::{Applicability, FatalError, PResult};
1012
use rustc_feature::{
1113
AttributeSafety, AttributeTemplate, BuiltinAttribute, Features, BUILTIN_ATTRIBUTE_MAP,
@@ -184,9 +186,13 @@ pub(super) fn check_cfg_attr_bad_delim(psess: &ParseSess, span: DelimSpan, delim
184186

185187
/// Checks that the given meta-item is compatible with this `AttributeTemplate`.
186188
fn is_attr_template_compatible(template: &AttributeTemplate, meta: &ast::MetaItemKind) -> bool {
189+
let is_one_allowed_subword = |items: &[NestedMetaItem]| match items {
190+
[item] => item.is_word() && template.one_of.iter().any(|&word| item.has_name(word)),
191+
_ => false,
192+
};
187193
match meta {
188194
MetaItemKind::Word => template.word,
189-
MetaItemKind::List(..) => template.list.is_some(),
195+
MetaItemKind::List(items) => template.list.is_some() || is_one_allowed_subword(items),
190196
MetaItemKind::NameValue(lit) if lit.kind.is_str() => template.name_value_str.is_some(),
191197
MetaItemKind::NameValue(..) => false,
192198
}
@@ -230,6 +236,7 @@ fn emit_malformed_attribute(
230236
if let Some(descr) = template.list {
231237
suggestions.push(format!("#{inner}[{name}({descr})]"));
232238
}
239+
suggestions.extend(template.one_of.iter().map(|&word| format!("#{inner}[{name}({word})]")));
233240
if let Some(descr) = template.name_value_str {
234241
suggestions.push(format!("#{inner}[{name} = \"{descr}\"]"));
235242
}

0 commit comments

Comments
 (0)