Skip to content

Commit 860a5b7

Browse files
committed
---
yaml --- r: 341407 b: refs/heads/rxwei-patch-1 c: f3655ed h: refs/heads/master i: 341405: 96be4ac 341403: aedd6ea 341399: 8186b18 341391: 50801cd 341375: 2994580
1 parent 1ed893a commit 860a5b7

37 files changed

+227
-409
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,7 @@ refs/tags/swift-DEVELOPMENT-SNAPSHOT-2018-08-18-a: b10b1fce14385faa6d44f6b933e95
10151015
refs/heads/rdar-43033749-fix-batch-mode-no-diags-swift-5.0-branch: a14e64eaad30de89f0f5f0b2a782eed7ecdcb255
10161016
refs/heads/revert-19006-error-bridging-integer-type: 8a9065a3696535305ea53fe9b71f91cbe6702019
10171017
refs/heads/revert-19050-revert-19006-error-bridging-integer-type: ecf752d54b05dd0a20f510f0bfa54a3fec3bcaca
1018-
refs/heads/rxwei-patch-1: ccc0a8980ee05ed9910dc048e0c9f43b34b1e8e9
1018+
refs/heads/rxwei-patch-1: f3655ed5b482362759304d970f5e26e9050430de
10191019
refs/heads/shahmishal-patch-1: e58ec0f7488258d42bef51bc3e6d7b3dc74d7b2a
10201020
refs/heads/typelist-existential: 4046359efd541fb5c72d69a92eefc0a784df8f5e
10211021
refs/tags/swift-4.2-DEVELOPMENT-SNAPSHOT-2018-08-20-a: 4319ba09e4fb8650ee86061075c74a016b6baab9

branches/rxwei-patch-1/include/swift/AST/DiagnosticsSIL.def

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,6 @@ ERROR(unsupported_c_function_pointer_conversion,none,
113113
"C function pointer signature %0 is not compatible with expected type %1",
114114
(Type, Type))
115115

116-
ERROR(c_function_pointer_from_function_with_context,none,
117-
"a C function pointer cannot be formed from a "
118-
"%select{local function|closure}0 that captures "
119-
"%select{context|generic parameters|dynamic Self type|<<error>}1",
120-
(bool, unsigned))
121-
122116
ERROR(objc_selector_malformed,none,"the type ObjectiveC.Selector is malformed",
123117
())
124118

