Skip to content

[AST] NFC: Do not reinterpret_cast 'Type' #14266

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions include/swift/AST/ExistentialLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#ifndef SWIFT_EXISTENTIAL_LAYOUT_H
#define SWIFT_EXISTENTIAL_LAYOUT_H

#include "swift/Basic/ArrayRefView.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/Type.h"
#include "llvm/ADT/SmallVector.h"
Expand Down Expand Up @@ -66,21 +67,24 @@ struct ExistentialLayout {
// constraints?
bool isErrorExistential() const;

ArrayRef<ProtocolType *> getProtocols() const {
if (singleProtocol)
return ArrayRef<ProtocolType *>{&singleProtocol, 1};
return multipleProtocols;
static inline ProtocolType *getProtocolType(const Type &Ty) {
return cast<ProtocolType>(Ty.getPointer());
}
typedef ArrayRefView<Type,ProtocolType*,getProtocolType> ProtocolTypeArrayRef;

ProtocolTypeArrayRef getProtocols() const {
return protocols;
}

LayoutConstraint getLayoutConstraint() const;

private:
// Inline storage for 'protocols' member above when computing
// layout of a single ProtocolType
ProtocolType *singleProtocol;
Type singleProtocol;

/// Zero or more protocol constraints.
ArrayRef<ProtocolType *> multipleProtocols;
ArrayRef<Type> protocols;
};

}
Expand Down
6 changes: 2 additions & 4 deletions lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ ExistentialLayout::ExistentialLayout(ProtocolType *type) {
containsNonObjCProtocol = !protoDecl->isObjC();

singleProtocol = type;
protocols = { &singleProtocol, 1 };
}

ExistentialLayout::ExistentialLayout(ProtocolCompositionType *type) {
Expand All @@ -270,10 +271,7 @@ ExistentialLayout::ExistentialLayout(ProtocolCompositionType *type) {
}

singleProtocol = nullptr;
multipleProtocols = {
reinterpret_cast<ProtocolType * const *>(members.data()),
members.size()
};
protocols = { members.data(), members.size() };
}


Expand Down
9 changes: 5 additions & 4 deletions lib/ParseSIL/ParseSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1529,7 +1529,7 @@ bool SILParser::parseSubstitutions(SmallVectorImpl<ParsedSubstitution> &parsed,
/// Collect conformances by looking up the conformance from replacement
/// type and protocol decl.
static bool getConformancesForSubstitution(Parser &P,
ArrayRef<ProtocolType *> protocols,
ExistentialLayout::ProtocolTypeArrayRef protocols,
Type subReplacement,
SourceLoc loc,
SmallVectorImpl<ProtocolConformanceRef> &conformances) {
Expand Down Expand Up @@ -1578,11 +1578,12 @@ bool getApplySubstitutionsFromParsed(
parses = parses.slice(1);

SmallVector<ProtocolConformanceRef, 2> conformances;
SmallVector<ProtocolType *, 2> protocols;
SmallVector<Type, 2> protocols;
for (auto reqt : reqts)
protocols.push_back(reqt.getSecondType()->castTo<ProtocolType>());
protocols.push_back(reqt.getSecondType());

if (getConformancesForSubstitution(SP.P, protocols,
if (getConformancesForSubstitution(SP.P,
ExistentialLayout::ProtocolTypeArrayRef(protocols),
parsed.replacement,
parsed.loc, conformances))
return true;
Expand Down