-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Sema] Downgrade noasync
diagnostic to warning for closure macro args
#80853
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
hamishknight
merged 3 commits into
swiftlang:main
from
hamishknight:macro-async-warning
Apr 18, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
b6a09d1
Reapply: [CS] Make sure macro arguments go through `coerceCallArguments`
hamishknight 14bd411
NFC: Abstract away the use of '7' to represent the next language mode
hamishknight b36eb57
[Sema] Downgrade `noasync` diagnostic to warning for closure macro args
hamishknight File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -276,16 +276,18 @@ static bool shouldAllowReferenceToUnavailableInSwiftDeclaration( | |
return false; | ||
} | ||
|
||
// Utility function to help determine if noasync diagnostics are still | ||
// appropriate even if a `DeclContext` returns `false` from `isAsyncContext()`. | ||
static bool shouldTreatDeclContextAsAsyncForDiagnostics(const DeclContext *DC) { | ||
if (auto *D = DC->getAsDecl()) | ||
if (auto *FD = dyn_cast<FuncDecl>(D)) | ||
/// Retrieve the innermost DeclContext that should be consulted for noasync | ||
/// checking. | ||
static const DeclContext * | ||
getInnermostDeclContextForNoAsync(const DeclContext *DC) { | ||
if (auto *D = DC->getAsDecl()) { | ||
if (auto *FD = dyn_cast<FuncDecl>(D)) { | ||
if (FD->isDeferBody()) | ||
// If this is a defer body, we should delegate to its parent. | ||
return shouldTreatDeclContextAsAsyncForDiagnostics(DC->getParent()); | ||
|
||
return DC->isAsyncContext(); | ||
return getInnermostDeclContextForNoAsync(DC->getParent()); | ||
} | ||
} | ||
return DC; | ||
} | ||
|
||
/// A class that walks the AST to find the innermost (i.e., deepest) node that | ||
|
@@ -2758,7 +2760,8 @@ static bool | |
diagnoseDeclAsyncAvailability(const ValueDecl *D, SourceRange R, | ||
const Expr *call, const ExportContext &Where) { | ||
// If we are not in an (effective) async context, don't check it | ||
if (!shouldTreatDeclContextAsAsyncForDiagnostics(Where.getDeclContext())) | ||
auto *noAsyncDC = getInnermostDeclContextForNoAsync(Where.getDeclContext()); | ||
if (!noAsyncDC->isAsyncContext()) | ||
return false; | ||
|
||
ASTContext &ctx = Where.getDeclContext()->getASTContext(); | ||
|
@@ -2774,14 +2777,27 @@ diagnoseDeclAsyncAvailability(const ValueDecl *D, SourceRange R, | |
} | ||
} | ||
|
||
// In Swift 6 we previously didn't coerce macro arguments to parameter types, | ||
// so closure arguments may be treated as async in cases where they weren't in | ||
// Swift 6. As such we need to warn if the use is within a closure macro | ||
// argument until the next language mode. | ||
auto shouldWarnUntilFutureVersion = [&]() { | ||
auto *CE = dyn_cast<ClosureExpr>(noAsyncDC); | ||
return CE && CE->isMacroArgument(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was originally hoping we could use |
||
}; | ||
|
||
// @available(noasync) spelling | ||
if (auto attr = D->getNoAsyncAttr()) { | ||
SourceLoc diagLoc = call ? call->getLoc() : R.Start; | ||
auto diag = ctx.Diags.diagnose(diagLoc, diag::async_unavailable_decl, D, | ||
attr->getMessage()); | ||
diag.warnUntilSwiftVersion(6); | ||
diag.limitBehaviorWithPreconcurrency(DiagnosticBehavior::Warning, | ||
D->preconcurrency()); | ||
if (D->preconcurrency()) { | ||
diag.limitBehavior(DiagnosticBehavior::Warning); | ||
} else if (shouldWarnUntilFutureVersion()) { | ||
diag.warnUntilFutureSwiftVersion(); | ||
} else { | ||
diag.warnUntilSwiftVersion(6); | ||
} | ||
|
||
if (!attr->getRename().empty()) { | ||
fixItAvailableAttrRename(diag, R, D, attr->getRename(), call); | ||
|
@@ -2797,10 +2813,16 @@ diagnoseDeclAsyncAvailability(const ValueDecl *D, SourceRange R, | |
// @_unavailableFromAsync spelling | ||
const UnavailableFromAsyncAttr *attr = | ||
D->getAttrs().getAttribute<UnavailableFromAsyncAttr>(); | ||
SourceLoc diagLoc = call ? call->getLoc() : R.Start; | ||
ctx.Diags | ||
.diagnose(diagLoc, diag::async_unavailable_decl, D, attr->Message) | ||
.warnUntilSwiftVersion(6); | ||
{ | ||
SourceLoc diagLoc = call ? call->getLoc() : R.Start; | ||
auto diag = ctx.Diags.diagnose(diagLoc, diag::async_unavailable_decl, D, | ||
attr->Message); | ||
if (shouldWarnUntilFutureVersion()) { | ||
diag.warnUntilFutureSwiftVersion(); | ||
} else { | ||
diag.warnUntilSwiftVersion(6); | ||
} | ||
} | ||
D->diagnose(diag::decl_declared_here, D); | ||
return true; | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.