Skip to content

[5.1 06/12][CSApply] Don't try to transform editor placeholder if it's type is invalid #25463

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
19 changes: 18 additions & 1 deletion lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4041,6 +4041,19 @@ namespace {
simplifyExprType(E);
auto valueType = cs.getType(E);

// TODO(diagnostics): Once all of the diagnostics are moved to
// new diagnostics framework this check could be eliminated.
//
// Only way for this to happen is CSDiag try to re-typecheck
// sub-expression which contains this placeholder with
// `AllowUnresolvedTypeVariables` flag set.
//
// A better solution could be to replace placeholders with this
// implicit call early on and type-check that call together with
// the rest of the constraint system.
if (valueType->hasUnresolvedType())
return nullptr;

auto &tc = cs.getTypeChecker();
auto &ctx = tc.Context;
// Synthesize a call to _undefined() of appropriate type.
Expand Down Expand Up @@ -7597,7 +7610,11 @@ namespace {
ClosuresToTypeCheck.push_back(closure);
}

tc.ClosuresWithUncomputedCaptures.push_back(closure);
// Don't try to register captures if constraint system is used to
// produce diagnostics for one of the sub-expressions.
if (!cs.Options.contains(
ConstraintSystemFlags::SubExpressionDiagnostics))
tc.ClosuresWithUncomputedCaptures.push_back(closure);

return { false, closure };
}
Expand Down
8 changes: 8 additions & 0 deletions lib/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,14 @@ enum class ConstraintSystemFlags {
/// If set, the top-level expression may be able to provide an underlying
/// type for the contextual opaque archetype.
UnderlyingTypeForOpaqueReturnType = 0x40,

/// FIXME(diagnostics): Once diagnostics are completely switched to new
/// framework, this flag could be removed as obsolete.
///
/// If set, this identifies constraint system as being used to re-typecheck
/// one of the sub-expressions as part of the expression diagnostics, which
/// is attempting to narrow down failure location.
SubExpressionDiagnostics = 0x80,
};

/// Options that affect the constraint system as a whole.
Expand Down
3 changes: 3 additions & 0 deletions lib/Sema/TypeCheckConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2190,6 +2190,9 @@ Type TypeChecker::typeCheckExpressionImpl(Expr *&expr, DeclContext *dc,
if (options.contains(TypeCheckExprFlags::ConvertTypeIsOpaqueReturnType))
csOptions |= ConstraintSystemFlags::UnderlyingTypeForOpaqueReturnType;

if (options.contains(TypeCheckExprFlags::SubExpressionDiagnostics))
csOptions |= ConstraintSystemFlags::SubExpressionDiagnostics;

ConstraintSystem cs(*this, dc, csOptions, expr);
cs.baseCS = baseCS;

Expand Down
11 changes: 11 additions & 0 deletions validation-test/compiler_crashers_2_fixed/0196-rdar48937223.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// RUN: %target-typecheck-verify-swift

protocol P {}

func fn<T, U: P>(_ arg1: T, arg2: (T) -> U) {}

func test(str: String) {
fn(str) { arg in
<#FOO#> // expected-error {{editor placeholder in source file}}
}
}