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

Fix Issue 22997 - DMD crash: copy ctor can't call other ctor #13976

Merged
merged 1 commit into from
Apr 13, 2022

Conversation

RazvanN7
Copy link
Contributor

struct Forward{}

struct Foo{
    
    /// 1.
    this(scope ref typeof(this) rhs){
    	this(rhs, Forward.init);  //call ctor 2.
    }
    
    /// 2.
    this(scope ref typeof(this) rhs, Forward){}
    
    /// 3.
    this(scope typeof(this) rhs, int i, double d, string s){}
}

When calling the second constructor, the compiler needs to check all overloads to see the matches. Once it tries the 3rd constructor it tries to match its parameters and therefore it tries to copy construct rhs in the scope of the 3rd constructor. This leads to infinite recursion, because the new constructor call needs to check all of the constructors again. To break this cycle, I have added a new condition that checks whether the number of parameters non default parameters is greater than the number of arguments. If that is the case, then simply return no match and don't try to match args to params anymore. For that I needed to change a few tests, but I think that the error message is better anyway.

I am targeting master since I am going to do some follow up patches for the copy constructor that rely on this patch.

@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @RazvanN7! 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

Auto-close Bugzilla Severity Description
22997 regression DMD crash: copy ctor can't call other ctor

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

To target stable perform these two steps:

  1. Rebase your branch to upstream/stable:
git rebase --onto upstream/stable upstream/master
  1. Change the base branch of your PR to stable

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 + dmd#13976"

@RazvanN7
Copy link
Contributor Author

RazvanN7 commented Apr 12, 2022

Does anyone know what I can do to make Windows 64bit happy?

src\dmd\mtype.d(4691): Deprecation: argument `nargs` for format specification `"%lu"` must be `uint`, not `const(ulong)`

Edit: I hammered it with a cast, following the example here https://github.com/dlang/dmd/blob/master/src/dmd/expressionsem.d#L4154


struct Foo
{
this(scope ref typeof(this) rhs)
Copy link
Contributor

Choose a reason for hiding this comment

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

Since Foo has no pointers, scope is stripped in type semantic. Is it relevant to the bug?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nope, I simply copy-pasted the provided bug report. I can strip it manually.

@@ -4685,6 +4685,14 @@ extern (C++) final class TypeFunction : TypeNext
match = MATCH.convert; // match ... with a "conversion" match level
}

if (parameterList.varargs == VarArg.none && nparams > nargs && !parameterList[nargs].defaultArg)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add a link to https://issues.dlang.org/show_bug.cgi?id=22997 in a comment? It's not self-explanatory why this branch is here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure.

@RazvanN7
Copy link
Contributor Author

@dkorpel Done.

@thewilsonator
Copy link
Contributor

This should go to stable.

@RazvanN7
Copy link
Contributor Author

@thewilsonator I am intentionally targeting master since I need to do some follow-up changes that depend on this PR that have the potential to break user code.

@dlang-bot dlang-bot merged commit 4d1bfcf into dlang:master Apr 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants