Skip to content

[pull] swiftwasm-release/5.3 from release/5.3 #1004

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 19 commits into from
May 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
29d8bfd
Make the lazy assignment of an assoociated type/wtable atomic.
rjmccall May 13, 2020
74c2106
ReplaceOpaqueTypesWithUnderlyingTypes: Handle a type being "substitut…
jckarter May 13, 2020
1a4f523
Reduce generics in Codable (#31278) (#31772)
tbkka May 14, 2020
f378ad5
[CodeCompletion] Handle "KeyPath as function" thunk in SanitizeExpr
rintaro May 13, 2020
e35083d
[CodeCompletion] Avoid re-typechcking pre-checked expressions
rintaro May 13, 2020
13076ab
Merge pull request #31788 from jckarter/replace-opaque-underlying-typ…
tkremenek May 14, 2020
ecea9aa
Improve diagnostic for keypath used without 'keyPath:' label
artemcm May 11, 2020
a44ddac
Merge pull request #31792 from rintaro/5.3-ide-completion-rdar60982638
rintaro May 14, 2020
9077a87
SILGen: Remove obsolete hack preventing emission of allocating init f…
slavapestov May 14, 2020
f329eec
Merge pull request #31776 from artemcm/5.3-UnlabeledKeypathFix
artemcm May 15, 2020
7131885
Merge pull request #31780 from rjmccall/associated-witness-atomics-5.3
tkremenek May 15, 2020
810069e
[ConstraintSystem] Detect and diagnose inability to infer type of clo…
xedin May 15, 2020
6916fcf
Merge pull request #31801 from slavapestov/unavailable-allocating-cto…
slavapestov May 15, 2020
1152371
SILGen: Extend scope for evaluation in memberwise initializers to inc…
jckarter May 15, 2020
27a1d5b
[ASTPrinter] Don't print inferred opaque result type witness
rintaro May 15, 2020
6ce886f
Merge pull request #31810 from xedin/rdar-63230293-5.3
xedin May 15, 2020
39914b4
Merge pull request #31834 from rintaro/5.3-ide-completion-rdar59817674
akyrtzi May 16, 2020
a4e2270
Merge pull request #31833 from jckarter/memberwise-initializer-scope-5.3
jckarter May 16, 2020
83697ed
[5.3] Revert RangeSet additions (#31827)
natecook1000 May 16, 2020
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
3 changes: 3 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ ERROR(no_candidates_match_result_type,none,
"no '%0' candidates produce the expected contextual result type %1",
(StringRef, Type))

ERROR(cannot_infer_closure_parameter_type,none,
"unable to infer type of a closure parameter %0 in the current context",
(StringRef))
ERROR(cannot_infer_closure_type,none,
"unable to infer closure type in the current context", ())
ERROR(cannot_infer_closure_result_type,none,
Expand Down
39 changes: 26 additions & 13 deletions lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3043,12 +3043,21 @@ ProtocolConformanceRef ReplaceOpaqueTypesWithUnderlyingTypes::
operator()(CanType maybeOpaqueType, Type replacementType,
ProtocolDecl *protocol) const {
auto abstractRef = ProtocolConformanceRef(protocol);

auto archetypeAndRoot = getArchetypeAndRootOpaqueArchetype(maybeOpaqueType);
if (!archetypeAndRoot) {
assert(maybeOpaqueType->isTypeParameter() ||
maybeOpaqueType->is<ArchetypeType>());
return abstractRef;
if (maybeOpaqueType->isTypeParameter() ||
maybeOpaqueType->is<ArchetypeType>())
return abstractRef;

// SIL type lowering may have already substituted away the opaque type, in
// which case we'll end up "substituting" the same type.
if (maybeOpaqueType->isEqual(replacementType)) {
return inContext->getParentModule()
->lookupConformance(replacementType, protocol);
}

llvm_unreachable("origType should have been an opaque type or type parameter");
}

auto archetype = archetypeAndRoot->first;
Expand Down Expand Up @@ -3510,24 +3519,28 @@ static Type getMemberForBaseType(LookupConformanceFn lookupConformances,

// Retrieve the type witness.
auto witness =
conformance.getConcrete()->getTypeWitness(assocType, options);
if (!witness || witness->hasError())
conformance.getConcrete()->getTypeWitnessAndDecl(assocType, options);

auto witnessTy = witness.getWitnessType();
if (!witnessTy || witnessTy->hasError())
return failed();

// This is a hacky feature allowing code completion to migrate to
// using Type::subst() without changing output.
if (options & SubstFlags::DesugarMemberTypes) {
if (auto *aliasType =
dyn_cast<TypeAliasType>(witness.getPointer())) {
if (!aliasType->is<ErrorType>())
witness = aliasType->getSinglyDesugaredType();
}
if (auto *aliasType = dyn_cast<TypeAliasType>(witnessTy.getPointer()))
witnessTy = aliasType->getSinglyDesugaredType();

// Another hack. If the type witness is a opaque result type. They can
// only be referred using the name of the associated type.
if (witnessTy->is<OpaqueTypeArchetypeType>())
witnessTy = witness.getWitnessDecl()->getDeclaredInterfaceType();
}

if (witness->is<ErrorType>())
if (witnessTy->is<ErrorType>())
return failed();

return witness;
return witnessTy;
}

return failed();
Expand Down
16 changes: 10 additions & 6 deletions lib/IDE/ExprContextAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,12 +379,16 @@ static void collectPossibleCalleesByQualifiedLookup(
tyExpr->getTypeLoc().setType(nullptr);
}

auto baseTyOpt = getTypeOfCompletionContextExpr(
DC.getASTContext(), &DC, CompletionTypeCheckKind::Normal, baseExpr, ref);
if (!baseTyOpt)
return;

auto baseTy = (*baseTyOpt)->getWithoutSpecifierType();
Type baseTy = baseExpr->getType();
if (!baseTy || baseTy->is<ErrorType>()) {
auto baseTyOpt = getTypeOfCompletionContextExpr(
DC.getASTContext(), &DC, CompletionTypeCheckKind::Normal, baseExpr,
ref);
if (!baseTyOpt)
return;
baseTy = *baseTyOpt;
}
baseTy = baseTy->getWithoutSpecifierType();
if (!baseTy->getMetatypeInstanceType()->mayHaveMembers())
return;

Expand Down
7 changes: 0 additions & 7 deletions lib/SILGen/SILGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -955,13 +955,6 @@ void SILGenModule::emitConstructor(ConstructorDecl *decl) {
if (isa<ProtocolDecl>(decl->getDeclContext()))
return;

// Always-unavailable imported constructors are factory methods
// that have been imported as constructors and then hidden by an
// imported init method.
if (decl->hasClangNode() &&
decl->getAttrs().isUnavailable(decl->getASTContext()))
return;

SILDeclRef constant(decl);
DeclContext *declCtx = decl->getDeclContext();

Expand Down
3 changes: 2 additions & 1 deletion lib/SILGen/SILGenConstructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ static void emitImplicitValueConstructor(SILGenFunction &SGF,
selfTy.getFieldType(field, SGF.SGM.M, SGF.getTypeExpansionContext());
RValue value;

FullExpr scope(SGF.Cleanups, field->getParentPatternBinding());

// If it's memberwise initialized, do so now.
if (field->isMemberwiseInitialized(/*preferDeclaredProperties=*/false)) {
assert(elti != eltEnd && "number of args does not match number of fields");
Expand All @@ -276,7 +278,6 @@ static void emitImplicitValueConstructor(SILGenFunction &SGF,
}

// Cleanup after this initialization.
FullExpr scope(SGF.Cleanups, field->getParentPatternBinding());
SILValue v = maybeEmitPropertyWrapperInitFromValue(SGF, Loc, field, subs,
std::move(value))
.forwardAsSingleStorageValue(SGF, fieldTy, Loc);
Expand Down
20 changes: 9 additions & 11 deletions lib/Sema/CSBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1090,27 +1090,25 @@ bool TypeVariableBinding::attempt(ConstraintSystem &cs) const {
// resolved and had to be bound to a placeholder "hole" type.
cs.increaseScore(SK_Hole);

ConstraintFix *fix = nullptr;
if (auto *GP = TypeVar->getImpl().getGenericParameter()) {
auto path = dstLocator->getPath();
// Drop `generic parameter` locator element so that all missing
// generic parameters related to the same path can be coalesced later.
auto *fix = DefaultGenericArgument::create(
fix = DefaultGenericArgument::create(
cs, GP,
cs.getConstraintLocator(dstLocator->getAnchor(), path.drop_back()));
if (cs.recordFix(fix))
return true;
} else if (TypeVar->getImpl().isClosureParameterType()) {
fix = SpecifyClosureParameterType::create(cs, dstLocator);
} else if (TypeVar->getImpl().isClosureResultType()) {
auto *fix = SpecifyClosureReturnType::create(
cs, TypeVar->getImpl().getLocator());
if (cs.recordFix(fix))
return true;
fix = SpecifyClosureReturnType::create(cs, dstLocator);
} else if (srcLocator->getAnchor() &&
isa<ObjectLiteralExpr>(srcLocator->getAnchor())) {
auto *fix = SpecifyObjectLiteralTypeImport::create(
cs, TypeVar->getImpl().getLocator());
if (cs.recordFix(fix))
return true;
fix = SpecifyObjectLiteralTypeImport::create(cs, dstLocator);
}

if (fix && cs.recordFix(fix))
return true;
}
}

Expand Down
74 changes: 74 additions & 0 deletions lib/Sema/CSDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
using namespace swift;
using namespace constraints;

static bool hasFixFor(const Solution &solution, ConstraintLocator *locator) {
return llvm::any_of(solution.Fixes, [&locator](const ConstraintFix *fix) {
return fix->getLocator() == locator;
});
}

FailureDiagnostic::~FailureDiagnostic() {}

bool FailureDiagnostic::diagnose(bool asNote) {
Expand Down Expand Up @@ -6127,6 +6133,74 @@ bool MissingContextualBaseInMemberRefFailure::diagnoseAsError() {
return true;
}

bool UnableToInferClosureParameterType::diagnoseAsError() {
auto *closure = castToExpr<ClosureExpr>(getRawAnchor());

// Let's check whether this closure is an argument to
// a call which couldn't be properly resolved e.g.
// missing member or invalid contextual reference and
// if so let's not diagnose this problem because main
// issue here is inability to establish context for
// closure inference.
//
// TODO(diagnostics): Once we gain an ability to determine
// originating source of type holes this check could be
// significantly simplified.
{
auto &solution = getSolution();

// If there is a contextual mismatch associated with this
// closure, let's not diagnose any parameter type issues.
if (hasFixFor(solution, getConstraintLocator(
closure, LocatorPathElt::ContextualType())))
return false;

if (auto *parentExpr = findParentExpr(closure)) {
while (parentExpr &&
(isa<TupleExpr>(parentExpr) || isa<ParenExpr>(parentExpr))) {
parentExpr = findParentExpr(parentExpr);
}

if (parentExpr) {
// Missing or invalid member reference in call.
if (auto *AE = dyn_cast<ApplyExpr>(parentExpr)) {
if (getType(AE->getFn())->isHole())
return false;
}

// Any fix anchored on parent expression makes it unnecessary
// to diagnose unability to infer parameter type because it's
// an indication that proper context couldn't be established to
// resolve the closure.
if (llvm::any_of(solution.Fixes,
[&parentExpr](const ConstraintFix *fix) -> bool {
return fix->getAnchor() == parentExpr;
}))
return false;
}
}
}

auto paramIdx = getLocator()
->castLastElementTo<LocatorPathElt::TupleElement>()
.getIndex();

auto *PD = closure->getParameters()->get(paramIdx);

llvm::SmallString<16> id;
llvm::raw_svector_ostream OS(id);

if (PD->isAnonClosureParam()) {
OS << "$" << paramIdx;
} else {
OS << "'" << PD->getParameterName() << "'";
}

auto loc = PD->isAnonClosureParam() ? getLoc() : PD->getLoc();
emitDiagnosticAt(loc, diag::cannot_infer_closure_parameter_type, OS.str());
return true;
}

bool UnableToInferClosureReturnType::diagnoseAsError() {
auto *closure = castToExpr<ClosureExpr>(getRawAnchor());

Expand Down
9 changes: 9 additions & 0 deletions lib/Sema/CSDiagnostics.h
Original file line number Diff line number Diff line change
Expand Up @@ -1973,6 +1973,15 @@ class MissingContextualBaseInMemberRefFailure final : public FailureDiagnostic {
bool diagnoseAsError();
};

class UnableToInferClosureParameterType final : public FailureDiagnostic {
public:
UnableToInferClosureParameterType(const Solution &solution,
ConstraintLocator *locator)
: FailureDiagnostic(solution, locator) {}

bool diagnoseAsError();
};

class UnableToInferClosureReturnType final : public FailureDiagnostic {
public:
UnableToInferClosureReturnType(const Solution &solution,
Expand Down
33 changes: 33 additions & 0 deletions lib/Sema/CSFix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "ConstraintSystem.h"
#include "OverloadChoice.h"
#include "swift/AST/Expr.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/Type.h"
#include "swift/AST/Types.h"
#include "swift/Basic/SourceManager.h"
Expand Down Expand Up @@ -1226,6 +1227,38 @@ SpecifyBaseTypeForContextualMember *SpecifyBaseTypeForContextualMember::create(
SpecifyBaseTypeForContextualMember(cs, member, locator);
}

std::string SpecifyClosureParameterType::getName() const {
std::string name;
llvm::raw_string_ostream OS(name);

auto *closure = cast<ClosureExpr>(getAnchor());
auto paramLoc =
getLocator()->castLastElementTo<LocatorPathElt::TupleElement>();

auto *PD = closure->getParameters()->get(paramLoc.getIndex());

OS << "specify type for parameter ";
if (PD->isAnonClosureParam()) {
OS << "$" << paramLoc.getIndex();
} else {
OS << "'" << PD->getParameterName() << "'";
}

return OS.str();
}

bool SpecifyClosureParameterType::diagnose(const Solution &solution,
bool asNote) const {
UnableToInferClosureParameterType failure(solution, getLocator());
return failure.diagnose(asNote);
}

SpecifyClosureParameterType *
SpecifyClosureParameterType::create(ConstraintSystem &cs,
ConstraintLocator *locator) {
return new (cs.getAllocator()) SpecifyClosureParameterType(cs, locator);
}

bool SpecifyClosureReturnType::diagnose(const Solution &solution,
bool asNote) const {
UnableToInferClosureReturnType failure(solution, getLocator());
Expand Down
19 changes: 18 additions & 1 deletion lib/Sema/CSFix.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ enum class FixKind : uint8_t {
/// inferred and has to be specified explicitly.
SpecifyBaseTypeForContextualMember,

/// Type of the closure parameter used in the body couldn't be inferred
/// and has to be specified explicitly.
SpecifyClosureParameterType,

/// Closure return type has to be explicitly specified because it can't be
/// inferred in current context e.g. because it's a multi-statement closure.
SpecifyClosureReturnType,
Expand All @@ -251,7 +255,7 @@ enum class FixKind : uint8_t {

/// A warning fix that allows a coercion to perform a force-cast.
AllowCoercionToForceCast,

/// Allow key path root type mismatch when applying a key path that has a
/// root type not convertible to the type of the base instance.
AllowKeyPathRootTypeMismatch,
Expand Down Expand Up @@ -1706,6 +1710,19 @@ class SpecifyBaseTypeForContextualMember final : public ConstraintFix {
create(ConstraintSystem &cs, DeclNameRef member, ConstraintLocator *locator);
};

class SpecifyClosureParameterType final : public ConstraintFix {
SpecifyClosureParameterType(ConstraintSystem &cs, ConstraintLocator *locator)
: ConstraintFix(cs, FixKind::SpecifyClosureParameterType, locator) {}

public:
std::string getName() const;

bool diagnose(const Solution &solution, bool asNote = false) const;

static SpecifyClosureParameterType *create(ConstraintSystem &cs,
ConstraintLocator *locator);
};

class SpecifyClosureReturnType final : public ConstraintFix {
SpecifyClosureReturnType(ConstraintSystem &cs, ConstraintLocator *locator)
: ConstraintFix(cs, FixKind::SpecifyClosureReturnType, locator) {}
Expand Down
Loading