-
-
Notifications
You must be signed in to change notification settings - Fork 608
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 16206 - traits getOverloads fails when one of the overload is a templatized function #8195
Conversation
|
Thanks for your pull request and interest in making D better, @Biotronic! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
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
Testing this PR locallyIf 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#8195" |
|
It would be nice if the commit message contained the same info as the PR description. Perhaps worth a changelog entry. |
|
@bbasile Can you please investigate the iz failure in Jenkins? |
|
@JinShil, iz fix + tag pushed. Not sure of what was the last change in this PR, so maybe it wasn't necessary to do anything from my side ? |
|
I don't know if adding the templates to the overloads is a good idea. The problem was mostly that the count was different. The fix, as done right now, is actually a breaking change. |
|
So I was proven wrong immediately:
The other option would be to create a new trait, something like For that matter, the issue of |
|
@Biotronic I worked on this PR a couple of months ago : https://github.com/dlang/dmd/pull/7959/files . Maybe it will help you if you plan on fixing traits(getOverloads) entirely |
|
I seen it, but considered it separate, since it addresses a different issue. It doesn't seem to touch any of the same code. |
Yes. This in a first time sounds more reasonable. |
|
Possible trick to prevent the breaking change: add an optional bool parameter (which would mean "include templatized funcs or not"). |
| { | ||
| VarExp ve = cast(VarExp)ex; | ||
| auto td = ve.var.isTemplateDeclaration(); | ||
| f = td; |
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.
f = ve.var.isTemplateDeclaration and you can get rid of td
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 can do - I use td.funcroot later, and that's defined in TemplateDeclaration, not in Dsymbol.
src/dmd/traits.d
Outdated
| if (td && td.funcroot) | ||
| f = td.funcroot; | ||
| ex = null; | ||
| } | ||
|
|
||
| overloadApply(f, (Dsymbol s) | ||
| { | ||
| auto fd = s.isFuncDeclaration(); | ||
| if (!fd) |
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 think the code would look nicer like this:
auto fd = s.isFuncDeclaration();
auto td = s.isTemplateDeclaration();
if (!fd && !td)
return 0;
if (td) { /* do template stuff */}
else { /* do function stuff */} // fd and td are mutually exclusive
test/compilable/Test16206.d
Outdated
|
|
||
| static assert(S.foo() == 0); | ||
| static assert(S.foo(1) == 1); | ||
| static assert(S.foo("") == 2); |
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.
add newline
a50b505
to
574e355
Compare
|
Removed templates from result, since it makes Jenkins sad. Will make a separate PR for template overloads. |
You misunderstand the problem. The problem is that you didn't only fix the issue, you also broke the semantic of getOverloads. Your separate PR will also make Jenkins sad. This is why i reverted https://github.com/BBasile/iz/commit/6cacdec694aef27e20ef98dcbd235d16f056d2df. |
|
I'm still somewhat confused. My second PR will be on the form Strictly speaking, there could be code out there that depends on the current behavior of |
Yes but ONLY with a second optional parameter of type bool, so that when the second trait parameter is not specified the current behavior is maintained, fixing the bug and not breaking the world. |
|
Seems sensible. As shown in Test16206.d, |
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.
With the hope that the optional param was not a bad idea.
Once something optional is added it's locked forever.
src/dmd/traits.d
Outdated
| b = b.ctfeInterpret(); | ||
| if (!b.type.equals(Type.tbool)) | ||
| { | ||
| e.error("bool expected as third argument of __traits(getOverloads)."); |
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.
tick marks around bool and __traits(getOverloads) for highlighting in the error message.
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.
Also, we need a test case for this error message.
| @@ -824,7 +824,7 @@ extern (C++) Expression semanticTraits(TraitsExp e, Scope* sc) | |||
| e.ident == Id.getVirtualMethods || | |||
| e.ident == Id.getVirtualFunctions) | |||
| { | |||
| if (dim != 2) | |||
| if (dim != 2 && !(dim == 3 && e.ident == Id.getOverloads)) | |||
| return dimError(2); | |||
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.
codecov shows this line isn't covered in the test suit.
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.
Question: Should this be covered by a separate test file in the fail_compilation folder, or is a static assert(!__traits(compiles)) in Test16206.d good enough?
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.
Scratch that. Looking at the existing tests, I was able to notice a pattern. :p
|
This will require a documentation update at https://dlang.org/spec/traits.html#getOverloads |
c88bee8
to
9620131
Compare
|
Added changelog entry, and a spec PR in dlang/dlang.org#2351 |
|
Is adding an unnamed boolean argument preferred over having a new trait such as ‘getTemplateOverloads’. Personally I think at the call site the API without the “magic” bool would be quite a bit more readable. |
|
Since |
|
Sorry for taking so long to look into this. Of course it's possible - it's basically what I'm much more partial to aliak00's suggestion of I've already written and tested that code, so changing it to |
|
@WalterBright, any concerns about this? |
|
But all suggestions IMO so far are better than a bool at least. |
|
|
I think |
|
True, but the current implementation will return any symbol, regardless of whether it's possible to overload: The reasoning being you often don't really care if it's possible to overload, only that it exists at all. |
|
@JinShil anything else blocking the merge of this PR? |
|
I was hoping @WalterBright or @andralex would weigh in, since it affects the public API. |
|
I was also thinking about maybe using a bitArray enum for the selection criteria. enum OverloadSelection
{
nonTemplates = 1;
templates = 2;
couldThereBeOthers = 4;
}
__traits(getOverloads, mixin(__MODULE__), "S", OverloadSelection.Templates);But that may be totally unnecessary. Thoughts? |
|
@JinShil I think that what you propose may be a candidate for std.traits . For now, I think that this PR is ready to go. |
| --- | ||
| */ | ||
|
|
||
| struct S { |
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.
Curly braces on their own lines. Applies to all test files and the changelog entry.
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.
OT:
Btw I am not sure whether we need to be so picky for the test files, because it mostly doesn't matter there.
No one looks actively at the tests anyhow, but only when they fail.
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 I made a comment and not "Request Changes".
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.
BTW, I think that tests should be treated just as the rest of the code. Same standards, style, refactoring and so on.
This adds templates to the result of
__traits(getOverloads), allowing them to be iterated and inspected.There's a few issues other than 16206 that will be fixed by this - I'll have a look-see through bugzilla tomorrow to find them.
It's possible this breaks code by considering template overloads where only function overloads were expected earlier. I expect this to happen rarely in real code, but it's certainly possible. It might be that some templates in Phobos will require updating to handle mixed function/template overload sets.