Basic: out-of-line equality operator (NFCI)#29032
Merged
Merged
Conversation
MSVC did not like the original code and would fail to build as: ``` swift\include\swift/Basic/Located.h(50): error C2995: 'bool swift::operator ==(const swift::Located<T> &,const swift::Located<T> &)': function template has already been defined swift\include\swift/Basic/Located.h(50): note: see declaration of 'swift::operator ==' llvm\include\llvm/Support/TrailingObjects.h(76): note: see reference to class template instantiation 'swift::Located<swift::Identifier>' being compiled llvm\include\llvm/Support/TrailingObjects.h(233): note: see reference to class template instantiation 'llvm::trailing_objects_internal::AlignmentCalcHelper<swift::Located<swift::Identifier>>' being compiled swift\include\swift/AST/Decl.h(1512): note: see reference to class template instantiation 'llvm::TrailingObjects<swift::ImportDecl,swift::Located<swift::Identifier>>' being compiled ``` The original code is odd. There appears to be some unnecessary complexity. First, the member function is marked as a friend of a `struct` type which does not change the member's visibility, thus all the members are `public`, and the function need not be friended. Second, the function is templated over the same parameter type, which means that the original template parameter could be used and the standard member equality operator could be used rather than the free-standing form. It is unclear why the member equality operator is insufficient, and the extraneous template instatiations here seem wasteful. Out-of-line the free-standing form and not mark it as a friend to restore the build. Switching to a member form can be a follow up change.
Member
Author
Member
Author
|
@swift-ci please smoke test |
Member
Author
|
CC: @CodaFi |
Member
Author
|
@swift-ci please test macOS platform |
Member
Author
|
@swift-ci please smoke test macOS platform |
Contributor
|
Now I'm wondering why it didn't fail with clang. Does this lead to an ODR violation? It seems a bit odd that one compiler errors for it and another one lets it through. Maybe it is a clang bug? |
varungandhi-apple
approved these changes
Jan 7, 2020
Member
Author
|
It shouldn't really result in an ODR violation, the template types should get coalesced by the linker and you should end up with a single instance of it. I would agree that clang could detect this and warn on it. |
Member
Author
|
@varungandhi-apple, seems related to your subsequent change: |
Contributor
|
Build failed |
Member
Author
Member
Author
|
@swift-ci please smoke test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
MSVC did not like the original code and would fail to build as:
The original code is odd. There appears to be some unnecessary
complexity.
First, the member function is marked as a friend of a
structtype which does not change the member's visibility, thus allthe members are
public, and the function need not be friended.Second, the function is templated over the same parameter type, which
means that the original template parameter could be used and the
standard member equality operator could be used rather than the
free-standing form.
It is unclear why the member equality operator is insufficient, and the
extraneous template instatiations here seem wasteful. Out-of-line the
free-standing form and not mark it as a friend to restore the build.
Switching to a member form can be a follow up change.
Replace this paragraph with a description of your changes and rationale. Provide links to external references/discussions if appropriate.
Resolves SR-NNNN.