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 18020 - [Reg 2.078] no property opCmp for anon class #7378
Conversation
Thanks for your pull request, @JinShil! Bugzilla references
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mind the Jenkins failures
Technically classes don't have linkage a type, only functions do. In the previous PR #7315, a couple of mutually exclusive booleans were converted to a single enum to actually enforce the exclusiveness. For example, a class cannot be anonymous and C++ or Objective-C at the same time. |
... but it can be |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to be the right way, but please also fix the issue stated in #7315
Yes. |
Done. |
@@ -719,7 +719,7 @@ private void membersToDt(AggregateDeclaration ad, DtBuilder dtb, | |||
{ | |||
dtb.xoff(toVtblSymbol(concreteType), 0); // __vptr | |||
offset = Target.ptrsize; | |||
if (!cd.classKind == ClassKind.cpp) | |||
if (cd.classKind != ClassKind.cpp) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we look for more of these?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did. I didn't find any. Feel free to check yourself.
@@ -242,6 +240,10 @@ extern (C++) class ClassDeclaration : AggregateDeclaration | |||
/// to prevent recursive attempts | |||
private bool inuse; | |||
|
|||
/// true if this class has an identifier, but was originally declared anonymous |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really need to mention the second part, after the comma?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, yes.
@@ -242,6 +240,10 @@ extern (C++) class ClassDeclaration : AggregateDeclaration | |||
/// to prevent recursive attempts | |||
private bool inuse; | |||
|
|||
/// true if this class has an identifier, but was originally declared anonymous | |||
/// used in support of https://issues.dlang.org/show_bug.cgi?id=17371 | |||
private bool isActuallyAnonymous; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the D convention is, as far as I know, to use the same name as the getter but prefix it with an underscore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't seen that. Show me where the precedent is established.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know where/if it's written down somewhere. But I think the name is a bit strange anyway, having Actually
as part of the name doesn't add anything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The "Actually" documents the fact that we are lying, because the class has a __anonclass
identifier. And remember I had to overload a method called isAnonymous
, so that name is off limits.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And remember I had to overload a method called isAnonymous, so that name is off limits.
That's why it's common to name the field either _isAnonymous
or isAnonymous_
. I prefer the latter but I think the former is more common in Phobos.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's why it's common to name the field either
_isAnonymous
orisAnonymous_
I would prefer something like that as well, but that doesn't seem to be the convention in DMD.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, but in the rest of the D community, at least in the core projects, the ones in the dlang
GitHub organization. Keep in mind that DMD is quite an old code base written in a very old style, C with classes. We're trying to improve that and in my opinion any new code should follow the standard D conventions. We don't want to make it worse by adding new code that is written in the old style 😃.
The C++ header need to be updated as well. |
aggregate.h was updated. Are you referring to something else? |
No, but the new field was not added. |
Which new field? |
@jacob-carlborg It shouldn't, since it is private. |
|
The sizes of the D and the C++ classes need to match, as far as I know. |
None of the private fields are declared in |
It was only one,
Please add the |
Done. Thank you! |
Example: $ cat foo.d
module foo;
struct Foo
{
private int a;
int b;
}
extern (C) Foo* getFoo()
{
import core.stdc.stdlib;
auto foo = cast(Foo*) malloc(Foo.sizeof);
foo.a = 3;
foo.b = 4;
return foo;
}
$ cat main.c
#include <stdio.h>
struct Foo
{
int b;
};
struct Foo *getFoo();
int main (int i, char** c)
{
struct Foo *foo = getFoo();
printf("%d\n", foo->b);
}
The above prints 3 and not 4 because the sizes of the structs on the D side and the C side doesn't match. It's the same idea with the D class and the C++ class. |
The C++ header file looks good now. |
BTW, if you want to, the fields can be |
I'm don't want to refactor anything; I just want to fix the regression. The others fields are |
Fair enough. |
LGTM 👌. |
The only thing preventing me from pulling this is I'm not sure if it should target master or stable. The PR that introduced this regression was only merged yesterday, so I'm guessing it needs to go to master. Please advise. |
|
It must be master. I don't see the changes in stable yet. auto-merge toggling on. |
In fact anonymous classes always seem to be ClassKind.d, at least I didn't manage to produce a non-D anon class. But it's better to make this explicit, instead of relying on the fact that sth. cannot be expressed in the language's grammar. |
Hence |
Yes, that's correct. It was originally put in the enum to enforce that a class in the AST should not be anonymous and Objective-C/C++ at the same time. Which was possible before and is now possible again. |
Well it's still not possible in the language and the code the more self-explaining now. |
I disagree. Ideally the API should force you do to the correct things as much as possible. In this case none of the solutions are prefect. |
Could easily add an assertion that anonymous implies extern(D) atm. |
@@ -269,7 +269,7 @@ extern (C++) class Dsymbol : RootObject | |||
return false; | |||
} | |||
|
|||
final bool isAnonymous() | |||
bool isAnonymous() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So you made this function virtual, but didn't reflect it in c++ headers?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought I had the C++ headers covered but apparently not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #7385
Fix C++ Interface after PR #7378
ClassKind.anonymous
is not a linkage type.In this case, this should be targeting "master", right?
cc @RazvanN7