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
This commit is contained in:
bors 2023-11-07 10:41:10 +00:00
commit 61a3eea804
24 changed files with 503 additions and 99 deletions

View File

@ -3566,6 +3566,15 @@ impl<'hir> OwnerNode<'hir> {
} }
} }
pub fn fn_sig(self) -> Option<&'hir FnSig<'hir>> {
match self {
OwnerNode::TraitItem(TraitItem { kind: TraitItemKind::Fn(fn_sig, _), .. })
| OwnerNode::ImplItem(ImplItem { kind: ImplItemKind::Fn(fn_sig, _), .. })
| OwnerNode::Item(Item { kind: ItemKind::Fn(fn_sig, _, _), .. }) => Some(fn_sig),
_ => None,
}
}
pub fn fn_decl(self) -> Option<&'hir FnDecl<'hir>> { pub fn fn_decl(self) -> Option<&'hir FnDecl<'hir>> {
match self { match self {
OwnerNode::TraitItem(TraitItem { kind: TraitItemKind::Fn(fn_sig, _), .. }) OwnerNode::TraitItem(TraitItem { kind: TraitItemKind::Fn(fn_sig, _), .. })

View File

@ -66,8 +66,9 @@ pub fn walk_expr<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, expr: &Exp
Use { source } => visitor.visit_expr(&visitor.thir()[source]), Use { source } => visitor.visit_expr(&visitor.thir()[source]),
NeverToAny { source } => visitor.visit_expr(&visitor.thir()[source]), NeverToAny { source } => visitor.visit_expr(&visitor.thir()[source]),
PointerCoercion { source, cast: _ } => visitor.visit_expr(&visitor.thir()[source]), PointerCoercion { source, cast: _ } => visitor.visit_expr(&visitor.thir()[source]),
Let { expr, .. } => { Let { expr, ref pat } => {
visitor.visit_expr(&visitor.thir()[expr]); visitor.visit_expr(&visitor.thir()[expr]);
visitor.visit_pat(pat);
} }
Loop { body } => visitor.visit_expr(&visitor.thir()[body]), Loop { body } => visitor.visit_expr(&visitor.thir()[body]),
Match { scrutinee, ref arms, .. } => { Match { scrutinee, ref arms, .. } => {

View File

@ -320,6 +320,7 @@ mir_build_unreachable_pattern = unreachable pattern
.label = unreachable pattern .label = unreachable pattern
.catchall_label = matches any value .catchall_label = matches any value
mir_build_unsafe_fn_safe_body = an unsafe function restricts its caller, but its body is safe by default
mir_build_unsafe_not_inherited = items do not inherit unsafety from separate enclosing items mir_build_unsafe_not_inherited = items do not inherit unsafety from separate enclosing items
mir_build_unsafe_op_in_unsafe_fn_borrow_of_layout_constrained_field_requires_unsafe = 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
mir_build_unused_unsafe_enclosing_block_label = because it's nested under this `unsafe` block mir_build_unused_unsafe_enclosing_block_label = because it's nested under this `unsafe` block
mir_build_variant_defined_here = not covered mir_build_variant_defined_here = not covered
mir_build_wrap_suggestion = consider wrapping the function body in an unsafe block

View File

@ -35,6 +35,10 @@ struct UnsafetyVisitor<'a, 'tcx> {
param_env: ParamEnv<'tcx>, param_env: ParamEnv<'tcx>,
inside_adt: bool, inside_adt: bool,
warnings: &'a mut Vec<UnusedUnsafeWarning>, warnings: &'a mut Vec<UnusedUnsafeWarning>,
/// Flag to ensure that we only suggest wrapping the entire function body in
/// an unsafe block once.
suggest_unsafe_block: bool,
} }
impl<'tcx> UnsafetyVisitor<'_, 'tcx> { impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
@ -95,7 +99,13 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
SafetyContext::UnsafeFn if unsafe_op_in_unsafe_fn_allowed => {} SafetyContext::UnsafeFn if unsafe_op_in_unsafe_fn_allowed => {}
SafetyContext::UnsafeFn => { SafetyContext::UnsafeFn => {
// unsafe_op_in_unsafe_fn is disallowed // unsafe_op_in_unsafe_fn is disallowed
kind.emit_unsafe_op_in_unsafe_fn_lint(self.tcx, self.hir_context, span); kind.emit_unsafe_op_in_unsafe_fn_lint(
self.tcx,
self.hir_context,
span,
self.suggest_unsafe_block,
);
self.suggest_unsafe_block = false;
} }
SafetyContext::Safe => { SafetyContext::Safe => {
kind.emit_requires_unsafe_err( kind.emit_requires_unsafe_err(
@ -297,6 +307,7 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
} }
PatKind::InlineConstant { def, .. } => { PatKind::InlineConstant { def, .. } => {
self.visit_inner_body(*def); self.visit_inner_body(*def);
visit::walk_pat(self, pat);
} }
_ => { _ => {
visit::walk_pat(self, pat); visit::walk_pat(self, pat);
@ -394,7 +405,9 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
} }
} }
ExprKind::Deref { arg } => { ExprKind::Deref { arg } => {
if let ExprKind::StaticRef { def_id, .. } = self.thir[arg].kind { if let ExprKind::StaticRef { def_id, .. } | ExprKind::ThreadLocalRef(def_id) =
self.thir[arg].kind
{
if self.tcx.is_mutable_static(def_id) { if self.tcx.is_mutable_static(def_id) {
self.requires_unsafe(expr.span, UseOfMutableStatic); self.requires_unsafe(expr.span, UseOfMutableStatic);
} else if self.tcx.is_foreign_item(def_id) { } else if self.tcx.is_foreign_item(def_id) {
@ -482,14 +495,6 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
} }
} }
} }
ExprKind::Let { expr: expr_id, .. } => {
let let_expr = &self.thir[expr_id];
if let ty::Adt(adt_def, _) = let_expr.ty.kind()
&& adt_def.is_union()
{
self.requires_unsafe(expr.span, AccessToUnionField);
}
}
_ => {} _ => {}
} }
visit::walk_expr(self, expr); visit::walk_expr(self, expr);
@ -543,7 +548,22 @@ impl UnsafeOpKind {
tcx: TyCtxt<'_>, tcx: TyCtxt<'_>,
hir_id: hir::HirId, hir_id: hir::HirId,
span: Span, span: Span,
suggest_unsafe_block: bool,
) { ) {
let parent_id = tcx.hir().get_parent_item(hir_id);
let parent_owner = tcx.hir().owner(parent_id);
let should_suggest = parent_owner.fn_sig().map_or(false, |sig| sig.header.is_unsafe());
let unsafe_not_inherited_note = if should_suggest {
suggest_unsafe_block.then(|| {
let body_span = tcx.hir().body(parent_owner.body_id().unwrap()).value.span;
UnsafeNotInheritedLintNote {
signature_span: tcx.def_span(parent_id.def_id),
body_span,
}
})
} else {
None
};
// FIXME: ideally we would want to trim the def paths, but this is not // FIXME: ideally we would want to trim the def paths, but this is not
// feasible with the current lint emission API (see issue #106126). // feasible with the current lint emission API (see issue #106126).
match self { match self {
@ -554,61 +574,89 @@ impl UnsafeOpKind {
UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafe { UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafe {
span, span,
function: &with_no_trimmed_paths!(tcx.def_path_str(*did)), function: &with_no_trimmed_paths!(tcx.def_path_str(*did)),
unsafe_not_inherited_note,
}, },
), ),
CallToUnsafeFunction(None) => tcx.emit_spanned_lint( CallToUnsafeFunction(None) => tcx.emit_spanned_lint(
UNSAFE_OP_IN_UNSAFE_FN, UNSAFE_OP_IN_UNSAFE_FN,
hir_id, hir_id,
span, span,
UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafeNameless { span }, UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafeNameless {
span,
unsafe_not_inherited_note,
},
), ),
UseOfInlineAssembly => tcx.emit_spanned_lint( UseOfInlineAssembly => tcx.emit_spanned_lint(
UNSAFE_OP_IN_UNSAFE_FN, UNSAFE_OP_IN_UNSAFE_FN,
hir_id, hir_id,
span, span,
UnsafeOpInUnsafeFnUseOfInlineAssemblyRequiresUnsafe { span }, UnsafeOpInUnsafeFnUseOfInlineAssemblyRequiresUnsafe {
span,
unsafe_not_inherited_note,
},
), ),
InitializingTypeWith => tcx.emit_spanned_lint( InitializingTypeWith => tcx.emit_spanned_lint(
UNSAFE_OP_IN_UNSAFE_FN, UNSAFE_OP_IN_UNSAFE_FN,
hir_id, hir_id,
span, span,
UnsafeOpInUnsafeFnInitializingTypeWithRequiresUnsafe { span }, UnsafeOpInUnsafeFnInitializingTypeWithRequiresUnsafe {
span,
unsafe_not_inherited_note,
},
), ),
UseOfMutableStatic => tcx.emit_spanned_lint( UseOfMutableStatic => tcx.emit_spanned_lint(
UNSAFE_OP_IN_UNSAFE_FN, UNSAFE_OP_IN_UNSAFE_FN,
hir_id, hir_id,
span, span,
UnsafeOpInUnsafeFnUseOfMutableStaticRequiresUnsafe { span }, UnsafeOpInUnsafeFnUseOfMutableStaticRequiresUnsafe {
span,
unsafe_not_inherited_note,
},
), ),
UseOfExternStatic => tcx.emit_spanned_lint( UseOfExternStatic => tcx.emit_spanned_lint(
UNSAFE_OP_IN_UNSAFE_FN, UNSAFE_OP_IN_UNSAFE_FN,
hir_id, hir_id,
span, span,
UnsafeOpInUnsafeFnUseOfExternStaticRequiresUnsafe { span }, UnsafeOpInUnsafeFnUseOfExternStaticRequiresUnsafe {
span,
unsafe_not_inherited_note,
},
), ),
DerefOfRawPointer => tcx.emit_spanned_lint( DerefOfRawPointer => tcx.emit_spanned_lint(
UNSAFE_OP_IN_UNSAFE_FN, UNSAFE_OP_IN_UNSAFE_FN,
hir_id, hir_id,
span, span,
UnsafeOpInUnsafeFnDerefOfRawPointerRequiresUnsafe { span }, UnsafeOpInUnsafeFnDerefOfRawPointerRequiresUnsafe {
span,
unsafe_not_inherited_note,
},
), ),
AccessToUnionField => tcx.emit_spanned_lint( AccessToUnionField => tcx.emit_spanned_lint(
UNSAFE_OP_IN_UNSAFE_FN, UNSAFE_OP_IN_UNSAFE_FN,
hir_id, hir_id,
span, span,
UnsafeOpInUnsafeFnAccessToUnionFieldRequiresUnsafe { span }, UnsafeOpInUnsafeFnAccessToUnionFieldRequiresUnsafe {
span,
unsafe_not_inherited_note,
},
), ),
MutationOfLayoutConstrainedField => tcx.emit_spanned_lint( MutationOfLayoutConstrainedField => tcx.emit_spanned_lint(
UNSAFE_OP_IN_UNSAFE_FN, UNSAFE_OP_IN_UNSAFE_FN,
hir_id, hir_id,
span, span,
UnsafeOpInUnsafeFnMutationOfLayoutConstrainedFieldRequiresUnsafe { span }, UnsafeOpInUnsafeFnMutationOfLayoutConstrainedFieldRequiresUnsafe {
span,
unsafe_not_inherited_note,
},
), ),
BorrowOfLayoutConstrainedField => tcx.emit_spanned_lint( BorrowOfLayoutConstrainedField => tcx.emit_spanned_lint(
UNSAFE_OP_IN_UNSAFE_FN, UNSAFE_OP_IN_UNSAFE_FN,
hir_id, hir_id,
span, span,
UnsafeOpInUnsafeFnBorrowOfLayoutConstrainedFieldRequiresUnsafe { span }, UnsafeOpInUnsafeFnBorrowOfLayoutConstrainedFieldRequiresUnsafe {
span,
unsafe_not_inherited_note,
},
), ),
CallToFunctionWith(did) => tcx.emit_spanned_lint( CallToFunctionWith(did) => tcx.emit_spanned_lint(
UNSAFE_OP_IN_UNSAFE_FN, UNSAFE_OP_IN_UNSAFE_FN,
@ -617,6 +665,7 @@ impl UnsafeOpKind {
UnsafeOpInUnsafeFnCallToFunctionWithRequiresUnsafe { UnsafeOpInUnsafeFnCallToFunctionWithRequiresUnsafe {
span, span,
function: &with_no_trimmed_paths!(tcx.def_path_str(*did)), function: &with_no_trimmed_paths!(tcx.def_path_str(*did)),
unsafe_not_inherited_note,
}, },
), ),
} }
@ -831,6 +880,7 @@ pub fn thir_check_unsafety(tcx: TyCtxt<'_>, def: LocalDefId) {
param_env: tcx.param_env(def), param_env: tcx.param_env(def),
inside_adt: false, inside_adt: false,
warnings: &mut warnings, warnings: &mut warnings,
suggest_unsafe_block: true,
}; };
visitor.visit_expr(&thir[expr]); visitor.visit_expr(&thir[expr]);

View File

@ -29,6 +29,8 @@ pub struct UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafe<'a> {
#[label] #[label]
pub span: Span, pub span: Span,
pub function: &'a str, pub function: &'a str,
#[subdiagnostic]
pub unsafe_not_inherited_note: Option<UnsafeNotInheritedLintNote>,
} }
#[derive(LintDiagnostic)] #[derive(LintDiagnostic)]
@ -37,6 +39,8 @@ pub struct UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafe<'a> {
pub struct UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafeNameless { pub struct UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafeNameless {
#[label] #[label]
pub span: Span, pub span: Span,
#[subdiagnostic]
pub unsafe_not_inherited_note: Option<UnsafeNotInheritedLintNote>,
} }
#[derive(LintDiagnostic)] #[derive(LintDiagnostic)]
@ -45,6 +49,8 @@ pub struct UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafeNameless {
pub struct UnsafeOpInUnsafeFnUseOfInlineAssemblyRequiresUnsafe { pub struct UnsafeOpInUnsafeFnUseOfInlineAssemblyRequiresUnsafe {
#[label] #[label]
pub span: Span, pub span: Span,
#[subdiagnostic]
pub unsafe_not_inherited_note: Option<UnsafeNotInheritedLintNote>,
} }
#[derive(LintDiagnostic)] #[derive(LintDiagnostic)]
@ -53,6 +59,8 @@ pub struct UnsafeOpInUnsafeFnUseOfInlineAssemblyRequiresUnsafe {
pub struct UnsafeOpInUnsafeFnInitializingTypeWithRequiresUnsafe { pub struct UnsafeOpInUnsafeFnInitializingTypeWithRequiresUnsafe {
#[label] #[label]
pub span: Span, pub span: Span,
#[subdiagnostic]
pub unsafe_not_inherited_note: Option<UnsafeNotInheritedLintNote>,
} }
#[derive(LintDiagnostic)] #[derive(LintDiagnostic)]
@ -61,6 +69,8 @@ pub struct UnsafeOpInUnsafeFnInitializingTypeWithRequiresUnsafe {
pub struct UnsafeOpInUnsafeFnUseOfMutableStaticRequiresUnsafe { pub struct UnsafeOpInUnsafeFnUseOfMutableStaticRequiresUnsafe {
#[label] #[label]
pub span: Span, pub span: Span,
#[subdiagnostic]
pub unsafe_not_inherited_note: Option<UnsafeNotInheritedLintNote>,
} }
#[derive(LintDiagnostic)] #[derive(LintDiagnostic)]
@ -69,6 +79,8 @@ pub struct UnsafeOpInUnsafeFnUseOfMutableStaticRequiresUnsafe {
pub struct UnsafeOpInUnsafeFnUseOfExternStaticRequiresUnsafe { pub struct UnsafeOpInUnsafeFnUseOfExternStaticRequiresUnsafe {
#[label] #[label]
pub span: Span, pub span: Span,
#[subdiagnostic]
pub unsafe_not_inherited_note: Option<UnsafeNotInheritedLintNote>,
} }
#[derive(LintDiagnostic)] #[derive(LintDiagnostic)]
@ -77,6 +89,8 @@ pub struct UnsafeOpInUnsafeFnUseOfExternStaticRequiresUnsafe {
pub struct UnsafeOpInUnsafeFnDerefOfRawPointerRequiresUnsafe { pub struct UnsafeOpInUnsafeFnDerefOfRawPointerRequiresUnsafe {
#[label] #[label]
pub span: Span, pub span: Span,
#[subdiagnostic]
pub unsafe_not_inherited_note: Option<UnsafeNotInheritedLintNote>,
} }
#[derive(LintDiagnostic)] #[derive(LintDiagnostic)]
@ -85,6 +99,8 @@ pub struct UnsafeOpInUnsafeFnDerefOfRawPointerRequiresUnsafe {
pub struct UnsafeOpInUnsafeFnAccessToUnionFieldRequiresUnsafe { pub struct UnsafeOpInUnsafeFnAccessToUnionFieldRequiresUnsafe {
#[label] #[label]
pub span: Span, pub span: Span,
#[subdiagnostic]
pub unsafe_not_inherited_note: Option<UnsafeNotInheritedLintNote>,
} }
#[derive(LintDiagnostic)] #[derive(LintDiagnostic)]
@ -93,6 +109,8 @@ pub struct UnsafeOpInUnsafeFnAccessToUnionFieldRequiresUnsafe {
pub struct UnsafeOpInUnsafeFnMutationOfLayoutConstrainedFieldRequiresUnsafe { pub struct UnsafeOpInUnsafeFnMutationOfLayoutConstrainedFieldRequiresUnsafe {
#[label] #[label]
pub span: Span, pub span: Span,
#[subdiagnostic]
pub unsafe_not_inherited_note: Option<UnsafeNotInheritedLintNote>,
} }
#[derive(LintDiagnostic)] #[derive(LintDiagnostic)]
@ -100,6 +118,8 @@ pub struct UnsafeOpInUnsafeFnMutationOfLayoutConstrainedFieldRequiresUnsafe {
pub struct UnsafeOpInUnsafeFnBorrowOfLayoutConstrainedFieldRequiresUnsafe { pub struct UnsafeOpInUnsafeFnBorrowOfLayoutConstrainedFieldRequiresUnsafe {
#[label] #[label]
pub span: Span, pub span: Span,
#[subdiagnostic]
pub unsafe_not_inherited_note: Option<UnsafeNotInheritedLintNote>,
} }
#[derive(LintDiagnostic)] #[derive(LintDiagnostic)]
@ -109,6 +129,8 @@ pub struct UnsafeOpInUnsafeFnCallToFunctionWithRequiresUnsafe<'a> {
#[label] #[label]
pub span: Span, pub span: Span,
pub function: &'a str, pub function: &'a str,
#[subdiagnostic]
pub unsafe_not_inherited_note: Option<UnsafeNotInheritedLintNote>,
} }
#[derive(Diagnostic)] #[derive(Diagnostic)]
@ -376,6 +398,27 @@ pub struct UnsafeNotInheritedNote {
pub span: Span, pub span: Span,
} }
pub struct UnsafeNotInheritedLintNote {
pub signature_span: Span,
pub body_span: Span,
}
impl AddToDiagnostic for UnsafeNotInheritedLintNote {
fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
where
F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
{
diag.span_note(self.signature_span, fluent::mir_build_unsafe_fn_safe_body);
let body_start = self.body_span.shrink_to_lo();
let body_end = self.body_span.shrink_to_hi();
diag.tool_only_multipart_suggestion(
fluent::mir_build_wrap_suggestion,
vec![(body_start, "{ unsafe ".into()), (body_end, "}".into())],
Applicability::MaybeIncorrect,
);
}
}
#[derive(LintDiagnostic)] #[derive(LintDiagnostic)]
#[diag(mir_build_unused_unsafe)] #[diag(mir_build_unused_unsafe)]
pub struct UnusedUnsafe { pub struct UnusedUnsafe {

View File

@ -3962,7 +3962,7 @@ impl<'test> TestCx<'test> {
// And finally, compile the fixed code and make sure it both // And finally, compile the fixed code and make sure it both
// succeeds and has no diagnostics. // succeeds and has no diagnostics.
let rustc = self.make_compile_args( let rustc = self.make_compile_args(
&self.testpaths.file.with_extension(UI_FIXED), &self.expected_output_path(UI_FIXED),
TargetLocation::ThisFile(self.make_exe_name()), TargetLocation::ThisFile(self.make_exe_name()),
emit_metadata, emit_metadata,
AllowUnused::No, AllowUnused::No,

View File

@ -1,5 +1,5 @@
error[E0658]: mutable references are not allowed in constant functions error[E0658]: mutable references are not allowed in constant functions
--> $DIR/thread-local-static.rs:7:12 --> $DIR/thread-local-static.rs:10:12
| |
LL | const fn g(x: &mut [u32; 8]) { LL | const fn g(x: &mut [u32; 8]) {
| ^ | ^
@ -8,13 +8,13 @@ LL | const fn g(x: &mut [u32; 8]) {
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error[E0625]: thread-local statics cannot be accessed at compile-time error[E0625]: thread-local statics cannot be accessed at compile-time
--> $DIR/thread-local-static.rs:9:28 --> $DIR/thread-local-static.rs:12:28
| |
LL | std::mem::swap(x, &mut STATIC_VAR_2) LL | std::mem::swap(x, &mut STATIC_VAR_2)
| ^^^^^^^^^^^^ | ^^^^^^^^^^^^
error[E0013]: constant functions cannot refer to statics error[E0013]: constant functions cannot refer to statics
--> $DIR/thread-local-static.rs:9:28 --> $DIR/thread-local-static.rs:12:28
| |
LL | std::mem::swap(x, &mut STATIC_VAR_2) LL | std::mem::swap(x, &mut STATIC_VAR_2)
| ^^^^^^^^^^^^ | ^^^^^^^^^^^^
@ -22,7 +22,7 @@ LL | std::mem::swap(x, &mut STATIC_VAR_2)
= help: consider extracting the value of the `static` to a `const`, and referring to that = help: consider extracting the value of the `static` to a `const`, and referring to that
error[E0658]: mutable references are not allowed in constant functions error[E0658]: mutable references are not allowed in constant functions
--> $DIR/thread-local-static.rs:9:23 --> $DIR/thread-local-static.rs:12:23
| |
LL | std::mem::swap(x, &mut STATIC_VAR_2) LL | std::mem::swap(x, &mut STATIC_VAR_2)
| ^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^
@ -31,7 +31,7 @@ LL | std::mem::swap(x, &mut STATIC_VAR_2)
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error[E0133]: use of mutable static is unsafe and requires unsafe function or block error[E0133]: use of mutable static is unsafe and requires unsafe function or block
--> $DIR/thread-local-static.rs:9:23 --> $DIR/thread-local-static.rs:12:23
| |
LL | std::mem::swap(x, &mut STATIC_VAR_2) LL | std::mem::swap(x, &mut STATIC_VAR_2)
| ^^^^^^^^^^^^^^^^^ use of mutable static | ^^^^^^^^^^^^^^^^^ use of mutable static

View File

@ -1,7 +1,10 @@
// edition:2018 // edition:2018
// revisions: mir thir
//thir: -Zthir-unsafeck
#![feature(thread_local)] #![feature(thread_local)]
#![feature(const_swap)] #![feature(const_swap)]
#[thread_local] #[thread_local]
static mut STATIC_VAR_2: [u32; 8] = [4; 8]; static mut STATIC_VAR_2: [u32; 8] = [4; 8];
const fn g(x: &mut [u32; 8]) { const fn g(x: &mut [u32; 8]) {

View File

@ -0,0 +1,44 @@
error[E0658]: mutable references are not allowed in constant functions
--> $DIR/thread-local-static.rs:10:12
|
LL | const fn g(x: &mut [u32; 8]) {
| ^
|
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error[E0625]: thread-local statics cannot be accessed at compile-time
--> $DIR/thread-local-static.rs:12:28
|
LL | std::mem::swap(x, &mut STATIC_VAR_2)
| ^^^^^^^^^^^^
error[E0013]: constant functions cannot refer to statics
--> $DIR/thread-local-static.rs:12:28
|
LL | std::mem::swap(x, &mut STATIC_VAR_2)
| ^^^^^^^^^^^^
|
= help: consider extracting the value of the `static` to a `const`, and referring to that
error[E0658]: mutable references are not allowed in constant functions
--> $DIR/thread-local-static.rs:12:23
|
LL | std::mem::swap(x, &mut STATIC_VAR_2)
| ^^^^^^^^^^^^^^^^^
|
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error[E0133]: use of mutable static is unsafe and requires unsafe function or block
--> $DIR/thread-local-static.rs:12:23
|
LL | std::mem::swap(x, &mut STATIC_VAR_2)
| ^^^^^^^^^^^^^^^^^ use of mutable static
|
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
error: aborting due to 5 previous errors
Some errors have detailed explanations: E0013, E0133, E0625, E0658.
For more information about an error, try `rustc --explain E0013`.

View File

@ -1,5 +1,5 @@
error[E0133]: access to union field is unsafe and requires unsafe function or block error[E0133]: access to union field is unsafe and requires unsafe function or block
--> $DIR/union-unsafe.rs:33:5 --> $DIR/union-unsafe.rs:34:5
| |
LL | *(u.p) = 13; LL | *(u.p) = 13;
| ^^^^^^^^^^^ access to union field | ^^^^^^^^^^^ access to union field
@ -7,7 +7,7 @@ LL | *(u.p) = 13;
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
error[E0133]: access to union field is unsafe and requires unsafe function or block error[E0133]: access to union field is unsafe and requires unsafe function or block
--> $DIR/union-unsafe.rs:46:6 --> $DIR/union-unsafe.rs:47:6
| |
LL | *u3.a = T::default(); LL | *u3.a = T::default();
| ^^^^ access to union field | ^^^^ access to union field
@ -15,7 +15,7 @@ LL | *u3.a = T::default();
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
error[E0133]: access to union field is unsafe and requires unsafe function or block error[E0133]: access to union field is unsafe and requires unsafe function or block
--> $DIR/union-unsafe.rs:52:6 --> $DIR/union-unsafe.rs:53:6
| |
LL | *u3.a = T::default(); LL | *u3.a = T::default();
| ^^^^ access to union field | ^^^^ access to union field
@ -23,7 +23,7 @@ LL | *u3.a = T::default();
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
error[E0133]: access to union field is unsafe and requires unsafe function or block error[E0133]: access to union field is unsafe and requires unsafe function or block
--> $DIR/union-unsafe.rs:60:13 --> $DIR/union-unsafe.rs:61:13
| |
LL | let a = u1.a; LL | let a = u1.a;
| ^^^^ access to union field | ^^^^ access to union field
@ -31,7 +31,7 @@ LL | let a = u1.a;
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
error[E0133]: access to union field is unsafe and requires unsafe function or block error[E0133]: access to union field is unsafe and requires unsafe function or block
--> $DIR/union-unsafe.rs:63:14 --> $DIR/union-unsafe.rs:64:14
| |
LL | let U1 { a } = u1; LL | let U1 { a } = u1;
| ^ access to union field | ^ access to union field
@ -39,7 +39,7 @@ LL | let U1 { a } = u1;
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
error[E0133]: access to union field is unsafe and requires unsafe function or block error[E0133]: access to union field is unsafe and requires unsafe function or block
--> $DIR/union-unsafe.rs:64:12 --> $DIR/union-unsafe.rs:65:12
| |
LL | if let U1 { a: 12 } = u1 {} LL | if let U1 { a: 12 } = u1 {}
| ^^^^^^^^^^^^ access to union field | ^^^^^^^^^^^^ access to union field
@ -47,7 +47,15 @@ LL | if let U1 { a: 12 } = u1 {}
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
error[E0133]: access to union field is unsafe and requires unsafe function or block error[E0133]: access to union field is unsafe and requires unsafe function or block
--> $DIR/union-unsafe.rs:69:6 --> $DIR/union-unsafe.rs:66:12
|
LL | if let Some(U1 { a: 13 }) = Some(u1) {}
| ^^^^^^^^^^^^^^^^^^ access to union field
|
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
error[E0133]: access to union field is unsafe and requires unsafe function or block
--> $DIR/union-unsafe.rs:71:6
| |
LL | *u2.a = String::from("new"); LL | *u2.a = String::from("new");
| ^^^^ access to union field | ^^^^ access to union field
@ -55,7 +63,7 @@ LL | *u2.a = String::from("new");
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
error[E0133]: access to union field is unsafe and requires unsafe function or block error[E0133]: access to union field is unsafe and requires unsafe function or block
--> $DIR/union-unsafe.rs:73:6 --> $DIR/union-unsafe.rs:75:6
| |
LL | *u3.a = 1; LL | *u3.a = 1;
| ^^^^ access to union field | ^^^^ access to union field
@ -63,13 +71,13 @@ LL | *u3.a = 1;
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
error[E0133]: access to union field is unsafe and requires unsafe function or block error[E0133]: access to union field is unsafe and requires unsafe function or block
--> $DIR/union-unsafe.rs:77:6 --> $DIR/union-unsafe.rs:79:6
| |
LL | *u3.a = String::from("new"); LL | *u3.a = String::from("new");
| ^^^^ access to union field | ^^^^ access to union field
| |
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
error: aborting due to 9 previous errors error: aborting due to 10 previous errors
For more information about this error, try `rustc --explain E0133`. For more information about this error, try `rustc --explain E0133`.

View File

@ -1,30 +1,31 @@
// revisions: mir thir // revisions: mir thir
// [thir]compile-flags: -Z thir-unsafeck // [thir]compile-flags: -Z thir-unsafeck
use std::mem::ManuallyDrop;
use std::cell::RefCell; use std::cell::RefCell;
use std::mem::ManuallyDrop;
union U1 { union U1 {
a: u8 a: u8,
} }
union U2 { union U2 {
a: ManuallyDrop<String> a: ManuallyDrop<String>,
} }
union U3<T> { union U3<T> {
a: ManuallyDrop<T> a: ManuallyDrop<T>,
} }
union U4<T: Copy> { union U4<T: Copy> {
a: T a: T,
} }
union URef { union URef {
p: &'static mut i32, p: &'static mut i32,
} }
union URefCell { // field that does not drop but is not `Copy`, either union URefCell {
// field that does not drop but is not `Copy`, either
a: (ManuallyDrop<RefCell<i32>>, i32), a: (ManuallyDrop<RefCell<i32>>, i32),
} }
@ -62,6 +63,7 @@ fn main() {
let U1 { a } = u1; //~ ERROR access to union field is unsafe let U1 { a } = u1; //~ ERROR access to union field is unsafe
if let U1 { a: 12 } = u1 {} //~ ERROR access to union field is unsafe if let U1 { a: 12 } = u1 {} //~ ERROR access to union field is unsafe
if let Some(U1 { a: 13 }) = Some(u1) {} //~ ERROR access to union field is unsafe
// let U1 { .. } = u1; // OK // let U1 { .. } = u1; // OK
let mut u2 = U2 { a: ManuallyDrop::new(String::from("old")) }; // OK let mut u2 = U2 { a: ManuallyDrop::new(String::from("old")) }; // OK

View File

@ -1,5 +1,5 @@
error[E0133]: access to union field is unsafe and requires unsafe function or block error[E0133]: access to union field is unsafe and requires unsafe function or block
--> $DIR/union-unsafe.rs:33:6 --> $DIR/union-unsafe.rs:34:6
| |
LL | *(u.p) = 13; LL | *(u.p) = 13;
| ^^^^^ access to union field | ^^^^^ access to union field
@ -7,7 +7,7 @@ LL | *(u.p) = 13;
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
error[E0133]: access to union field is unsafe and requires unsafe function or block error[E0133]: access to union field is unsafe and requires unsafe function or block
--> $DIR/union-unsafe.rs:46:6 --> $DIR/union-unsafe.rs:47:6
| |
LL | *u3.a = T::default(); LL | *u3.a = T::default();
| ^^^^ access to union field | ^^^^ access to union field
@ -15,7 +15,7 @@ LL | *u3.a = T::default();
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
error[E0133]: access to union field is unsafe and requires unsafe function or block error[E0133]: access to union field is unsafe and requires unsafe function or block
--> $DIR/union-unsafe.rs:52:6 --> $DIR/union-unsafe.rs:53:6
| |
LL | *u3.a = T::default(); LL | *u3.a = T::default();
| ^^^^ access to union field | ^^^^ access to union field
@ -23,7 +23,7 @@ LL | *u3.a = T::default();
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
error[E0133]: access to union field is unsafe and requires unsafe function or block error[E0133]: access to union field is unsafe and requires unsafe function or block
--> $DIR/union-unsafe.rs:60:13 --> $DIR/union-unsafe.rs:61:13
| |
LL | let a = u1.a; LL | let a = u1.a;
| ^^^^ access to union field | ^^^^ access to union field
@ -31,7 +31,7 @@ LL | let a = u1.a;
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
error[E0133]: access to union field is unsafe and requires unsafe function or block error[E0133]: access to union field is unsafe and requires unsafe function or block
--> $DIR/union-unsafe.rs:63:14 --> $DIR/union-unsafe.rs:64:14
| |
LL | let U1 { a } = u1; LL | let U1 { a } = u1;
| ^ access to union field | ^ access to union field
@ -39,15 +39,23 @@ LL | let U1 { a } = u1;
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
error[E0133]: access to union field is unsafe and requires unsafe function or block error[E0133]: access to union field is unsafe and requires unsafe function or block
--> $DIR/union-unsafe.rs:64:8 --> $DIR/union-unsafe.rs:65:20
| |
LL | if let U1 { a: 12 } = u1 {} LL | if let U1 { a: 12 } = u1 {}
| ^^^^^^^^^^^^^^^^^^^^^ access to union field | ^^ access to union field
| |
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
error[E0133]: access to union field is unsafe and requires unsafe function or block error[E0133]: access to union field is unsafe and requires unsafe function or block
--> $DIR/union-unsafe.rs:69:6 --> $DIR/union-unsafe.rs:66:25
|
LL | if let Some(U1 { a: 13 }) = Some(u1) {}
| ^^ access to union field
|
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
error[E0133]: access to union field is unsafe and requires unsafe function or block
--> $DIR/union-unsafe.rs:71:6
| |
LL | *u2.a = String::from("new"); LL | *u2.a = String::from("new");
| ^^^^ access to union field | ^^^^ access to union field
@ -55,7 +63,7 @@ LL | *u2.a = String::from("new");
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
error[E0133]: access to union field is unsafe and requires unsafe function or block error[E0133]: access to union field is unsafe and requires unsafe function or block
--> $DIR/union-unsafe.rs:73:6 --> $DIR/union-unsafe.rs:75:6
| |
LL | *u3.a = 1; LL | *u3.a = 1;
| ^^^^ access to union field | ^^^^ access to union field
@ -63,13 +71,13 @@ LL | *u3.a = 1;
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
error[E0133]: access to union field is unsafe and requires unsafe function or block error[E0133]: access to union field is unsafe and requires unsafe function or block
--> $DIR/union-unsafe.rs:77:6 --> $DIR/union-unsafe.rs:79:6
| |
LL | *u3.a = String::from("new"); LL | *u3.a = String::from("new");
| ^^^^ access to union field | ^^^^ access to union field
| |
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
error: aborting due to 9 previous errors error: aborting due to 10 previous errors
For more information about this error, try `rustc --explain E0133`. For more information about this error, try `rustc --explain E0133`.

View File

@ -1,12 +1,12 @@
warning: call to unsafe function is unsafe and requires unsafe block (error E0133) warning: call to unsafe function is unsafe and requires unsafe block (error E0133)
--> $DIR/edition-2024-unsafe_op_in_unsafe_fn.rs:12:5 --> $DIR/edition-2024-unsafe_op_in_unsafe_fn.rs:13:5
| |
LL | unsf(); LL | unsf();
| ^^^^^^ call to unsafe function | ^^^^^^ call to unsafe function
| |
= note: consult the function's documentation for information on how to avoid undefined behavior = note: consult the function's documentation for information on how to avoid undefined behavior
note: an unsafe function restricts its caller, but its body is safe by default note: an unsafe function restricts its caller, but its body is safe by default
--> $DIR/edition-2024-unsafe_op_in_unsafe_fn.rs:11:1 --> $DIR/edition-2024-unsafe_op_in_unsafe_fn.rs:12:1
| |
LL | unsafe fn foo() { LL | unsafe fn foo() {
| ^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^

View File

@ -1,17 +1,21 @@
// edition: 2024 // edition: 2024
// compile-flags: -Zunstable-options // compile-flags: -Zunstable-options
// check-pass // check-pass
// revisions: mir thir
// [thir]compile-flags: -Zthir-unsafeck
#![crate_type = "lib"] #![crate_type = "lib"]
#![deny(unused_unsafe)] #![deny(unused_unsafe)]
unsafe fn unsf() {} unsafe fn unsf() {}
unsafe fn foo() { unsafe fn foo() {
unsf(); unsf();
//~^ WARN call to unsafe function is unsafe and requires unsafe block //[mir]~^ WARN call to unsafe function is unsafe and requires unsafe block
//[thir]~^^ WARN call to unsafe function `unsf` is unsafe and requires unsafe block
// no unused_unsafe // no unused_unsafe
unsafe { unsf(); } unsafe {
unsf();
}
} }

View File

@ -0,0 +1,16 @@
warning: call to unsafe function `unsf` is unsafe and requires unsafe block (error E0133)
--> $DIR/edition-2024-unsafe_op_in_unsafe_fn.rs:13:5
|
LL | unsf();
| ^^^^^^ call to unsafe function
|
= note: consult the function's documentation for information on how to avoid undefined behavior
note: an unsafe function restricts its caller, but its body is safe by default
--> $DIR/edition-2024-unsafe_op_in_unsafe_fn.rs:12:1
|
LL | unsafe fn foo() {
| ^^^^^^^^^^^^^^^
= note: `#[warn(unsafe_op_in_unsafe_fn)]` on by default
warning: 1 warning emitted

View File

@ -6,6 +6,14 @@ LL | let y = &mut x.0;
| |
= note: mutating layout constrained fields cannot statically be checked for valid values = note: mutating layout constrained fields cannot statically be checked for valid values
error: aborting due to previous error error[E0133]: mutation of layout constrained field is unsafe and requires unsafe function or block
--> $DIR/ranged_ints2.rs:12:25
|
LL | if let Some(NonZero(ref mut y)) = Some(x) {}
| ^^^^^^^^^ mutation of layout constrained field
|
= note: mutating layout constrained fields cannot statically be checked for valid values
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0133`. For more information about this error, try `rustc --explain E0133`.

View File

@ -9,4 +9,5 @@ pub(crate) struct NonZero<T>(pub(crate) T);
fn main() { fn main() {
let mut x = unsafe { NonZero(1) }; let mut x = unsafe { NonZero(1) };
let y = &mut x.0; //~ ERROR mutation of layout constrained field is unsafe let y = &mut x.0; //~ ERROR mutation of layout constrained field is unsafe
if let Some(NonZero(ref mut y)) = Some(x) {} //~ ERROR mutation of layout constrained field is unsafe
} }

View File

@ -6,6 +6,14 @@ LL | let y = &mut x.0;
| |
= note: mutating layout constrained fields cannot statically be checked for valid values = note: mutating layout constrained fields cannot statically be checked for valid values
error: aborting due to previous error error[E0133]: mutation of layout constrained field is unsafe and requires unsafe function or block
--> $DIR/ranged_ints2.rs:12:25
|
LL | if let Some(NonZero(ref mut y)) = Some(x) {}
| ^^^^^^^^^ mutation of layout constrained field
|
= note: mutating layout constrained fields cannot statically be checked for valid values
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0133`. For more information about this error, try `rustc --explain E0133`.

View File

@ -5,6 +5,11 @@ LL | unsf();
| ^^^^^^ call to unsafe function | ^^^^^^ call to unsafe function
| |
= note: consult the function's documentation for information on how to avoid undefined behavior = note: consult the function's documentation for information on how to avoid undefined behavior
note: an unsafe function restricts its caller, but its body is safe by default
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:11:1
|
LL | unsafe fn deny_level() {
| ^^^^^^^^^^^^^^^^^^^^^^
note: the lint level is defined here note: the lint level is defined here
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:4:9 --> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:4:9
| |
@ -46,6 +51,11 @@ LL | unsf();
| ^^^^^^ call to unsafe function | ^^^^^^ call to unsafe function
| |
= note: consult the function's documentation for information on how to avoid undefined behavior = note: consult the function's documentation for information on how to avoid undefined behavior
note: an unsafe function restricts its caller, but its body is safe by default
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:27:1
|
LL | unsafe fn warning_level() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^
note: the lint level is defined here note: the lint level is defined here
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:26:8 --> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:26:8
| |

View File

@ -1,7 +1,10 @@
// run-rustfix // run-rustfix
// aux-build:external_unsafe_macro.rs // aux-build:external_unsafe_macro.rs
// revisions: mir thir
// [thir]compile-flags: -Zthir-unsafeck
#![deny(unsafe_op_in_unsafe_fn)] //~ NOTE #![deny(unsafe_op_in_unsafe_fn)] //~ NOTE
#![crate_name = "wrapping_unsafe_block_sugg"]
extern crate external_unsafe_macro; extern crate external_unsafe_macro;
@ -9,11 +12,13 @@ unsafe fn unsf() {}
pub unsafe fn foo() { unsafe { pub unsafe fn foo() { unsafe {
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default //~^ NOTE an unsafe function restricts its caller, but its body is safe by default
unsf(); //~ ERROR call to unsafe function is unsafe unsf(); //[mir]~ ERROR call to unsafe function is unsafe
//~^ NOTE //[thir]~^ ERROR call to unsafe function `unsf` is unsafe
//~^^ NOTE
//~| NOTE //~| NOTE
unsf(); //~ ERROR call to unsafe function is unsafe unsf(); //[mir]~ ERROR call to unsafe function is unsafe
//~^ NOTE //[thir]~^ ERROR call to unsafe function `unsf` is unsafe
//~^^ NOTE
//~| NOTE //~| NOTE
}} }}
@ -39,10 +44,12 @@ pub unsafe fn baz() -> i32 { unsafe {
}} }}
macro_rules! unsafe_macro { () => (unsf()) } macro_rules! unsafe_macro { () => (unsf()) }
//~^ ERROR call to unsafe function is unsafe //[mir]~^ ERROR call to unsafe function is unsafe
//[thir]~^^ ERROR call to unsafe function `unsf` is unsafe
//~| NOTE //~| NOTE
//~| NOTE //~| NOTE
//~| ERROR call to unsafe function is unsafe //[mir]~| ERROR call to unsafe function is unsafe
//[thir]~| ERROR call to unsafe function `unsf` is unsafe
//~| NOTE //~| NOTE
//~| NOTE //~| NOTE

View File

@ -1,21 +1,3 @@
error: call to unsafe function is unsafe and requires unsafe block (error E0133)
--> $DIR/wrapping-unsafe-block-sugg.rs:12:5
|
LL | unsf();
| ^^^^^^ call to unsafe function
|
= note: consult the function's documentation for information on how to avoid undefined behavior
note: an unsafe function restricts its caller, but its body is safe by default
--> $DIR/wrapping-unsafe-block-sugg.rs:10:1
|
LL | pub unsafe fn foo() {
| ^^^^^^^^^^^^^^^^^^^
note: the lint level is defined here
--> $DIR/wrapping-unsafe-block-sugg.rs:4:9
|
LL | #![deny(unsafe_op_in_unsafe_fn)]
| ^^^^^^^^^^^^^^^^^^^^^^
error: call to unsafe function is unsafe and requires unsafe block (error E0133) error: call to unsafe function is unsafe and requires unsafe block (error E0133)
--> $DIR/wrapping-unsafe-block-sugg.rs:15:5 --> $DIR/wrapping-unsafe-block-sugg.rs:15:5
| |
@ -23,22 +5,40 @@ LL | unsf();
| ^^^^^^ call to unsafe function | ^^^^^^ call to unsafe function
| |
= note: consult the function's documentation for information on how to avoid undefined behavior = note: consult the function's documentation for information on how to avoid undefined behavior
note: an unsafe function restricts its caller, but its body is safe by default
--> $DIR/wrapping-unsafe-block-sugg.rs:13:1
|
LL | pub unsafe fn foo() {
| ^^^^^^^^^^^^^^^^^^^
note: the lint level is defined here
--> $DIR/wrapping-unsafe-block-sugg.rs:6:9
|
LL | #![deny(unsafe_op_in_unsafe_fn)]
| ^^^^^^^^^^^^^^^^^^^^^^
error: call to unsafe function is unsafe and requires unsafe block (error E0133)
--> $DIR/wrapping-unsafe-block-sugg.rs:19:5
|
LL | unsf();
| ^^^^^^ call to unsafe function
|
= note: consult the function's documentation for information on how to avoid undefined behavior
error: dereference of raw pointer is unsafe and requires unsafe block (error E0133) error: dereference of raw pointer is unsafe and requires unsafe block (error E0133)
--> $DIR/wrapping-unsafe-block-sugg.rs:22:13 --> $DIR/wrapping-unsafe-block-sugg.rs:27:13
| |
LL | let y = *x; LL | let y = *x;
| ^^ dereference of raw pointer | ^^ dereference of raw pointer
| |
= note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior = note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
note: an unsafe function restricts its caller, but its body is safe by default note: an unsafe function restricts its caller, but its body is safe by default
--> $DIR/wrapping-unsafe-block-sugg.rs:20:1 --> $DIR/wrapping-unsafe-block-sugg.rs:25:1
| |
LL | pub unsafe fn bar(x: *const i32) -> i32 { LL | pub unsafe fn bar(x: *const i32) -> i32 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: dereference of raw pointer is unsafe and requires unsafe block (error E0133) error: dereference of raw pointer is unsafe and requires unsafe block (error E0133)
--> $DIR/wrapping-unsafe-block-sugg.rs:25:9 --> $DIR/wrapping-unsafe-block-sugg.rs:30:9
| |
LL | y + *x LL | y + *x
| ^^ dereference of raw pointer | ^^ dereference of raw pointer
@ -46,20 +46,20 @@ LL | y + *x
= note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior = note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
error: use of mutable static is unsafe and requires unsafe block (error E0133) error: use of mutable static is unsafe and requires unsafe block (error E0133)
--> $DIR/wrapping-unsafe-block-sugg.rs:33:13 --> $DIR/wrapping-unsafe-block-sugg.rs:38:13
| |
LL | let y = BAZ; LL | let y = BAZ;
| ^^^ use of mutable static | ^^^ use of mutable static
| |
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior = note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
note: an unsafe function restricts its caller, but its body is safe by default note: an unsafe function restricts its caller, but its body is safe by default
--> $DIR/wrapping-unsafe-block-sugg.rs:31:1 --> $DIR/wrapping-unsafe-block-sugg.rs:36:1
| |
LL | pub unsafe fn baz() -> i32 { LL | pub unsafe fn baz() -> i32 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^
error: use of mutable static is unsafe and requires unsafe block (error E0133) error: use of mutable static is unsafe and requires unsafe block (error E0133)
--> $DIR/wrapping-unsafe-block-sugg.rs:36:9 --> $DIR/wrapping-unsafe-block-sugg.rs:41:9
| |
LL | y + BAZ LL | y + BAZ
| ^^^ use of mutable static | ^^^ use of mutable static
@ -67,7 +67,7 @@ LL | y + BAZ
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior = note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
error: call to unsafe function is unsafe and requires unsafe block (error E0133) error: call to unsafe function is unsafe and requires unsafe block (error E0133)
--> $DIR/wrapping-unsafe-block-sugg.rs:41:36 --> $DIR/wrapping-unsafe-block-sugg.rs:46:36
| |
LL | macro_rules! unsafe_macro { () => (unsf()) } LL | macro_rules! unsafe_macro { () => (unsf()) }
| ^^^^^^ call to unsafe function | ^^^^^^ call to unsafe function
@ -77,14 +77,14 @@ LL | unsafe_macro!();
| |
= note: consult the function's documentation for information on how to avoid undefined behavior = note: consult the function's documentation for information on how to avoid undefined behavior
note: an unsafe function restricts its caller, but its body is safe by default note: an unsafe function restricts its caller, but its body is safe by default
--> $DIR/wrapping-unsafe-block-sugg.rs:49:1 --> $DIR/wrapping-unsafe-block-sugg.rs:56:1
| |
LL | pub unsafe fn unsafe_in_macro() { LL | pub unsafe fn unsafe_in_macro() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: this error originates in the macro `unsafe_macro` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the macro `unsafe_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
error: call to unsafe function is unsafe and requires unsafe block (error E0133) error: call to unsafe function is unsafe and requires unsafe block (error E0133)
--> $DIR/wrapping-unsafe-block-sugg.rs:41:36 --> $DIR/wrapping-unsafe-block-sugg.rs:46:36
| |
LL | macro_rules! unsafe_macro { () => (unsf()) } LL | macro_rules! unsafe_macro { () => (unsf()) }
| ^^^^^^ call to unsafe function | ^^^^^^ call to unsafe function

View File

@ -1,7 +1,10 @@
// run-rustfix // run-rustfix
// aux-build:external_unsafe_macro.rs // aux-build:external_unsafe_macro.rs
// revisions: mir thir
// [thir]compile-flags: -Zthir-unsafeck
#![deny(unsafe_op_in_unsafe_fn)] //~ NOTE #![deny(unsafe_op_in_unsafe_fn)] //~ NOTE
#![crate_name = "wrapping_unsafe_block_sugg"]
extern crate external_unsafe_macro; extern crate external_unsafe_macro;
@ -9,11 +12,13 @@ unsafe fn unsf() {}
pub unsafe fn foo() { pub unsafe fn foo() {
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default //~^ NOTE an unsafe function restricts its caller, but its body is safe by default
unsf(); //~ ERROR call to unsafe function is unsafe unsf(); //[mir]~ ERROR call to unsafe function is unsafe
//~^ NOTE //[thir]~^ ERROR call to unsafe function `unsf` is unsafe
//~^^ NOTE
//~| NOTE //~| NOTE
unsf(); //~ ERROR call to unsafe function is unsafe unsf(); //[mir]~ ERROR call to unsafe function is unsafe
//~^ NOTE //[thir]~^ ERROR call to unsafe function `unsf` is unsafe
//~^^ NOTE
//~| NOTE //~| NOTE
} }
@ -39,10 +44,12 @@ pub unsafe fn baz() -> i32 {
} }
macro_rules! unsafe_macro { () => (unsf()) } macro_rules! unsafe_macro { () => (unsf()) }
//~^ ERROR call to unsafe function is unsafe //[mir]~^ ERROR call to unsafe function is unsafe
//[thir]~^^ ERROR call to unsafe function `unsf` is unsafe
//~| NOTE //~| NOTE
//~| NOTE //~| NOTE
//~| ERROR call to unsafe function is unsafe //[mir]~| ERROR call to unsafe function is unsafe
//[thir]~| ERROR call to unsafe function `unsf` is unsafe
//~| NOTE //~| NOTE
//~| NOTE //~| NOTE

View File

@ -0,0 +1,73 @@
// run-rustfix
// aux-build:external_unsafe_macro.rs
// revisions: mir thir
// [thir]compile-flags: -Zthir-unsafeck
#![deny(unsafe_op_in_unsafe_fn)] //~ NOTE
#![crate_name = "wrapping_unsafe_block_sugg"]
extern crate external_unsafe_macro;
unsafe fn unsf() {}
pub unsafe fn foo() { unsafe {
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default
unsf(); //[mir]~ ERROR call to unsafe function is unsafe
//[thir]~^ ERROR call to unsafe function `unsf` is unsafe
//~^^ NOTE
//~| NOTE
unsf(); //[mir]~ ERROR call to unsafe function is unsafe
//[thir]~^ ERROR call to unsafe function `unsf` is unsafe
//~^^ NOTE
//~| NOTE
}}
pub unsafe fn bar(x: *const i32) -> i32 { unsafe {
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default
let y = *x; //~ ERROR dereference of raw pointer is unsafe and requires unsafe block
//~^ NOTE
//~| NOTE
y + *x //~ ERROR dereference of raw pointer is unsafe and requires unsafe block
//~^ NOTE
//~| NOTE
}}
static mut BAZ: i32 = 0;
pub unsafe fn baz() -> i32 { unsafe {
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default
let y = BAZ; //~ ERROR use of mutable static is unsafe and requires unsafe block
//~^ NOTE
//~| NOTE
y + BAZ //~ ERROR use of mutable static is unsafe and requires unsafe block
//~^ NOTE
//~| NOTE
}}
macro_rules! unsafe_macro { () => (unsf()) }
//[mir]~^ ERROR call to unsafe function is unsafe
//[thir]~^^ ERROR call to unsafe function `unsf` is unsafe
//~| NOTE
//~| NOTE
//[mir]~| ERROR call to unsafe function is unsafe
//[thir]~| ERROR call to unsafe function `unsf` is unsafe
//~| NOTE
//~| NOTE
pub unsafe fn unsafe_in_macro() { unsafe {
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default
unsafe_macro!();
//~^ NOTE
//~| NOTE
unsafe_macro!();
//~^ NOTE
//~| NOTE
}}
pub unsafe fn unsafe_in_external_macro() {
// FIXME: https://github.com/rust-lang/rust/issues/112504
// FIXME: ~^ NOTE an unsafe function restricts its caller, but its body is safe by default
external_unsafe_macro::unsafe_macro!();
external_unsafe_macro::unsafe_macro!();
}
fn main() {}

View File

@ -0,0 +1,99 @@
error: call to unsafe function `unsf` is unsafe and requires unsafe block (error E0133)
--> $DIR/wrapping-unsafe-block-sugg.rs:15:5
|
LL | unsf();
| ^^^^^^ call to unsafe function
|
= note: consult the function's documentation for information on how to avoid undefined behavior
note: an unsafe function restricts its caller, but its body is safe by default
--> $DIR/wrapping-unsafe-block-sugg.rs:13:1
|
LL | pub unsafe fn foo() {
| ^^^^^^^^^^^^^^^^^^^
note: the lint level is defined here
--> $DIR/wrapping-unsafe-block-sugg.rs:6:9
|
LL | #![deny(unsafe_op_in_unsafe_fn)]
| ^^^^^^^^^^^^^^^^^^^^^^
error: call to unsafe function `unsf` is unsafe and requires unsafe block (error E0133)
--> $DIR/wrapping-unsafe-block-sugg.rs:19:5
|
LL | unsf();
| ^^^^^^ call to unsafe function
|
= note: consult the function's documentation for information on how to avoid undefined behavior
error: dereference of raw pointer is unsafe and requires unsafe block (error E0133)
--> $DIR/wrapping-unsafe-block-sugg.rs:27:13
|
LL | let y = *x;
| ^^ dereference of raw pointer
|
= note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
note: an unsafe function restricts its caller, but its body is safe by default
--> $DIR/wrapping-unsafe-block-sugg.rs:25:1
|
LL | pub unsafe fn bar(x: *const i32) -> i32 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: dereference of raw pointer is unsafe and requires unsafe block (error E0133)
--> $DIR/wrapping-unsafe-block-sugg.rs:30:9
|
LL | y + *x
| ^^ dereference of raw pointer
|
= note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
error: use of mutable static is unsafe and requires unsafe block (error E0133)
--> $DIR/wrapping-unsafe-block-sugg.rs:38:13
|
LL | let y = BAZ;
| ^^^ use of mutable static
|
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
note: an unsafe function restricts its caller, but its body is safe by default
--> $DIR/wrapping-unsafe-block-sugg.rs:36:1
|
LL | pub unsafe fn baz() -> i32 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
error: use of mutable static is unsafe and requires unsafe block (error E0133)
--> $DIR/wrapping-unsafe-block-sugg.rs:41:9
|
LL | y + BAZ
| ^^^ use of mutable static
|
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
error: call to unsafe function `unsf` is unsafe and requires unsafe block (error E0133)
--> $DIR/wrapping-unsafe-block-sugg.rs:46:36
|
LL | macro_rules! unsafe_macro { () => (unsf()) }
| ^^^^^^ call to unsafe function
...
LL | unsafe_macro!();
| --------------- in this macro invocation
|
= note: consult the function's documentation for information on how to avoid undefined behavior
note: an unsafe function restricts its caller, but its body is safe by default
--> $DIR/wrapping-unsafe-block-sugg.rs:56:1
|
LL | pub unsafe fn unsafe_in_macro() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: this error originates in the macro `unsafe_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
error: call to unsafe function `unsf` is unsafe and requires unsafe block (error E0133)
--> $DIR/wrapping-unsafe-block-sugg.rs:46:36
|
LL | macro_rules! unsafe_macro { () => (unsf()) }
| ^^^^^^ call to unsafe function
...
LL | unsafe_macro!();
| --------------- in this macro invocation
|
= note: consult the function's documentation for information on how to avoid undefined behavior
= note: this error originates in the macro `unsafe_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 8 previous errors