Skip to content

Commit 061f1ae

Browse files
authored
Merge pull request #37750 from bnbarham/5.5-cherry-stress-crashes
[5.5] Fix asserts and crashes caused by skipping function bodies or allowing errors
2 parents b8c5c12 + e859679 commit 061f1ae

File tree

10 files changed

+87
-2
lines changed

10 files changed

+87
-2
lines changed

lib/Parse/ParseDecl.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5173,7 +5173,9 @@ bool Parser::delayParsingDeclList(SourceLoc LBLoc, SourceLoc &RBLoc,
51735173
if (Tok.is(tok::r_brace)) {
51745174
RBLoc = consumeToken();
51755175
} else {
5176-
RBLoc = Tok.getLoc();
5176+
// Non-delayed parsing would set the RB location to the LB if it is missing,
5177+
// match that behaviour here
5178+
RBLoc = LBLoc;
51775179
error = true;
51785180
}
51795181
return error;

lib/Serialization/Deserialization.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4505,6 +4505,9 @@ llvm::Error DeclDeserializer::deserializeDeclCommon() {
45054505
skipAttr = true;
45064506
} else
45074507
return deserialized.takeError();
4508+
} else if (!deserialized.get() && MF.allowCompilerErrors()) {
4509+
// Serialized an invalid attribute, just skip it when allowing errors
4510+
skipAttr = true;
45084511
} else {
45094512
auto *TE = TypeExpr::createImplicit(deserialized.get(), ctx);
45104513
auto custom = CustomAttr::create(ctx, SourceLoc(), TE, isImplicit);

lib/Serialization/Serialization.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1440,7 +1440,8 @@ void Serializer::writeASTBlockEntity(
14401440
data.push_back(addDeclRef(req));
14411441
data.push_back(addDeclRef(witness.getDecl()));
14421442
assert(witness.getDecl() || req->getAttrs().hasAttribute<OptionalAttr>()
1443-
|| req->getAttrs().isUnavailable(req->getASTContext()));
1443+
|| req->getAttrs().isUnavailable(req->getASTContext())
1444+
|| allowCompilerErrors());
14441445

14451446
// If there is no witness, we're done.
14461447
if (!witness.getDecl()) return;
@@ -1992,6 +1993,8 @@ getStableSelfAccessKind(swift::SelfAccessKind MM) {
19921993
# define DECL(KIND, PARENT)\
19931994
LLVM_ATTRIBUTE_UNUSED \
19941995
static void verifyAttrSerializable(const KIND ## Decl *D) {\
1996+
if (D->Decl::getASTContext().LangOpts.AllowModuleWithCompilerErrors)\
1997+
return;\
19951998
for (auto Attr : D->getAttrs()) {\
19961999
assert(Attr->canAppearOnDecl(D) && "attribute cannot appear on a " #KIND);\
19972000
}\
@@ -3913,6 +3916,9 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
39133916
using namespace decls_block;
39143917
verifyAttrSerializable(dtor);
39153918

3919+
if (S.allowCompilerErrors() && dtor->isInvalid())
3920+
return;
3921+
39163922
auto contextID = S.addDeclContextRef(dtor->getDeclContext());
39173923

39183924
unsigned abbrCode = S.DeclTypeAbbrCodes[DestructorLayout::Code];
@@ -4143,6 +4149,13 @@ class Serializer::TypeSerializer : public TypeVisitor<TypeSerializer> {
41434149
}
41444150

41454151
void visitUnresolvedType(const UnresolvedType *) {
4152+
// If for some reason we have an unresolved type while compiling with
4153+
// errors, just serialize an ErrorType and continue.
4154+
if (S.getASTContext().LangOpts.AllowModuleWithCompilerErrors) {
4155+
visitErrorType(
4156+
cast<ErrorType>(ErrorType::get(S.getASTContext()).getPointer()));
4157+
return;
4158+
}
41464159
llvm_unreachable("should not serialize an UnresolvedType");
41474160
}
41484161

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// RUN: not %target-swift-frontend -typecheck -experimental-skip-all-function-bodies %s
2+
3+
#if os(
4+
struct Anything {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: not %target-swift-frontend -typecheck -experimental-skip-all-function-bodies %s
2+
3+
struct A {
4+
let prop: Int = {
5+
6+
struct B {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// RUN: %target-swift-frontend -module-name errors -emit-module -o %t/errors.swiftmodule -experimental-allow-module-with-compiler-errors %s
4+
5+
// Property wrappers are invalid on top level code, check allowing errors
6+
// does not crash
7+
8+
@propertyWrapper
9+
public struct Wrapper<T> {
10+
public var wrappedValue: T
11+
public init() {}
12+
}
13+
@Wrapper
14+
public var topLevelVar: Int = 10
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// RUN: %target-swift-frontend -module-name errors -emit-module -o %t/errors.swiftmodule -experimental-allow-module-with-compiler-errors %s
4+
5+
// @discardableResult is not allowed on a struct, make sure we don't crash
6+
// when allowing errors
7+
8+
@discardableResult
9+
struct SomeStruct {}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// RUN: %target-swift-frontend -module-name errors -emit-module -o %t/errors.swiftmodule -experimental-allow-module-with-compiler-errors %s
4+
5+
// deinit is only valid on a class, make sure we don't crash when allowing
6+
// errors
7+
8+
deinit {}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// RUN: %target-swift-frontend -module-name errors -emit-module -o %t/errors.swiftmodule -experimental-allow-module-with-compiler-errors %s
4+
5+
public protocol SomeProto {
6+
init(from: SomeProto)
7+
}
8+
9+
struct A {}
10+
struct B: SomeProto {
11+
let a: A
12+
}
13+
14+
let thing = B(a: A())
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// RUN: %target-swift-frontend -module-name errors -emit-module -o %t/errors.swiftmodule -experimental-allow-module-with-compiler-errors %s
4+
5+
protocol SomeProto {}
6+
7+
extension SomeProto {
8+
func someFunc(arg:
9+
10+
enum SomeEnum {
11+
case a
12+
}

0 commit comments

Comments
 (0)