Skip to content
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

Make toDelegate safe for function pointers #8769

Closed
wants to merge 4 commits into from
Closed

Conversation

Bolpat
Copy link
Contributor

@Bolpat Bolpat commented Jun 20, 2023

No description provided.

@Bolpat Bolpat requested a review from andralex as a code owner June 20, 2023 15:32
@dlang-bot
Copy link
Contributor

dlang-bot commented Jun 20, 2023

Thanks for your pull request and interest in making D better, @Bolpat! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

⚠️⚠️⚠️ Warnings ⚠️⚠️⚠️

  • In preparation for migrating from Bugzilla to GitHub Issues, the issue reference syntax has changed. Please add the word "Bugzilla" to issue references. For example, Fix Bugzilla Issue 12345 or Fix Bugzilla 12345.(Reminder: the edit needs to be done in the Git commit message, not the GitHub pull request.)

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + phobos#8769"

Copy link
Contributor

@dkorpel dkorpel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is there a new branch for function pointers, is the final else case obsolete now?

  • $(LI Does not work with @safe functions.)

This line is no longer accurate now

@Bolpat
Copy link
Contributor Author

Bolpat commented Jun 21, 2023

Why is there a new branch for function pointers, is the final else case obsolete now?

Because toDelegate works with any callable, the else branch cannot be @safe in general. To be @safe for function pointers, toDelegate needed a specific branch (or it looked like the best solution to me).

@Bolpat
Copy link
Contributor Author

Bolpat commented Jun 21, 2023

  • $(LI Does not work with @safe functions.)

This line is no longer accurate now

That should have been a review comment.

@dkorpel
Copy link
Contributor

dkorpel commented Jun 21, 2023

That should have been a review comment.

I can't comment on code not in the diff

@dkorpel
Copy link
Contributor

dkorpel commented Jun 21, 2023

Because toDelegate works with any callable

There's now branches for delegate, function, and opCall, what remaining cases does toDelegate support?

@dkorpel
Copy link
Contributor

dkorpel commented Jun 28, 2023

Ping @Bolpat

1 similar comment
@dkorpel
Copy link
Contributor

dkorpel commented Jul 5, 2023

Ping @Bolpat

@Bolpat
Copy link
Contributor Author

Bolpat commented Oct 20, 2023

Sorry, I was absent for a while. I looked into the failed checks and didn’t understand what’s wrong.

Should I force-push?

@PetarKirov
Copy link
Member

@Bolpat yes, please rebase & force-push.

@dkorpel
Copy link
Contributor

dkorpel commented Oct 31, 2023

Also, please answer #8769 (comment)

@thewilsonator thewilsonator added the Review:Salvage This PR needs work, but we want to keep it and it can be salvaged. label Oct 27, 2024

() @trusted
{
// assigning funcptr is @system, but it’s safe here because `fp` needs no context
Copy link
Member

@0xEAB 0xEAB Dec 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, what did we expect the compiler to do that makes a delegate call to the resulting delegate a valid function call?

@0xEAB
Copy link
Member

0xEAB commented Dec 29, 2024

The way this current patch implements the delegate-to-function conversion by leaving the context pointer on the delegate default to null still won’t change that a delegate call makes a function receive a context-pointer parameter.

I’ve tried to visualize the issue:

import std.stdio;

void main() {
	auto fun = function (size_t a) {
		writeln("Fun: ", a);
	};
	auto del = delegate (size_t a) {
		writeln("Del: ", a);
	};

	auto ehm = function(size_t a, size_t b) {
		writeln("ehm: ", a);
		writeln("ehm: ", b);
	};

	void delegate(size_t) fake1;
	fake1.funcptr = fun;

	void delegate(size_t) fake2;
	fake2.ptr = cast(void*) 69;
	fake2.funcptr = cast(void function(size_t)) ehm;


	fun(7);
	del(7);
	fake1(7);
	fake2(7);
}
Fun: 7
Del: 7
Fun: 0
ehm: 69
ehm: 7

std/functional.d Outdated Show resolved Hide resolved
@LightBender
Copy link
Contributor

Closed as superseded by: #10599

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Review:Salvage This PR needs work, but we want to keep it and it can be salvaged. Severity:Enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants