Skip to content

Make toDelegate safe for function pointers#8769

Closed
Bolpat wants to merge 4 commits into
dlang:masterfrom
Bolpat:patch-1
Closed

Make toDelegate safe for function pointers#8769
Bolpat wants to merge 4 commits into
dlang:masterfrom
Bolpat:patch-1

Conversation

@Bolpat

@Bolpat Bolpat commented Jun 20, 2023

Copy link
Copy Markdown
Contributor

No description provided.

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

dlang-bot commented Jun 20, 2023

Copy link
Copy Markdown
Contributor

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"

@dkorpel dkorpel left a comment

Copy link
Copy Markdown
Contributor

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

Bolpat commented Jun 21, 2023

Copy link
Copy Markdown
Contributor Author

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

Bolpat commented Jun 21, 2023

Copy link
Copy Markdown
Contributor Author
  • $(LI Does not work with @safe functions.)

This line is no longer accurate now

That should have been a review comment.

@dkorpel

dkorpel commented Jun 21, 2023

Copy link
Copy Markdown
Contributor

That should have been a review comment.

I can't comment on code not in the diff

@dkorpel

dkorpel commented Jun 21, 2023

Copy link
Copy Markdown
Contributor

Because toDelegate works with any callable

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

@dkorpel

dkorpel commented Jun 28, 2023

Copy link
Copy Markdown
Contributor

Ping @Bolpat

1 similar comment
@dkorpel

dkorpel commented Jul 5, 2023

Copy link
Copy Markdown
Contributor

Ping @Bolpat

@Bolpat

Bolpat commented Oct 20, 2023

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
Member

@Bolpat yes, please rebase & force-push.

@dkorpel

dkorpel commented Oct 31, 2023

Copy link
Copy Markdown
Contributor

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
Comment thread std/functional.d

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

@0xEAB 0xEAB Dec 29, 2024

Copy link
Copy Markdown
Member

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

0xEAB commented Dec 29, 2024

Copy link
Copy Markdown
Member

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

Comment thread std/functional.d Outdated
@LightBender

Copy link
Copy Markdown
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