Skip to content

Treat safe target_feature functions as unsafe by default [less invasive variant] #134353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Try to render shorthand differently
  • Loading branch information
oli-obk committed Jan 15, 2025
commit 1952b87780758652477bcd8ae11acc850781e2b4
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,15 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {

if self_ty.is_fn() {
let fn_sig = self_ty.fn_sig(self.tcx);
let shortname = match fn_sig.safety() {
hir::Safety::Safe => "fn",
hir::Safety::Unsafe => "unsafe fn",
let shortname = if let ty::FnDef(def_id, _) = self_ty.kind()
&& self.tcx.codegen_fn_attrs(def_id).safe_target_features
{
"#[target_feature] fn"
} else {
match fn_sig.safety() {
hir::Safety::Safe => "fn",
hir::Safety::Unsafe => "unsafe fn",
}
};
flags.push((sym::_Self, Some(shortname.to_owned())));
}
Expand Down
9 changes: 3 additions & 6 deletions tests/ui/rfcs/rfc-2396-target_feature-11/fn-traits.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ error[E0277]: expected a `Fn()` closure, found `#[target_features] fn() {foo}`
--> $DIR/fn-traits.rs:24:10
|
LL | call(foo);
| ---- ^^^ call the function in a closure: `|| unsafe { /* code */ }`
| ---- ^^^ expected an `Fn()` closure, found `#[target_features] fn() {foo}`
| |
| required by a bound introduced by this call
|
= help: the trait `Fn()` is not implemented for fn item `#[target_features] fn() {foo}`
= note: unsafe function cannot be called generically without an unsafe block
= note: wrap the `#[target_features] fn() {foo}` in a closure with no arguments: `|| { /* code */ }`
= note: `#[target_feature]` functions do not implement the `Fn` traits
note: required by a bound in `call`
Expand All @@ -20,12 +19,11 @@ error[E0277]: expected a `FnMut()` closure, found `#[target_features] fn() {foo}
--> $DIR/fn-traits.rs:25:14
|
LL | call_mut(foo);
| -------- ^^^ call the function in a closure: `|| unsafe { /* code */ }`
| -------- ^^^ expected an `FnMut()` closure, found `#[target_features] fn() {foo}`
| |
| required by a bound introduced by this call
|
= help: the trait `FnMut()` is not implemented for fn item `#[target_features] fn() {foo}`
= note: unsafe function cannot be called generically without an unsafe block
= note: wrap the `#[target_features] fn() {foo}` in a closure with no arguments: `|| { /* code */ }`
= note: `#[target_feature]` functions do not implement the `Fn` traits
note: required by a bound in `call_mut`
Expand All @@ -38,12 +36,11 @@ error[E0277]: expected a `FnOnce()` closure, found `#[target_features] fn() {foo
--> $DIR/fn-traits.rs:26:15
|
LL | call_once(foo);
| --------- ^^^ call the function in a closure: `|| unsafe { /* code */ }`
| --------- ^^^ expected an `FnOnce()` closure, found `#[target_features] fn() {foo}`
| |
| required by a bound introduced by this call
|
= help: the trait `FnOnce()` is not implemented for fn item `#[target_features] fn() {foo}`
= note: unsafe function cannot be called generically without an unsafe block
= note: wrap the `#[target_features] fn() {foo}` in a closure with no arguments: `|| { /* code */ }`
= note: `#[target_feature]` functions do not implement the `Fn` traits
note: required by a bound in `call_once`
Expand Down