Skip to content

Commit 4f84c2a

Browse files
committed
Use the default constructor to clean up some APIs
Use ProtocolConformanceRef::forInvalid() in implementations only as a semantic signal. In one place, use the default constructor to drop the final use of Optional<ProtocolConformanceRef>.
1 parent b849e51 commit 4f84c2a

File tree

9 files changed

+41
-55
lines changed

9 files changed

+41
-55
lines changed

include/swift/AST/Stmt.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -805,8 +805,7 @@ class ForEachStmt : public LabeledStmt {
805805
BraceStmt *Body;
806806

807807
// Set by Sema:
808-
ProtocolConformanceRef sequenceConformance =
809-
ProtocolConformanceRef::forInvalid();
808+
ProtocolConformanceRef sequenceConformance = ProtocolConformanceRef();
810809
ConcreteDeclRef makeIterator;
811810
ConcreteDeclRef iteratorNext;
812811
VarDecl *iteratorVar = nullptr;

include/swift/AST/Types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3903,7 +3903,7 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
39033903
SubstitutionMap substitutions, bool genericSigIsImplied,
39043904
const ASTContext &ctx,
39053905
ProtocolConformanceRef witnessMethodConformance =
3906-
ProtocolConformanceRef::forInvalid());
3906+
ProtocolConformanceRef());
39073907

39083908
/// Return a structurally-identical function type with a slightly tweaked
39093909
/// ExtInfo.

include/swift/SIL/SILType.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -590,8 +590,7 @@ CanSILFunctionType getNativeSILFunctionType(
590590
CanAnyFunctionType substType, Optional<SILDeclRef> origConstant = None,
591591
Optional<SILDeclRef> constant = None,
592592
Optional<SubstitutionMap> reqtSubs = None,
593-
ProtocolConformanceRef witnessMethodConformance =
594-
ProtocolConformanceRef::forInvalid());
593+
ProtocolConformanceRef witnessMethodConformance = ProtocolConformanceRef());
595594

