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 7443 - Better diagnostic on wrongly written static constructor #8164

Merged
merged 1 commit into from Apr 17, 2018

Conversation

RazvanN7
Copy link
Contributor

This should probably go through a deprecation cycle. Opinions?

@dlang-bot
Copy link
Contributor

dlang-bot commented Apr 11, 2018

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 annotated coverage diff directly on GitHub with CodeCov's browser extension
  • 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
7443 normal Better diagnostic on wrongly written static constructor

Testing this PR locally

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

dub fetch digger
dub run digger -- build "master + dmd#8164"

Copy link
Contributor

@jacob-carlborg jacob-carlborg left a comment

Choose a reason for hiding this comment

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

Yes, please make it a deprecation instead of an error. Especially since it was necessary to change a test.

@RazvanN7
Copy link
Contributor Author

@jacob-carlborg Thanks for the answer. Done.

Copy link
Contributor

@jacob-carlborg jacob-carlborg left a comment

Choose a reason for hiding this comment

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

A changelog is required as well, describing the problem and what to do to solve it.

@RazvanN7
Copy link
Contributor Author

@jacob-carlborg Done.

{
static
{
this()
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing semicolon?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, added.

keyword does not have any effect on the constructor.
The solution is to declare the constructor outside the
static block either as a normal constructor or a static
one (`static this()`).
Copy link
Contributor

Choose a reason for hiding this comment

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

Why the indentation for many of the paragraphs?

Copy link
Contributor Author

@RazvanN7 RazvanN7 Apr 12, 2018

Choose a reason for hiding this comment

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

Fixed the indentation. I thought it looked nicer that way

@RazvanN7 RazvanN7 force-pushed the Issue_7443 branch 2 times, most recently from 1c0c3f5 to ccd921a Compare April 12, 2018 11:34
@jacob-carlborg
Copy link
Contributor

Same question here, do we need a spec update?

@RazvanN7
Copy link
Contributor Author

I don't think so : https://dlang.org/spec/class.html#static-constructor . The spec does not explicitly say it is an error, just that it's not a static constructor. At most, the example in the spec could be updated to pinpoint that the code will issue an error once the deprecation is over.

@jacob-carlborg
Copy link
Contributor

Ok, so the spec was missing this. Or rather there was a bug in the compiler and shouldn't have worked in the first place.

@jacob-carlborg
Copy link
Contributor

LGTM 👌. Going to leave this open a bit longer for others to get a chance to look as well.

@JinShil
Copy link
Contributor

JinShil commented Apr 15, 2018

From the spec:

"The static in the static constructor declaration is not an attribute, it must appear immediately before the this"

Why does a static constructor need to be given extra special treatment? Can't the compiler be fixed to recognize that the constructor is in a static block and define it as a static constructor instead of emitting an error?

@RazvanN7
Copy link
Contributor Author

RazvanN7 commented Apr 16, 2018

@JinShil

Why does a static constructor need to be given extra special treatment? Can't the compiler be fixed to recognize that the constructor is in a static block and define it as a static constructor instead of emitting an error?

Although I might admit that this is not the best solution, I think that these are the 2 reasons why the static
constructor needs extra treatment :

  1. Implementation specific: at parse time there is no way to know whether the declaration is inside a static block so the parser cannot create a StaticCtorDeclaration AST node. The information is known at semantic time by checking the scope in which the constructor is declared. This can be changed by rewriting the AST and turning the constructor into a static constructor (with a bit of overhead, of course).

  2. User specific : the meaning of static in a static constructor is slightly different from the meaning of static in a static method. While the first means "run this method once for initialization before running the main function" , the second has a different meaning. Issuing an error when a constructor is defined in a static block is a way to ensure that the user knows exactly the difference between the 2 is and rules out cases in which a static: definition accidentally includes a constructor definition.

@andralex
Copy link
Member

@JinShil changing behavior from one working semantics to a different working semantics is the most perilous so we want to avoid that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants