Skip to content

Commit 6082173

Browse files
authored
Merge pull request swiftlang#80280 from xedin/rdar-131732245
[TypeChecker] Avoid dropping pre-check diagnostics in `typeCheckParam…
2 parents 433ff59 + d656f74 commit 6082173

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

lib/Sema/TypeCheckConstraints.cpp

+13-6
Original file line numberDiff line numberDiff line change
@@ -419,10 +419,12 @@ TypeChecker::typeCheckTarget(SyntacticElementTarget &target,
419419
PrettyStackTraceLocation stackTrace(Context, "type-checking-target",
420420
target.getLoc());
421421

422-
// First, pre-check the target, validating any types that occur in the
423-
// expression and folding sequence expressions.
424-
if (ConstraintSystem::preCheckTarget(target))
425-
return errorResult();
422+
if (!options.contains(TypeCheckExprFlags::AvoidInvalidatingAST)) {
423+
// First, pre-check the target, validating any types that occur in the
424+
// expression and folding sequence expressions.
425+
if (ConstraintSystem::preCheckTarget(target))
426+
return errorResult();
427+
}
426428

427429
// Check whether given target has a code completion token which requires
428430
// special handling. Returns true if handled, in which case we've already
@@ -509,6 +511,11 @@ Type TypeChecker::typeCheckParameterDefault(Expr *&defaultValue,
509511

510512
auto paramInterfaceTy = paramType->mapTypeOutOfContext();
511513

514+
// Attempt to pre-check expression first, if that fails - skip type-checking.
515+
// This would make sure that diagnostics about invalid AST are never dropped.
516+
if (ConstraintSystem::preCheckTarget(defaultExprTarget))
517+
return Type();
518+
512519
{
513520
// Buffer all of the diagnostics produced by \c typeCheckExpression
514521
// since in some cases we need to try type-checking again with a
@@ -531,8 +538,8 @@ Type TypeChecker::typeCheckParameterDefault(Expr *&defaultValue,
531538
// First, let's try to type-check default expression using
532539
// archetypes, which guarantees that it would work for any
533540
// substitution of the generic parameter (if they are involved).
534-
if (auto result = typeCheckExpression(
535-
defaultExprTarget, options, &diagnostics)) {
541+
if (auto result =
542+
typeCheckTarget(defaultExprTarget, options, &diagnostics)) {
536543
defaultValue = result->getAsExpr();
537544
return defaultValue->getType();
538545
}

lib/Sema/TypeChecker.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ enum class TypeCheckExprFlags {
135135
/// Don't expand macros.
136136
DisableMacroExpansions = 0x04,
137137

138-
/// If set, typeCheckExpression will avoid invalidating the AST if
139-
/// type-checking fails. Do not add new uses of this.
138+
/// If set, typeCheckExpression will avoid pre-checking and invalidating
139+
/// the AST if type-checking fails. Do not add new uses of this.
140140
AvoidInvalidatingAST = 0x08,
141141
};
142142

stdlib/public/Concurrency/TaskSleepDuration.swift

+2
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ extension Task where Success == Never, Failure == Never {
117117
}
118118
}
119119

120+
#if !$Embedded
120121
/// Suspends the current task until the given deadline within a tolerance.
121122
///
122123
/// If the task is canceled before the time ends, this function throws
@@ -153,6 +154,7 @@ extension Task where Success == Never, Failure == Never {
153154
) async throws {
154155
try await clock.sleep(for: duration, tolerance: tolerance)
155156
}
157+
#endif
156158
}
157159
#else
158160
@available(SwiftStdlib 5.7, *)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
// https://github.com/apple/swift/issues/73986
4+
5+
struct S {
6+
init(_: () -> some Any = { Nonexistent() }) {}
7+
// expected-error@-1 {{cannot find 'Nonexistent' in scope}}
8+
9+
init<C>(other: C = { _ = 42; return Nonexistent() }) {}
10+
// expected-error@-1 {{cannot find 'Nonexistent' in scope}}
11+
}
12+
13+
func test(x: some FixedWidthInteger = UNKNOWN_CONSTANT) {
14+
// expected-error@-1 {{cannot find 'UNKNOWN_CONSTANT' in scope}}
15+
}

0 commit comments

Comments
 (0)