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
This commit is contained in:
commit
d61adbffe1
7 changed files with 59 additions and 4 deletions
|
@ -773,7 +773,7 @@ pub(super) fn expand_global_asm<'cx>(
|
||||||
kind: ast::VisibilityKind::Inherited,
|
kind: ast::VisibilityKind::Inherited,
|
||||||
tokens: None,
|
tokens: None,
|
||||||
},
|
},
|
||||||
span: ecx.with_def_site_ctxt(sp),
|
span: sp,
|
||||||
tokens: None,
|
tokens: None,
|
||||||
})])
|
})])
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -72,8 +72,11 @@ lint_builtin_explicit_outlives = outlives requirements can be inferred
|
||||||
|
|
||||||
lint_builtin_export_name_fn = declaration of a function with `export_name`
|
lint_builtin_export_name_fn = declaration of a function with `export_name`
|
||||||
lint_builtin_export_name_method = declaration of a method with `export_name`
|
lint_builtin_export_name_method = declaration of a method with `export_name`
|
||||||
|
|
||||||
lint_builtin_export_name_static = declaration of a static with `export_name`
|
lint_builtin_export_name_static = declaration of a static with `export_name`
|
||||||
|
|
||||||
|
lint_builtin_global_asm = usage of `core::arch::global_asm`
|
||||||
|
lint_builtin_global_macro_unsafety = using this macro is unsafe even though it does not need an `unsafe` block
|
||||||
|
|
||||||
lint_builtin_impl_unsafe_method = implementation of an `unsafe` method
|
lint_builtin_impl_unsafe_method = implementation of an `unsafe` method
|
||||||
|
|
||||||
lint_builtin_incomplete_features = the feature `{$name}` is incomplete and may not be safe to use and/or cause compiler crashes
|
lint_builtin_incomplete_features = the feature `{$name}` is incomplete and may not be safe to use and/or cause compiler crashes
|
||||||
|
|
|
@ -393,6 +393,10 @@ impl EarlyLintPass for UnsafeCode {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ast::ItemKind::GlobalAsm(..) => {
|
||||||
|
self.report_unsafe(cx, it.span, BuiltinUnsafe::GlobalAsm);
|
||||||
|
}
|
||||||
|
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -114,6 +114,9 @@ pub enum BuiltinUnsafe {
|
||||||
DeclUnsafeMethod,
|
DeclUnsafeMethod,
|
||||||
#[diag(lint_builtin_impl_unsafe_method)]
|
#[diag(lint_builtin_impl_unsafe_method)]
|
||||||
ImplUnsafeMethod,
|
ImplUnsafeMethod,
|
||||||
|
#[diag(lint_builtin_global_asm)]
|
||||||
|
#[note(lint_builtin_global_macro_unsafety)]
|
||||||
|
GlobalAsm,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(LintDiagnostic)]
|
#[derive(LintDiagnostic)]
|
||||||
|
|
|
@ -9,8 +9,6 @@ error[E0472]: inline assembly is unsupported on this target
|
||||||
|
|
|
|
||||||
LL | global_asm!("");
|
LL | global_asm!("");
|
||||||
| ^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^
|
||||||
|
|
|
||||||
= note: this error originates in the macro `global_asm` (in Nightly builds, run with -Z macro-backtrace for more info)
|
|
||||||
|
|
||||||
error: aborting due to 2 previous errors
|
error: aborting due to 2 previous errors
|
||||||
|
|
||||||
|
|
20
tests/ui/lint/unsafe_code/lint-global-asm-as-unsafe.rs
Normal file
20
tests/ui/lint/unsafe_code/lint-global-asm-as-unsafe.rs
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
//@ needs-asm-support
|
||||||
|
#![deny(unsafe_code)]
|
||||||
|
|
||||||
|
use std::arch::global_asm;
|
||||||
|
|
||||||
|
#[allow(unsafe_code)]
|
||||||
|
mod allowed_unsafe {
|
||||||
|
std::arch::global_asm!("");
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! unsafe_in_macro {
|
||||||
|
() => {
|
||||||
|
global_asm!(""); //~ ERROR: usage of `core::arch::global_asm`
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
global_asm!(""); //~ ERROR: usage of `core::arch::global_asm`
|
||||||
|
unsafe_in_macro!();
|
||||||
|
|
||||||
|
fn main() {}
|
27
tests/ui/lint/unsafe_code/lint-global-asm-as-unsafe.stderr
Normal file
27
tests/ui/lint/unsafe_code/lint-global-asm-as-unsafe.stderr
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
error: usage of `core::arch::global_asm`
|
||||||
|
--> $DIR/lint-global-asm-as-unsafe.rs:17:1
|
||||||
|
|
|
||||||
|
LL | global_asm!("");
|
||||||
|
| ^^^^^^^^^^^^^^^
|
||||||
|
|
|
||||||
|
= note: using this macro is unsafe even though it does not need an `unsafe` block
|
||||||
|
note: the lint level is defined here
|
||||||
|
--> $DIR/lint-global-asm-as-unsafe.rs:2:9
|
||||||
|
|
|
||||||
|
LL | #![deny(unsafe_code)]
|
||||||
|
| ^^^^^^^^^^^
|
||||||
|
|
||||||
|
error: usage of `core::arch::global_asm`
|
||||||
|
--> $DIR/lint-global-asm-as-unsafe.rs:13:9
|
||||||
|
|
|
||||||
|
LL | global_asm!("");
|
||||||
|
| ^^^^^^^^^^^^^^^
|
||||||
|
...
|
||||||
|
LL | unsafe_in_macro!();
|
||||||
|
| ------------------ in this macro invocation
|
||||||
|
|
|
||||||
|
= note: using this macro is unsafe even though it does not need an `unsafe` block
|
||||||
|
= note: this error originates in the macro `unsafe_in_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||||
|
|
||||||
|
error: aborting due to 2 previous errors
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue