Skip to content

Commit f1a081e

Browse files
calebzulawskiindirect
authored andcommitted
Make #[target_feature] Fn trait error message less confusing
1 parent 4e3ee2c commit f1a081e

File tree

2 files changed

+21
-22
lines changed
  • src

2 files changed

+21
-22
lines changed

src/librustc_trait_selection/traits/error_reporting/mod.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -287,18 +287,6 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
287287
.starts_with("std::convert::From<");
288288
let is_unsize =
289289
{ Some(trait_ref.def_id()) == self.tcx.lang_items().unsize_trait() };
290-
let is_fn_trait = [
291-
self.tcx.lang_items().fn_trait(),
292-
self.tcx.lang_items().fn_mut_trait(),
293-
self.tcx.lang_items().fn_once_trait(),
294-
]
295-
.contains(&Some(trait_ref.def_id()));
296-
let is_target_feature_fn =
297-
if let ty::FnDef(def_id, _) = trait_ref.skip_binder().self_ty().kind {
298-
!self.tcx.codegen_fn_attrs(def_id).target_features.is_empty()
299-
} else {
300-
false
301-
};
302290
let (message, note) = if is_try && is_from {
303291
(
304292
Some(format!(
@@ -441,11 +429,22 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
441429
);
442430
}
443431

432+
let is_fn_trait = [
433+
self.tcx.lang_items().fn_trait(),
434+
self.tcx.lang_items().fn_mut_trait(),
435+
self.tcx.lang_items().fn_once_trait(),
436+
]
437+
.contains(&Some(trait_ref.def_id()));
438+
let is_target_feature_fn =
439+
if let ty::FnDef(def_id, _) = trait_ref.skip_binder().self_ty().kind {
440+
!self.tcx.codegen_fn_attrs(def_id).target_features.is_empty()
441+
} else {
442+
false
443+
};
444444
if is_fn_trait && is_target_feature_fn {
445-
err.note(&format!(
446-
"`{}` has `#[target_feature]` and is unsafe to call",
447-
trait_ref.skip_binder().self_ty(),
448-
));
445+
err.note(
446+
"`#[target_feature]` functions do not implement the `Fn` traits",
447+
);
449448
}
450449

451450
// Try to report a help message

src/test/ui/rfcs/rfc-2396-target_feature-11/fn-traits.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL | call(foo);
99
|
1010
= help: the trait `std::ops::Fn<()>` is not implemented for `fn() {foo}`
1111
= note: wrap the `fn() {foo}` in a closure with no arguments: `|| { /* code */ }
12-
= note: `fn() {foo}` has `#[target_feature]` and is unsafe to call
12+
= note: `#[target_feature]` functions do not implement the `Fn` traits
1313

1414
error[E0277]: expected a `std::ops::FnMut<()>` closure, found `fn() {foo}`
1515
--> $DIR/fn-traits.rs:25:14
@@ -22,7 +22,7 @@ LL | call_mut(foo);
2222
|
2323
= help: the trait `std::ops::FnMut<()>` is not implemented for `fn() {foo}`
2424
= note: wrap the `fn() {foo}` in a closure with no arguments: `|| { /* code */ }
25-
= note: `fn() {foo}` has `#[target_feature]` and is unsafe to call
25+
= note: `#[target_feature]` functions do not implement the `Fn` traits
2626

2727
error[E0277]: expected a `std::ops::FnOnce<()>` closure, found `fn() {foo}`
2828
--> $DIR/fn-traits.rs:26:15
@@ -35,7 +35,7 @@ LL | call_once(foo);
3535
|
3636
= help: the trait `std::ops::FnOnce<()>` is not implemented for `fn() {foo}`
3737
= note: wrap the `fn() {foo}` in a closure with no arguments: `|| { /* code */ }
38-
= note: `fn() {foo}` has `#[target_feature]` and is unsafe to call
38+
= note: `#[target_feature]` functions do not implement the `Fn` traits
3939

4040
error[E0277]: expected a `std::ops::Fn<()>` closure, found `unsafe fn() {foo_unsafe}`
4141
--> $DIR/fn-traits.rs:28:10
@@ -48,7 +48,7 @@ LL | call(foo_unsafe);
4848
|
4949
= help: the trait `std::ops::Fn<()>` is not implemented for `unsafe fn() {foo_unsafe}`
5050
= note: wrap the `unsafe fn() {foo_unsafe}` in a closure with no arguments: `|| { /* code */ }
51-
= note: `unsafe fn() {foo_unsafe}` has `#[target_feature]` and is unsafe to call
51+
= note: `#[target_feature]` functions do not implement the `Fn` traits
5252

5353
error[E0277]: expected a `std::ops::FnMut<()>` closure, found `unsafe fn() {foo_unsafe}`
5454
--> $DIR/fn-traits.rs:30:14
@@ -61,7 +61,7 @@ LL | call_mut(foo_unsafe);
6161
|
6262
= help: the trait `std::ops::FnMut<()>` is not implemented for `unsafe fn() {foo_unsafe}`
6363
= note: wrap the `unsafe fn() {foo_unsafe}` in a closure with no arguments: `|| { /* code */ }
64-
= note: `unsafe fn() {foo_unsafe}` has `#[target_feature]` and is unsafe to call
64+
= note: `#[target_feature]` functions do not implement the `Fn` traits
6565

6666
error[E0277]: expected a `std::ops::FnOnce<()>` closure, found `unsafe fn() {foo_unsafe}`
6767
--> $DIR/fn-traits.rs:32:15
@@ -74,7 +74,7 @@ LL | call_once(foo_unsafe);
7474
|
7575
= help: the trait `std::ops::FnOnce<()>` is not implemented for `unsafe fn() {foo_unsafe}`
7676
= note: wrap the `unsafe fn() {foo_unsafe}` in a closure with no arguments: `|| { /* code */ }
77-
= note: `unsafe fn() {foo_unsafe}` has `#[target_feature]` and is unsafe to call
77+
= note: `#[target_feature]` functions do not implement the `Fn` traits
7878

7979
error: aborting due to 6 previous errors
8080

0 commit comments

Comments
 (0)