Skip to content

Commit 61ea488

Browse files
committed
Emit an error for invalid use of the #[no_sanitize] attribute
1 parent 4d48a6b commit 61ea488

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed

compiler/rustc_passes/src/check_attr.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
125125
[sym::inline, ..] => self.check_inline(hir_id, attr, span, target),
126126
[sym::coverage, ..] => self.check_coverage(attr, span, target),
127127
[sym::optimize, ..] => self.check_optimize(hir_id, attr, target),
128+
[sym::no_sanitize, ..] => self.check_no_sanitize(hir_id, attr, span, target),
128129
[sym::non_exhaustive, ..] => self.check_non_exhaustive(hir_id, attr, span, target),
129130
[sym::marker, ..] => self.check_marker(hir_id, attr, span, target),
130131
[sym::target_feature, ..] => {
@@ -255,7 +256,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
255256
| sym::may_dangle // FIXME(dropck_eyepatch)
256257
| sym::pointee // FIXME(derive_smart_pointer)
257258
| sym::linkage // FIXME(linkage)
258-
| sym::no_sanitize // FIXME(no_sanitize)
259259
| sym::omit_gdb_pretty_printer_section // FIXME(omit_gdb_pretty_printer_section)
260260
| sym::used // handled elsewhere to restrict to static items
261261
| sym::repr // handled elsewhere to restrict to type decls items
@@ -450,6 +450,11 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
450450
}
451451
}
452452

453+
/// Checks that `#[no_sanitize(..)]` is applied to a function or method.
454+
fn check_no_sanitize(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) {
455+
self.check_applied_to_fn_or_method(hir_id, attr, span, target)
456+
}
457+
453458
fn check_generic_attr(
454459
&self,
455460
hir_id: HirId,

tests/ui/attributes/no-sanitize.rs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#![feature(no_sanitize)]
2+
#![feature(stmt_expr_attributes)]
3+
#![deny(unused_attributes)]
4+
#![allow(dead_code)]
5+
6+
fn invalid() {
7+
#[no_sanitize(memory)] //~ ERROR attribute should be applied to a function definition
8+
{
9+
1
10+
};
11+
}
12+
13+
#[no_sanitize(memory)] //~ ERROR attribute should be applied to a function definition
14+
type InvalidTy = ();
15+
16+
#[no_sanitize(memory)] //~ ERROR attribute should be applied to a function definition
17+
mod invalid_module {}
18+
19+
fn main() {
20+
let _ = #[no_sanitize(memory)] //~ ERROR attribute should be applied to a function definition
21+
(|| 1);
22+
}
23+
24+
#[no_sanitize(memory)] //~ ERROR attribute should be applied to a function definition
25+
struct F;
26+
27+
#[no_sanitize(memory)] //~ ERROR attribute should be applied to a function definition
28+
impl F {
29+
#[no_sanitize(memory)]
30+
fn valid(&self) {}
31+
}
32+
33+
#[no_sanitize(memory)]
34+
fn valid() {}
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
error: attribute should be applied to a function definition
2+
--> $DIR/no-sanitize.rs:7:5
3+
|
4+
LL | #[no_sanitize(memory)]
5+
| ^^^^^^^^^^^^^^^^^^^^^^
6+
LL | / {
7+
LL | | 1
8+
LL | | };
9+
| |_____- not a function definition
10+
11+
error: attribute should be applied to a function definition
12+
--> $DIR/no-sanitize.rs:13:1
13+
|
14+
LL | #[no_sanitize(memory)]
15+
| ^^^^^^^^^^^^^^^^^^^^^^
16+
LL | type InvalidTy = ();
17+
| -------------------- not a function definition
18+
19+
error: attribute should be applied to a function definition
20+
--> $DIR/no-sanitize.rs:16:1
21+
|
22+
LL | #[no_sanitize(memory)]
23+
| ^^^^^^^^^^^^^^^^^^^^^^
24+
LL | mod invalid_module {}
25+
| --------------------- not a function definition
26+
27+
error: attribute should be applied to a function definition
28+
--> $DIR/no-sanitize.rs:20:13
29+
|
30+
LL | let _ = #[no_sanitize(memory)]
31+
| ^^^^^^^^^^^^^^^^^^^^^^
32+
LL | (|| 1);
33+
| ------ not a function definition
34+
35+
error: attribute should be applied to a function definition
36+
--> $DIR/no-sanitize.rs:24:1
37+
|
38+
LL | #[no_sanitize(memory)]
39+
| ^^^^^^^^^^^^^^^^^^^^^^
40+
LL | struct F;
41+
| --------- not a function definition
42+
43+
error: attribute should be applied to a function definition
44+
--> $DIR/no-sanitize.rs:27:1
45+
|
46+
LL | #[no_sanitize(memory)]
47+
| ^^^^^^^^^^^^^^^^^^^^^^
48+
LL | / impl F {
49+
LL | | #[no_sanitize(memory)]
50+
LL | | fn valid(&self) {}
51+
LL | | }
52+
| |_- not a function definition
53+
54+
error: aborting due to 6 previous errors
55+

0 commit comments

Comments
 (0)