branches/rxwei-patch-1/include/swift/AST/DiagnosticsSema.def

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,6 +1121,11 @@ ERROR(c_function_pointer_from_method,none,
11211121
ERROR(c_function_pointer_from_generic_function,none,
11221122
"a C function pointer cannot be formed from a reference to a generic "
11231123
"function", ())
1124+
ERROR(c_function_pointer_from_function_with_context,none,
1125+
"a C function pointer cannot be formed from a "
1126+
"%select{local function|closure}0 that captures "
1127+
"%select{context|generic parameters|dynamic Self type|<<error>}1",
1128+
(bool, unsigned))
11241129
ERROR(invalid_autoclosure_forwarding,none,
11251130
"add () to forward @autoclosure parameter", ())
11261131

@@ -3332,9 +3337,6 @@ ERROR(trailing_closure_requires_parens,none,
33323337
"trailing closure requires parentheses for disambiguation in this"
33333338
" context", ())
33343339

3335-
ERROR(opaque_type_var_no_init,none,
3336-
"property declares an opaque return type, but has no initializer "
3337-
"expression from which to infer an underlying type", ())
33383340
ERROR(opaque_type_no_underlying_type_candidates,none,
33393341
"function declares an opaque return type, but has no return statements "
33403342
"in its body from which to infer an underlying type", ())
@@ -3346,9 +3348,6 @@ NOTE(opaque_type_underlying_type_candidate_here,none,
33463348
ERROR(opaque_type_self_referential_underlying_type,none,
33473349
"function opaque return type was inferred as %0, which defines the "
33483350
"opaque type in terms of itself", (Type))
3349-
ERROR(opaque_type_var_no_underlying_type,none,
3350-
"property declares an opaque return type, but cannot infer the "
3351-
"underlying type from its initializer expression", ())
33523351

33533352
//------------------------------------------------------------------------------
33543353
// MARK: Type Check Patterns

branches/rxwei-patch-1/lib/Frontend/CompilerInvocation.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ static void updateRuntimeLibraryPaths(SearchPathOptions &SearchPathOpts,
8989
if (!SearchPathOpts.SDKPath.empty()) {
9090
LibPath = SearchPathOpts.SDKPath;
9191
llvm::sys::path::append(LibPath, "usr", "lib", "swift");
92+
if (!Triple.isOSDarwin()) {
93+
llvm::sys::path::append(LibPath, getPlatformNameForTriple(Triple));
94+
llvm::sys::path::append(LibPath, swift::getMajorArchitectureName(Triple));
95+
}
9296
SearchPathOpts.RuntimeLibraryImportPaths.push_back(LibPath.str());
9397
}
9498
}

branches/rxwei-patch-1/lib/IDE/Formatting.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -355,20 +355,6 @@ class FormatContext {
355355
}
356356
}
357357

358-
// func foo(a: Int,
359-
// b: Int
360-
// ) {} <- Avoid adding indentation here
361-
SourceLoc SignatureEnd;
362-
if (auto *AFD = dyn_cast_or_null<AbstractFunctionDecl>(Cursor->getAsDecl())) {
363-
SignatureEnd = AFD->getSignatureSourceRange().End;
364-
} else if (auto *SD = dyn_cast_or_null<SubscriptDecl>(Cursor->getAsDecl())) {
365-
SignatureEnd = SD->getSignatureSourceRange().End;
366-
}
367-
if (SignatureEnd.isValid() && TInfo &&
368-
TInfo.StartOfLineTarget->getLoc() == SignatureEnd) {
369-
return false;
370-
}
371-
372358
// If we're at the beginning of a brace on a separate line in the context
373359
// of anything other than BraceStmt, don't add an indent.
374360
// For example:

branches/rxwei-patch-1/lib/SIL/TypeLowering.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ namespace {
436436
}
437437
assert(props.isFixedABI() && "unsupported combination for now");
438438
if (props.isTrivial()) {
439-
return asImpl().handleTrivial(type, props);
439+
return asImpl().handleTrivial(type);
440440
}
441441
return asImpl().handleNonTrivialAggregate(type, props);
442442
}
@@ -1532,10 +1532,8 @@ TypeConverter::getTypeLowering(AbstractionPattern origType,
15321532

15331533
if (prev == nullptr)
15341534
insert(key, lowering);
1535-
else {
1535+
else
15361536
prev->NextExpansion = lowering;
1537-
assert(prev->isResilient() == lowering->isResilient());
1538-
}
15391537

15401538
return *lowering;
15411539
}
@@ -1682,10 +1680,9 @@ TypeConverter::getTypeLoweringForLoweredType(TypeKey key,
16821680
forExpansion,
16831681
key.isDependent()).visit(key.SubstType);
16841682

1685-
if (prev) {
1683+
if (prev)
16861684
prev->NextExpansion = lowering;
1687-
assert(prev->isResilient() == lowering->isResilient());
1688-
} else
1685+
else
16891686
insert(key, lowering);
16901687
return *lowering;
16911688
}

branches/rxwei-patch-1/lib/SILGen/SILGenExpr.cpp

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,57 +1514,17 @@ ManagedValue emitCFunctionPointer(SILGenFunction &SGF,
15141514
} else if (auto memberRef = dyn_cast<MemberRefExpr>(semanticExpr)) {
15151515
setLocFromConcreteDeclRef(memberRef->getMember());
15161516
} else if (auto closure = dyn_cast<AbstractClosureExpr>(semanticExpr)) {
1517-
// Emit the closure body.
1518-
SGF.SGM.emitClosure(closure);
1519-
15201517
loc = closure;
1521-
} else if (auto captureList = dyn_cast<CaptureListExpr>(semanticExpr)) {
1522-
// Ensure that weak captures are in a separate scope.
1523-
DebugScope scope(SGF, CleanupLocation(captureList));
1524-
// CaptureListExprs evaluate their bound variables.
1525-
for (auto capture : captureList->getCaptureList()) {
1526-
SGF.visit(capture.Var);
1527-
SGF.visit(capture.Init);
1528-
}
1529-
15301518
// Emit the closure body.
1531-
auto *closure = captureList->getClosureBody();
15321519
SGF.SGM.emitClosure(closure);
1533-
1534-
loc = closure;
15351520
} else {
15361521
llvm_unreachable("c function pointer converted from a non-concrete decl ref");
15371522
}
15381523

