Transform EditorPlaceholderExpr into trap if executed in playground

mode (take 2)

Allow untyped placeholder to take arbitrary type, but default to Void.
Add _undefined<T>() function, which is like fatalError() but has
arbitrary return type. In playground mode, merely warn about outstanding
placeholders instead of erroring out, and transform placeholders into
calls to _undefined(). This way, code with outstanding placeholders will
only crash when it attempts to evaluate such placeholders.

When generating constraints for an iterated sequence of type T, emit

    T convertible to $T1
    $T1 conforms to SequenceType

instead of

    T convertible to SequenceType

This ensures that an untyped placeholder in for-each sequence position
doesn't get inferred to have type SequenceType. (The conversion is still
necessary because the sequence may have IUO type.) The new constraint
system precipitates changes in CSSimplify and CSDiag, and ends up fixing
18741539 along the way.

(NOTE: There is a small regression in diagnosis of issues like the
following:

    class C {}
    class D: C {}
    func f(a: [C]!) { for _: D in a {} }

It complains that [C]! doesn't conform to SequenceType when it should be
complaining that C is not convertible to D.)

<rdar://problem/21167372>

(Originally Swift SVN r31481)
This commit is contained in:
Chris Willmore
2015-12-09 23:26:59 -08:00
parent f6329bcc79
commit c99c02b5a6
27 changed files with 226 additions and 51 deletions

View File

@@ -3247,6 +3247,29 @@ namespace {
}
Expr *visitEditorPlaceholderExpr(EditorPlaceholderExpr *E) {
Type valueType = simplifyType(E->getType());
E->setType(valueType);
auto &tc = cs.getTypeChecker();
auto &ctx = tc.Context;
// Synthesize a call to _undefined() of appropriate type.
FuncDecl *undefinedDecl = ctx.getUndefinedDecl(&tc);
if (!undefinedDecl) {
tc.diagnose(E->getLoc(), diag::missing_undefined_runtime);
return nullptr;
}
DeclRefExpr *fnRef = new (ctx) DeclRefExpr(undefinedDecl, SourceLoc(),
/*Implicit=*/true);
StringRef msg = "attempt to evaluate editor placeholder";
Expr *argExpr = new (ctx) StringLiteralExpr(msg, E->getLoc(),
/*implicit*/true);
argExpr = new (ctx) ParenExpr(E->getLoc(), argExpr, E->getLoc(),
/*hasTrailingClosure*/false);
Expr *callExpr = new (ctx) CallExpr(fnRef, argExpr, /*implicit*/true);
bool invalid = tc.typeCheckExpression(callExpr, cs.DC, valueType,
CTP_CannotFail);
assert(!invalid && "conversion cannot fail");
E->setSemanticExpr(callExpr);
return E;
}