Skip to content
Merged
18 changes: 6 additions & 12 deletions include/swift/SIL/TypeLowering.h
Original file line number Diff line number Diff line change
Expand Up @@ -545,20 +545,15 @@ struct SILConstantInfo {

/// The SIL function type of the constant.
CanSILFunctionType SILFnType;

/// The generic environment used by the constant.
GenericEnvironment *GenericEnv;

SILConstantInfo(CanAnyFunctionType formalType,
AbstractionPattern formalPattern,
CanAnyFunctionType loweredType,
CanSILFunctionType silFnTy,
GenericEnvironment *env)
CanSILFunctionType silFnTy)
: FormalType(formalType),
FormalPattern(formalPattern),
LoweredType(loweredType),
SILFnType(silFnTy),
GenericEnv(env) {}
SILFnType(silFnTy) {}

SILType getSILType() const {
return SILType::getPrimitiveObjectType(SILFnType);
Expand All @@ -567,8 +562,7 @@ struct SILConstantInfo {
friend bool operator==(SILConstantInfo lhs, SILConstantInfo rhs) {
return lhs.FormalType == rhs.FormalType &&
lhs.LoweredType == rhs.LoweredType &&
lhs.SILFnType == rhs.SILFnType &&
lhs.GenericEnv == rhs.GenericEnv;
lhs.SILFnType == rhs.SILFnType;
}
friend bool operator!=(SILConstantInfo lhs, SILConstantInfo rhs) {
return !(lhs == rhs);
Expand Down Expand Up @@ -707,9 +701,6 @@ class TypeConverter {

CanAnyFunctionType makeConstantInterfaceType(SILDeclRef constant);

/// Get the generic environment for a constant.
GenericEnvironment *getConstantGenericEnvironment(SILDeclRef constant);

// Types converted during foreign bridging.
#define BRIDGING_KNOWN_TYPE(BridgedModule,BridgedType) \
Optional<CanType> BridgedType##Ty;
Expand Down Expand Up @@ -830,6 +821,9 @@ class TypeConverter {
/// for a constant reference.
const SILConstantInfo &getConstantInfo(SILDeclRef constant);

/// Get the generic environment for a constant.
GenericEnvironment *getConstantGenericEnvironment(SILDeclRef constant);

/// Returns the SIL type of a constant reference.
SILType getConstantType(SILDeclRef constant) {
return getConstantInfo(constant).getSILType();
Expand Down
3 changes: 3 additions & 0 deletions lib/AST/ASTVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,9 @@ class Verifier : public ASTWalker {
abort();
}

if (!type->hasArchetype())
return;

bool foundError = type->getCanonicalType().findIf([&](Type type) -> bool {
if (auto archetype = type->getAs<ArchetypeType>()) {
// Only visit each archetype once.
Expand Down
3 changes: 1 addition & 2 deletions lib/AST/GenericSignatureBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5440,8 +5440,7 @@ class GenericSignatureBuilder::InferRequirementsWalker : public TypeWalker {
return Action::Continue;

/// Retrieve the substitution.
auto subMap = ty->getContextSubstitutionMap(&module, decl,
decl->getGenericEnvironment());
auto subMap = ty->getContextSubstitutionMap(&module, decl);

// Handle the requirements.
// FIXME: Inaccurate TypeReprs.
Expand Down
7 changes: 2 additions & 5 deletions lib/SIL/SILFunctionType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2159,7 +2159,6 @@ const SILConstantInfo &TypeConverter::getConstantInfo(SILDeclRef constant) {
// First, get a function type for the constant. This creates the
// right type for a getter or setter.
auto formalInterfaceType = makeConstantInterfaceType(constant);
auto *genericEnv = getConstantGenericEnvironment(constant);

// The formal type is just that with the right representation.
auto rep = getDeclRefRepresentation(constant);
Expand Down Expand Up @@ -2192,8 +2191,7 @@ const SILConstantInfo &TypeConverter::getConstantInfo(SILDeclRef constant) {
auto result = ::new (resultBuf) SILConstantInfo{formalInterfaceType,
bridgedTypes.Pattern,
loweredInterfaceType,
silFnType,
genericEnv};
silFnType};
if (DisableConstantInfoCache)
return *result;

Expand Down Expand Up @@ -2380,8 +2378,7 @@ TypeConverter::getConstantOverrideInfo(SILDeclRef derived, SILDeclRef base) {
derivedInterfaceTy,
bridgedTypes.Pattern,
overrideLoweredInterfaceTy,
fnTy,
derivedInfo.GenericEnv};
fnTy};

auto inserted = ConstantOverrideTypes.insert({{derived, base}, result});
assert(inserted.second);
Expand Down
2 changes: 1 addition & 1 deletion lib/SIL/SILVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2527,7 +2527,7 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
assert(!methodTy->isCoroutine());

// Map interface types to archetypes.
if (auto *env = constantInfo.GenericEnv) {
if (auto *env = F.getModule().Types.getConstantGenericEnvironment(method)) {
auto subs = env->getForwardingSubstitutionMap();
methodTy = methodTy->substGenericArgs(F.getModule(), subs);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/SILGen/SILGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ void SILGenModule::preEmitFunction(SILDeclRef constant,
assert(F->empty() && "already emitted function?!");

if (F->getLoweredFunctionType()->isPolymorphic())
F->setGenericEnvironment(Types.getConstantInfo(constant).GenericEnv);
F->setGenericEnvironment(Types.getConstantGenericEnvironment(constant));

// Create a debug scope for the function using astNode as source location.
F->setDebugScope(new (M) SILDebugScope(Loc, F));
Expand Down
7 changes: 2 additions & 5 deletions lib/SILGen/SILGenBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1268,9 +1268,6 @@ static SILFunctionType *emitObjCThunkArguments(SILGenFunction &SGF,
auto swiftFormalFnTy = substGenericArgs(swiftInfo.LoweredType, subs);
SILFunctionConventions swiftConv(swiftFnTy, SGF.SGM.M);

// We must have the same context archetypes as the unthunked function.
assert(objcInfo.GenericEnv == swiftInfo.GenericEnv);

SmallVector<ManagedValue, 8> bridgedArgs;
bridgedArgs.reserve(objcFnTy->getParameters().size());

Expand Down Expand Up @@ -1399,7 +1396,7 @@ void SILGenFunction::emitNativeToForeignThunk(SILDeclRef thunk) {
SILFunctionConventions substConv(substTy, SGM.M);

// Use the same generic environment as the native entry point.
F.setGenericEnvironment(nativeInfo.GenericEnv);
F.setGenericEnvironment(SGM.Types.getConstantGenericEnvironment(native));

auto loc = thunk.getAsRegularLocation();
loc.markAutoGenerated();
Expand Down Expand Up @@ -1607,7 +1604,7 @@ void SILGenFunction::emitForeignToNativeThunk(SILDeclRef thunk) {
assert(nativeFnTy == nativeCI.SILFnType);

// Use the same generic environment as the native entry point.
F.setGenericEnvironment(nativeCI.GenericEnv);
F.setGenericEnvironment(SGM.Types.getConstantGenericEnvironment(thunk));

SILDeclRef foreignDeclRef = thunk.asForeign(true);
SILConstantInfo foreignCI = getConstantInfo(foreignDeclRef);
Expand Down
4 changes: 2 additions & 2 deletions lib/SILGen/SILGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2858,7 +2858,7 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM,
// If the storage declaration is from a protocol, chase the override chain
// back to the declaration whose getter introduced the witness table
// entry.
if (auto origProto = dyn_cast<ProtocolDecl>(property->getDeclContext())) {
if (isa<ProtocolDecl>(property->getDeclContext())) {
auto getter = property->getGetter();
if (!SILDeclRef::requiresNewWitnessTableEntry(getter)) {
// Find the getter that does have a witness table entry.
Expand Down Expand Up @@ -2989,7 +2989,7 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM,
// If the storage declaration is from a protocol, chase the override chain
// back to the declaration whose setter introduced the witness table
// entry.
if (auto origProto = dyn_cast<ProtocolDecl>(property->getDeclContext())) {
if (isa<ProtocolDecl>(property->getDeclContext())) {
auto setter = property->getSetter();
if (!SILDeclRef::requiresNewWitnessTableEntry(setter)) {
// Find the setter that does have a witness table entry.
Expand Down
9 changes: 0 additions & 9 deletions lib/Sema/ConstraintLocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ void ConstraintLocator::Profile(llvm::FoldingSetNodeID &id, Expr *anchor,
id.AddPointer(elt.getWitness());
break;

case AssociatedType:
id.AddPointer(elt.getAssociatedType());
break;

case ApplyArgument:
case ApplyFunction:
case FunctionArgument:
Expand Down Expand Up @@ -117,11 +113,6 @@ void ConstraintLocator::dump(SourceManager *sm, raw_ostream &out) {
out << "archetype '" << elt.getArchetype()->getString() << "'";
break;

case AssociatedType:
out << "associated type '"
<< elt.getAssociatedType()->getNameStr() << "'";
break;

case ApplyArgument:
out << "apply argument";
break;
Expand Down
22 changes: 1 addition & 21 deletions lib/Sema/ConstraintLocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ class ConstraintLocator : public llvm::FoldingSetNode {
///
/// Also contains the archetype itself.
Archetype,
/// An associated type reference.
///
/// Contains the associated type itself.
AssociatedType,
/// \brief The argument type of a function.
FunctionArgument,
/// \brief The result type of a function.
Expand Down Expand Up @@ -138,7 +134,6 @@ class ConstraintLocator : public llvm::FoldingSetNode {
case ApplyArgument:
case ApplyFunction:
case Archetype:
case AssociatedType:
case FunctionArgument:
case FunctionResult:
case OptionalPayload:
Expand Down Expand Up @@ -211,7 +206,6 @@ class ConstraintLocator : public llvm::FoldingSetNode {
case SubscriptMember:
case OpenedGeneric:
case Archetype:
case AssociatedType:
case GenericArgument:
case NamedTupleElement:
case TupleElement:
Expand Down Expand Up @@ -316,13 +310,6 @@ class ConstraintLocator : public llvm::FoldingSetNode {
(kind == Witness && getWitness() == decl)));
}

PathElement(AssociatedTypeDecl *decl)
: storage((reinterpret_cast<uintptr_t>(decl) >> 2)),
storedKind(StoredRequirement)
{
assert(getAssociatedType() == decl);
}

/// \brief Retrieve a path element for a tuple element referred to by
/// its position.
static PathElement getTupleElement(unsigned position) {
Expand Down Expand Up @@ -370,8 +357,7 @@ class ConstraintLocator : public llvm::FoldingSetNode {
return Archetype;

case StoredRequirement:
return isa<AssociatedTypeDecl>(getRequirement()) ? AssociatedType
: Requirement;
return Requirement;

case StoredWitness:
return Witness;
Expand Down Expand Up @@ -427,12 +413,6 @@ class ConstraintLocator : public llvm::FoldingSetNode {
return reinterpret_cast<ValueDecl *>(storage << 2);
}

/// Retrieve the declaration for an associated type path element.
AssociatedTypeDecl *getAssociatedType() const {
assert(getKind() == AssociatedType && "Is not an associated type");
return reinterpret_cast<AssociatedTypeDecl *>(storage << 2);
}

/// \brief Return the summary flags for this particular element.
unsigned getNewSummaryFlags() const {
return getSummaryFlagsForPathElement(getKind());
Expand Down
7 changes: 2 additions & 5 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -801,8 +801,7 @@ void ConstraintSystem::recordOpenedTypes(
SmallVector<LocatorPathElt, 2> pathElts;
Expr *anchor = locator.getLocatorParts(pathElts);
if (!pathElts.empty() &&
(pathElts.back().getKind() == ConstraintLocator::Archetype ||
pathElts.back().getKind() == ConstraintLocator::AssociatedType))
pathElts.back().getKind() == ConstraintLocator::Archetype)
return;

// If the locator is empty, ignore it.
Expand Down Expand Up @@ -1017,8 +1016,6 @@ static void bindArchetypesFromContext(
ConstraintLocator *locatorPtr,
const OpenedTypeMap &replacements) {

auto *genericEnv = cs.DC->getGenericEnvironmentOfContext();

auto bindContextArchetype = [&](Type paramTy, Type contextTy) {
auto found = replacements.find(cast<GenericTypeParamType>(
paramTy->getCanonicalType()));
Expand Down Expand Up @@ -1054,7 +1051,7 @@ static void bindArchetypesFromContext(
break;

for (auto *paramTy : genericSig->getGenericParams()) {
Type contextTy = genericEnv->mapTypeIntoContext(paramTy);
Type contextTy = cs.DC->mapTypeIntoContext(paramTy);
bindContextArchetype(paramTy, contextTy);
}

Expand Down
24 changes: 4 additions & 20 deletions lib/Sema/TypeCheckConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "swift/AST/ExistentialLayout.h"
#include "swift/AST/Initializer.h"
#include "swift/AST/NameLookup.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/PrettyStackTrace.h"
#include "swift/AST/SubstitutionMap.h"
#include "swift/AST/TypeCheckerDebugConsumer.h"
Expand Down Expand Up @@ -215,29 +216,12 @@ Expr *ConstraintLocatorBuilder::trySimplifyToExpr() const {
//===----------------------------------------------------------------------===//

static unsigned getNumArgs(ValueDecl *value) {
if (!isa<FuncDecl>(value)) return ~0U;

AnyFunctionType *fnTy = value->getInterfaceType()->castTo<AnyFunctionType>();
if (value->getDeclContext()->isTypeContext())
fnTy = fnTy->getResult()->castTo<AnyFunctionType>();

auto params = fnTy->getParams();
if (params.size() == 1) {
const auto &param = params.front();
if (param.hasLabel() || param.isVariadic())
return 1;

if (auto *tuple = param.getOldType()->getAs<TupleType>())
return tuple->getNumElements();
}

return params.size();
if (auto *func = dyn_cast<FuncDecl>(value))
return func->getParameters()->size();
return ~0U;
}

static bool matchesDeclRefKind(ValueDecl *value, DeclRefKind refKind) {
if (value->getInterfaceType()->hasError())
return true;

switch (refKind) {
// An ordinary reference doesn't ignore anything.
case DeclRefKind::Ordinary:
Expand Down
7 changes: 0 additions & 7 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3934,8 +3934,6 @@ void TypeChecker::validateDecl(ValueDecl *D) {
aliasDecl->getUnderlyingTypeLoc().setType(Type());
aliasDecl->setInterfaceType(Type());

(void) aliasDecl->getFormalAccess();

// Check generic parameters, if needed.
if (aliasDecl->hasValidationStarted()) {
validateTypealiasType(*this, aliasDecl);
Expand Down Expand Up @@ -4444,8 +4442,6 @@ void TypeChecker::validateDeclForNameLookup(ValueDecl *D) {
for (auto paramDecl : *gp)
paramDecl->setDepth(depth);

(void) proto->getFormalAccess();

for (auto ATD : proto->getAssociatedTypeMembers()) {
validateDeclForNameLookup(ATD);
}
Expand All @@ -4468,7 +4464,6 @@ void TypeChecker::validateDeclForNameLookup(ValueDecl *D) {
if (assocType->hasInterfaceType())
return;
assocType->computeType();
(void) assocType->getFormalAccess();
break;
}
case DeclKind::TypeAlias: {
Expand All @@ -4482,8 +4477,6 @@ void TypeChecker::validateDeclForNameLookup(ValueDecl *D) {
if (typealias->isBeingValidated()) return;

auto helper = [&] {
(void) typealias->getFormalAccess();

TypeResolutionOptions options(TypeResolverContext::TypeAliasDecl);
if (validateType(typealias->getUnderlyingTypeLoc(),
TypeResolution::forStructural(typealias), options)) {
Expand Down
2 changes: 2 additions & 0 deletions lib/Serialization/ModuleFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,8 @@ class ModuleFile::DeclMemberNamesTableInfo {
return {DeclBaseName::Kind::Subscript, StringRef()};
case static_cast<uint8_t>(DeclNameKind::Destructor):
return {DeclBaseName::Kind::Destructor, StringRef()};
case static_cast<uint8_t>(DeclNameKind::Constructor):
return {DeclBaseName::Kind::Constructor, StringRef()};
default:
llvm_unreachable("Unknown DeclNameKind");
}
Expand Down
3 changes: 2 additions & 1 deletion test/Parse/matching_patterns.swift
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ case (1, 2, 3):

// patterns in expression-only positions are errors.
case +++(_, var d, 3):
// expected-error@-1{{'+++' is not a prefix unary operator}}
// expected-error@-1{{'_' can only appear in a pattern or on the left side of an assignment}}
// expected-error@-2{{'var' binding pattern cannot appear in an expression}}
()
case (_, var e, 3) +++ (1, 2, 3):
// expected-error@-1{{'_' can only appear in a pattern}}
Expand Down