Skip to content

Commit a4bfa3f

Browse files
authored
Merge pull request #82275 from slavapestov/more-fuzzer-fixes-2
More fuzzer fixes
2 parents e29b425 + 1b4178f commit a4bfa3f

26 files changed

+108
-61
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,10 +1436,10 @@ ERROR(did_not_call_function_value,none,
14361436
())
14371437
ERROR(did_not_call_function,none,
14381438
"function %0 was used as a property; add () to call it",
1439-
(Identifier))
1439+
(DeclBaseName))
14401440
ERROR(did_not_call_method,none,
14411441
"method %0 was used as a property; add () to call it",
1442-
(Identifier))
1442+
(DeclBaseName))
14431443

14441444
ERROR(init_not_instance_member_use_assignment,none,
14451445
"'init' is a member of the type; use assignment "
@@ -3781,9 +3781,6 @@ ERROR(enum_non_integer_convertible_raw_type_no_value,none,
37813781
"expressible by integer or string literal", ())
37823782
ERROR(enum_raw_value_not_unique,none,
37833783
"raw value for enum case is not unique", ())
3784-
ERROR(enum_raw_value_magic_literal,none,
3785-
"use of '%0' literal as raw value for enum case is not supported",
3786-
(StringRef))
37873784
NOTE(enum_raw_value_used_here,none,
37883785
"raw value previously used here", ())
37893786
NOTE(enum_raw_value_incrementing_from_here,none,

lib/AST/ASTScope.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,8 @@ SourceRange NominalTypeScope::getBraces() const { return decl->getBraces(); }
356356

357357
NullablePtr<NominalTypeDecl>
358358
ExtensionScope::getCorrespondingNominalTypeDecl() const {
359+
if (!decl->hasBeenBound())
360+
return nullptr;
359361
return decl->getExtendedNominal();
360362
}
361363

lib/AST/ProtocolConformance.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -990,9 +990,12 @@ static bool isVanishingTupleConformance(
990990

991991
auto replacementTypes = substitutions.getReplacementTypes();
992992
assert(replacementTypes.size() == 1);
993-
auto packType = replacementTypes[0]->castTo<PackType>();
994993

995-
return (packType->getNumElements() == 1 &&
994+
// This might not be an actual pack type with an invalid tuple conformance.
995+
auto packType = replacementTypes[0]->getAs<PackType>();
996+
997+
return (packType &&
998+
packType->getNumElements() == 1 &&
996999
!packType->getElementTypes()[0]->is<PackExpansionType>());
9971000
}
9981001

lib/AST/RequirementMachine/RequirementLowering.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ static void desugarSameShapeRequirement(
451451
!req.getSecondType()->isParameterPack()) {
452452
errors.push_back(RequirementError::forInvalidShapeRequirement(
453453
req, loc));
454+
return;
454455
}
455456

456457
result.emplace_back(RequirementKind::SameShape,

lib/Parse/ParseDecl.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6337,8 +6337,8 @@ ParserStatus Parser::parseDecl(bool IsAtStartOfLineOrPreviousHadSemi,
63376337
DescriptiveKind = DescriptiveDeclKind::StaticProperty;
63386338
break;
63396339
case StaticSpellingKind::KeywordClass:
6340-
llvm_unreachable("kw_class is only parsed as a modifier if it's "
6341-
"followed by a keyword");
6340+
DescriptiveKind = DescriptiveDeclKind::ClassProperty;
6341+
break;
63426342
}
63436343

63446344
diagnose(Tok.getLoc(), diag::expected_keyword_in_decl, "var",
@@ -6366,8 +6366,7 @@ ParserStatus Parser::parseDecl(bool IsAtStartOfLineOrPreviousHadSemi,
63666366
DescriptiveKind = DescriptiveDeclKind::StaticMethod;
63676367
break;
63686368
case StaticSpellingKind::KeywordClass:
6369-
llvm_unreachable("kw_class is only parsed as a modifier if it's "
6370-
"followed by a keyword");
6369+
DescriptiveKind = DescriptiveDeclKind::ClassMethod;
63716370
}
63726371
}
63736372

@@ -9229,6 +9228,20 @@ ParserResult<EnumDecl> Parser::parseDeclEnum(ParseDeclOptions Flags,
92299228
return DCC.fixupParserResult(Status, ED);
92309229
}
92319230

9231+
static bool isValidEnumRawValueLiteral(LiteralExpr *expr) {
9232+
if (expr == nullptr)
9233+
return false;
9234+
9235+
if (!isa<IntegerLiteralExpr>(expr) &&
9236+
!isa<FloatLiteralExpr>(expr) &&
9237+
!isa<StringLiteralExpr>(expr) &&
9238+
!isa<BooleanLiteralExpr>(expr) &&
9239+
!isa<NilLiteralExpr>(expr))
9240+
return false;
9241+
9242+
return true;
9243+
}
9244+
92329245
/// Parse a 'case' of an enum.
92339246
///
92349247
/// \verbatim
@@ -9346,8 +9359,7 @@ Parser::parseDeclEnumCase(ParseDeclOptions Flags,
93469359
}
93479360
// The raw value must be syntactically a simple literal.
93489361
LiteralRawValueExpr = dyn_cast<LiteralExpr>(RawValueExpr.getPtrOrNull());
9349-
if (!LiteralRawValueExpr
9350-
|| isa<InterpolatedStringLiteralExpr>(LiteralRawValueExpr)) {
9362+
if (!isValidEnumRawValueLiteral(LiteralRawValueExpr)) {
93519363
diagnose(RawValueExpr.getPtrOrNull()->getLoc(),
93529364
diag::nonliteral_enum_case_raw_value);
93539365
LiteralRawValueExpr = nullptr;

lib/Sema/CSDiagnostics.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3940,22 +3940,22 @@ bool MissingCallFailure::diagnoseAsError() {
39403940

39413941
if (auto *DRE = getAsExpr<DeclRefExpr>(anchor)) {
39423942
emitDiagnostic(diag::did_not_call_function,
3943-
DRE->getDecl()->getBaseIdentifier())
3943+
DRE->getDecl()->getBaseName())
39443944
.fixItInsertAfter(insertLoc, "()");
39453945
return true;
39463946
}
39473947

39483948
if (auto *UDE = getAsExpr<UnresolvedDotExpr>(anchor)) {
39493949
emitDiagnostic(diag::did_not_call_method,
3950-
UDE->getName().getBaseIdentifier())
3950+
UDE->getName().getBaseName())
39513951
.fixItInsertAfter(insertLoc, "()");
39523952
return true;
39533953
}
39543954

39553955
if (auto *DSCE = getAsExpr<DotSyntaxCallExpr>(anchor)) {
39563956
if (auto *DRE = dyn_cast<DeclRefExpr>(DSCE->getFn())) {
39573957
emitDiagnostic(diag::did_not_call_method,
3958-
DRE->getDecl()->getBaseIdentifier())
3958+
DRE->getDecl()->getBaseName())
39593959
.fixItInsertAfter(insertLoc, "()");
39603960
return true;
39613961
}

lib/Sema/DerivedConformance/DerivedConformance.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,9 @@ void DerivedConformance::diagnoseIfSynthesisUnsupportedForDecl(
272272
shouldDiagnose = !isa<EnumDecl>(nominal);
273273
}
274274

275+
if (isa<BuiltinTupleDecl>(nominal))
276+
shouldDiagnose = false;
277+
275278
if (shouldDiagnose) {
276279
auto &ctx = nominal->getASTContext();
277280
ctx.Diags.diagnose(nominal->getLoc(),

lib/Sema/MiscDiagnostics.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6268,9 +6268,9 @@ diagnoseDictionaryLiteralDuplicateKeyEntries(const Expr *E,
62686268
note.fixItRemove(duplicated.first->getSourceRange());
62696269
if (duplicatedEltIdx < commanLocs.size()) {
62706270
note.fixItRemove(commanLocs[duplicatedEltIdx]);
6271-
} else {
6271+
} else if (!commanLocs.empty()) {
62726272
// For the last element remove the previous comma.
6273-
note.fixItRemove(commanLocs[duplicatedEltIdx - 1]);
6273+
note.fixItRemove(commanLocs[commanLocs.size() - 1]);
62746274
}
62756275
};
62766276

lib/Sema/TypeCheckDecl.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,15 +1304,6 @@ EnumRawValuesRequest::evaluate(Evaluator &eval, EnumDecl *ED,
13041304
SourceLoc diagLoc = uncheckedRawValueOf(elt)->isImplicit()
13051305
? elt->getLoc()
13061306
: uncheckedRawValueOf(elt)->getLoc();
1307-
if (auto magicLiteralExpr =
1308-
dyn_cast<MagicIdentifierLiteralExpr>(prevValue)) {
1309-
auto kindString =
1310-
magicLiteralExpr->getKindString(magicLiteralExpr->getKind());
1311-
Diags.diagnose(diagLoc, diag::enum_raw_value_magic_literal, kindString);
1312-
elt->setInvalid();
1313-
continue;
1314-
}
1315-
13161307
// Check that the raw value is unique.
13171308
RawValueKey key{prevValue};
13181309
RawValueSource source{elt, lastExplicitValueElt};

lib/Sema/TypeCheckDeclOverride.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ bool swift::isOverrideBasedOnType(const ValueDecl *decl, Type declTy,
217217
return false;
218218
}
219219

220+
if (declTy->is<ErrorType>())
221+
return false;
222+
220223
auto fnType1 = declTy->castTo<AnyFunctionType>();
221224
auto fnType2 = parentDeclTy->castTo<AnyFunctionType>();
222225
return AnyFunctionType::equalParams(fnType1->getParams(),

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3888,10 +3888,19 @@ filterProtocolRequirements(
38883888
return Filtered;
38893889
}
38903890

3891-
const auto getProtocolSubstitutionMap = [&](ValueDecl *Req) {
3892-
auto *const PD = cast<ProtocolDecl>(Req->getDeclContext());
3893-
auto Conformance = lookupConformance(Adoptee, PD);
3894-
return SubstitutionMap::getProtocolSubstitutions(Conformance);
3891+
const auto getProtocolSubstitutionMap = [&](ValueDecl *req) {
3892+
ASSERT(isa<ProtocolDecl>(req->getDeclContext()));
3893+
auto genericSig = req->getInnermostDeclContext()
3894+
->getGenericSignatureOfContext();
3895+
SmallVector<Type, 2> args;
3896+
for (auto paramTy : genericSig.getGenericParams()) {
3897+
if (args.empty())
3898+
args.push_back(Adoptee);
3899+
else
3900+
args.push_back(paramTy);
3901+
}
3902+
return SubstitutionMap::get(genericSig, args,
3903+
LookUpConformanceInModule());
38953904
};
38963905

38973906
llvm::SmallDenseMap<DeclName, llvm::SmallVector<ValueDecl *, 2>, 4>
@@ -3907,11 +3916,11 @@ filterProtocolRequirements(
39073916
auto OverloadTy = Req->getOverloadSignatureType();
39083917
if (OverloadTy) {
39093918
auto Subs = getProtocolSubstitutionMap(Req);
3910-
// FIXME: This is wrong if the overload has its own generic parameters
3911-
if (auto GenericFnTy = dyn_cast<GenericFunctionType>(OverloadTy))
3919+
if (auto GenericFnTy = dyn_cast<GenericFunctionType>(OverloadTy)) {
39123920
OverloadTy = GenericFnTy.substGenericArgs(Subs);
3913-
else
3921+
} else {
39143922
OverloadTy = OverloadTy.subst(Subs)->getCanonicalType();
3923+
}
39153924
}
39163925
if (llvm::any_of(DeclsByName[Req->getName()], [&](ValueDecl *OtherReq) {
39173926
auto OtherOverloadTy = OtherReq->getOverloadSignatureType();

lib/Sema/TypeCheckStorage.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2996,9 +2996,10 @@ LazyStoragePropertyRequest::evaluate(Evaluator &evaluator,
29962996
addMemberToContextIfNeeded(PBD, VD->getDeclContext(), Storage);
29972997

29982998
// Make sure the original init is marked as subsumed.
2999-
auto *originalPBD = VD->getParentPatternBinding();
3000-
auto originalIndex = originalPBD->getPatternEntryIndexForVarDecl(VD);
3001-
originalPBD->setInitializerSubsumed(originalIndex);
2999+
if (auto *originalPBD = VD->getParentPatternBinding()) {
3000+
auto originalIndex = originalPBD->getPatternEntryIndexForVarDecl(VD);
3001+
originalPBD->setInitializerSubsumed(originalIndex);
3002+
}
30023003

30033004
return Storage;
30043005
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// RUN: not %target-swift-frontend -typecheck %s
2+
3+
// Just don't crash.
4+
extension () : Comparable {}
5+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
_ = a.init
4+
_ = b.init
5+
6+
enum a : Int { case x = #/ /# } // expected-error {{raw value for enum case must be a literal}}
7+
enum b : String { case x = #file } // expected-error {{raw value for enum case must be a literal}}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
protocol P1 {
4+
associatedtype A
5+
}
6+
7+
protocol P2 {
8+
func f<T>(_: T, _: T) // expected-note {{protocol requires function 'f' with type '<T> (T, T) -> ()'}}
9+
func f<T: P1>(_: T, _: T.A) // expected-note {{protocol requires function 'f' with type '<T> (T, T.A) -> ()'}}
10+
}
11+
12+
struct S: P2 {} // expected-error {{type 'S' does not conform to protocol 'P2'}}
13+
// expected-note@-1 {{add stubs for conformance}}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
// {"signature":"swift::Parser::parseNewDeclAttribute(swift::DeclAttributes&, swift::SourceLoc, swift::DeclAttrKind, bool)::$_4::operator()() const"}
2-
// RUN: not --crash %target-swift-frontend -typecheck %s
2+
// RUN: not %target-swift-frontend -typecheck %s
33
class a {
44
class override b
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
// {"signature":"swift::LazyStoragePropertyRequest::evaluate(swift::Evaluator&, swift::VarDecl*) const"}
2-
// RUN: not --crash %target-swift-frontend -typecheck %s
2+
// RUN: not %target-swift-frontend -typecheck %s
33
class a {
44
lazy(b, c) {
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// {"signature":"swift::Parser::parseDecl(bool, bool, llvm::function_ref<void (swift::Decl*)>, bool)"}
2-
// RUN: not --crash %target-swift-frontend -typecheck %s
2+
// RUN: not %target-swift-frontend -typecheck %s
33
class a { class override ( override
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// {"signature":"swift::GenericContext::getGenericParams() const"}
2-
// RUN: not --crash %target-swift-frontend -typecheck %s
2+
// RUN: not %target-swift-frontend -typecheck %s
33
typealias a = () extension a : Comparable

validation-test/compiler_crashers_2/2617a198505b47e4.swift renamed to validation-test/compiler_crashers_2_fixed/2617a198505b47e4.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// {"signature":"swift::rewriting::RequirementMachine::getReducedShape(swift::Type, llvm::ArrayRef<swift::GenericTypeParamType*>) const"}
2-
// RUN: not --crash %target-swift-frontend -typecheck %s
2+
// RUN: not %target-swift-frontend -typecheck %s
33
protocol a{ b < c > (c, _ : c}
44
protocol d : a{
55
b<c : e>(c, c.c) protocol e {
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// {"signature":"deriveBodyRawRepresentable_init(swift::AbstractFunctionDecl*, void*)"}
2-
// RUN: not --crash %target-swift-frontend -typecheck %s
2+
// RUN: not %target-swift-frontend -typecheck %s
33
a enum a : Int { case = #/

validation-test/compiler_crashers_2/90773c979d435f7.swift renamed to validation-test/compiler_crashers_2_fixed/90773c979d435f7.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// {"signature":"swift::isOverrideBasedOnType(swift::ValueDecl const*, swift::Type, swift::ValueDecl const*)"}
2-
// RUN: not --crash %target-swift-frontend -typecheck %s
2+
// RUN: not %target-swift-frontend -typecheck %s
33
struct a < b {
44
protocol c { associatedtype d init(e : d
55
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// {"signature":"diagnoseDictionaryLiteralDuplicateKeyEntries(swift::Expr const*, swift::DeclContext const*)::DiagnoseWalker::walkToExprPre(swift::Expr*)"}
2-
// RUN: not --crash %target-swift-frontend -typecheck %s
2+
// RUN: not %target-swift-frontend -typecheck %s
33
[ 1.01: "" 1.01: ""
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// {"signature":"(anonymous namespace)::ABIDependencyEvaluator::computeABIDependenciesForModule(swift::ModuleDecl*)"}
2-
// RUN: not --crash %target-swift-frontend -typecheck %s
2+
// RUN: not %target-swift-frontend -typecheck %s
33
struct a { init? { init!
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
// {"signature":"swift::rewriting::performConcreteContraction(llvm::ArrayRef<swift::StructuralRequirement>, llvm::SmallVectorImpl<swift::StructuralRequirement>&, llvm::SmallVectorImpl<swift::rewriting::RequirementError>&, bool)"}
2-
// RUN: not --crash %target-swift-frontend -typecheck %s
2+
// RUN: not %target-swift-frontend -typecheck %s
33
func a < each b, each c where(repeat(each c each b)) : {
44
typealias d<each b> = () func e where d<repeat each b> ==

validation-test/compiler_crashers_2_fixed/issue-55443.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22

33
// https://github.com/apple/swift/issues/55443
44

5-
enum FooString: String { // expected-error {{'FooString' declares raw type 'String', but does not conform to RawRepresentable and conformance could not be synthesized}} expected-note {{add stubs for conformance}}
6-
case bar1 = #file // expected-error {{use of '#file' literal as raw value for enum case is not supported}}
7-
case bar2 = #function // expected-error {{use of '#function' literal as raw value for enum case is not supported}}
8-
case bar3 = #filePath // expected-error {{use of '#filePath' literal as raw value for enum case is not supported}}
9-
case bar4 = #line // expected-error {{cannot convert value of type 'Int' to raw type 'String'}}
10-
case bar5 = #column // expected-error {{cannot convert value of type 'Int' to raw type 'String'}}
11-
case bar6 = #dsohandle // expected-error {{cannot convert value of type 'UnsafeRawPointer' to raw type 'String'}}
5+
enum FooString: String {
6+
case bar1 = #file // expected-error {{raw value for enum case must be a literal}}
7+
case bar2 = #function // expected-error {{raw value for enum case must be a literal}}
8+
case bar3 = #filePath // expected-error {{raw value for enum case must be a literal}}
9+
case bar4 = #line // expected-error {{raw value for enum case must be a literal}}
10+
case bar5 = #column // expected-error {{raw value for enum case must be a literal}}
11+
case bar6 = #dsohandle // expected-error {{raw value for enum case must be a literal}}
1212
}
1313

14-
enum FooInt: Int { // expected-error {{'FooInt' declares raw type 'Int', but does not conform to RawRepresentable and conformance could not be synthesized}} expected-note {{add stubs for conformance}}
15-
case bar1 = #file // expected-error {{cannot convert value of type 'String' to raw type 'Int'}}
16-
case bar2 = #function // expected-error {{cannot convert value of type 'String' to raw type 'Int'}}
17-
case bar3 = #filePath // expected-error {{cannot convert value of type 'String' to raw type 'Int'}}
18-
case bar4 = #line // expected-error {{use of '#line' literal as raw value for enum case is not supported}}
19-
case bar5 = #column // expected-error {{use of '#column' literal as raw value for enum case is not supported}}
20-
case bar6 = #dsohandle // expected-error {{cannot convert value of type 'UnsafeRawPointer' to raw type 'Int'}}
14+
enum FooInt: Int {
15+
case bar1 = #file // expected-error {{raw value for enum case must be a literal}}
16+
case bar2 = #function // expected-error {{raw value for enum case must be a literal}}
17+
case bar3 = #filePath // expected-error {{raw value for enum case must be a literal}}
18+
case bar4 = #line // expected-error {{raw value for enum case must be a literal}}
19+
case bar5 = #column // expected-error {{raw value for enum case must be a literal}}
20+
case bar6 = #dsohandle // expected-error {{raw value for enum case must be a literal}}
2121
}

0 commit comments

Comments
 (0)