Skip to content

More small fixes #20626

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 4 commits into from
Nov 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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: 1 addition & 2 deletions include/swift/AST/GenericSignatureBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -626,8 +626,7 @@ class GenericSignatureBuilder {
/// where \c Dictionary requires that its key type be \c Hashable,
/// the requirement \c K : Hashable is inferred from the parameter type,
/// because the type \c Dictionary<K,V> cannot be formed without it.
void inferRequirements(ModuleDecl &module, ParameterList *params,
GenericParamList *genericParams);
void inferRequirements(ModuleDecl &module, ParameterList *params);

/// \brief Finalize the set of requirements and compute the generic
/// signature.
Expand Down
11 changes: 8 additions & 3 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5890,9 +5890,14 @@ Type EnumElementDecl::getArgumentInterfaceType() const {

auto funcTy = interfaceType->castTo<AnyFunctionType>();
funcTy = funcTy->getResult()->castTo<FunctionType>();
return AnyFunctionType::composeInput(funcTy->getASTContext(),
funcTy->getParams(),
/*canonicalVararg=*/false);

auto &ctx = getASTContext();
SmallVector<TupleTypeElt, 4> elements;
for (const auto &param : funcTy->getParams()) {
Type eltType = param.getParameterType(/*canonicalVararg=*/false, &ctx);
elements.emplace_back(eltType, param.getLabel());
}
return TupleType::get(elements, ctx);
}

EnumCaseDecl *EnumElementDecl::getParentCase() const {
Expand Down
6 changes: 1 addition & 5 deletions lib/AST/GenericSignatureBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5458,11 +5458,7 @@ void GenericSignatureBuilder::inferRequirements(

void GenericSignatureBuilder::inferRequirements(
ModuleDecl &module,
ParameterList *params,
GenericParamList *genericParams) {
if (genericParams == nullptr)
return;

ParameterList *params) {
for (auto P : *params) {
inferRequirements(module, P->getTypeLoc().getType(),
P->getTypeLoc().getTypeRepr(),
Expand Down
91 changes: 48 additions & 43 deletions lib/SILGen/SILGenPattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1566,41 +1566,42 @@ emitCastOperand(SILGenFunction &SGF, SILLocation loc,
SGFContext ctx;
if (requiresAddress) {
init = SGF.emitTemporary(loc, srcAbstractTL);

// Okay, if all we need to do is drop the value in an address,
// this is easy.
if (!hasAbstraction) {
// TODO: Refactor this into a materialize method on CastConsumptionKind.
ManagedValue finalValue = src.getFinalManagedValue();
if (finalValue.getOwnershipKind() == ValueOwnershipKind::Guaranteed)
finalValue = finalValue.copy(SGF, loc);
SGF.B.emitStoreValueOperation(loc, finalValue.forward(SGF),
init->getAddress(),
StoreOwnershipQualifier::Init);
init->finishInitialization(SGF);
// If we had borrow_always, we need to switch to copy_on_success since
// that is the address only variant of borrow_always.
auto consumption = src.getFinalConsumption();
if (consumption == CastConsumptionKind::BorrowAlways)
consumption = CastConsumptionKind::CopyOnSuccess;
ConsumableManagedValue result = {init->getManagedAddress(), consumption};
if (ArgUnforwarder::requiresUnforwarding(SGF, src))
borrowedValues.push_back(result);
return result;
}

ctx = SGFContext(init.get());
}

assert(hasAbstraction);
assert(src.getType().isObject() &&
"address-only type with abstraction difference?");

// Produce the value at +1.
ManagedValue substValue = SGF.getManagedValue(loc, src);
ManagedValue origValue =
SGF.emitSubstToOrigValue(loc, substValue, abstraction, sourceType);
return ConsumableManagedValue::forOwned(origValue);
ManagedValue finalValue;
if (hasAbstraction) {
assert(src.getType().isObject() &&
"address-only type with abstraction difference?");
// Produce the value at +1.
finalValue = SGF.emitSubstToOrigValue(loc, substValue,
abstraction, sourceType, ctx);
} else {
finalValue = substValue;
}

if (requiresAddress) {
if (finalValue.getOwnershipKind() == ValueOwnershipKind::Guaranteed)
finalValue = finalValue.copy(SGF, loc);
SGF.B.emitStoreValueOperation(loc, finalValue.forward(SGF),
init->getAddress(),
StoreOwnershipQualifier::Init);
init->finishInitialization(SGF);

// If we had borrow_always, we need to switch to copy_on_success since
// that is the address only variant of borrow_always.
auto consumption = src.getFinalConsumption();
if (consumption == CastConsumptionKind::BorrowAlways)
consumption = CastConsumptionKind::CopyOnSuccess;
ConsumableManagedValue result = {init->getManagedAddress(), consumption};
if (ArgUnforwarder::requiresUnforwarding(SGF, src))
borrowedValues.push_back(result);

return result;
}

return ConsumableManagedValue::forOwned(finalValue);
}

/// Perform specialized dispatch for a sequence of IsPatterns.
Expand Down Expand Up @@ -2536,6 +2537,7 @@ class Lowering::PatternMatchContext {
static void emitDiagnoseOfUnexpectedEnumCaseValue(SILGenFunction &SGF,
SILLocation loc,
ManagedValue value,
Type subjectTy,
const EnumDecl *enumDecl) {
ASTContext &ctx = SGF.getASTContext();
auto diagnoseFailure = ctx.getDiagnoseUnexpectedEnumCaseValue(nullptr);
Expand All @@ -2549,10 +2551,9 @@ static void emitDiagnoseOfUnexpectedEnumCaseValue(SILGenFunction &SGF,
assert(value.getType().isTrivial(SGF.getModule()));

// Get the enum type as an Any.Type value.
CanType switchedValueSwiftType = value.getType().getASTType();
SILType metatypeType = SGF.getLoweredType(
CanMetatypeType::get(switchedValueSwiftType,
MetatypeRepresentation::Thick));
AbstractionPattern::getOpaque(),
MetatypeType::get(subjectTy));
SILValue metatype = SGF.B.createMetatype(loc, metatypeType);

// Bitcast the enum value to its raw type. (This is only safe for @objc
Expand All @@ -2573,7 +2574,7 @@ static void emitDiagnoseOfUnexpectedEnumCaseValue(SILGenFunction &SGF,
assert(genericParam->getIndex() < 2);
switch (genericParam->getIndex()) {
case 0:
return switchedValueSwiftType;
return subjectTy;

case 1:
return enumDecl->getRawType();
Expand All @@ -2592,7 +2593,8 @@ static void emitDiagnoseOfUnexpectedEnumCaseValue(SILGenFunction &SGF,

static void emitDiagnoseOfUnexpectedEnumCase(SILGenFunction &SGF,
SILLocation loc,
ManagedValue value) {
ManagedValue value,
Type subjectTy) {
ASTContext &ctx = SGF.getASTContext();
auto diagnoseFailure = ctx.getDiagnoseUnexpectedEnumCase(nullptr);
if (!diagnoseFailure) {
Expand All @@ -2601,16 +2603,15 @@ static void emitDiagnoseOfUnexpectedEnumCase(SILGenFunction &SGF,
}

// Get the switched-upon value's type.
CanType switchedValueSwiftType = value.getType().getASTType();
SILType metatypeType = SGF.getLoweredType(
CanMetatypeType::get(switchedValueSwiftType,
MetatypeRepresentation::Thick));
AbstractionPattern::getOpaque(),
MetatypeType::get(subjectTy)->getCanonicalType());
ManagedValue metatype = SGF.B.createValueMetatype(loc, metatypeType, value);

auto diagnoseSignature = diagnoseFailure->getGenericSignature();
auto genericArgsMap = SubstitutionMap::get(
diagnoseSignature,
[&](SubstitutableType *type) -> Type { return switchedValueSwiftType; },
[&](SubstitutableType *type) -> Type { return subjectTy; },
LookUpConformanceInSignature(*diagnoseSignature));

SGF.emitApplyOfLibraryIntrinsic(loc, diagnoseFailure, genericArgsMap,
Expand All @@ -2622,9 +2623,12 @@ void SILGenFunction::emitSwitchStmt(SwitchStmt *S) {
LLVM_DEBUG(llvm::dbgs() << "emitting switch stmt\n";
S->dump(llvm::dbgs());
llvm::dbgs() << '\n');

auto subjectTy = S->getSubjectExpr()->getType();

// If the subject expression is uninhabited, we're already dead.
// Emit an unreachable in place of the switch statement.
if (S->getSubjectExpr()->getType()->isStructurallyUninhabited()) {
if (subjectTy->isStructurallyUninhabited()) {
emitIgnoredExpr(S->getSubjectExpr());
B.createUnreachable(S);
return;
Expand Down Expand Up @@ -2789,12 +2793,13 @@ void SILGenFunction::emitSwitchStmt(SwitchStmt *S) {
if (singleEnumDecl->isObjC()) {
emitDiagnoseOfUnexpectedEnumCaseValue(*this, location,
subject.getFinalManagedValue(),
singleEnumDecl);
subjectTy, singleEnumDecl);
return;
}
}
emitDiagnoseOfUnexpectedEnumCase(*this, location,
subject.getFinalManagedValue());
subject.getFinalManagedValue(),
subjectTy);
};

// Set up an initial clause matrix.
Expand Down
Loading