Skip to content

Commit 28819cb

Browse files
committed
Formatting changes + add UI test
1 parent b7360fa commit 28819cb

File tree

3 files changed

+59
-10
lines changed

3 files changed

+59
-10
lines changed

compiler/rustc_mir_transform/src/check_unsafety.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use hir::{BlockCheckMode, ExprKind, Node};
21
use rustc_data_structures::fx::FxHashSet;
32
use rustc_errors::struct_span_err;
43
use rustc_hir as hir;
54
use rustc_hir::def_id::{DefId, LocalDefId};
65
use rustc_hir::hir_id::HirId;
76
use rustc_hir::intravisit;
7+
use rustc_hir::{BlockCheckMode, ExprKind, Node};
88
use rustc_middle::mir::visit::{MutatingUseContext, PlaceContext, Visitor};
99
use rustc_middle::mir::*;
1010
use rustc_middle::ty::query::Providers;
@@ -521,10 +521,7 @@ pub fn check_unsafety(tcx: TyCtxt<'_>, def_id: LocalDefId) {
521521
match kind {
522522
UnsafetyViolationKind::General => {
523523
// once
524-
// Mutable statics always require an unsafe block
525-
let unsafe_fn_msg = if unsafe_op_in_unsafe_fn_allowed(tcx, lint_root)
526-
&& details != UnsafetyViolationDetails::UseOfMutableStatic
527-
{
524+
let unsafe_fn_msg = if unsafe_op_in_unsafe_fn_allowed(tcx, lint_root) {
528525
" function or"
529526
} else {
530527
""
@@ -542,12 +539,14 @@ pub fn check_unsafety(tcx: TyCtxt<'_>, def_id: LocalDefId) {
542539
let note_non_inherited = tcx.hir().parent_iter(lint_root).find(|(id, node)| {
543540
if let Node::Expr(block) = node
544541
&& let ExprKind::Block(block, _) = block.kind
545-
&& let BlockCheckMode::UnsafeBlock(_) = block.rules {
546-
true
547-
}
542+
&& let BlockCheckMode::UnsafeBlock(_) = block.rules
543+
{
544+
true
545+
}
548546
else if let Some(sig) = tcx.hir().fn_sig_by_hir_id(*id)
549-
&& sig.header.is_unsafe() {
550-
true
547+
&& sig.header.is_unsafe()
548+
{
549+
true
551550
} else {
552551
false
553552
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#![allow(unused, dead_code)]
2+
3+
static mut FOO: u64 = 0;
4+
5+
fn static_mod() {
6+
unsafe {static BAR: u64 = FOO;}
7+
//~^ ERROR: use of mutable static is unsafe
8+
//~| NOTE: use of mutable static
9+
//~| NOTE: mutable statics can be mutated by multiple threads
10+
//~| NOTE: items do not inherit unsafety
11+
}
12+
13+
unsafe fn unsafe_call() {}
14+
fn foo() {
15+
unsafe {
16+
//~^ NOTE: items do not inherit unsafety
17+
fn bar() {
18+
unsafe_call();
19+
//~^ ERROR: call to unsafe function
20+
//~| NOTE: call to unsafe function
21+
//~| NOTE: consult the function's documentation
22+
}
23+
}
24+
}
25+
26+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error[E0133]: use of mutable static is unsafe and requires unsafe function or block
2+
--> $DIR/unsafe-not-inherited.rs:6:31
3+
|
4+
LL | unsafe {static BAR: u64 = FOO;}
5+
| ------ ^^^ use of mutable static
6+
| |
7+
| items do not inherit unsafety from separate enclosing items
8+
|
9+
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
10+
11+
error[E0133]: call to unsafe function is unsafe and requires unsafe function or block
12+
--> $DIR/unsafe-not-inherited.rs:18:13
13+
|
14+
LL | unsafe {
15+
| ------ items do not inherit unsafety from separate enclosing items
16+
...
17+
LL | unsafe_call();
18+
| ^^^^^^^^^^^^^ call to unsafe function
19+
|
20+
= note: consult the function's documentation for information on how to avoid undefined behavior
21+
22+
error: aborting due to 2 previous errors
23+
24+
For more information about this error, try `rustc --explain E0133`.

0 commit comments

Comments
 (0)