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 21785 - Improve error messages for opaque enums #12325

Merged
merged 1 commit into from
Apr 4, 2021

Conversation

MoonlightSentinel
Copy link
Contributor

@MoonlightSentinel MoonlightSentinel commented Mar 31, 2021

The error messages for default initialized variables of opaque enums were horrid and contained misleading informations.
This PR removes them by by checking whether semantic was already run in case of an enum without members.

Some examples:

enum NoBase;
enum WithBase : long;

// Not handled yet
struct S;
enum OpaqueBase : S;

void main()
{
    NoBase nb;
    WithBase wb;
    OpaqueBase ob;
}

@Geod24
Copy link
Member

Geod24 commented Mar 31, 2021

Well we don't know the enum definition, so assuming the initializer is its base .init is wrong. The enum could start at 1 for example, or long.min.

@MoonlightSentinel
Copy link
Contributor Author

Well we don't know the enum definition, so assuming the initializer is its base .init is wrong. The enum could start at 1 for example, or long.min.

Aren't opaque enums more intended as a strong typedef (e.g. __c_long).

But i guess I'll adapt this PR to create a proper error message.

@ibuclaw
Copy link
Member

ibuclaw commented Apr 1, 2021

Aren't opaque enums more intended as a strong typedef (e.g. __c_long).

But i guess I'll adapt this PR to create a proper error message.

Nope, they are just hidden/forward reference types, typically you pass them around by pointer, same as opaque structs.

C long and friends are just a special case.

@MoonlightSentinel MoonlightSentinel changed the title Fix 21785 - Allow initializers for opaque enums with base type Fix 21785 - Improve error messages for opaque enums Apr 2, 2021
@MoonlightSentinel MoonlightSentinel force-pushed the opaque-enum-init branch 4 times, most recently from 33d125a to 81268eb Compare April 2, 2021 15:20
static assert(is(E1 e == enum) && is(e == int));

enum E2;
static assert(is(E2 e == enum));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This snippet doesn't involve a forward reference and therefore compiles now.

@MoonlightSentinel MoonlightSentinel marked this pull request as ready for review April 2, 2021 16:36
if (semanticRun >= PASS.semanticdone)
error(loc, "is opaque and has no default initializer");
else
error(loc, "forward reference of `%s.init`", toChars());
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This line isn't covered because actual forward references crash dmd...

Will open another PR for those errors.

@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @MoonlightSentinel! 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
21785 normal Cannot declare variable of opaque enum with base type

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

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.

LGTM. Just a question: What does the error looks like with e.g. enum NoBase; Nobase nb = void; ?

The error messages for default initialized variables of opaque enums
were horrid and contained misleading informations.
This PR removes them by by checking whether semantic was already
run in case of an enum without members.

Some examples:

```d
enum NoBase;
enum WithBase : long;

// Not handled yet
struct S;
enum OpaqueBase : S;

void main()
{
    NoBase nb;
    WithBase wb;
    OpaqueBase ob;
}
```
@MoonlightSentinel
Copy link
Contributor Author

MoonlightSentinel commented Apr 3, 2021

There is no error because NoBase defaults to int (spec 17.1.3) and void initialization is allowed when the base type is not opaque.
Added two examples to the test,

@thewilsonator thewilsonator merged commit 40daaf4 into dlang:master Apr 4, 2021
@MoonlightSentinel MoonlightSentinel deleted the opaque-enum-init branch April 4, 2021 02:02
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