Skip to content

Commit 040050a

Browse files
committed
Basic: out-of-line equality operator (NFCI)
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.
1 parent 4a1a17d commit 040050a

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

include/swift/Basic/Located.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ namespace swift {
3030
/// the ClangImporter needs to keep track of where imports were originally written.
3131
/// Located makes it easy to do so while making the code more readable, compared to
3232
/// using `std::pair`.
33-
template<typename T>
33+
template <typename T>
3434
struct Located {
35-
3635
/// The main item whose source location is being tracked.
3736
T Item;
3837

@@ -45,13 +44,13 @@ struct Located {
4544

4645
SWIFT_DEBUG_DUMP;
4746
void dump(raw_ostream &os) const;
48-
49-
template<typename U>
50-
friend bool operator ==(const Located<U> &lhs, const Located<U> &rhs) {
51-
return lhs.Item == rhs.Item && lhs.Loc == rhs.Loc;
52-
}
5347
};
5448

49+
template <typename T>
50+
bool operator ==(const Located<T> &lhs, const Located<T> &rhs) {
51+
return lhs.Item == rhs.Item && lhs.Loc == rhs.Loc;
52+
}
53+
5554
} // end namespace swift
5655

5756
namespace llvm {

0 commit comments

Comments
 (0)