Skip to content

Commit 64fa0ee

Browse files
committed
[TypeChecker] Add Builtin operation to trigger/test fallback diagnostic
1 parent 35202ab commit 64fa0ee

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

include/swift/AST/Builtins.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,7 @@ BUILTIN_SANITIZER_OPERATION(TSanInoutAccess, "tsanInoutAccess", "")
572572
BUILTIN_TYPE_CHECKER_OPERATION(TypeJoin, type_join)
573573
BUILTIN_TYPE_CHECKER_OPERATION(TypeJoinInout, type_join_inout)
574574
BUILTIN_TYPE_CHECKER_OPERATION(TypeJoinMeta, type_join_meta)
575+
BUILTIN_TYPE_CHECKER_OPERATION(TriggerFallbackDiagnostic, trigger_fallback_diagnostic)
575576

576577
#undef BUILTIN_TYPE_CHECKER_OPERATION
577578

lib/AST/Builtins.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,12 @@ static ValueDecl *getTypeJoinMetaOperation(ASTContext &Context, Identifier Id) {
10121012
return builder.build(Id);
10131013
}
10141014

1015+
static ValueDecl *getTriggerFallbackDiagnosticOperation(ASTContext &Context,
1016+
Identifier Id) {
1017+
// () -> Void
1018+
return getBuiltinFunction(Id, {}, Context.TheEmptyTupleType);
1019+
}
1020+
10151021
static ValueDecl *getCanBeObjCClassOperation(ASTContext &Context,
10161022
Identifier Id) {
10171023
// <T> T.Type -> Builtin.Int8
@@ -1876,7 +1882,10 @@ ValueDecl *swift::getBuiltinValueDecl(ASTContext &Context, Identifier Id) {
18761882
return getTypeJoinInoutOperation(Context, Id);
18771883

18781884
case BuiltinValueKind::TypeJoinMeta:
1879-
return getTypeJoinMetaOperation(Context, Id);
1885+
return getTypeJoinMetaOperation(Context, Id);
1886+
1887+
case BuiltinValueKind::TriggerFallbackDiagnostic:
1888+
return getTriggerFallbackDiagnosticOperation(Context, Id);
18801889
}
18811890

18821891
llvm_unreachable("bad builtin value!");

lib/Sema/CSGen.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,6 +1579,11 @@ namespace {
15791579
if (typeOperation != TypeOperation::None)
15801580
return CS.getASTContext().TheAnyType;
15811581

1582+
// If this is `Builtin.trigger_fallback_diagnostic()`, fail
1583+
// without producing any diagnostics, in order to test fallback error.
1584+
if (isTriggerFallbackDiagnosticBuiltin(expr, CS.getASTContext()))
1585+
return nullptr;
1586+
15821587
// Open a member constraint for constructor delegations on the
15831588
// subexpr type.
15841589
if (CS.TC.getSelfForInitDelegationInConstructor(CS.DC, expr)) {
@@ -3129,6 +3134,19 @@ namespace {
31293134
return tv;
31303135
}
31313136

3137+
static bool isTriggerFallbackDiagnosticBuiltin(UnresolvedDotExpr *UDE,
3138+
ASTContext &Context) {
3139+
auto *DRE = dyn_cast<DeclRefExpr>(UDE->getBase());
3140+
if (!DRE)
3141+
return false;
3142+
3143+
if (DRE->getDecl() != Context.TheBuiltinModule)
3144+
return false;
3145+
3146+
auto member = UDE->getName().getBaseIdentifier().str();
3147+
return member.equals("trigger_fallback_diagnostic");
3148+
}
3149+
31323150
enum class TypeOperation { None,
31333151
Join,
31343152
JoinInout,

test/Sema/rdar38885760.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %empty-directory(%t)
2+
//
3+
// RUN: not %target-swift-frontend -typecheck %s -parse-stdlib 2>%t/fallback_diagnostic.txt
4+
// RUN: %FileCheck %s --check-prefix FALLBACK-DIAGNOSTIC <%t/fallback_diagnostic.txt
5+
//
6+
// FALLBACK-DIAGNOSTIC: error: failed to produce diagnostic for expression; please file a bug report with your project
7+
8+
import Swift
9+
10+
Builtin.trigger_fallback_diagnostic()

0 commit comments

Comments
 (0)