Skip to content

Commit dd543e6

Browse files
authored
Merge pull request swiftlang#37701 from bnbarham/stress-crashes
Fix asserts and crashes caused by skipping function bodies or allowing errors
2 parents 8f876cf + 1fea98c commit dd543e6

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
@@ -5174,7 +5174,9 @@ bool Parser::delayParsingDeclList(SourceLoc LBLoc, SourceLoc &RBLoc,
51745174
if (Tok.is(tok::r_brace)) {
51755175
RBLoc = consumeToken();
51765176
} else {
5177-
RBLoc = Tok.getLoc();
5177+
// Non-delayed parsing would set the RB location to the LB if it is missing,
5178+
// match that behaviour here
5179+
RBLoc = LBLoc;
51785180
error = true;
51795181
}
51805182
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
@@ -1460,7 +1460,8 @@ void Serializer::writeASTBlockEntity(
14601460
data.push_back(addDeclRef(req));
14611461
data.push_back(addDeclRef(witness.getDecl()));
14621462
assert(witness.getDecl() || req->getAttrs().hasAttribute<OptionalAttr>()
1463-
|| req->getAttrs().isUnavailable(req->getASTContext()));
1463+
|| req->getAttrs().isUnavailable(req->getASTContext())
1464+
|| allowCompilerErrors());
14641465

14651466
// If there is no witness, we're done.
14661467
if (!witness.getDecl()) return;
@@ -2012,6 +2013,8 @@ getStableSelfAccessKind(swift::SelfAccessKind MM) {
20122013
# define DECL(KIND, PARENT)\
20132014
LLVM_ATTRIBUTE_UNUSED \
20142015
static void verifyAttrSerializable(const KIND ## Decl *D) {\
2016+
if (D->Decl::getASTContext().LangOpts.AllowModuleWithCompilerErrors)\
2017+
return;\
20152018
for (auto Attr : D->getAttrs()) {\
20162019
assert(Attr->canAppearOnDecl(D) && "attribute cannot appear on a " #KIND);\
20172020
}\
@@ -3933,6 +3936,9 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
39333936
using namespace decls_block;
39343937
verifyAttrSerializable(dtor);
39353938

3939+
if (S.allowCompilerErrors() && dtor->isInvalid())
3940+
return;
3941+
39363942
auto contextID = S.addDeclContextRef(dtor->getDeclContext());
39373943

39383944
unsigned abbrCode = S.DeclTypeAbbrCodes[DestructorLayout::Code];
@@ -4163,6 +4169,13 @@ class Serializer::TypeSerializer : public TypeVisitor<TypeSerializer> {
41634169
}
41644170

41654171
void visitUnresolvedType(const UnresolvedType *) {
4172+
// If for some reason we have an unresolved type while compiling with
4173+
// errors, just serialize an ErrorType and continue.
4174+
if (S.getASTContext().LangOpts.AllowModuleWithCompilerErrors) {
4175+
visitErrorType(
4176+
cast<ErrorType>(ErrorType::get(S.getASTContext()).getPointer()));
4177+
return;
4178+
}
41664179
llvm_unreachable("should not serialize an UnresolvedType");
41674180
}
41684181

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)