-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Basic: out-of-line equality operator (NFCI) #29032
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
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.
@swift-ci please smoke test |
CC: @CodaFi |
@swift-ci please test macOS platform |
@swift-ci please smoke test macOS platform |
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? |
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. |
@varungandhi-apple, seems related to your subsequent change:
|
Build failed |
@swift-ci please smoke test |
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
struct
type 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.