-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Don't implement Fn* traits for #[target_feature] functions #73306
Don't implement Fn* traits for #[target_feature] functions #73306
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @varkor (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
@@ -0,0 +1,39 @@ | |||
error[E0277]: expected a `std::ops::Fn<()>` closure, found `fn() {foo}` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find this error is confusing and misleading, but at the same time it is consistent with error emitted for unsafe functions. Maybe we want to change it to better explain why foo doesn’t implement Fn<()>
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a note (open to phrasing suggestions), however I didn't remove this potentially misleading note since it's applied to all closures without any arguments. I'm not sure if it's within the scope of this PR to fix the #[rustc_on_unimplemented]
on the Fn traits, but it's misleading for unsafe fn()
too, I think.
| ^^^ expected an `Fn<()>` closure, found `fn() {foo}` | ||
| | ||
= help: the trait `std::ops::Fn<()>` is not implemented for `fn() {foo}` | ||
= note: wrap the `fn() {foo}` in a closure with no arguments: `|| { /* code */ } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add a note or change this note?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW, this is misleading -- but then I think I would like to make this suggestion actually work, that's what #73631 is about.
cc @rust-lang/lang on the right approach for addressing #72012. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. Just one minor comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
re-read, one suggestion to improve the quality of the error message
self.tcx.lang_items().fn_once_trait(), | ||
] | ||
.contains(&Some(trait_ref.def_id())); | ||
let is_target_feature_fn = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we make this a Option<DefId>
, where the def_id
is the inner def-id...
if is_fn_trait && is_target_feature_fn { | ||
err.note(&format!( | ||
"`{}` has `#[target_feature]` and is unsafe to call", | ||
trait_ref.skip_binder().self_ty(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...and then here we can use the def-id
to give an error like foo
has #[target_feature]
, instead of fn() {foo}
, which is really unclear.
Also, I think "unsafe to call" is perhaps a bit misleading. Maybe just "foo
has #[target_feature]
annotations and therefore does not implement the Fn
traits" or something like that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at the error text again, I'm not sure it's even necessary to repeat the name of the function, since it's in the help text as well (albeit with the somewhat confusing fn() {foo}
). In my latest commit I've changed it to simply "#[target_feature]
functions do not implement the Fn
traits". Do you think it's necessary to specify the function name in the error note?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I don't.
r? @nikomatsakis (since @varkor approved, and I left a nit, I'll take on the assignment) |
@bors r+ |
📌 Commit 51858da has been approved by |
…trait-soundness, r=nikomatsakis Don't implement Fn* traits for #[target_feature] functions Closes rust-lang#72012.
…trait-soundness, r=nikomatsakis Don't implement Fn* traits for #[target_feature] functions Closes rust-lang#72012.
…trait-soundness, r=nikomatsakis Don't implement Fn* traits for #[target_feature] functions Closes rust-lang#72012.
…trait-soundness, r=nikomatsakis Don't implement Fn* traits for #[target_feature] functions Closes rust-lang#72012.
…arth Rollup of 16 pull requests Successful merges: - rust-lang#72569 (Remove legacy InnoSetup GUI installer) - rust-lang#73306 (Don't implement Fn* traits for #[target_feature] functions) - rust-lang#73345 (expand: Stop using nonterminals for passing tokens to attribute and derive macros) - rust-lang#73449 (Provide more information on duplicate lang item error.) - rust-lang#73569 (Handle `macro_rules!` tokens consistently across crates) - rust-lang#73803 (Recover extra trailing angle brackets in struct definition) - rust-lang#73839 (Split and expand nonstandard-style lints unicode unit test.) - rust-lang#73841 (Remove defunct `-Z print-region-graph`) - rust-lang#73848 (Fix markdown rendering in librustc_lexer docs) - rust-lang#73865 (Fix Zulip topic format) - rust-lang#73892 (Clean up E0712 explanation) - rust-lang#73898 (remove duplicate test for rust-lang#61935) - rust-lang#73906 (Add missing backtick in `ty_error_with_message`) - rust-lang#73909 (`#[deny(unsafe_op_in_unsafe_fn)]` in libstd/fs.rs) - rust-lang#73910 (Rewrite a few manual index loops with while-let) - rust-lang#73929 (Fix comment typo) Failed merges: r? @ghost
Closes #72012.