Skip to content

Commit 4a48525

Browse files
authored
Merge pull request #2937 from swiftwasm/main
[pull] swiftwasm from main
2 parents b4f99ee + f989c17 commit 4a48525

24 files changed

+424
-499
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8354,17 +8354,53 @@ static Type getOpenedResultBuilderTypeFor(ConstraintSystem &cs,
83548354
bool ConstraintSystem::resolveClosure(TypeVariableType *typeVar,
83558355
Type contextualType,
83568356
ConstraintLocatorBuilder locator) {
8357+
auto *closureLocator = typeVar->getImpl().getLocator();
8358+
auto *closure = castToExpr<ClosureExpr>(closureLocator->getAnchor());
8359+
auto *inferredClosureType = getClosureType(closure);
8360+
83578361
auto getContextualParamAt =
8358-
[&contextualType](unsigned index) -> Optional<AnyFunctionType::Param> {
8362+
[&contextualType, &inferredClosureType](
8363+
unsigned index) -> Optional<AnyFunctionType::Param> {
83598364
auto *fnType = contextualType->getAs<FunctionType>();
8360-
return fnType && fnType->getNumParams() > index
8361-
? fnType->getParams()[index]
8362-
: Optional<AnyFunctionType::Param>();
8365+
if (!fnType)
8366+
return None;
8367+
8368+
auto numContextualParams = fnType->getNumParams();
8369+
if (numContextualParams != inferredClosureType->getNumParams() ||
8370+
numContextualParams <= index)
8371+
return None;
8372+
8373+
return fnType->getParams()[index];
83638374
};
83648375

8365-
auto *closureLocator = typeVar->getImpl().getLocator();
8366-
auto *closure = castToExpr<ClosureExpr>(closureLocator->getAnchor());
8367-
auto *inferredClosureType = getClosureType(closure);
8376+
// Check whether given contextual parameter type could be
8377+
// used to bind external closure parameter type.
8378+
auto isSuitableContextualType = [](Type contextualTy) {
8379+
// We need to wait until contextual type
8380+
// is fully resolved before binding it.
8381+
if (contextualTy->isTypeVariableOrMember())
8382+
return false;
8383+
8384+
// If contextual type has an error, let's wait for inference,
8385+
// otherwise contextual would interfere with diagnostics.
8386+
if (contextualTy->hasError())
8387+
return false;
8388+
8389+
if (isa<TypeAliasType>(contextualTy.getPointer())) {
8390+
auto underlyingTy = contextualTy->getDesugaredType();
8391+
// FIXME: typealias pointing to an existential type is special
8392+
// because if the typealias has type variables then we'd end up
8393+
// opening existential from a type with unresolved generic
8394+
// parameter(s), which CSApply can't currently simplify while
8395+
// building type-checked AST because `OpenedArchetypeType` doesn't
8396+
// propagate flags. Example is as simple as `{ $0.description }`
8397+
// where `$0` is `Error` that inferred from a (generic) typealias.
8398+
if (underlyingTy->isExistentialType() && contextualTy->hasTypeVariable())
8399+
return false;
8400+
}
8401+
8402+
return true;
8403+
};
83688404

83698405
// Determine whether a result builder will be applied.
83708406
auto resultBuilderType = getOpenedResultBuilderTypeFor(*this, locator);
@@ -8456,6 +8492,24 @@ bool ConstraintSystem::resolveClosure(TypeVariableType *typeVar,
84568492
param.isVariadic() ? ArraySliceType::get(typeVar) : Type(typeVar);
84578493

84588494
auto externalType = param.getOldType();
8495+
8496+
// Performance optimization.
8497+
//
8498+
// If there is a concrete contextual type we could use, let's bind
8499+
// it to the external type right away because internal type has to
8500+
// be equal to that type anyway (through `BindParam` on external type
8501+
// i.e. <internal> bind param <external> conv <concrete contextual>).
8502+
//
8503+
// Note: it's correct to avoid doing this, but it would result
8504+
// in (a lot) more checking since solver would have to re-discover,
8505+
// re-attempt and fail parameter type while solving for overloaded
8506+
// choices in the body.
8507+
if (auto contextualParam = getContextualParamAt(i)) {
8508+
auto paramTy = simplifyType(contextualParam->getOldType());
8509+
if (isSuitableContextualType(paramTy))
8510+
addConstraint(ConstraintKind::Bind, externalType, paramTy, paramLoc);
8511+
}
8512+
84598513
if (oneWayConstraints) {
84608514
addConstraint(
84618515
ConstraintKind::OneWayBindParam, typeVar, externalType, paramLoc);

stdlib/public/SwiftShims/LibcShims.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ static inline __swift_size_t _swift_stdlib_strlen_unsigned(const unsigned char *
7070
SWIFT_READONLY
7171
static inline int _swift_stdlib_memcmp(const void *s1, const void *s2,
7272
__swift_size_t n) {
73+
#if defined(__APPLE__)
74+
extern int memcmp(const void * _Nullable, const void * _Nullable, __swift_size_t);
75+
#else
7376
extern int memcmp(const void *, const void *, __swift_size_t);
77+
#endif
7478
return memcmp(s1, s2, n);
7579
}
7680

test/Concurrency/Runtime/async_task_handle_cancellation.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
// This test is flaky on VS2017 (unknown reasons)
1010
// UNSUPPORTED: MSVC_VER=15.0
1111

12+
// This test is failing on windows. SR-14447.
13+
//
14+
// UNSUPPORTED: OS=windows-msvc
15+
1216
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
1317
@main struct Main {
1418
static func main() async {

test/Constraints/closures.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ func rdar21078316() {
377377

378378
// <rdar://problem/20978044> QoI: Poor diagnostic when using an incorrect tuple element in a closure
379379
var numbers = [1, 2, 3]
380-
zip(numbers, numbers).filter { $0.2 > 1 } // expected-error {{value of tuple type '(Int, Int)' has no member '2'}}
380+
zip(numbers, numbers).filter { $0.2 > 1 } // expected-error {{value of tuple type '(Array<Int>.Element, Array<Int>.Element)' has no member '2'}}
381381

382382

383383

test/Driver/Dependencies/dependencies-preservation-fine.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Verify that the top-level build record file from the last incremental
33
// compilation is preserved with the same name, suffixed by a '~'.
44

5+
// REQUIRES: rdar76238077
6+
57
// RUN: %empty-directory(%t)
68
// RUN: cp -r %S/Inputs/one-way-fine/* %t
79
// RUN: %{python} %S/Inputs/touch.py 443865900 %t/*

test/Driver/Dependencies/one-way-merge-module-fine.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/// other ==> main
22

3+
// REQUIRES: rdar76238077
4+
35
// RUN: %empty-directory(%t)
46
// RUN: cp -r %S/Inputs/one-way-fine/* %t
57
// RUN: touch -t 201401240005 %t/*

0 commit comments

Comments
 (0)