Skip to content

Sema: Stop checking @main if no decl synthesized. #36529

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 1 commit into from
Mar 23, 2021

Conversation

nate-chandler
Copy link
Contributor

@nate-chandler nate-chandler commented Mar 19, 2021

When requestifying the synthesis of the main function for the type annotated @main via SynthesizeMainFunctionRequest, what previously were bailouts from AttributeChecker::visitMainTypeAttr had to become returns of nullptr from SynthesizeMainFunctionRequest::evaluate. Consequently, AttributeChecker::visitMainTypeAttr must check whether synthesis actually succeeded before proceeding to to register the main decl for a file.

In simple illegal cases, not checking happened to work because SourceFile::registerMainDecl would return early if the decl being registered was the same as the already registered main decl and in particular if the decl being registered was nullptr and the previously registered one was nullptr as well. When, however, there are multiple types annotated @main, if a function is successfully synthesized for one type but synthesis fails for the second, an attempt will be made to register nullptr as the main decl and will move past the check at the beginning of SourceFile::registerMainDecl, resulting in a crash.

Here, we bail from AttributeChecker::visitMainTypeAttr if function synthesis fails and add an assert to SourceFile::registerMainDecl that the provided decl is non-null.

rdar://75547146

When requestifying the synthesis of the main function for the type
annotated @main via SynthesizeMainFunctionRequest, what were previously
were bailouts from AttributeChecker::visitMainTypeAttr had to become
returns of nullptr from SynthesizeMainFunctionRequest::evaluate.
Consequently, AttributeChecker::visitMainTypeAttr must check whether
synthesis actually succeeded before proceeding to to register the main
decl for a file.

In simple cases, this happened to work because
SourceFile::registerMainDecl would return early if the decl being
registered was the same as the already registered main decl and in
particular if the decl being registered was nullptr and the previously
registered one was nullptr as well.  When, however, there are multiple
types annotated @main, if a function is successfully synthesized for one
type but synthesis fails for the second, an attempt will be made to
register nullptr as the main decl and will move past the check at the
beginning of SourceFile::registerMainDecl, resulting in a crash.

Here, we bail from AttributeChecker::visitMainTypeAttr if function
synthesis fails and add an assert to SourceFile::registerMainDecl that
the provided decl is non-null.

rdar://75547146
@nate-chandler
Copy link
Contributor Author

@swift-ci please test

@nate-chandler nate-chandler merged commit 64c7582 into swiftlang:main Mar 23, 2021
@nate-chandler nate-chandler deleted the rdar75547146 branch March 23, 2021 15:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant