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 20842 - Structs with disabled default/copy ctors can't be i… #11159

Merged
merged 1 commit into from
May 20, 2020

Conversation

pbackus
Copy link
Contributor

@pbackus pbackus commented May 18, 2020

…nitialized

A struct must now have at least one non-@disabled constructor to be
ineligible for brace initialization.

@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @pbackus! 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
20842 normal Structs with disabled default/copy ctors can't be initialized

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#11159"


if (ctor)
{
auto v = new HasNonDisabledCtorVisitor();
Copy link
Contributor

Choose a reason for hiding this comment

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

Use

scope v = new HasNonDisabledCtorVisitor();

}
}

if (ctor)
Copy link
Contributor

Choose a reason for hiding this comment

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

if (!ctor)
     return false;

static extern (C++) class HasNonDisabledCtorVisitor : Visitor
{
    ...
}
scope v = new HasNonDisabledCtorVisitor();
ctor.accept(v);
return v.result;

Copy link
Member

@Geod24 Geod24 left a comment

Choose a reason for hiding this comment

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

Thanks a lot! This bug is indeed very annoying.
Two things:

  • Can you use scope instead of auto for the visitor ? auto leads to GC allocation, while scope means it will be class allocated, so it matters quite a bit.
  • Can you add similar tests for union ?

The other comments are purely stylistic.

Comment on lines 617 to 620
this()
{
result = false;
}
Copy link
Member

Choose a reason for hiding this comment

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

It's the default value, no need to initialize it

Comment on lines 646 to 655
if (ctor)
{
auto v = new HasNonDisabledCtorVisitor();
ctor.accept(v);
return v.result;
}
else
{
return false;
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (ctor)
{
auto v = new HasNonDisabledCtorVisitor();
ctor.accept(v);
return v.result;
}
else
{
return false;
}
if (!ctor)
return false;
scope v = new HasNonDisabledCtorVisitor();
ctor.accept(v);
return v.result;

In general, we prefer to use early returns like this, to get the obvious case "out of the way" and simplify the flow, as well as reduce the amount of unneeded indentation.

Comment on lines 626 to 629
if (!(cd.storage_class & STC.disable))
{
result = true;
}
Copy link
Member

Choose a reason for hiding this comment

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

Here and in the OverloadSet overload, there is a 100% overhead in vertical space because of the optional {}.
Personally not found of it, but if you feel strongly about keeping it, it's fine.

@pbackus
Copy link
Contributor Author

pbackus commented May 19, 2020

@Geod24 @thewilsonator Thanks for the feedback; I've made the changes you suggested.

…nitialized

A struct must now have at least one non-@disabled constructor to be
ineligible for brace initialization.
Copy link
Member

@Geod24 Geod24 left a comment

Choose a reason for hiding this comment

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

Nice, LGTM

@pbackus
Copy link
Contributor Author

pbackus commented May 19, 2020

CI failures appear to be spurious (timeouts).

@dlang-bot dlang-bot merged commit 7de9187 into dlang:master May 20, 2020
@RazvanN7
Copy link
Contributor

This PR has introduced a regression: https://issues.dlang.org/show_bug.cgi?id=21547 .

I have looked at this PR and the problem is that the visitor does not check for all overloads. It simply looks at the first ctor declaration.

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.

5 participants