Skip to content

AST/SILGen: Use @_alwaysEmitIntoClient diagnostic helper in unavailable code #70547

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
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: 4 additions & 0 deletions include/swift/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -1526,6 +1526,10 @@ class ASTContext final {
/// The declared interface type of Builtin.TheTupleType.
BuiltinTupleType *getBuiltinTupleType();

/// The declaration for the `_diagnoseUnavailableCodeReached()` declaration
/// that ought to be used for the configured deployment target.
FuncDecl *getDiagnoseUnavailableCodeReachedDecl();

Type getNamedSwiftType(ModuleDecl *module, StringRef name);

/// Set the plugin loader.
Expand Down
1 change: 1 addition & 0 deletions include/swift/AST/KnownDecls.def
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ FUNC_DECL(DiagnoseUnexpectedNilOptional, "_diagnoseUnexpectedNilOptional")
FUNC_DECL(DiagnoseUnexpectedEnumCase, "_diagnoseUnexpectedEnumCase")
FUNC_DECL(DiagnoseUnexpectedEnumCaseValue, "_diagnoseUnexpectedEnumCaseValue")
FUNC_DECL(DiagnoseUnavailableCodeReached, "_diagnoseUnavailableCodeReached")
FUNC_DECL(DiagnoseUnavailableCodeReachedAEIC, "_diagnoseUnavailableCodeReached_aeic")

FUNC_DECL(GetErrorEmbeddedNSError, "_getErrorEmbeddedNSError")

Expand Down
8 changes: 8 additions & 0 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6312,6 +6312,14 @@ BuiltinTupleType *ASTContext::getBuiltinTupleType() {
return result;
}

FuncDecl *ASTContext::getDiagnoseUnavailableCodeReachedDecl() {
// FIXME: Remove this with rdar://119892482
if (AvailabilityContext::forDeploymentTarget(*this).isContainedIn(
getSwift59Availability()))
return getDiagnoseUnavailableCodeReached();
return getDiagnoseUnavailableCodeReachedAEIC();
}

void ASTContext::setPluginLoader(std::unique_ptr<PluginLoader> loader) {
getImpl().Plugins = std::move(loader);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/SILGen/SILGenApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6111,7 +6111,7 @@ void SILGenFunction::emitApplyOfUnavailableCodeReached() {
return;

auto loc = RegularLocation::getAutoGeneratedLocation(F.getLocation());
FuncDecl *fd = getASTContext().getDiagnoseUnavailableCodeReached();
FuncDecl *fd = getASTContext().getDiagnoseUnavailableCodeReachedDecl();

if (!fd) {
// Broken stdlib?
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/DerivedConformances.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ DerivedConformance::createBuiltinCall(ASTContext &ctx,

CallExpr *DerivedConformance::createDiagnoseUnavailableCodeReachedCallExpr(
ASTContext &ctx) {
FuncDecl *diagnoseDecl = ctx.getDiagnoseUnavailableCodeReached();
FuncDecl *diagnoseDecl = ctx.getDiagnoseUnavailableCodeReachedDecl();
auto diagnoseDeclRefExpr =
new (ctx) DeclRefExpr(diagnoseDecl, DeclNameLoc(), true);
diagnoseDeclRefExpr->setType(diagnoseDecl->getInterfaceType());
Expand Down
11 changes: 11 additions & 0 deletions stdlib/public/core/AssertCommon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,17 @@ internal func _diagnoseUnexpectedEnumCase<SwitchedValue>(
@_semantics("unavailable_code_reached")
@usableFromInline // COMPILER_INTRINSIC
internal func _diagnoseUnavailableCodeReached() -> Never {
_diagnoseUnavailableCodeReached_aeic()
}

// FIXME: Remove this with rdar://119892482
/// An `@_alwaysEmitIntoClient` variant of `_diagnoseUnavailableCodeReached()`.
/// This is temporarily needed by the compiler to reference from back deployed
/// clients.
@_alwaysEmitIntoClient
@inline(never)
@_semantics("unavailable_code_reached")
internal func _diagnoseUnavailableCodeReached_aeic() -> Never {
_assertionFailure(
"Fatal error", "Unavailable code reached", flags: _fatalErrorFlags())
}
2 changes: 1 addition & 1 deletion test/SILGen/unavailable_clang_enum_typedef.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ typedef MyOptions MyOptionsTypedef;
let _ = MyOptionsTypedef(rawValue: 1)

// CHECK-LABEL: sil shared [transparent] [serialized]{{.*}} @$sSo9MyOptionsa8rawValueABs5Int32V_tcfC : $@convention(method) (Int32, @thin MyOptions.Type) -> MyOptions {
// CHECK-NOT: ss31_diagnoseUnavailableCodeReacheds5NeverOyFTwb
// CHECK-NOT: _diagnoseUnavailableCodeReached
// CHECK: } // end sil function '$sSo9MyOptionsa8rawValueABs5Int32V_tcfC'
2 changes: 1 addition & 1 deletion test/SILGen/unavailable_decl_optimization_stub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public struct S {}

// CHECK-LABEL: sil{{.*}}@$s4Test15unavailableFuncAA1SVyF
// CHECK: [[FNREF:%.*]] = function_ref @$[[DIAGNOSEFN:ss31_diagnoseUnavailableCodeReacheds5NeverOy(FTwb|F)]] : $@convention(thin) () -> Never
// CHECK: [[FNREF:%.*]] = function_ref @$[[DIAGNOSEFN:(ss36_diagnoseUnavailableCodeReached_aeics5NeverOyF|ss31_diagnoseUnavailableCodeReacheds5NeverOyF)]] : $@convention(thin) () -> Never
// CHECK-NEXT: [[APPLY:%.*]] = apply [[FNREF]]()
// CHECK: function_ref @$s4Test1SVACycfC
// CHECK: } // end sil function '$s4Test15unavailableFuncAA1SVyF'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ func foo() {}
public class ExplicitInitClass {
// ExplicitInitClass.s.getter
// CHECK-LABEL: sil{{.*}}@$s4Test17ExplicitInitClassC1sAA1SVvg
// CHECK: [[FNREF:%.*]] = function_ref @$[[DIAGNOSEFN:ss31_diagnoseUnavailableCodeReacheds5NeverOy(FTwb|F)]] : $@convention(thin) () -> Never
// CHECK: [[FNREF:%.*]] = function_ref @$[[DIAGNOSEFN:(ss36_diagnoseUnavailableCodeReached_aeics5NeverOyF|ss31_diagnoseUnavailableCodeReacheds5NeverOyF)]] : $@convention(thin) () -> Never
// CHECK-NEXT: [[APPLY:%.*]] = apply [[FNREF]]()
// CHECK: load
// CHECK: } // end sil function '$s4Test17ExplicitInitClassC1sAA1SVvg'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func foo() {}
@objc public class C: NSObject {
// C.__allocating_init()
// CHECK-LABEL: sil{{.*}}@$s4Test1CCACycfC
// CHECK: [[FNREF:%.*]] = function_ref @$[[DIAGNOSEFN:ss31_diagnoseUnavailableCodeReacheds5NeverOy(FTwb|F)]] : $@convention(thin) () -> Never
// CHECK: [[FNREF:%.*]] = function_ref @$[[DIAGNOSEFN:(ss36_diagnoseUnavailableCodeReached_aeics5NeverOyF|ss31_diagnoseUnavailableCodeReacheds5NeverOyF)]] : $@convention(thin) () -> Never
// CHECK-NEXT: [[APPLY:%.*]] = apply [[FNREF]]()
// CHECK: {{%.*}} = function_ref @$s4Test1CCACycfcTD
// CHECK: } // end sil function '$s4Test1CCACycfC'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public struct S {}
extension S: P {}

// CHECK-LABEL: sil{{.*}}@$s4Test27unavailableOpaqueReturnFuncQryF
// CHECK: [[FNREF:%.*]] = function_ref @$[[DIAGNOSEFN:ss31_diagnoseUnavailableCodeReacheds5NeverOy(FTwb|F)]] : $@convention(thin) () -> Never
// CHECK: [[FNREF:%.*]] = function_ref @$[[DIAGNOSEFN:(ss36_diagnoseUnavailableCodeReached_aeics5NeverOyF|ss31_diagnoseUnavailableCodeReacheds5NeverOyF)]] : $@convention(thin) () -> Never
// CHECK-NEXT: [[APPLY:%.*]] = apply [[FNREF]]()
// CHECK: function_ref @$s4Test1SVACycfC
// CHECK: } // end sil function '$s4Test27unavailableOpaqueReturnFuncQryF'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extension EnumWithProtocolWitness: P {}
// protocol witness for static P.requirement(_:) in conformance EnumWithProtocolWitness
//
// CHECK-LABEL: sil{{.*}}@$s4Test23EnumWithProtocolWitnessOAA1PA2aDP11requirementyxAA1SVFZTW
// CHECK: [[FNREF:%.*]] = function_ref @$[[DIAGNOSEFN:ss31_diagnoseUnavailableCodeReacheds5NeverOy(FTwb|F)]] : $@convention(thin) () -> Never
// CHECK: [[FNREF:%.*]] = function_ref @$[[DIAGNOSEFN:(ss36_diagnoseUnavailableCodeReached_aeics5NeverOyF|ss31_diagnoseUnavailableCodeReacheds5NeverOyF)]] : $@convention(thin) () -> Never
// CHECK-NEXT: [[APPLY:%.*]] = apply [[FNREF]]()
// CHECK: {{%.*}} = function_ref @$s4Test23EnumWithProtocolWitnessO11requirementyAcA1SVcACmF
// CHECK: } // end sil function '$s4Test23EnumWithProtocolWitnessOAA1PA2aDP11requirementyxAA1SVFZTW'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public struct S {}
@available(*, unavailable)
public struct ImplicitInitStruct {
// CHECK-LABEL: sil hidden {{.*}} @$s4Test18ImplicitInitStructVACycfC
// CHECK: [[FNREF:%.*]] = function_ref @$[[DIAGNOSEFN:ss31_diagnoseUnavailableCodeReacheds5NeverOy(FTwb|F)]] : $@convention(thin) () -> Never
// CHECK: [[FNREF:%.*]] = function_ref @$[[DIAGNOSEFN:(ss36_diagnoseUnavailableCodeReached_aeics5NeverOyF|ss31_diagnoseUnavailableCodeReacheds5NeverOyF)]] : $@convention(thin) () -> Never
// CHECK-NEXT: [[APPLY:%.*]] = apply [[FNREF]]()
// CHECK: return
// CHECK: } // end sil function '$s4Test18ImplicitInitStructVACycfC'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public struct S {}

// CHECK-LABEL: sil{{.*}}@$s4Test15unavailableFuncAA1SVyF
// CHECK: [[FNREF:%.*]] = function_ref @$[[DIAGNOSEFN:ss31_diagnoseUnavailableCodeReacheds5NeverOy(FTwb|F)]] : $@convention(thin) () -> Never
// CHECK: [[FNREF:%.*]] = function_ref @$[[DIAGNOSEFN:(ss36_diagnoseUnavailableCodeReached_aeics5NeverOyF|ss31_diagnoseUnavailableCodeReacheds5NeverOyF)]] : $@convention(thin) () -> Never
// CHECK-NEXT: [[APPLY:%.*]] = apply [[FNREF]]()
// CHECK-NEXT: unreachable
// CHECK-NEXT: } // end sil function '$s4Test15unavailableFuncAA1SVyF'
Expand Down
10 changes: 5 additions & 5 deletions test/decl/enum/derived_hashable_equatable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ enum HasUnavailableElement: Hashable {
// CHECK-NEXT: case .a:
// CHECK-NEXT: index_a = 0
// CHECK-NEXT: case .b:
// CHECK-NEXT: _diagnoseUnavailableCodeReached()
// CHECK-NEXT: _diagnoseUnavailableCodeReached{{.*}}()
// CHECK-NEXT: }
// CHECK-NEXT: private var index_b: Int
// CHECK-NEXT: switch b {
// CHECK-NEXT: case .a:
// CHECK-NEXT: index_b = 0
// CHECK-NEXT: case .b:
// CHECK-NEXT: _diagnoseUnavailableCodeReached()
// CHECK-NEXT: _diagnoseUnavailableCodeReached{{.*}}()
// CHECK-NEXT: }
// CHECK-NEXT: return index_a == index_b
// CHECK-NEXT: }
Expand All @@ -124,7 +124,7 @@ enum HasUnavailableElement: Hashable {
// CHECK-NEXT: case .a:
// CHECK-NEXT: discriminator = 0
// CHECK-NEXT: case .b:
// CHECK-NEXT: _diagnoseUnavailableCodeReached()
// CHECK-NEXT: _diagnoseUnavailableCodeReached{{.*}}()
// CHECK-NEXT: }
// CHECK-NEXT: hasher.combine(discriminator)
// CHECK-NEXT: }
Expand All @@ -151,7 +151,7 @@ enum HasAssociatedValuesAndUnavailableElement: Hashable {
// CHECK-NEXT: hasher.combine(0)
// CHECK-NEXT: hasher.combine(a0)
// CHECK-NEXT: case .b:
// CHECK-NEXT: _diagnoseUnavailableCodeReached()
// CHECK-NEXT: _diagnoseUnavailableCodeReached{{.*}}()
// CHECK-NEXT: }
// CHECK-NEXT: }

Expand All @@ -163,7 +163,7 @@ enum HasAssociatedValuesAndUnavailableElement: Hashable {
// CHECK-NEXT: }
// CHECK-NEXT: return true
// CHECK-NEXT: case (.b, .b):
// CHECK-NEXT: _diagnoseUnavailableCodeReached()
// CHECK-NEXT: _diagnoseUnavailableCodeReached{{.*}}()
// CHECK-NEXT: default:
// CHECK-NEXT: return false
// CHECK-NEXT: }
Expand Down
23 changes: 15 additions & 8 deletions test/decl/enum/derived_hashable_equatable_macos.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// RUN: %target-swift-frontend -print-ast %s | %FileCheck %s
// RUN: %target-swift-frontend -target %target-cpu-apple-macosx10.51 -print-ast %s | %FileCheck %s
// RUN: %target-swift-frontend -print-ast %s | %FileCheck %s --check-prefixes=CHECK,CHECK-PRE-SWIFT5_9
// RUN: %target-swift-frontend -target %target-cpu-apple-macosx10.51 -print-ast %s | %FileCheck %s --check-prefixes=CHECK,CHECK-PRE-SWIFT5_9
// RUN: %target-swift-frontend -target %target-cpu-apple-macosx14 -print-ast %s | %FileCheck %s --check-prefixes=CHECK,CHECK-SWIFT5_9
// REQUIRES: OS=macosx

// CHECK-LABEL: internal enum HasElementsWithAvailability : Hashable
Expand Down Expand Up @@ -29,9 +30,11 @@ enum HasElementsWithAvailability: Hashable {
// CHECK-NEXT: case .alwaysAvailable:
// CHECK-NEXT: index_a = 0
// CHECK-NEXT: case .neverAvailable:
// CHECK-NEXT: _diagnoseUnavailableCodeReached()
// CHECK-PRE-SWIFT5_9-NEXT: _diagnoseUnavailableCodeReached_aeic()
// CHECK-SWIFT5_9-NEXT: _diagnoseUnavailableCodeReached()
// CHECK-NEXT: case .unavailableMacOS:
// CHECK-NEXT: _diagnoseUnavailableCodeReached()
// CHECK-PRE-SWIFT5_9-NEXT: _diagnoseUnavailableCodeReached_aeic()
// CHECK-SWIFT5_9-NEXT: _diagnoseUnavailableCodeReached()
// CHECK-NEXT: case .obsoleted10_50:
// CHECK-NEXT: index_a = 1
// CHECK-NEXT: case .introduced10_50:
Expand All @@ -42,9 +45,11 @@ enum HasElementsWithAvailability: Hashable {
// CHECK-NEXT: case .alwaysAvailable:
// CHECK-NEXT: index_b = 0
// CHECK-NEXT: case .neverAvailable:
// CHECK-NEXT: _diagnoseUnavailableCodeReached()
// CHECK-PRE-SWIFT5_9-NEXT: _diagnoseUnavailableCodeReached_aeic()
// CHECK-SWIFT5_9-NEXT: _diagnoseUnavailableCodeReached()
// CHECK-NEXT: case .unavailableMacOS:
// CHECK-NEXT: _diagnoseUnavailableCodeReached()
// CHECK-PRE-SWIFT5_9-NEXT: _diagnoseUnavailableCodeReached_aeic()
// CHECK-SWIFT5_9-NEXT: _diagnoseUnavailableCodeReached()
// CHECK-NEXT: case .obsoleted10_50:
// CHECK-NEXT: index_b = 1
// CHECK-NEXT: case .introduced10_50:
Expand All @@ -59,9 +64,11 @@ enum HasElementsWithAvailability: Hashable {
// CHECK-NEXT: case .alwaysAvailable:
// CHECK-NEXT: discriminator = 0
// CHECK-NEXT: case .neverAvailable:
// CHECK-NEXT: _diagnoseUnavailableCodeReached()
// CHECK-PRE-SWIFT5_9-NEXT: _diagnoseUnavailableCodeReached_aeic()
// CHECK-SWIFT5_9-NEXT: _diagnoseUnavailableCodeReached()
// CHECK-NEXT: case .unavailableMacOS:
// CHECK-NEXT: _diagnoseUnavailableCodeReached()
// CHECK-PRE-SWIFT5_9-NEXT: _diagnoseUnavailableCodeReached_aeic()
// CHECK-SWIFT5_9-NEXT: _diagnoseUnavailableCodeReached()
// CHECK-NEXT: case .obsoleted10_50:
// CHECK-NEXT: discriminator = 1
// CHECK-NEXT: case .introduced10_50:
Expand Down