Skip to content

Commit d61adbf

Browse files
authored
Rollup merge of #121318 - kadiwa4:no_assembly_in_supposedly_safe_code, r=Nilstrieb
Trigger `unsafe_code` lint on invocations of `global_asm` `unsafe_code` already warns about things that don't involve the `unsafe` keyword, e.g. `#[no_mangle]`. This makes it warn on `core::arch::global_asm` too. Fixes #103078
2 parents 739f83d + dc7a016 commit d61adbf

File tree

7 files changed

+59
-4
lines changed

7 files changed

+59
-4
lines changed

Diff for: compiler/rustc_builtin_macros/src/asm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ pub(super) fn expand_global_asm<'cx>(
773773
kind: ast::VisibilityKind::Inherited,
774774
tokens: None,
775775
},
776-
span: ecx.with_def_site_ctxt(sp),
776+
span: sp,
777777
tokens: None,
778778
})])
779779
} else {

Diff for: compiler/rustc_lint/messages.ftl

+4-1
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,11 @@ lint_builtin_explicit_outlives = outlives requirements can be inferred
7272
7373
lint_builtin_export_name_fn = declaration of a function with `export_name`
7474
lint_builtin_export_name_method = declaration of a method with `export_name`
75-
7675
lint_builtin_export_name_static = declaration of a static with `export_name`
76+
77+
lint_builtin_global_asm = usage of `core::arch::global_asm`
78+
lint_builtin_global_macro_unsafety = using this macro is unsafe even though it does not need an `unsafe` block
79+
7780
lint_builtin_impl_unsafe_method = implementation of an `unsafe` method
7881
7982
lint_builtin_incomplete_features = the feature `{$name}` is incomplete and may not be safe to use and/or cause compiler crashes

Diff for: compiler/rustc_lint/src/builtin.rs

+4
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,10 @@ impl EarlyLintPass for UnsafeCode {
393393
}
394394
}
395395

396+
ast::ItemKind::GlobalAsm(..) => {
397+
self.report_unsafe(cx, it.span, BuiltinUnsafe::GlobalAsm);
398+
}
399+
396400
_ => {}
397401
}
398402
}

Diff for: compiler/rustc_lint/src/lints.rs

+3
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ pub enum BuiltinUnsafe {
114114
DeclUnsafeMethod,
115115
#[diag(lint_builtin_impl_unsafe_method)]
116116
ImplUnsafeMethod,
117+
#[diag(lint_builtin_global_asm)]
118+
#[note(lint_builtin_global_macro_unsafety)]
119+
GlobalAsm,
117120
}
118121

119122
#[derive(LintDiagnostic)]

Diff for: tests/ui/asm/bad-arch.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ error[E0472]: inline assembly is unsupported on this target
99
|
1010
LL | global_asm!("");
1111
| ^^^^^^^^^^^^^^^
12-
|
13-
= note: this error originates in the macro `global_asm` (in Nightly builds, run with -Z macro-backtrace for more info)
1412

1513
error: aborting due to 2 previous errors
1614

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//@ needs-asm-support
2+
#![deny(unsafe_code)]
3+
4+
use std::arch::global_asm;
5+
6+
#[allow(unsafe_code)]
7+
mod allowed_unsafe {
8+
std::arch::global_asm!("");
9+
}
10+
11+
macro_rules! unsafe_in_macro {
12+
() => {
13+
global_asm!(""); //~ ERROR: usage of `core::arch::global_asm`
14+
};
15+
}
16+
17+
global_asm!(""); //~ ERROR: usage of `core::arch::global_asm`
18+
unsafe_in_macro!();
19+
20+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error: usage of `core::arch::global_asm`
2+
--> $DIR/lint-global-asm-as-unsafe.rs:17:1
3+
|
4+
LL | global_asm!("");
5+
| ^^^^^^^^^^^^^^^
6+
|
7+
= note: using this macro is unsafe even though it does not need an `unsafe` block
8+
note: the lint level is defined here
9+
--> $DIR/lint-global-asm-as-unsafe.rs:2:9
10+
|
11+
LL | #![deny(unsafe_code)]
12+
| ^^^^^^^^^^^
13+
14+
error: usage of `core::arch::global_asm`
15+
--> $DIR/lint-global-asm-as-unsafe.rs:13:9
16+
|
17+
LL | global_asm!("");
18+
| ^^^^^^^^^^^^^^^
19+
...
20+
LL | unsafe_in_macro!();
21+
| ------------------ in this macro invocation
22+
|
23+
= note: using this macro is unsafe even though it does not need an `unsafe` block
24+
= note: this error originates in the macro `unsafe_in_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
25+
26+
error: aborting due to 2 previous errors
27+

0 commit comments

Comments
 (0)