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 b1deae5

Browse files
committedJan 14, 2025
Avoid notes that only make sense for unsafe functions
1 parent c6d9ec6 commit b1deae5

File tree

4 files changed

+9
-17
lines changed

4 files changed

+9
-17
lines changed
 

‎compiler/rustc_mir_build/src/check_unsafety.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,10 @@ impl UnsafeOpKind {
747747
) {
748748
let parent_id = tcx.hir().get_parent_item(hir_id);
749749
let parent_owner = tcx.hir_owner_node(parent_id);
750-
let should_suggest = parent_owner.fn_sig().is_some_and(|sig| sig.header.is_unsafe());
750+
let should_suggest = parent_owner.fn_sig().is_some_and(|sig| {
751+
// Do not suggest for safe target_feature functions
752+
matches!(sig.header.safety, hir::HeaderSafety::Normal(hir::Safety::Unsafe))
753+
});
751754
let unsafe_not_inherited_note = if should_suggest {
752755
suggest_unsafe_block.then(|| {
753756
let body_span = tcx.hir().body(parent_owner.body_id().unwrap()).value.span;
@@ -910,7 +913,7 @@ impl UnsafeOpKind {
910913
{
911914
true
912915
} else if let Some(sig) = tcx.hir().fn_sig_by_hir_id(*id)
913-
&& sig.header.is_unsafe()
916+
&& matches!(sig.header.safety, hir::HeaderSafety::Normal(hir::Safety::Unsafe))
914917
{
915918
true
916919
} else {

‎compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -461,9 +461,11 @@ impl<T> Trait<T> for X {
461461
(ty::FnPtr(_, hdr), ty::FnDef(def_id, _))
462462
| (ty::FnDef(def_id, _), ty::FnPtr(_, hdr)) => {
463463
if tcx.fn_sig(def_id).skip_binder().safety() < hdr.safety {
464-
diag.note(
464+
if !tcx.codegen_fn_attrs(def_id).safe_target_features {
465+
diag.note(
465466
"unsafe functions cannot be coerced into safe function pointers",
466-
);
467+
);
468+
}
467469
}
468470
}
469471
(ty::Adt(_, _), ty::Adt(def, args))

‎tests/ui/rfcs/rfc-2396-target_feature-11/fn-ptr.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ LL | let foo: fn() = foo;
1212
= note: expected fn pointer `fn()`
1313
found fn item `#[target_features] fn() {foo}`
1414
= note: functions with `#[target_feature]` can only be coerced to `unsafe` function pointers
15-
= note: unsafe functions cannot be coerced into safe function pointers
1615

1716
error: aborting due to 1 previous error
1817

‎tests/ui/rfcs/rfc-2396-target_feature-11/safe-calls.stderr

-12
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ LL | Quux.avx_bmi2();
2626
error[E0133]: call to function `avx_bmi2` with `#[target_feature]` is unsafe and requires unsafe function or block
2727
--> $DIR/safe-calls.rs:38:5
2828
|
29-
LL | fn bar() {
30-
| -------- items do not inherit unsafety from separate enclosing items
31-
LL | sse2();
3229
LL | avx_bmi2();
3330
| ^^^^^^^^^^ call to function with `#[target_feature]`
3431
|
@@ -37,9 +34,6 @@ LL | avx_bmi2();
3734
error[E0133]: call to function `Quux::avx_bmi2` with `#[target_feature]` is unsafe and requires unsafe function or block
3835
--> $DIR/safe-calls.rs:40:5
3936
|
40-
LL | fn bar() {
41-
| -------- items do not inherit unsafety from separate enclosing items
42-
...
4337
LL | Quux.avx_bmi2();
4438
| ^^^^^^^^^^^^^^^ call to function with `#[target_feature]`
4539
|
@@ -48,9 +42,6 @@ LL | Quux.avx_bmi2();
4842
error[E0133]: call to function `avx_bmi2` with `#[target_feature]` is unsafe and requires unsafe function or block
4943
--> $DIR/safe-calls.rs:47:5
5044
|
51-
LL | fn baz() {
52-
| -------- items do not inherit unsafety from separate enclosing items
53-
LL | sse2();
5445
LL | avx_bmi2();
5546
| ^^^^^^^^^^ call to function with `#[target_feature]`
5647
|
@@ -59,9 +50,6 @@ LL | avx_bmi2();
5950
error[E0133]: call to function `Quux::avx_bmi2` with `#[target_feature]` is unsafe and requires unsafe function or block
6051
--> $DIR/safe-calls.rs:49:5
6152
|
62-
LL | fn baz() {
63-
| -------- items do not inherit unsafety from separate enclosing items
64-
...
6553
LL | Quux.avx_bmi2();
6654
| ^^^^^^^^^^^^^^^ call to function with `#[target_feature]`
6755
|

0 commit comments

Comments
 (0)