Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a2ffbeb

Browse files
committedNov 6, 2023
Auto merge of #117229 - matthewjasper:thir-unsafeck-fixes, r=cjgillot
Thir unsafeck fixes - Recognise thread local statics in THIR unsafeck - Add suggestion for unsafe_op_in_unsafe_fn - Fix unsafe checking of let expressions
2 parents aea82b2 + 868de8e commit a2ffbeb

24 files changed

+489
-85
lines changed
 

‎compiler/rustc_hir/src/hir.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3566,6 +3566,15 @@ impl<'hir> OwnerNode<'hir> {
35663566
}
35673567
}
35683568

3569+
pub fn fn_sig(self) -> Option<&'hir FnSig<'hir>> {
3570+
match self {
3571+
OwnerNode::TraitItem(TraitItem { kind: TraitItemKind::Fn(fn_sig, _), .. })
3572+
| OwnerNode::ImplItem(ImplItem { kind: ImplItemKind::Fn(fn_sig, _), .. })
3573+
| OwnerNode::Item(Item { kind: ItemKind::Fn(fn_sig, _, _), .. }) => Some(fn_sig),
3574+
_ => None,
3575+
}
3576+
}
3577+
35693578
pub fn fn_decl(self) -> Option<&'hir FnDecl<'hir>> {
35703579
match self {
35713580
OwnerNode::TraitItem(TraitItem { kind: TraitItemKind::Fn(fn_sig, _), .. })

‎compiler/rustc_middle/src/thir/visit.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ pub fn walk_expr<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, expr: &Exp
6666
Use { source } => visitor.visit_expr(&visitor.thir()[source]),
6767
NeverToAny { source } => visitor.visit_expr(&visitor.thir()[source]),
6868
PointerCoercion { source, cast: _ } => visitor.visit_expr(&visitor.thir()[source]),
69-
Let { expr, .. } => {
69+
Let { expr, ref pat } => {
7070
visitor.visit_expr(&visitor.thir()[expr]);
71+
visitor.visit_pat(pat);
7172
}
7273
Loop { body } => visitor.visit_expr(&visitor.thir()[body]),
7374
Match { scrutinee, ref arms, .. } => {

‎compiler/rustc_mir_build/messages.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ mir_build_unreachable_pattern = unreachable pattern
320320
.label = unreachable pattern
321321
.catchall_label = matches any value
322322
323+
mir_build_unsafe_fn_safe_body = an unsafe function restricts its caller, but its body is safe by default
323324
mir_build_unsafe_not_inherited = items do not inherit unsafety from separate enclosing items
324325
325326
mir_build_unsafe_op_in_unsafe_fn_borrow_of_layout_constrained_field_requires_unsafe =
@@ -386,3 +387,5 @@ mir_build_unused_unsafe = unnecessary `unsafe` block
386387
mir_build_unused_unsafe_enclosing_block_label = because it's nested under this `unsafe` block
387388
388389
mir_build_variant_defined_here = not covered
390+
391+
mir_build_wrap_suggestion = consider wrapping the function body in an unsafe block

‎compiler/rustc_mir_build/src/check_unsafety.rs

Lines changed: 69 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ struct UnsafetyVisitor<'a, 'tcx> {
3535
param_env: ParamEnv<'tcx>,
3636
inside_adt: bool,
3737
warnings: &'a mut Vec<UnusedUnsafeWarning>,
38+
39+
/// Flag to ensure that we only suggest wrapping the entire function body in
40+
/// an unsafe block once.
41+
suggest_unsafe_block: bool,
3842
}
3943

4044
impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
@@ -95,7 +99,13 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
9599
SafetyContext::UnsafeFn if unsafe_op_in_unsafe_fn_allowed => {}
96100
SafetyContext::UnsafeFn => {
97101
// unsafe_op_in_unsafe_fn is disallowed
98-
kind.emit_unsafe_op_in_unsafe_fn_lint(self.tcx, self.hir_context, span);
102+
kind.emit_unsafe_op_in_unsafe_fn_lint(
103+
self.tcx,
104+
self.hir_context,
105+
span,
106+
self.suggest_unsafe_block,
107+
);
108+
self.suggest_unsafe_block = false;
99109
}
100110
SafetyContext::Safe => {
101111
kind.emit_requires_unsafe_err(
@@ -297,6 +307,7 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
297307
}
298308
PatKind::InlineConstant { def, .. } => {
299309
self.visit_inner_body(*def);
310+
visit::walk_pat(self, pat);
300311
}
301312
_ => {
302313
visit::walk_pat(self, pat);
@@ -394,7 +405,9 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
394405
}
395406
}
396407
ExprKind::Deref { arg } => {
397-
if let ExprKind::StaticRef { def_id, .. } = self.thir[arg].kind {
408+
if let ExprKind::StaticRef { def_id, .. } | ExprKind::ThreadLocalRef(def_id) =
409+
self.thir[arg].kind
410+
{
398411
if self.tcx.is_mutable_static(def_id) {
399412
self.requires_unsafe(expr.span, UseOfMutableStatic);
400413
} else if self.tcx.is_foreign_item(def_id) {
@@ -482,14 +495,6 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
482495
}
483496
}
484497
}
485-
ExprKind::Let { expr: expr_id, .. } => {
486-
let let_expr = &self.thir[expr_id];
487-
if let ty::Adt(adt_def, _) = let_expr.ty.kind()
488-
&& adt_def.is_union()
489-
{
490-
self.requires_unsafe(expr.span, AccessToUnionField);
491-
}
492-
}
493498
_ => {}
494499
}
495500
visit::walk_expr(self, expr);
@@ -543,7 +548,22 @@ impl UnsafeOpKind {
543548
tcx: TyCtxt<'_>,
544549
hir_id: hir::HirId,
545550
span: Span,
551+
suggest_unsafe_block: bool,
546552
) {
553+
let parent_id = tcx.hir().get_parent_item(hir_id);
554+
let parent_owner = tcx.hir().owner(parent_id);
555+
let should_suggest = parent_owner.fn_sig().map_or(false, |sig| sig.header.is_unsafe());
556+
let unsafe_not_inherited_note = if should_suggest {
557+
suggest_unsafe_block.then(|| {
558+
let body_span = tcx.hir().body(parent_owner.body_id().unwrap()).value.span;
559+
UnsafeNotInheritedLintNote {
560+
signature_span: tcx.def_span(parent_id.def_id),
561+
body_span,
562+
}
563+
})
564+
} else {
565+
None
566+
};
547567
// FIXME: ideally we would want to trim the def paths, but this is not
548568
// feasible with the current lint emission API (see issue #106126).
549569
match self {
@@ -554,61 +574,89 @@ impl UnsafeOpKind {
554574
UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafe {
555575
span,
556576
function: &with_no_trimmed_paths!(tcx.def_path_str(*did)),
577+
unsafe_not_inherited_note,
557578
},
558579
),
559580
CallToUnsafeFunction(None) => tcx.emit_spanned_lint(
560581
UNSAFE_OP_IN_UNSAFE_FN,
561582
hir_id,
562583
span,
563-
UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafeNameless { span },
584+
UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafeNameless {
585+
span,
586+
unsafe_not_inherited_note,
587+
},
564588
),
565589
UseOfInlineAssembly => tcx.emit_spanned_lint(
566590
UNSAFE_OP_IN_UNSAFE_FN,
567591
hir_id,
568592
span,
569-
UnsafeOpInUnsafeFnUseOfInlineAssemblyRequiresUnsafe { span },
593+
UnsafeOpInUnsafeFnUseOfInlineAssemblyRequiresUnsafe {
594+
span,
595+
unsafe_not_inherited_note,
596+
},
570597
),
571598
InitializingTypeWith => tcx.emit_spanned_lint(
572599
UNSAFE_OP_IN_UNSAFE_FN,
573600
hir_id,
574601
span,
575-
UnsafeOpInUnsafeFnInitializingTypeWithRequiresUnsafe { span },
602+
UnsafeOpInUnsafeFnInitializingTypeWithRequiresUnsafe {
603+
span,
604+
unsafe_not_inherited_note,
605+
},
576606
),
577607
UseOfMutableStatic => tcx.emit_spanned_lint(
578608
UNSAFE_OP_IN_UNSAFE_FN,
579609
hir_id,
580610
span,
581-
UnsafeOpInUnsafeFnUseOfMutableStaticRequiresUnsafe { span },
611+
UnsafeOpInUnsafeFnUseOfMutableStaticRequiresUnsafe {
612+
span,
613+
unsafe_not_inherited_note,
614+
},
582615
),
583616
UseOfExternStatic => tcx.emit_spanned_lint(
584617
UNSAFE_OP_IN_UNSAFE_FN,
585618
hir_id,
586619
span,
587-
UnsafeOpInUnsafeFnUseOfExternStaticRequiresUnsafe { span },
620+
UnsafeOpInUnsafeFnUseOfExternStaticRequiresUnsafe {
621+
span,
622+
unsafe_not_inherited_note,
623+
},
588624
),
589625
DerefOfRawPointer => tcx.emit_spanned_lint(
590626
UNSAFE_OP_IN_UNSAFE_FN,
591627
hir_id,
592628
span,
593-
UnsafeOpInUnsafeFnDerefOfRawPointerRequiresUnsafe { span },
629+
UnsafeOpInUnsafeFnDerefOfRawPointerRequiresUnsafe {
630+
span,
631+
unsafe_not_inherited_note,
632+
},
594633
),
595634
AccessToUnionField => tcx.emit_spanned_lint(
596635
UNSAFE_OP_IN_UNSAFE_FN,
597636
hir_id,
598637
span,
599-
UnsafeOpInUnsafeFnAccessToUnionFieldRequiresUnsafe { span },
638+
UnsafeOpInUnsafeFnAccessToUnionFieldRequiresUnsafe {
639+
span,
640+
unsafe_not_inherited_note,
641+
},
600642
),
601643
MutationOfLayoutConstrainedField => tcx.emit_spanned_lint(
602644
UNSAFE_OP_IN_UNSAFE_FN,
603645
hir_id,
604646
span,
605-
UnsafeOpInUnsafeFnMutationOfLayoutConstrainedFieldRequiresUnsafe { span },
647+
UnsafeOpInUnsafeFnMutationOfLayoutConstrainedFieldRequiresUnsafe {
648+
span,
649+
unsafe_not_inherited_note,
650+
},
606651
),
607652
BorrowOfLayoutConstrainedField => tcx.emit_spanned_lint(
608653
UNSAFE_OP_IN_UNSAFE_FN,
609654
hir_id,
610655
span,
611-
UnsafeOpInUnsafeFnBorrowOfLayoutConstrainedFieldRequiresUnsafe { span },
656+
UnsafeOpInUnsafeFnBorrowOfLayoutConstrainedFieldRequiresUnsafe {
657+
span,
658+
unsafe_not_inherited_note,
659+
},
612660
),
613661
CallToFunctionWith(did) => tcx.emit_spanned_lint(
614662
UNSAFE_OP_IN_UNSAFE_FN,
@@ -617,6 +665,7 @@ impl UnsafeOpKind {
617665
UnsafeOpInUnsafeFnCallToFunctionWithRequiresUnsafe {
618666
span,
619667
function: &with_no_trimmed_paths!(tcx.def_path_str(*did)),
668+
unsafe_not_inherited_note,
620669
},
621670
),
622671
}
@@ -831,6 +880,7 @@ pub fn thir_check_unsafety(tcx: TyCtxt<'_>, def: LocalDefId) {
831880
param_env: tcx.param_env(def),
832881
inside_adt: false,
833882
warnings: &mut warnings,
883+
suggest_unsafe_block: true,
834884
};
835885
visitor.visit_expr(&thir[expr]);
836886

‎compiler/rustc_mir_build/src/errors.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ pub struct UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafe<'a> {
2929
#[label]
3030
pub span: Span,
3131
pub function: &'a str,
32+
#[subdiagnostic]
33+
pub unsafe_not_inherited_note: Option<UnsafeNotInheritedLintNote>,
3234
}
3335

3436
#[derive(LintDiagnostic)]
@@ -37,6 +39,8 @@ pub struct UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafe<'a> {
3739
pub struct UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafeNameless {
3840
#[label]
3941
pub span: Span,
42+
#[subdiagnostic]
43+
pub unsafe_not_inherited_note: Option<UnsafeNotInheritedLintNote>,
4044
}
4145

4246
#[derive(LintDiagnostic)]
@@ -45,6 +49,8 @@ pub struct UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafeNameless {
4549
pub struct UnsafeOpInUnsafeFnUseOfInlineAssemblyRequiresUnsafe {
4650
#[label]
4751
pub span: Span,
52+
#[subdiagnostic]
53+
pub unsafe_not_inherited_note: Option<UnsafeNotInheritedLintNote>,
4854
}
4955

5056
#[derive(LintDiagnostic)]
@@ -53,6 +59,8 @@ pub struct UnsafeOpInUnsafeFnUseOfInlineAssemblyRequiresUnsafe {
5359
pub struct UnsafeOpInUnsafeFnInitializingTypeWithRequiresUnsafe {
5460
#[label]
5561
pub span: Span,
62+
#[subdiagnostic]
63+
pub unsafe_not_inherited_note: Option<UnsafeNotInheritedLintNote>,
5664
}
5765

5866
#[derive(LintDiagnostic)]
@@ -61,6 +69,8 @@ pub struct UnsafeOpInUnsafeFnInitializingTypeWithRequiresUnsafe {
6169
pub struct UnsafeOpInUnsafeFnUseOfMutableStaticRequiresUnsafe {
6270
#[label]
6371
pub span: Span,
72+
#[subdiagnostic]
73+
pub unsafe_not_inherited_note: Option<UnsafeNotInheritedLintNote>,
6474
}
6575

6676
#[derive(LintDiagnostic)]
@@ -69,6 +79,8 @@ pub struct UnsafeOpInUnsafeFnUseOfMutableStaticRequiresUnsafe {
6979
pub struct UnsafeOpInUnsafeFnUseOfExternStaticRequiresUnsafe {
7080
#[label]
7181
pub span: Span,
82+
#[subdiagnostic]
83+
pub unsafe_not_inherited_note: Option<UnsafeNotInheritedLintNote>,
7284
}
7385

7486
#[derive(LintDiagnostic)]
@@ -77,6 +89,8 @@ pub struct UnsafeOpInUnsafeFnUseOfExternStaticRequiresUnsafe {
7789
pub struct UnsafeOpInUnsafeFnDerefOfRawPointerRequiresUnsafe {
7890
#[label]
7991
pub span: Span,
92+
#[subdiagnostic]
93+
pub unsafe_not_inherited_note: Option<UnsafeNotInheritedLintNote>,
8094
}
8195

8296
#[derive(LintDiagnostic)]
@@ -85,6 +99,8 @@ pub struct UnsafeOpInUnsafeFnDerefOfRawPointerRequiresUnsafe {
8599
pub struct UnsafeOpInUnsafeFnAccessToUnionFieldRequiresUnsafe {
86100
#[label]
87101
pub span: Span,
102+
#[subdiagnostic]
103+
pub unsafe_not_inherited_note: Option<UnsafeNotInheritedLintNote>,
88104
}
89105

90106
#[derive(LintDiagnostic)]
@@ -93,13 +109,17 @@ pub struct UnsafeOpInUnsafeFnAccessToUnionFieldRequiresUnsafe {
93109
pub struct UnsafeOpInUnsafeFnMutationOfLayoutConstrainedFieldRequiresUnsafe {
94110
#[label]
95111
pub span: Span,
112+
#[subdiagnostic]
113+
pub unsafe_not_inherited_note: Option<UnsafeNotInheritedLintNote>,
96114
}
97115

98116
#[derive(LintDiagnostic)]
99117
#[diag(mir_build_unsafe_op_in_unsafe_fn_borrow_of_layout_constrained_field_requires_unsafe)]
100118
pub struct UnsafeOpInUnsafeFnBorrowOfLayoutConstrainedFieldRequiresUnsafe {
101119
#[label]
102120
pub span: Span,
121+
#[subdiagnostic]
122+
pub unsafe_not_inherited_note: Option<UnsafeNotInheritedLintNote>,
103123
}
104124

105125
#[derive(LintDiagnostic)]
@@ -109,6 +129,8 @@ pub struct UnsafeOpInUnsafeFnCallToFunctionWithRequiresUnsafe<'a> {
109129
#[label]
110130
pub span: Span,
111131
pub function: &'a str,
132+
#[subdiagnostic]
133+
pub unsafe_not_inherited_note: Option<UnsafeNotInheritedLintNote>,
112134
}
113135

114136
#[derive(Diagnostic)]
@@ -376,6 +398,27 @@ pub struct UnsafeNotInheritedNote {
376398
pub span: Span,
377399
}
378400

401+
pub struct UnsafeNotInheritedLintNote {
402+
pub signature_span: Span,
403+
pub body_span: Span,
404+
}
405+
406+
impl AddToDiagnostic for UnsafeNotInheritedLintNote {
407+
fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
408+
where
409+
F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
410+
{
411+
diag.span_note(self.signature_span, fluent::mir_build_unsafe_fn_safe_body);
412+
let body_start = self.body_span.shrink_to_lo();
413+
let body_end = self.body_span.shrink_to_hi();
414+
diag.tool_only_multipart_suggestion(
415+
fluent::mir_build_wrap_suggestion,
416+
vec![(body_start, "{ unsafe ".into()), (body_end, "}".into())],
417+
Applicability::MaybeIncorrect,
418+
);
419+
}
420+
}
421+
379422
#[derive(LintDiagnostic)]
380423
#[diag(mir_build_unused_unsafe)]
381424
pub struct UnusedUnsafe {

‎src/tools/compiletest/src/runtest.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3962,7 +3962,7 @@ impl<'test> TestCx<'test> {
39623962
// And finally, compile the fixed code and make sure it both
39633963
// succeeds and has no diagnostics.
39643964
let rustc = self.make_compile_args(
3965-
&self.testpaths.file.with_extension(UI_FIXED),
3965+
&self.expected_output_path(UI_FIXED),
39663966
TargetLocation::ThisFile(self.make_exe_name()),
39673967
emit_metadata,
39683968
AllowUnused::No,

‎tests/ui/thread-local/thread-local-static.stderr renamed to ‎tests/ui/thread-local/thread-local-static.mir.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0658]: mutable references are not allowed in constant functions
2-
--> $DIR/thread-local-static.rs:7:12
2+
--> $DIR/thread-local-static.rs:10:12
33
|
44
LL | const fn g(x: &mut [u32; 8]) {
55
| ^
@@ -8,21 +8,21 @@ LL | const fn g(x: &mut [u32; 8]) {
88
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
99

1010
error[E0625]: thread-local statics cannot be accessed at compile-time
11-
--> $DIR/thread-local-static.rs:9:28
11+
--> $DIR/thread-local-static.rs:12:28
1212
|
1313
LL | std::mem::swap(x, &mut STATIC_VAR_2)
1414
| ^^^^^^^^^^^^
1515

1616
error[E0013]: constant functions cannot refer to statics
17-
--> $DIR/thread-local-static.rs:9:28
17+
--> $DIR/thread-local-static.rs:12:28
1818
|
1919
LL | std::mem::swap(x, &mut STATIC_VAR_2)
2020
| ^^^^^^^^^^^^
2121
|
2222
= help: consider extracting the value of the `static` to a `const`, and referring to that
2323

2424
error[E0658]: mutable references are not allowed in constant functions
25-
--> $DIR/thread-local-static.rs:9:23
25+
--> $DIR/thread-local-static.rs:12:23
2626
|
2727
LL | std::mem::swap(x, &mut STATIC_VAR_2)
2828
| ^^^^^^^^^^^^^^^^^
@@ -31,7 +31,7 @@ LL | std::mem::swap(x, &mut STATIC_VAR_2)
3131
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
3232

3333
error[E0133]: use of mutable static is unsafe and requires unsafe function or block
34-
--> $DIR/thread-local-static.rs:9:23
34+
--> $DIR/thread-local-static.rs:12:23
3535
|
3636
LL | std::mem::swap(x, &mut STATIC_VAR_2)
3737
| ^^^^^^^^^^^^^^^^^ use of mutable static

‎tests/ui/thread-local/thread-local-static.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
// edition:2018
2+
// revisions: mir thir
3+
//thir: -Zthir-unsafeck
24

35
#![feature(thread_local)]
46
#![feature(const_swap)]
7+
58
#[thread_local]
69
static mut STATIC_VAR_2: [u32; 8] = [4; 8];
710
const fn g(x: &mut [u32; 8]) {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
error[E0658]: mutable references are not allowed in constant functions
2+
--> $DIR/thread-local-static.rs:10:12
3+
|
4+
LL | const fn g(x: &mut [u32; 8]) {
5+
| ^
6+
|
7+
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
8+
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
9+
10+
error[E0625]: thread-local statics cannot be accessed at compile-time
11+
--> $DIR/thread-local-static.rs:12:28
12+
|
13+
LL | std::mem::swap(x, &mut STATIC_VAR_2)
14+
| ^^^^^^^^^^^^
15+
16+
error[E0013]: constant functions cannot refer to statics
17+
--> $DIR/thread-local-static.rs:12:28
18+
|
19+
LL | std::mem::swap(x, &mut STATIC_VAR_2)
20+
| ^^^^^^^^^^^^
21+
|
22+
= help: consider extracting the value of the `static` to a `const`, and referring to that
23+
24+
error[E0658]: mutable references are not allowed in constant functions
25+
--> $DIR/thread-local-static.rs:12:23
26+
|
27+
LL | std::mem::swap(x, &mut STATIC_VAR_2)
28+
| ^^^^^^^^^^^^^^^^^
29+
|
30+
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
31+
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
32+
33+
error[E0133]: use of mutable static is unsafe and requires unsafe function or block
34+
--> $DIR/thread-local-static.rs:12:23
35+
|
36+
LL | std::mem::swap(x, &mut STATIC_VAR_2)
37+
| ^^^^^^^^^^^^^^^^^ use of mutable static
38+
|
39+
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
40+
41+
error: aborting due to 5 previous errors
42+
43+
Some errors have detailed explanations: E0013, E0133, E0625, E0658.
44+
For more information about an error, try `rustc --explain E0013`.
Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,83 @@
11
error[E0133]: access to union field is unsafe and requires unsafe function or block
2-
--> $DIR/union-unsafe.rs:33:5
2+
--> $DIR/union-unsafe.rs:34:5
33
|
44
LL | *(u.p) = 13;
55
| ^^^^^^^^^^^ access to union field
66
|
77
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
88

99
error[E0133]: access to union field is unsafe and requires unsafe function or block
10-
--> $DIR/union-unsafe.rs:46:6
10+
--> $DIR/union-unsafe.rs:47:6
1111
|
1212
LL | *u3.a = T::default();
1313
| ^^^^ access to union field
1414
|
1515
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
1616

1717
error[E0133]: access to union field is unsafe and requires unsafe function or block
18-
--> $DIR/union-unsafe.rs:52:6
18+
--> $DIR/union-unsafe.rs:53:6
1919
|
2020
LL | *u3.a = T::default();
2121
| ^^^^ access to union field
2222
|
2323
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
2424

2525
error[E0133]: access to union field is unsafe and requires unsafe function or block
26-
--> $DIR/union-unsafe.rs:60:13
26+
--> $DIR/union-unsafe.rs:61:13
2727
|
2828
LL | let a = u1.a;
2929
| ^^^^ access to union field
3030
|
3131
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
3232

3333
error[E0133]: access to union field is unsafe and requires unsafe function or block
34-
--> $DIR/union-unsafe.rs:63:14
34+
--> $DIR/union-unsafe.rs:64:14
3535
|
3636
LL | let U1 { a } = u1;
3737
| ^ access to union field
3838
|
3939
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
4040

4141
error[E0133]: access to union field is unsafe and requires unsafe function or block
42-
--> $DIR/union-unsafe.rs:64:12
42+
--> $DIR/union-unsafe.rs:65:12
4343
|
4444
LL | if let U1 { a: 12 } = u1 {}
4545
| ^^^^^^^^^^^^ access to union field
4646
|
4747
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
4848

4949
error[E0133]: access to union field is unsafe and requires unsafe function or block
50-
--> $DIR/union-unsafe.rs:69:6
50+
--> $DIR/union-unsafe.rs:66:12
51+
|
52+
LL | if let Some(U1 { a: 13 }) = Some(u1) {}
53+
| ^^^^^^^^^^^^^^^^^^ access to union field
54+
|
55+
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
56+
57+
error[E0133]: access to union field is unsafe and requires unsafe function or block
58+
--> $DIR/union-unsafe.rs:71:6
5159
|
5260
LL | *u2.a = String::from("new");
5361
| ^^^^ access to union field
5462
|
5563
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
5664

5765
error[E0133]: access to union field is unsafe and requires unsafe function or block
58-
--> $DIR/union-unsafe.rs:73:6
66+
--> $DIR/union-unsafe.rs:75:6
5967
|
6068
LL | *u3.a = 1;
6169
| ^^^^ access to union field
6270
|
6371
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
6472

6573
error[E0133]: access to union field is unsafe and requires unsafe function or block
66-
--> $DIR/union-unsafe.rs:77:6
74+
--> $DIR/union-unsafe.rs:79:6
6775
|
6876
LL | *u3.a = String::from("new");
6977
| ^^^^ access to union field
7078
|
7179
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
7280

73-
error: aborting due to 9 previous errors
81+
error: aborting due to 10 previous errors
7482

7583
For more information about this error, try `rustc --explain E0133`.

‎tests/ui/union/union-unsafe.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
11
// revisions: mir thir
22
// [thir]compile-flags: -Z thir-unsafeck
33

4-
use std::mem::ManuallyDrop;
54
use std::cell::RefCell;
5+
use std::mem::ManuallyDrop;
66

77
union U1 {
8-
a: u8
8+
a: u8,
99
}
1010

1111
union U2 {
12-
a: ManuallyDrop<String>
12+
a: ManuallyDrop<String>,
1313
}
1414

1515
union U3<T> {
16-
a: ManuallyDrop<T>
16+
a: ManuallyDrop<T>,
1717
}
1818

1919
union U4<T: Copy> {
20-
a: T
20+
a: T,
2121
}
2222

2323
union URef {
2424
p: &'static mut i32,
2525
}
2626

27-
union URefCell { // field that does not drop but is not `Copy`, either
27+
union URefCell {
28+
// field that does not drop but is not `Copy`, either
2829
a: (ManuallyDrop<RefCell<i32>>, i32),
2930
}
3031

@@ -62,6 +63,7 @@ fn main() {
6263

6364
let U1 { a } = u1; //~ ERROR access to union field is unsafe
6465
if let U1 { a: 12 } = u1 {} //~ ERROR access to union field is unsafe
66+
if let Some(U1 { a: 13 }) = Some(u1) {} //~ ERROR access to union field is unsafe
6567
// let U1 { .. } = u1; // OK
6668

6769
let mut u2 = U2 { a: ManuallyDrop::new(String::from("old")) }; // OK
Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,83 @@
11
error[E0133]: access to union field is unsafe and requires unsafe function or block
2-
--> $DIR/union-unsafe.rs:33:6
2+
--> $DIR/union-unsafe.rs:34:6
33
|
44
LL | *(u.p) = 13;
55
| ^^^^^ access to union field
66
|
77
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
88

99
error[E0133]: access to union field is unsafe and requires unsafe function or block
10-
--> $DIR/union-unsafe.rs:46:6
10+
--> $DIR/union-unsafe.rs:47:6
1111
|
1212
LL | *u3.a = T::default();
1313
| ^^^^ access to union field
1414
|
1515
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
1616

1717
error[E0133]: access to union field is unsafe and requires unsafe function or block
18-
--> $DIR/union-unsafe.rs:52:6
18+
--> $DIR/union-unsafe.rs:53:6
1919
|
2020
LL | *u3.a = T::default();
2121
| ^^^^ access to union field
2222
|
2323
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
2424

2525
error[E0133]: access to union field is unsafe and requires unsafe function or block
26-
--> $DIR/union-unsafe.rs:60:13
26+
--> $DIR/union-unsafe.rs:61:13
2727
|
2828
LL | let a = u1.a;
2929
| ^^^^ access to union field
3030
|
3131
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
3232

3333
error[E0133]: access to union field is unsafe and requires unsafe function or block
34-
--> $DIR/union-unsafe.rs:63:14
34+
--> $DIR/union-unsafe.rs:64:14
3535
|
3636
LL | let U1 { a } = u1;
3737
| ^ access to union field
3838
|
3939
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
4040

4141
error[E0133]: access to union field is unsafe and requires unsafe function or block
42-
--> $DIR/union-unsafe.rs:64:8
42+
--> $DIR/union-unsafe.rs:65:20
4343
|
4444
LL | if let U1 { a: 12 } = u1 {}
45-
| ^^^^^^^^^^^^^^^^^^^^^ access to union field
45+
| ^^ access to union field
4646
|
4747
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
4848

4949
error[E0133]: access to union field is unsafe and requires unsafe function or block
50-
--> $DIR/union-unsafe.rs:69:6
50+
--> $DIR/union-unsafe.rs:66:25
51+
|
52+
LL | if let Some(U1 { a: 13 }) = Some(u1) {}
53+
| ^^ access to union field
54+
|
55+
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
56+
57+
error[E0133]: access to union field is unsafe and requires unsafe function or block
58+
--> $DIR/union-unsafe.rs:71:6
5159
|
5260
LL | *u2.a = String::from("new");
5361
| ^^^^ access to union field
5462
|
5563
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
5664

5765
error[E0133]: access to union field is unsafe and requires unsafe function or block
58-
--> $DIR/union-unsafe.rs:73:6
66+
--> $DIR/union-unsafe.rs:75:6
5967
|
6068
LL | *u3.a = 1;
6169
| ^^^^ access to union field
6270
|
6371
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
6472

6573
error[E0133]: access to union field is unsafe and requires unsafe function or block
66-
--> $DIR/union-unsafe.rs:77:6
74+
--> $DIR/union-unsafe.rs:79:6
6775
|
6876
LL | *u3.a = String::from("new");
6977
| ^^^^ access to union field
7078
|
7179
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
7280

73-
error: aborting due to 9 previous errors
81+
error: aborting due to 10 previous errors
7482

7583
For more information about this error, try `rustc --explain E0133`.

‎tests/ui/unsafe/edition-2024-unsafe_op_in_unsafe_fn.stderr renamed to ‎tests/ui/unsafe/edition-2024-unsafe_op_in_unsafe_fn.mir.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
warning: call to unsafe function is unsafe and requires unsafe block (error E0133)
2-
--> $DIR/edition-2024-unsafe_op_in_unsafe_fn.rs:12:5
2+
--> $DIR/edition-2024-unsafe_op_in_unsafe_fn.rs:13:5
33
|
44
LL | unsf();
55
| ^^^^^^ call to unsafe function
66
|
77
= note: consult the function's documentation for information on how to avoid undefined behavior
88
note: an unsafe function restricts its caller, but its body is safe by default
9-
--> $DIR/edition-2024-unsafe_op_in_unsafe_fn.rs:11:1
9+
--> $DIR/edition-2024-unsafe_op_in_unsafe_fn.rs:12:1
1010
|
1111
LL | unsafe fn foo() {
1212
| ^^^^^^^^^^^^^^^
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
// edition: 2024
22
// compile-flags: -Zunstable-options
33
// check-pass
4+
// revisions: mir thir
5+
// [thir]compile-flags: -Zthir-unsafeck
46

57
#![crate_type = "lib"]
6-
78
#![deny(unused_unsafe)]
89

910
unsafe fn unsf() {}
1011

1112
unsafe fn foo() {
1213
unsf();
13-
//~^ WARN call to unsafe function is unsafe and requires unsafe block
14+
//[mir]~^ WARN call to unsafe function is unsafe and requires unsafe block
15+
//[thir]~^^ WARN call to unsafe function `unsf` is unsafe and requires unsafe block
1416

1517
// no unused_unsafe
16-
unsafe { unsf(); }
18+
unsafe {
19+
unsf();
20+
}
1721
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
warning: call to unsafe function `unsf` is unsafe and requires unsafe block (error E0133)
2+
--> $DIR/edition-2024-unsafe_op_in_unsafe_fn.rs:13:5
3+
|
4+
LL | unsf();
5+
| ^^^^^^ call to unsafe function
6+
|
7+
= note: consult the function's documentation for information on how to avoid undefined behavior
8+
note: an unsafe function restricts its caller, but its body is safe by default
9+
--> $DIR/edition-2024-unsafe_op_in_unsafe_fn.rs:12:1
10+
|
11+
LL | unsafe fn foo() {
12+
| ^^^^^^^^^^^^^^^
13+
= note: `#[warn(unsafe_op_in_unsafe_fn)]` on by default
14+
15+
warning: 1 warning emitted
16+

‎tests/ui/unsafe/ranged_ints2.mirunsafeck.stderr

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ LL | let y = &mut x.0;
66
|
77
= note: mutating layout constrained fields cannot statically be checked for valid values
88

9-
error: aborting due to previous error
9+
error[E0133]: mutation of layout constrained field is unsafe and requires unsafe function or block
10+
--> $DIR/ranged_ints2.rs:12:25
11+
|
12+
LL | if let Some(NonZero(ref mut y)) = Some(x) {}
13+
| ^^^^^^^^^ mutation of layout constrained field
14+
|
15+
= note: mutating layout constrained fields cannot statically be checked for valid values
16+
17+
error: aborting due to 2 previous errors
1018

1119
For more information about this error, try `rustc --explain E0133`.

‎tests/ui/unsafe/ranged_ints2.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ pub(crate) struct NonZero<T>(pub(crate) T);
99
fn main() {
1010
let mut x = unsafe { NonZero(1) };
1111
let y = &mut x.0; //~ ERROR mutation of layout constrained field is unsafe
12+
if let Some(NonZero(ref mut y)) = Some(x) {} //~ ERROR mutation of layout constrained field is unsafe
1213
}

‎tests/ui/unsafe/ranged_ints2.thirunsafeck.stderr

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ LL | let y = &mut x.0;
66
|
77
= note: mutating layout constrained fields cannot statically be checked for valid values
88

9-
error: aborting due to previous error
9+
error[E0133]: mutation of layout constrained field is unsafe and requires unsafe function or block
10+
--> $DIR/ranged_ints2.rs:12:25
11+
|
12+
LL | if let Some(NonZero(ref mut y)) = Some(x) {}
13+
| ^^^^^^^^^ mutation of layout constrained field
14+
|
15+
= note: mutating layout constrained fields cannot statically be checked for valid values
16+
17+
error: aborting due to 2 previous errors
1018

1119
For more information about this error, try `rustc --explain E0133`.

‎tests/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.thir.stderr

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ LL | unsf();
55
| ^^^^^^ call to unsafe function
66
|
77
= note: consult the function's documentation for information on how to avoid undefined behavior
8+
note: an unsafe function restricts its caller, but its body is safe by default
9+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:11:1
10+
|
11+
LL | unsafe fn deny_level() {
12+
| ^^^^^^^^^^^^^^^^^^^^^^
813
note: the lint level is defined here
914
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:4:9
1015
|
@@ -46,6 +51,11 @@ LL | unsf();
4651
| ^^^^^^ call to unsafe function
4752
|
4853
= note: consult the function's documentation for information on how to avoid undefined behavior
54+
note: an unsafe function restricts its caller, but its body is safe by default
55+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:27:1
56+
|
57+
LL | unsafe fn warning_level() {
58+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
4959
note: the lint level is defined here
5060
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:26:8
5161
|

‎tests/ui/unsafe/wrapping-unsafe-block-sugg.fixed renamed to ‎tests/ui/unsafe/wrapping-unsafe-block-sugg.mir.fixed

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
// run-rustfix
22
// aux-build:external_unsafe_macro.rs
3+
// revisions: mir thir
4+
// [thir]compile-flags: -Zthir-unsafeck
35

46
#![deny(unsafe_op_in_unsafe_fn)] //~ NOTE
7+
#![crate_name = "wrapping_unsafe_block_sugg"]
58

69
extern crate external_unsafe_macro;
710

811
unsafe fn unsf() {}
912

1013
pub unsafe fn foo() { unsafe {
1114
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default
12-
unsf(); //~ ERROR call to unsafe function is unsafe
13-
//~^ NOTE
15+
unsf(); //[mir]~ ERROR call to unsafe function is unsafe
16+
//[thir]~^ ERROR call to unsafe function `unsf` is unsafe
17+
//~^^ NOTE
1418
//~| NOTE
15-
unsf(); //~ ERROR call to unsafe function is unsafe
16-
//~^ NOTE
19+
unsf(); //[mir]~ ERROR call to unsafe function is unsafe
20+
//[thir]~^ ERROR call to unsafe function `unsf` is unsafe
21+
//~^^ NOTE
1722
//~| NOTE
1823
}}
1924

@@ -39,10 +44,12 @@ pub unsafe fn baz() -> i32 { unsafe {
3944
}}
4045

4146
macro_rules! unsafe_macro { () => (unsf()) }
42-
//~^ ERROR call to unsafe function is unsafe
47+
//[mir]~^ ERROR call to unsafe function is unsafe
48+
//[thir]~^^ ERROR call to unsafe function `unsf` is unsafe
4349
//~| NOTE
4450
//~| NOTE
45-
//~| ERROR call to unsafe function is unsafe
51+
//[mir]~| ERROR call to unsafe function is unsafe
52+
//[thir]~| ERROR call to unsafe function `unsf` is unsafe
4653
//~| NOTE
4754
//~| NOTE
4855

‎tests/ui/unsafe/wrapping-unsafe-block-sugg.stderr renamed to ‎tests/ui/unsafe/wrapping-unsafe-block-sugg.mir.stderr

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,73 @@
11
error: call to unsafe function is unsafe and requires unsafe block (error E0133)
2-
--> $DIR/wrapping-unsafe-block-sugg.rs:12:5
2+
--> $DIR/wrapping-unsafe-block-sugg.rs:15:5
33
|
44
LL | unsf();
55
| ^^^^^^ call to unsafe function
66
|
77
= note: consult the function's documentation for information on how to avoid undefined behavior
88
note: an unsafe function restricts its caller, but its body is safe by default
9-
--> $DIR/wrapping-unsafe-block-sugg.rs:10:1
9+
--> $DIR/wrapping-unsafe-block-sugg.rs:13:1
1010
|
1111
LL | pub unsafe fn foo() {
1212
| ^^^^^^^^^^^^^^^^^^^
1313
note: the lint level is defined here
14-
--> $DIR/wrapping-unsafe-block-sugg.rs:4:9
14+
--> $DIR/wrapping-unsafe-block-sugg.rs:6:9
1515
|
1616
LL | #![deny(unsafe_op_in_unsafe_fn)]
1717
| ^^^^^^^^^^^^^^^^^^^^^^
1818

1919
error: call to unsafe function is unsafe and requires unsafe block (error E0133)
20-
--> $DIR/wrapping-unsafe-block-sugg.rs:15:5
20+
--> $DIR/wrapping-unsafe-block-sugg.rs:19:5
2121
|
2222
LL | unsf();
2323
| ^^^^^^ call to unsafe function
2424
|
2525
= note: consult the function's documentation for information on how to avoid undefined behavior
2626

2727
error: dereference of raw pointer is unsafe and requires unsafe block (error E0133)
28-
--> $DIR/wrapping-unsafe-block-sugg.rs:22:13
28+
--> $DIR/wrapping-unsafe-block-sugg.rs:27:13
2929
|
3030
LL | let y = *x;
3131
| ^^ dereference of raw pointer
3232
|
3333
= note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
3434
note: an unsafe function restricts its caller, but its body is safe by default
35-
--> $DIR/wrapping-unsafe-block-sugg.rs:20:1
35+
--> $DIR/wrapping-unsafe-block-sugg.rs:25:1
3636
|
3737
LL | pub unsafe fn bar(x: *const i32) -> i32 {
3838
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3939

4040
error: dereference of raw pointer is unsafe and requires unsafe block (error E0133)
41-
--> $DIR/wrapping-unsafe-block-sugg.rs:25:9
41+
--> $DIR/wrapping-unsafe-block-sugg.rs:30:9
4242
|
4343
LL | y + *x
4444
| ^^ dereference of raw pointer
4545
|
4646
= note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
4747

4848
error: use of mutable static is unsafe and requires unsafe block (error E0133)
49-
--> $DIR/wrapping-unsafe-block-sugg.rs:33:13
49+
--> $DIR/wrapping-unsafe-block-sugg.rs:38:13
5050
|
5151
LL | let y = BAZ;
5252
| ^^^ use of mutable static
5353
|
5454
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
5555
note: an unsafe function restricts its caller, but its body is safe by default
56-
--> $DIR/wrapping-unsafe-block-sugg.rs:31:1
56+
--> $DIR/wrapping-unsafe-block-sugg.rs:36:1
5757
|
5858
LL | pub unsafe fn baz() -> i32 {
5959
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
6060

6161
error: use of mutable static is unsafe and requires unsafe block (error E0133)
62-
--> $DIR/wrapping-unsafe-block-sugg.rs:36:9
62+
--> $DIR/wrapping-unsafe-block-sugg.rs:41:9
6363
|
6464
LL | y + BAZ
6565
| ^^^ use of mutable static
6666
|
6767
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
6868

6969
error: call to unsafe function is unsafe and requires unsafe block (error E0133)
70-
--> $DIR/wrapping-unsafe-block-sugg.rs:41:36
70+
--> $DIR/wrapping-unsafe-block-sugg.rs:46:36
7171
|
7272
LL | macro_rules! unsafe_macro { () => (unsf()) }
7373
| ^^^^^^ call to unsafe function
@@ -77,14 +77,14 @@ LL | unsafe_macro!();
7777
|
7878
= note: consult the function's documentation for information on how to avoid undefined behavior
7979
note: an unsafe function restricts its caller, but its body is safe by default
80-
--> $DIR/wrapping-unsafe-block-sugg.rs:49:1
80+
--> $DIR/wrapping-unsafe-block-sugg.rs:56:1
8181
|
8282
LL | pub unsafe fn unsafe_in_macro() {
8383
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8484
= note: this error originates in the macro `unsafe_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
8585

8686
error: call to unsafe function is unsafe and requires unsafe block (error E0133)
87-
--> $DIR/wrapping-unsafe-block-sugg.rs:41:36
87+
--> $DIR/wrapping-unsafe-block-sugg.rs:46:36
8888
|
8989
LL | macro_rules! unsafe_macro { () => (unsf()) }
9090
| ^^^^^^ call to unsafe function

‎tests/ui/unsafe/wrapping-unsafe-block-sugg.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
// run-rustfix
22
// aux-build:external_unsafe_macro.rs
3+
// revisions: mir thir
4+
// [thir]compile-flags: -Zthir-unsafeck
35

46
#![deny(unsafe_op_in_unsafe_fn)] //~ NOTE
7+
#![crate_name = "wrapping_unsafe_block_sugg"]
58

69
extern crate external_unsafe_macro;
710

811
unsafe fn unsf() {}
912

1013
pub unsafe fn foo() {
1114
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default
12-
unsf(); //~ ERROR call to unsafe function is unsafe
13-
//~^ NOTE
15+
unsf(); //[mir]~ ERROR call to unsafe function is unsafe
16+
//[thir]~^ ERROR call to unsafe function `unsf` is unsafe
17+
//~^^ NOTE
1418
//~| NOTE
15-
unsf(); //~ ERROR call to unsafe function is unsafe
16-
//~^ NOTE
19+
unsf(); //[mir]~ ERROR call to unsafe function is unsafe
20+
//[thir]~^ ERROR call to unsafe function `unsf` is unsafe
21+
//~^^ NOTE
1722
//~| NOTE
1823
}
1924

@@ -39,10 +44,12 @@ pub unsafe fn baz() -> i32 {
3944
}
4045

4146
macro_rules! unsafe_macro { () => (unsf()) }
42-
//~^ ERROR call to unsafe function is unsafe
47+
//[mir]~^ ERROR call to unsafe function is unsafe
48+
//[thir]~^^ ERROR call to unsafe function `unsf` is unsafe
4349
//~| NOTE
4450
//~| NOTE
45-
//~| ERROR call to unsafe function is unsafe
51+
//[mir]~| ERROR call to unsafe function is unsafe
52+
//[thir]~| ERROR call to unsafe function `unsf` is unsafe
4653
//~| NOTE
4754
//~| NOTE
4855

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// run-rustfix
2+
// aux-build:external_unsafe_macro.rs
3+
// revisions: mir thir
4+
// [thir]compile-flags: -Zthir-unsafeck
5+
6+
#![deny(unsafe_op_in_unsafe_fn)] //~ NOTE
7+
#![crate_name = "wrapping_unsafe_block_sugg"]
8+
9+
extern crate external_unsafe_macro;
10+
11+
unsafe fn unsf() {}
12+
13+
pub unsafe fn foo() { unsafe {
14+
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default
15+
unsf(); //[mir]~ ERROR call to unsafe function is unsafe
16+
//[thir]~^ ERROR call to unsafe function `unsf` is unsafe
17+
//~^^ NOTE
18+
//~| NOTE
19+
unsf(); //[mir]~ ERROR call to unsafe function is unsafe
20+
//[thir]~^ ERROR call to unsafe function `unsf` is unsafe
21+
//~^^ NOTE
22+
//~| NOTE
23+
}}
24+
25+
pub unsafe fn bar(x: *const i32) -> i32 { unsafe {
26+
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default
27+
let y = *x; //~ ERROR dereference of raw pointer is unsafe and requires unsafe block
28+
//~^ NOTE
29+
//~| NOTE
30+
y + *x //~ ERROR dereference of raw pointer is unsafe and requires unsafe block
31+
//~^ NOTE
32+
//~| NOTE
33+
}}
34+
35+
static mut BAZ: i32 = 0;
36+
pub unsafe fn baz() -> i32 { unsafe {
37+
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default
38+
let y = BAZ; //~ ERROR use of mutable static is unsafe and requires unsafe block
39+
//~^ NOTE
40+
//~| NOTE
41+
y + BAZ //~ ERROR use of mutable static is unsafe and requires unsafe block
42+
//~^ NOTE
43+
//~| NOTE
44+
}}
45+
46+
macro_rules! unsafe_macro { () => (unsf()) }
47+
//[mir]~^ ERROR call to unsafe function is unsafe
48+
//[thir]~^^ ERROR call to unsafe function `unsf` is unsafe
49+
//~| NOTE
50+
//~| NOTE
51+
//[mir]~| ERROR call to unsafe function is unsafe
52+
//[thir]~| ERROR call to unsafe function `unsf` is unsafe
53+
//~| NOTE
54+
//~| NOTE
55+
56+
pub unsafe fn unsafe_in_macro() { unsafe {
57+
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default
58+
unsafe_macro!();
59+
//~^ NOTE
60+
//~| NOTE
61+
unsafe_macro!();
62+
//~^ NOTE
63+
//~| NOTE
64+
}}
65+
66+
pub unsafe fn unsafe_in_external_macro() {
67+
// FIXME: https://github.com/rust-lang/rust/issues/112504
68+
// FIXME: ~^ NOTE an unsafe function restricts its caller, but its body is safe by default
69+
external_unsafe_macro::unsafe_macro!();
70+
external_unsafe_macro::unsafe_macro!();
71+
}
72+
73+
fn main() {}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
error: call to unsafe function `unsf` is unsafe and requires unsafe block (error E0133)
2+
--> $DIR/wrapping-unsafe-block-sugg.rs:15:5
3+
|
4+
LL | unsf();
5+
| ^^^^^^ call to unsafe function
6+
|
7+
= note: consult the function's documentation for information on how to avoid undefined behavior
8+
note: an unsafe function restricts its caller, but its body is safe by default
9+
--> $DIR/wrapping-unsafe-block-sugg.rs:13:1
10+
|
11+
LL | pub unsafe fn foo() {
12+
| ^^^^^^^^^^^^^^^^^^^
13+
note: the lint level is defined here
14+
--> $DIR/wrapping-unsafe-block-sugg.rs:6:9
15+
|
16+
LL | #![deny(unsafe_op_in_unsafe_fn)]
17+
| ^^^^^^^^^^^^^^^^^^^^^^
18+
19+
error: call to unsafe function `unsf` is unsafe and requires unsafe block (error E0133)
20+
--> $DIR/wrapping-unsafe-block-sugg.rs:19:5
21+
|
22+
LL | unsf();
23+
| ^^^^^^ call to unsafe function
24+
|
25+
= note: consult the function's documentation for information on how to avoid undefined behavior
26+
27+
error: dereference of raw pointer is unsafe and requires unsafe block (error E0133)
28+
--> $DIR/wrapping-unsafe-block-sugg.rs:27:13
29+
|
30+
LL | let y = *x;
31+
| ^^ dereference of raw pointer
32+
|
33+
= note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
34+
note: an unsafe function restricts its caller, but its body is safe by default
35+
--> $DIR/wrapping-unsafe-block-sugg.rs:25:1
36+
|
37+
LL | pub unsafe fn bar(x: *const i32) -> i32 {
38+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
39+
40+
error: dereference of raw pointer is unsafe and requires unsafe block (error E0133)
41+
--> $DIR/wrapping-unsafe-block-sugg.rs:30:9
42+
|
43+
LL | y + *x
44+
| ^^ dereference of raw pointer
45+
|
46+
= note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
47+
48+
error: use of mutable static is unsafe and requires unsafe block (error E0133)
49+
--> $DIR/wrapping-unsafe-block-sugg.rs:38:13
50+
|
51+
LL | let y = BAZ;
52+
| ^^^ use of mutable static
53+
|
54+
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
55+
note: an unsafe function restricts its caller, but its body is safe by default
56+
--> $DIR/wrapping-unsafe-block-sugg.rs:36:1
57+
|
58+
LL | pub unsafe fn baz() -> i32 {
59+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
60+
61+
error: use of mutable static is unsafe and requires unsafe block (error E0133)
62+
--> $DIR/wrapping-unsafe-block-sugg.rs:41:9
63+
|
64+
LL | y + BAZ
65+
| ^^^ use of mutable static
66+
|
67+
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
68+
69+
error: call to unsafe function `unsf` is unsafe and requires unsafe block (error E0133)
70+
--> $DIR/wrapping-unsafe-block-sugg.rs:46:36
71+
|
72+
LL | macro_rules! unsafe_macro { () => (unsf()) }
73+
| ^^^^^^ call to unsafe function
74+
...
75+
LL | unsafe_macro!();
76+
| --------------- in this macro invocation
77+
|
78+
= note: consult the function's documentation for information on how to avoid undefined behavior
79+
note: an unsafe function restricts its caller, but its body is safe by default
80+
--> $DIR/wrapping-unsafe-block-sugg.rs:56:1
81+
|
82+
LL | pub unsafe fn unsafe_in_macro() {
83+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
84+
= note: this error originates in the macro `unsafe_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
85+
86+
error: call to unsafe function `unsf` is unsafe and requires unsafe block (error E0133)
87+
--> $DIR/wrapping-unsafe-block-sugg.rs:46:36
88+
|
89+
LL | macro_rules! unsafe_macro { () => (unsf()) }
90+
| ^^^^^^ call to unsafe function
91+
...
92+
LL | unsafe_macro!();
93+
| --------------- in this macro invocation
94+
|
95+
= note: consult the function's documentation for information on how to avoid undefined behavior
96+
= note: this error originates in the macro `unsafe_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
97+
98+
error: aborting due to 8 previous errors
99+

0 commit comments

Comments
 (0)
Please sign in to comment.