Skip to content

[5.5] Fix asserts and crashes caused by skipping function bodies or allowing errors #37750

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 6 commits into from
Jun 3, 2021
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
4 changes: 3 additions & 1 deletion lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5173,7 +5173,9 @@ bool Parser::delayParsingDeclList(SourceLoc LBLoc, SourceLoc &RBLoc,
if (Tok.is(tok::r_brace)) {
RBLoc = consumeToken();
} else {
RBLoc = Tok.getLoc();
// Non-delayed parsing would set the RB location to the LB if it is missing,
// match that behaviour here
RBLoc = LBLoc;
error = true;
}
return error;
Expand Down
3 changes: 3 additions & 0 deletions lib/Serialization/Deserialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4505,6 +4505,9 @@ llvm::Error DeclDeserializer::deserializeDeclCommon() {
skipAttr = true;
} else
return deserialized.takeError();
} else if (!deserialized.get() && MF.allowCompilerErrors()) {
// Serialized an invalid attribute, just skip it when allowing errors
skipAttr = true;
} else {
auto *TE = TypeExpr::createImplicit(deserialized.get(), ctx);
auto custom = CustomAttr::create(ctx, SourceLoc(), TE, isImplicit);
Expand Down
15 changes: 14 additions & 1 deletion lib/Serialization/Serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1440,7 +1440,8 @@ void Serializer::writeASTBlockEntity(
data.push_back(addDeclRef(req));
data.push_back(addDeclRef(witness.getDecl()));
assert(witness.getDecl() || req->getAttrs().hasAttribute<OptionalAttr>()
|| req->getAttrs().isUnavailable(req->getASTContext()));
|| req->getAttrs().isUnavailable(req->getASTContext())
|| allowCompilerErrors());

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

if (S.allowCompilerErrors() && dtor->isInvalid())
return;

auto contextID = S.addDeclContextRef(dtor->getDeclContext());

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

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

Expand Down
4 changes: 4 additions & 0 deletions validation-test/Parse/delayed-members-open-if.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// RUN: not %target-swift-frontend -typecheck -experimental-skip-all-function-bodies %s

#if os(
struct Anything {
6 changes: 6 additions & 0 deletions validation-test/Parse/delayed-members-open-prop-scope.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// RUN: not %target-swift-frontend -typecheck -experimental-skip-all-function-bodies %s

struct A {
let prop: Int = {

struct B {
14 changes: 14 additions & 0 deletions validation-test/Serialization/AllowErrors/invalid-attr-refs.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: %empty-directory(%t)

// RUN: %target-swift-frontend -module-name errors -emit-module -o %t/errors.swiftmodule -experimental-allow-module-with-compiler-errors %s

// Property wrappers are invalid on top level code, check allowing errors
// does not crash

@propertyWrapper
public struct Wrapper<T> {
public var wrappedValue: T
public init() {}
}
@Wrapper
public var topLevelVar: Int = 10
9 changes: 9 additions & 0 deletions validation-test/Serialization/AllowErrors/invalid-attr.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: %empty-directory(%t)

// RUN: %target-swift-frontend -module-name errors -emit-module -o %t/errors.swiftmodule -experimental-allow-module-with-compiler-errors %s

// @discardableResult is not allowed on a struct, make sure we don't crash
// when allowing errors

@discardableResult
struct SomeStruct {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// RUN: %empty-directory(%t)

// RUN: %target-swift-frontend -module-name errors -emit-module -o %t/errors.swiftmodule -experimental-allow-module-with-compiler-errors %s

// deinit is only valid on a class, make sure we don't crash when allowing
// errors

deinit {}
14 changes: 14 additions & 0 deletions validation-test/Serialization/AllowErrors/invalid-witness.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: %empty-directory(%t)

// RUN: %target-swift-frontend -module-name errors -emit-module -o %t/errors.swiftmodule -experimental-allow-module-with-compiler-errors %s

public protocol SomeProto {
init(from: SomeProto)
}

struct A {}
struct B: SomeProto {
let a: A
}

let thing = B(a: A())
12 changes: 12 additions & 0 deletions validation-test/Serialization/AllowErrors/unresolved-type.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// RUN: %empty-directory(%t)

// RUN: %target-swift-frontend -module-name errors -emit-module -o %t/errors.swiftmodule -experimental-allow-module-with-compiler-errors %s

protocol SomeProto {}

extension SomeProto {
func someFunc(arg:

enum SomeEnum {
case a
}