Skip to content

Commit

Permalink
Avoid notes that only make sense for unsafe functions
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Jan 14, 2025
1 parent c6d9ec6 commit b1deae5
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 17 deletions.
7 changes: 5 additions & 2 deletions compiler/rustc_mir_build/src/check_unsafety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,10 @@ impl UnsafeOpKind {
) {
let parent_id = tcx.hir().get_parent_item(hir_id);
let parent_owner = tcx.hir_owner_node(parent_id);
let should_suggest = parent_owner.fn_sig().is_some_and(|sig| sig.header.is_unsafe());
let should_suggest = parent_owner.fn_sig().is_some_and(|sig| {
// Do not suggest for safe target_feature functions
matches!(sig.header.safety, hir::HeaderSafety::Normal(hir::Safety::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;
Expand Down Expand Up @@ -910,7 +913,7 @@ impl UnsafeOpKind {
{
true
} else if let Some(sig) = tcx.hir().fn_sig_by_hir_id(*id)
&& sig.header.is_unsafe()
&& matches!(sig.header.safety, hir::HeaderSafety::Normal(hir::Safety::Unsafe))
{
true
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,11 @@ impl<T> Trait<T> for X {
(ty::FnPtr(_, hdr), ty::FnDef(def_id, _))
| (ty::FnDef(def_id, _), ty::FnPtr(_, hdr)) => {
if tcx.fn_sig(def_id).skip_binder().safety() < hdr.safety {
diag.note(
if !tcx.codegen_fn_attrs(def_id).safe_target_features {
diag.note(
"unsafe functions cannot be coerced into safe function pointers",
);
);
}
}
}
(ty::Adt(_, _), ty::Adt(def, args))
Expand Down
1 change: 0 additions & 1 deletion tests/ui/rfcs/rfc-2396-target_feature-11/fn-ptr.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ LL | let foo: fn() = foo;
= note: expected fn pointer `fn()`
found fn item `#[target_features] fn() {foo}`
= note: functions with `#[target_feature]` can only be coerced to `unsafe` function pointers
= note: unsafe functions cannot be coerced into safe function pointers

error: aborting due to 1 previous error

Expand Down
12 changes: 0 additions & 12 deletions tests/ui/rfcs/rfc-2396-target_feature-11/safe-calls.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ LL | Quux.avx_bmi2();
error[E0133]: call to function `avx_bmi2` with `#[target_feature]` is unsafe and requires unsafe function or block
--> $DIR/safe-calls.rs:38:5
|
LL | fn bar() {
| -------- items do not inherit unsafety from separate enclosing items
LL | sse2();
LL | avx_bmi2();
| ^^^^^^^^^^ call to function with `#[target_feature]`
|
Expand All @@ -37,9 +34,6 @@ LL | avx_bmi2();
error[E0133]: call to function `Quux::avx_bmi2` with `#[target_feature]` is unsafe and requires unsafe function or block
--> $DIR/safe-calls.rs:40:5
|
LL | fn bar() {
| -------- items do not inherit unsafety from separate enclosing items
...
LL | Quux.avx_bmi2();
| ^^^^^^^^^^^^^^^ call to function with `#[target_feature]`
|
Expand All @@ -48,9 +42,6 @@ LL | Quux.avx_bmi2();
error[E0133]: call to function `avx_bmi2` with `#[target_feature]` is unsafe and requires unsafe function or block
--> $DIR/safe-calls.rs:47:5
|
LL | fn baz() {
| -------- items do not inherit unsafety from separate enclosing items
LL | sse2();
LL | avx_bmi2();
| ^^^^^^^^^^ call to function with `#[target_feature]`
|
Expand All @@ -59,9 +50,6 @@ LL | avx_bmi2();
error[E0133]: call to function `Quux::avx_bmi2` with `#[target_feature]` is unsafe and requires unsafe function or block
--> $DIR/safe-calls.rs:49:5
|
LL | fn baz() {
| -------- items do not inherit unsafety from separate enclosing items
...
LL | Quux.avx_bmi2();
| ^^^^^^^^^^^^^^^ call to function with `#[target_feature]`
|
Expand Down

0 comments on commit b1deae5

Please sign in to comment.