596595
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, SILType T) {
597596
T.print(OS);

lib/AST/SubstitutionMap.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ SubstitutionMap::lookupConformance(CanType type, ProtocolDecl *proto) const {
368368
genericSig->getConformanceAccessPath(type, proto);
369369

370370
// Fall through because we cannot yet evaluate an access path.
371-
auto conformance = ProtocolConformanceRef::forInvalid();
371+
ProtocolConformanceRef conformance;
372372
for (const auto &step : accessPath) {
373373
// For the first step, grab the initial conformance.
374374
if (conformance.isInvalid()) {

lib/ParseSIL/ParseSIL.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5969,17 +5969,17 @@ parseRootProtocolConformance(Parser &P, SILParser &SP, Type ConformingTy,
59695969
SourceLoc Loc, KeywordLoc;
59705970
proto = parseProtocolDecl(P, SP);
59715971
if (!proto)
5972-
return ProtocolConformanceRef::forInvalid();
5972+
return ProtocolConformanceRef();
59735973

59745974
if (P.parseIdentifier(ModuleKeyword, KeywordLoc,
59755975
diag::expected_tok_in_sil_instr, "module") ||
59765976
SP.parseSILIdentifier(ModuleName, Loc,
59775977
diag::expected_sil_value_name))
5978-
return ProtocolConformanceRef::forInvalid();
5978+
return ProtocolConformanceRef();
59795979

59805980
if (ModuleKeyword.str() != "module") {
59815981
P.diagnose(KeywordLoc, diag::expected_tok_in_sil_instr, "module");
5982-
return ProtocolConformanceRef::forInvalid();
5982+
return ProtocolConformanceRef();
59835983
}
59845984

59855985
// Calling lookupConformance on a BoundGenericType will return a specialized
@@ -5990,7 +5990,7 @@ parseRootProtocolConformance(Parser &P, SILParser &SP, Type ConformingTy,
59905990
auto lookup = P.SF.getParentModule()->lookupConformance(lookupTy, proto);
59915991
if (lookup.isInvalid()) {
59925992
P.diagnose(KeywordLoc, diag::sil_witness_protocol_conformance_not_found);
5993-
return ProtocolConformanceRef::forInvalid();
5993+
return ProtocolConformanceRef();
59945994
}
59955995

59965996
// Use a concrete self-conformance if we're parsing this for a witness table.
@@ -6043,45 +6043,45 @@ ProtocolConformanceRef SILParser::parseProtocolConformanceHelper(
60436043
// Parse AST type.
60446044
ParserResult<TypeRepr> TyR = P.parseType();
60456045
if (TyR.isNull())
6046-
return ProtocolConformanceRef::forInvalid();
6046+
return ProtocolConformanceRef();
60476047
TypeLoc Ty = TyR.get();
60486048
if (defaultForProto) {
60496049
bindProtocolSelfInTypeRepr(Ty, defaultForProto);
60506050
}
60516051

60526052
if (performTypeLocChecking(Ty, /*IsSILType=*/ false, witnessEnv,
60536053
defaultForProto))
6054-
return ProtocolConformanceRef::forInvalid();
6054+
return ProtocolConformanceRef();
60556055
auto ConformingTy = Ty.getType();
60566056

60576057
if (P.parseToken(tok::colon, diag::expected_sil_witness_colon))
6058-
return ProtocolConformanceRef::forInvalid();
6058+
return ProtocolConformanceRef();
60596059

60606060
if (P.Tok.is(tok::identifier) && P.Tok.getText() == "specialize") {
60616061
P.consumeToken();
60626062

60636063
// Parse substitutions for specialized conformance.
60646064
SmallVector<ParsedSubstitution, 4> parsedSubs;
60656065
if (parseSubstitutions(parsedSubs, witnessEnv, defaultForProto))
6066-
return ProtocolConformanceRef::forInvalid();
6066+
return ProtocolConformanceRef();
60676067

60686068
if (P.parseToken(tok::l_paren, diag::expected_sil_witness_lparen))
6069-
return ProtocolConformanceRef::forInvalid();
6069+
return ProtocolConformanceRef();
60706070
ProtocolDecl *dummy;
60716071
GenericEnvironment *specializedEnv;
60726072
auto genericConform =
60736073
parseProtocolConformance(dummy, specializedEnv,
60746074
ConformanceContext::Ordinary,
60756075
defaultForProto);
60766076
if (genericConform.isInvalid() || !genericConform.isConcrete())
6077-
return ProtocolConformanceRef::forInvalid();
6077+
return ProtocolConformanceRef();
60786078
if (P.parseToken(tok::r_paren, diag::expected_sil_witness_rparen))
6079-
return ProtocolConformanceRef::forInvalid();
6079+
return ProtocolConformanceRef();
60806080

60816081
SubstitutionMap subMap =
60826082
getApplySubstitutionsFromParsed(*this, specializedEnv, parsedSubs);
60836083
if (!subMap)
6084-
return ProtocolConformanceRef::forInvalid();
6084+
return ProtocolConformanceRef();
60856085

60866086
auto result = P.Context.getSpecializedConformance(
60876087
ConformingTy, genericConform.getConcrete(), subMap);
@@ -6092,13 +6092,13 @@ ProtocolConformanceRef SILParser::parseProtocolConformanceHelper(
60926092
P.consumeToken();
60936093

60946094
if (P.parseToken(tok::l_paren, diag::expected_sil_witness_lparen))
6095-
return ProtocolConformanceRef::forInvalid();
6095+
return ProtocolConformanceRef();
60966096
auto baseConform = parseProtocolConformance(defaultForProto,
60976097
ConformanceContext::Ordinary);
60986098
if (baseConform.isInvalid() || !baseConform.isConcrete())
6099-
return ProtocolConformanceRef::forInvalid();
6099+
return ProtocolConformanceRef();
61006100
if (P.parseToken(tok::r_paren, diag::expected_sil_witness_rparen))
6101-
return ProtocolConformanceRef::forInvalid();
6101+
return ProtocolConformanceRef();
61026102

61036103
auto result = P.Context.getInheritedConformance(ConformingTy,
61046104
baseConform.getConcrete());

lib/SIL/SILFunctionType.cpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,7 +1713,7 @@ getSILFunctionTypeForClangDecl(TypeConverter &TC, const clang::Decl *clangDecl,
17131713
return getSILFunctionType(TC, origPattern, substInterfaceType, extInfo,
17141714
ObjCMethodConventions(method), foreignInfo,
17151715
constant, constant, None,
1716-
ProtocolConformanceRef::forInvalid());
1716+
ProtocolConformanceRef());
17171717
}
17181718

17191719
if (auto method = dyn_cast<clang::CXXMethodDecl>(clangDecl)) {
@@ -1722,7 +1722,7 @@ getSILFunctionTypeForClangDecl(TypeConverter &TC, const clang::Decl *clangDecl,
17221722
auto conventions = CXXMethodConventions(method);
17231723
return getSILFunctionType(TC, origPattern, substInterfaceType, extInfo,
17241724
conventions, foreignInfo, constant, constant,
1725-
None, ProtocolConformanceRef::forInvalid());
1725+
None, ProtocolConformanceRef());
17261726
}
17271727

17281728
if (auto func = dyn_cast<clang::FunctionDecl>(clangDecl)) {
@@ -1734,8 +1734,7 @@ getSILFunctionTypeForClangDecl(TypeConverter &TC, const clang::Decl *clangDecl,
17341734
: AbstractionPattern(origType, clangType);
17351735
return getSILFunctionType(TC, origPattern, substInterfaceType, extInfo,
17361736
CFunctionConventions(func), foreignInfo, constant,
1737-
constant, None,
1738-
ProtocolConformanceRef::forInvalid());
1737+
constant, None, ProtocolConformanceRef());
17391738
}
17401739

17411740
llvm_unreachable("call to unknown kind of C function");
@@ -1762,18 +1761,16 @@ getSILFunctionTypeForAbstractCFunction(TypeConverter &TC,
17621761
llvm_unreachable("unexpected type imported as a function type");
17631762
}
17641763
if (fnType) {
1765-
return getSILFunctionType(TC, origType, substType, extInfo,
1766-
CFunctionTypeConventions(fnType), ForeignInfo(),
1767-
constant, constant, None,
1768-
ProtocolConformanceRef::forInvalid());
1764+
return getSILFunctionType(
1765+
TC, origType, substType, extInfo, CFunctionTypeConventions(fnType),
1766+
ForeignInfo(), constant, constant, None, ProtocolConformanceRef());
17691767
}
17701768
}
17711769

17721770
// TODO: Ought to support captures in block funcs.
17731771
return getSILFunctionType(TC, origType, substType, extInfo,
17741772
DefaultBlockConventions(), ForeignInfo(), constant,
1775-
constant, None,
1776-
ProtocolConformanceRef::forInvalid());
1773+
constant, None, ProtocolConformanceRef());
17771774
}
17781775

17791776
/// Try to find a clang method declaration for the given function.
@@ -1940,7 +1937,7 @@ getSILFunctionTypeForObjCSelectorFamily(TypeConverter &TC, ObjCSelectorFamily fa
19401937
return getSILFunctionType(
19411938
TC, AbstractionPattern(origType), substInterfaceType, extInfo,
19421939
ObjCSelectorFamilyConventions(family), foreignInfo, constant, constant,
1943-
/*requirement subs*/ None, ProtocolConformanceRef::forInvalid());
1940+
/*requirement subs*/ None, ProtocolConformanceRef());
19441941
}
19451942

19461943
static bool isImporterGeneratedAccessor(const clang::Decl *clangDecl,
@@ -1973,7 +1970,7 @@ getUncachedSILFunctionTypeForConstant(TypeConverter &TC,
19731970
auto extInfo = origLoweredInterfaceType->getExtInfo();
19741971

19751972
if (!constant.isForeign) {
1976-
auto witnessMethodConformance = ProtocolConformanceRef::forInvalid();
1973+
ProtocolConformanceRef witnessMethodConformance;
19771974

19781975
if (extInfo.getSILRepresentation() ==
19791976
SILFunctionTypeRepresentation::WitnessMethod) {
@@ -2328,7 +2325,7 @@ TypeConverter::getConstantOverrideInfo(SILDeclRef derived, SILDeclRef base) {
23282325
// Build the SILFunctionType for the vtable thunk.
23292326
CanSILFunctionType fnTy = getNativeSILFunctionType(
23302327
*this, basePattern, overrideLoweredInterfaceTy, base, derived,
2331-
/*reqt subs*/ None, ProtocolConformanceRef::forInvalid());
2328+
/*reqt subs*/ None, ProtocolConformanceRef());
23322329

23332330
// Build the SILConstantInfo and cache it.
23342331
auto resultBuf = Context.Allocate(sizeof(SILConstantInfo),
@@ -2422,7 +2419,7 @@ class SILTypeSubstituter :
24222419
substYields.push_back(substInterface(origYield));
24232420
}
24242421

2425-
auto witnessMethodConformance = ProtocolConformanceRef::forInvalid();
2422+
ProtocolConformanceRef witnessMethodConformance;
24262423
if (auto conformance = origType->getWitnessMethodConformanceOrInvalid()) {
24272424
assert(origType->getExtInfo().hasSelfParam());
24282425
auto selfType = origType->getSelfParameter().getInterfaceType();

lib/Sema/CSRanking.cpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -206,47 +206,40 @@ computeSelfTypeRelationship(TypeChecker &tc, DeclContext *dc, ValueDecl *decl1,
206206
// If both declarations are operators, even through they
207207
// might have Self such types are unrelated.
208208
if (decl1->isOperator() && decl2->isOperator())
209-
return {SelfTypeRelationship::Unrelated,
210-
ProtocolConformanceRef::forInvalid()};
209+
return {SelfTypeRelationship::Unrelated, ProtocolConformanceRef()};
211210

212211
auto *dc1 = decl1->getDeclContext();
213212
auto *dc2 = decl2->getDeclContext();
214213

215214
// If at least one of the contexts is a non-type context, the two are
216215
// unrelated.
217216
if (!dc1->isTypeContext() || !dc2->isTypeContext())
218-
return {SelfTypeRelationship::Unrelated,
219-
ProtocolConformanceRef::forInvalid()};
217+
return {SelfTypeRelationship::Unrelated, ProtocolConformanceRef()};
220218

221219
Type type1 = dc1->getDeclaredInterfaceType();
222220
Type type2 = dc2->getDeclaredInterfaceType();
223221

224222
// If the types are equal, the answer is simple.
225223
if (type1->isEqual(type2))
226-
return {SelfTypeRelationship::Equivalent,
227-
ProtocolConformanceRef::forInvalid()};
224+
return {SelfTypeRelationship::Equivalent, ProtocolConformanceRef()};
228225

229226
// If both types can have superclasses, which whether one is a superclass
230227
// of the other. The subclass is the common base type.
231228
if (type1->mayHaveSuperclass() && type2->mayHaveSuperclass()) {
232229
if (isNominallySuperclassOf(type1, type2))
233-
return {SelfTypeRelationship::Superclass,
234-
ProtocolConformanceRef::forInvalid()};
230+
return {SelfTypeRelationship::Superclass, ProtocolConformanceRef()};
235231

236232
if (isNominallySuperclassOf(type2, type1))
237-
return {SelfTypeRelationship::Subclass,
238-
ProtocolConformanceRef::forInvalid()};
233+
return {SelfTypeRelationship::Subclass, ProtocolConformanceRef()};
239234

240-
return {SelfTypeRelationship::Unrelated,
241-
ProtocolConformanceRef::forInvalid()};
235+
return {SelfTypeRelationship::Unrelated, ProtocolConformanceRef()};
242236
}
243237

244238
// If neither or both are protocol types, consider the bases unrelated.
245239
bool isProtocol1 = isa<ProtocolDecl>(dc1);
246240
bool isProtocol2 = isa<ProtocolDecl>(dc2);
247241
if (isProtocol1 == isProtocol2)
248-
return {SelfTypeRelationship::Unrelated,
249-
ProtocolConformanceRef::forInvalid()};
242+
return {SelfTypeRelationship::Unrelated, ProtocolConformanceRef()};
250243

251244
// Just one of the two is a protocol. Check whether the other conforms to
252245
// that protocol.

lib/Sema/TypeCheckType.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2711,7 +2711,7 @@ Type TypeResolver::resolveSILFunctionType(FunctionTypeRepr *repr,
27112711
interfaceResults = results;
27122712
interfaceErrorResult = errorResult;
27132713
}
2714-
auto witnessMethodConformance = ProtocolConformanceRef::forInvalid();
2714+
ProtocolConformanceRef witnessMethodConformance;
27152715
if (witnessMethodProtocol) {
27162716
auto resolved = resolveType(witnessMethodProtocol, options);
27172717
if (resolved->hasError())

lib/Serialization/Deserialization.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5038,7 +5038,7 @@ class swift::TypeDeserializer {
50385038
errorResult = maybeErrorResult.get();
50395039
}
50405040

5041-
auto witnessMethodConformance = ProtocolConformanceRef::forInvalid();
5041+
ProtocolConformanceRef witnessMethodConformance;
50425042
if (*representation == SILFunctionTypeRepresentation::WitnessMethod) {
50435043
witnessMethodConformance = MF.readConformance(MF.DeclTypeCursor);
50445044
}
@@ -5388,9 +5388,7 @@ void ModuleFile::finishNormalConformance(NormalProtocolConformance *conformance,
53885388
// conformance requirements are on Self. This isn't actually a /safe/ change
53895389
// even in Objective-C, but we mostly just don't want to crash.
53905390

5391-
// FIXME: DenseMap requires that its value type be default-constructible,
5392-
// which ProtocolConformanceRef is not, hence the extra Optional.
5393-
llvm::SmallDenseMap<ProtocolDecl *, Optional<ProtocolConformanceRef>, 16>
5391+
llvm::SmallDenseMap<ProtocolDecl *, ProtocolConformanceRef, 16>
53945392
conformancesForProtocols;
53955393
while (conformanceCount--) {
53965394
ProtocolConformanceRef nextConformance = readConformance(DeclTypeCursor);
@@ -5405,7 +5403,7 @@ void ModuleFile::finishNormalConformance(NormalProtocolConformance *conformance,
54055403
req.getSecondType()->castTo<ProtocolType>()->getDecl();
54065404
auto iter = conformancesForProtocols.find(proto);
54075405
if (iter != conformancesForProtocols.end()) {
5408-
reqConformances.push_back(iter->getSecond().getValue());
5406+
reqConformances.push_back(iter->getSecond());
54095407
} else {
54105408
// Put in an abstract conformance as a placeholder. This is a lie, but
54115409
// there's not much better we can do. We're relying on the fact that

0 commit comments

Comments
 (0)