Skip to content

Commit d7af83b

Browse files
authored
Unrolled build for rust-lang#128552
Rollup merge of rust-lang#128552 - s7tya:check-no-sanitize-attribute-pos, r=BoxyUwU Emit an error for invalid use of the `#[no_sanitize]` attribute fixes rust-lang#128487. Currently, the use of the `#[no_sanitize]` attribute for Mod, Impl,... is incorrectly permitted. This PR will correct this issue by generating errors, and I've also added some UI test cases for it. Referenced rust-lang#128458. As far as I know, the `#[no_sanitize]` attribute can only be used with functions, so I changed that part to `Fn` and `Method` using `check_applied_to_fn_or_method`. However, I couldn't find explicit documentation on this, so I could be mistaken...
2 parents 0d65e5a + 61ea488 commit d7af83b

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, ..] => {
@@ -256,7 +257,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
256257
| sym::may_dangle // FIXME(dropck_eyepatch)
257258
| sym::pointee // FIXME(derive_smart_pointer)
258259
| sym::linkage // FIXME(linkage)
259-
| sym::no_sanitize // FIXME(no_sanitize)
260260
| sym::omit_gdb_pretty_printer_section // FIXME(omit_gdb_pretty_printer_section)
261261
| sym::used // handled elsewhere to restrict to static items
262262
| sym::repr // handled elsewhere to restrict to type decls items
@@ -451,6 +451,11 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
451451
}
452452
}
453453

454+
/// Checks that `#[no_sanitize(..)]` is applied to a function or method.
455+
fn check_no_sanitize(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) {
456+
self.check_applied_to_fn_or_method(hir_id, attr, span, target)
457+
}
458+
454459
fn check_generic_attr(
455460
&self,
456461
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)