15391524
// Produce a reference to the C-compatible entry point for the function.
1540-
SILDeclRef constant(loc, /*curried*/ false, /*foreign*/ true);
1525+
SILDeclRef constant(loc, /*uncurryLevel*/ 0, /*foreign*/ true);
15411526
SILConstantInfo constantInfo = SGF.getConstantInfo(constant);
15421527

1543-
// C function pointers cannot capture anything from their context.
1544-
auto captures = SGF.SGM.Types.getLoweredLocalCaptures(
1545-
*constant.getAnyFunctionRef());
1546-
1547-
if (captures.hasGenericParamCaptures() ||
1548-
captures.hasDynamicSelfCapture() ||
1549-
captures.hasLocalCaptures() ||
1550-
captures.hasOpaqueValueCapture()) {
1551-
unsigned kind;
1552-
if (captures.hasLocalCaptures())
1553-
kind = 0;
1554-
else if (captures.hasGenericParamCaptures())
1555-
kind = 1;
1556-
else if (captures.hasLocalCaptures())
1557-
kind = 2;
1558-
else
1559-
kind = 3;
1560-
SGF.SGM.diagnose(expr->getLoc(),
1561-
diag::c_function_pointer_from_function_with_context,
1562-
/*closure*/ constant.hasClosureExpr(),
1563-
kind);
1564-
1565-
return SGF.emitUndef(constantInfo.getSILType());
1566-
}
1567-
15681528
return convertCFunctionSignature(
15691529
SGF, conversionExpr,
15701530
constantInfo.getSILType(),
@@ -2347,7 +2307,7 @@ RValue RValueEmitter::visitDynamicTypeExpr(DynamicTypeExpr *E, SGFContext C) {
23472307
RValue RValueEmitter::visitCaptureListExpr(CaptureListExpr *E, SGFContext C) {
23482308
// Ensure that weak captures are in a separate scope.
23492309
DebugScope scope(SGF, CleanupLocation(E));
2350-
// CaptureListExprs evaluate their bound variables.
2310+
// ClosureExpr's evaluate their bound variables.
23512311
for (auto capture : E->getCaptureList()) {
23522312
SGF.visit(capture.Var);
23532313
SGF.visit(capture.Init);

branches/rxwei-patch-1/lib/Sema/CSApply.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5795,6 +5795,8 @@ maybeDiagnoseUnsupportedFunctionConversion(ConstraintSystem &cs, Expr *expr,
57955795
} else if (fn->getGenericParams()) {
57965796
tc.diagnose(expr->getLoc(),
57975797
diag::c_function_pointer_from_generic_function);
5798+
} else {
5799+
tc.maybeDiagnoseCaptures(expr, fn);
57985800
}
57995801
};
58005802

@@ -5815,8 +5817,10 @@ maybeDiagnoseUnsupportedFunctionConversion(ConstraintSystem &cs, Expr *expr,
58155817
semanticExpr = capture->getClosureBody();
58165818

58175819
// Can convert a literal closure that doesn't capture context.
5818-
if (auto closure = dyn_cast<ClosureExpr>(semanticExpr))
5820+
if (auto closure = dyn_cast<ClosureExpr>(semanticExpr)) {
5821+
tc.maybeDiagnoseCaptures(expr, closure);
58195822
return;
5823+
}
58205824

58215825
tc.diagnose(expr->getLoc(),
58225826
diag::invalid_c_function_pointer_conversion_expr);

branches/rxwei-patch-1/lib/Sema/CSDiag.cpp

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6686,10 +6686,10 @@ bool FailureDiagnosis::diagnoseMemberFailures(
66866686
}
66876687

66886688
if (!optionalResult.ViableCandidates.empty()) {
6689-
MemberAccessOnOptionalBaseFailure failure(
6690-
expr, CS, CS.getConstraintLocator(baseExpr), memberName,
6691-
/*resultOptional=*/false);
6692-
return failure.diagnoseAsError();
6689+
if (diagnoseBaseUnwrapForMemberAccess(baseExpr, baseObjTy, memberName,
6690+
/* additionalOptional= */ false,
6691+
memberRange))
6692+
return true;
66936693
}
66946694
}
66956695

@@ -7329,3 +7329,31 @@ ValueDecl *ConstraintSystem::findResolvedMemberRef(ConstraintLocator *locator) {
73297329

73307330
return nullptr;
73317331
}
7332+
7333+
bool swift::diagnoseBaseUnwrapForMemberAccess(Expr *baseExpr, Type baseType,
7334+
DeclName memberName,
7335+
bool resultOptional,
7336+
SourceRange memberRange) {
7337+
auto unwrappedBaseType = baseType->getOptionalObjectType();
7338+
if (!unwrappedBaseType)
7339+
return false;
7340+
7341+
ASTContext &ctx = baseType->getASTContext();
7342+
DiagnosticEngine &diags = ctx.Diags;
7343+
diags.diagnose(baseExpr->getLoc(), diag::optional_base_not_unwrapped,
7344+
baseType, memberName, unwrappedBaseType);
7345+
7346+
// FIXME: It would be nice to immediately offer "base?.member ?? defaultValue"
7347+
// for non-optional results where that would be appropriate. For the moment
7348+
// always offering "?" means that if the user chooses chaining, we'll end up
7349+
// in MissingOptionalUnwrapFailure:diagnose() to offer a default value during
7350+
// the next compile.
7351+
diags.diagnose(baseExpr->getLoc(), diag::optional_base_chain, memberName)
7352+
.fixItInsertAfter(baseExpr->getEndLoc(), "?");
7353+
7354+
if (!resultOptional) {
7355+
diags.diagnose(baseExpr->getLoc(), diag::unwrap_with_force_value)
7356+
.fixItInsertAfter(baseExpr->getEndLoc(), "!");
7357+
}
7358+
return true;
7359+
}

branches/rxwei-patch-1/lib/Sema/CSDiagnostics.cpp

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ bool MemberAccessOnOptionalBaseFailure::diagnoseAsError() {
886886
return false;
887887

888888
auto *anchor = getAnchor();
889-
auto baseType = getType(anchor)->getRValueType();
889+
auto type = getType(anchor)->getRValueType();
890890
bool resultIsOptional = ResultTypeIsOptional;
891891

892892
// If we've resolved the member overload to one that returns an optional
@@ -897,27 +897,8 @@ bool MemberAccessOnOptionalBaseFailure::diagnoseAsError() {
897897
if (overload && overload->ImpliedType->getOptionalObjectType())
898898
resultIsOptional = true;
899899

900-
auto unwrappedBaseType = baseType->getOptionalObjectType();
901-
if (!unwrappedBaseType)
902-
return false;
903-
904-
emitDiagnostic(anchor->getLoc(), diag::optional_base_not_unwrapped,
905-
baseType, Member, unwrappedBaseType);
906-
907-
// FIXME: It would be nice to immediately offer "base?.member ?? defaultValue"
908-
// for non-optional results where that would be appropriate. For the moment
909-
// always offering "?" means that if the user chooses chaining, we'll end up
910-
// in MissingOptionalUnwrapFailure:diagnose() to offer a default value during
911-
// the next compile.
912-
emitDiagnostic(anchor->getLoc(), diag::optional_base_chain, Member)
913-
.fixItInsertAfter(anchor->getEndLoc(), "?");
914-
915-
if (!resultIsOptional) {
916-
emitDiagnostic(anchor->getLoc(), diag::unwrap_with_force_value)
917-
.fixItInsertAfter(anchor->getEndLoc(), "!");
918-
}
919-
920-
return true;
900+
return diagnoseBaseUnwrapForMemberAccess(anchor, type, Member,
901+
resultIsOptional, SourceRange());
921902
}
922903

923904
void MissingOptionalUnwrapFailure::offerDefaultValueUnwrapFixIt(
@@ -2153,15 +2134,9 @@ bool MissingMemberFailure::diagnoseAsError() {
21532134
};
21542135

21552136
if (getName().getBaseName().getKind() == DeclBaseName::Kind::Subscript) {
2156-
auto loc = anchor->getLoc();
2157-
if (auto *metatype = baseType->getAs<MetatypeType>()) {
2158-
emitDiagnostic(loc, diag::could_not_find_type_member,
2159-
metatype->getInstanceType(), getName())
2137+
emitDiagnostic(anchor->getLoc(), diag::could_not_find_value_subscript,
2138+
baseType)
21602139
.highlight(baseExpr->getSourceRange());
2161-
} else {
2162-
emitDiagnostic(loc, diag::could_not_find_value_subscript, baseType)
2163-
.highlight(baseExpr->getSourceRange());
2164-
}
21652140
} else if (getName().getBaseName() == "deinit") {
21662141
// Specialised diagnostic if trying to access deinitialisers
21672142
emitDiagnostic(anchor->getLoc(), diag::destructor_not_accessible)

branches/rxwei-patch-1/lib/Sema/CSSolver.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,8 +1208,7 @@ ConstraintSystem::solveImpl(Expr *&expr,
12081208
auto constraintKind = ConstraintKind::Conversion;
12091209

12101210
if ((getContextualTypePurpose() == CTP_ReturnStmt ||
1211-
getContextualTypePurpose() == CTP_ReturnSingleExpr ||
1212-
getContextualTypePurpose() == CTP_Initialization)
1211+
getContextualTypePurpose() == CTP_ReturnSingleExpr)
12131212
&& Options.contains(ConstraintSystemFlags::UnderlyingTypeForOpaqueReturnType))
12141213
constraintKind = ConstraintKind::OpaqueUnderlyingType;
12151214

branches/rxwei-patch-1/lib/Sema/ConstraintSystem.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4245,6 +4245,14 @@ class InputMatcher {
42454245
size_t getNumSkippedParameters() const { return NumSkippedParameters; }
42464246
};
42474247

4248+
/// Diagnose an attempt to recover from a member access into a value of
4249+
/// optional type which needed to be unwrapped for the member to be found.
4250+
///
4251+
/// \returns true if a diagnostic was produced.
4252+
bool diagnoseBaseUnwrapForMemberAccess(Expr *baseExpr, Type baseType,
4253+
DeclName memberName, bool resultOptional,
4254+
SourceRange memberRange);
4255+
42484256
// Return true if, when replacing "<expr>" with "<expr> ?? T", parentheses need
42494257
// to be added around <expr> first in order to maintain the correct precedence.
42504258
bool exprNeedsParensBeforeAddingNilCoalescing(TypeChecker &TC,

branches/rxwei-patch-1/lib/Sema/TypeCheckCaptures.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,35 @@ class FindCapturedVars : public ASTWalker {
590590

591591
} // end anonymous namespace
592592

593+
void TypeChecker::maybeDiagnoseCaptures(Expr *E, AnyFunctionRef AFR) {
594+
if (!AFR.getCaptureInfo().hasBeenComputed()) {
595+
// The capture list is not always initialized by the point we reference
596+
// it. Remember we formed a C function pointer so we can diagnose later
597+
// if necessary.
598+
LocalCFunctionPointers[AFR].push_back(E);
599+
return;
600+
}
601+
602+
if (AFR.getCaptureInfo().hasGenericParamCaptures() ||
603+
AFR.getCaptureInfo().hasDynamicSelfCapture() ||
604+
AFR.getCaptureInfo().hasLocalCaptures() ||
605+
AFR.getCaptureInfo().hasOpaqueValueCapture()) {
606+
unsigned kind;
607+
if (AFR.getCaptureInfo().hasLocalCaptures())
608+
kind = 0;
609+
else if (AFR.getCaptureInfo().hasGenericParamCaptures())
610+
kind = 1;
611+
else if (AFR.getCaptureInfo().hasLocalCaptures())
612+
kind = 2;
613+
else
614+
kind = 3;
615+
diagnose(E->getLoc(),
616+
diag::c_function_pointer_from_function_with_context,
617+
/*closure*/ AFR.getAbstractClosureExpr() != nullptr,
618+
kind);
619+
}
620+
}
621+
593622
void TypeChecker::computeCaptures(AnyFunctionRef AFR) {
594623
if (AFR.getCaptureInfo().hasBeenComputed())
595624
return;
@@ -642,6 +671,13 @@ void TypeChecker::computeCaptures(AnyFunctionRef AFR) {
642671
}
643672
}
644673
}
674+
675+
// Diagnose if we have local captures and there were C pointers formed to
676+
// this function before we computed captures.
677+
auto cFunctionPointers = LocalCFunctionPointers.find(AFR);
678+
if (cFunctionPointers != LocalCFunctionPointers.end())
679+
for (auto *expr : cFunctionPointers->second)
680+
maybeDiagnoseCaptures(expr, AFR);
645681
}
646682

647683
void TypeChecker::checkPatternBindingCaptures(NominalTypeDecl *typeDecl) {

0 commit comments

Comments
 (0)