Skip to content

Commit 7f57ca9

Browse files
committed
WIP
Update TypeLowering.cpp
1 parent 11aa68b commit 7f57ca9

File tree

14 files changed

+190
-38
lines changed

14 files changed

+190
-38
lines changed

include/swift/Sema/CSFix.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,10 @@ enum class FixKind : uint8_t {
489489
/// sending result, but is passed a function typed parameter without a sending
490490
/// result.
491491
AllowSendingMismatch,
492+
493+
/// Ignore when a 'Vector' literal has mismatched number of elements to the
494+
/// type it's attempting to bind to.
495+
AllowVectorLiteralCountMismatch,
492496
};
493497

494498
class ConstraintFix {
@@ -3870,6 +3874,30 @@ class IgnoreKeyPathSubscriptIndexMismatch final : public ConstraintFix {
38703874
}
38713875
};
38723876

3877+
class AllowVectorLiteralCountMismatch final : public ConstraintFix {
3878+
Type lhsCount, rhsCount;
3879+
3880+
AllowVectorLiteralCountMismatch(ConstraintSystem &cs, Type lhsCount,
3881+
Type rhsCount, ConstraintLocator *locator)
3882+
: ConstraintFix(cs, FixKind::AllowVectorLiteralCountMismatch, locator),
3883+
lhsCount(lhsCount), rhsCount(rhsCount) {}
3884+
3885+
public:
3886+
std::string getName() const override {
3887+
return "allow vector literal count mismatch";
3888+
}
3889+
3890+
bool diagnose(const Solution &solution, bool asNote = false) const override;
3891+
3892+
static AllowVectorLiteralCountMismatch *
3893+
create(ConstraintSystem &cs, Type lhsCount, Type rhsCount,
3894+
ConstraintLocator *locator);
3895+
3896+
static bool classof(const ConstraintFix *fix) {
3897+
return fix->getKind() == FixKind::AllowVectorLiteralCountMismatch;
3898+
}
3899+
};
3900+
38733901
} // end namespace constraints
38743902
} // end namespace swift
38753903

lib/AST/ASTDumper.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,6 +1192,7 @@ namespace {
11921192
break;
11931193
case GenericTypeParamKind::Value:
11941194
printField((StringRef)"value", "param_kind");
1195+
printRec(decl->getValueType(), "value_type");
11951196
break;
11961197
}
11971198

lib/AST/GenericParamList.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "swift/AST/GenericParamList.h"
1818

1919
#include "swift/AST/ASTContext.h"
20+
#include "swift/AST/TypeCheckRequests.h"
2021
#include "swift/AST/TypeRepr.h"
2122
#include "swift/Basic/Assertions.h"
2223

@@ -78,6 +79,12 @@ GenericParamList::clone(DeclContext *dc) const {
7879
dc, param->getName(), GenericTypeParamDecl::InvalidDepth,
7980
param->getIndex(), param->getParamKind(), param->getOpaqueTypeRepr());
8081
newParam->setInherited(param->getInherited().getEntries());
82+
83+
// Cache the value type computed from the previous param to the new one.
84+
ctx.evaluator.cacheOutput(
85+
GenericTypeParamDeclGetValueTypeRequest{newParam},
86+
param->getValueType());
87+
8188
params.push_back(newParam);
8289
}
8390

lib/IRGen/GenArray.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,15 @@ class FixedArrayTypeInfoBase : public ArrayTypeInfoBase<BaseTypeInfo> {
206206
uint64_t paddingBytes = elementTI.getFixedStride().getValue()
207207
- elementTI.getFixedSize().getValue();
208208
auto byteTy = llvm::IntegerType::get(LLVMContext, 8);
209-
elementTy = llvm::StructType::get(LLVMContext,
210-
{elementTy,
211-
llvm::ArrayType::get(byteTy, paddingBytes)},
212-
/*packed*/ true);
209+
auto paddingArrayTy = llvm::ArrayType::get(byteTy, paddingBytes);
210+
211+
if (elementTI.getFixedSize() == Size(0)) {
212+
elementTy = paddingArrayTy;
213+
} else {
214+
elementTy = llvm::StructType::get(LLVMContext,
215+
{elementTy, paddingArrayTy},
216+
/*packed*/ true);
217+
}
213218
}
214219

215220
return llvm::ArrayType::get(elementTy, arraySize);

lib/SIL/IR/TypeLowering.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3305,6 +3305,14 @@ void TypeConverter::verifyTrivialLowering(const TypeLowering &lowering,
33053305
return !isTopLevel;
33063306
}
33073307

3308+
// If we've gotten this far with a Builtin.FixedArray type, then the
3309+
// generic instantiation most likely contains a trivial type who
3310+
// doesn't conform either because it's non-frozen or we couldn't infer
3311+
// it somehow.
3312+
if (isa<BuiltinFixedArrayType>(ty)) {
3313+
return false;
3314+
}
3315+
33083316
// ReferenceStorageTypes with unmanaged ownership do not themselves
33093317
// conform (case (4)).
33103318
auto rst = dyn_cast<ReferenceStorageType>(ty);
@@ -3436,6 +3444,12 @@ void TypeConverter::verifyTrivialLowering(const TypeLowering &lowering,
34363444
return false;
34373445
}
34383446

3447+
// Builtin.FixedArray can be non-trivial and conform to
3448+
// BitwiseCopyable when operating with a generic Element type who is
3449+
// statically known to conform to BitwiseCopyable.
3450+
if (isa<BuiltinFixedArrayType>(ty))
3451+
return false;
3452+
34393453
return true;
34403454
});
34413455
if (hasNoConformingArchetypeNode) {

lib/SILGen/SILGenExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4635,7 +4635,7 @@ static RValue emitVectorLiteral(SILGenFunction &SGF, CollectionExpr *E,
46354635
vectorVal = SGF.B.createUncheckedAddrCast(E, alloc, loweredVectorType);
46364636
} else {
46374637
// Otherwise, this vector is loadable. Load it.
4638-
vectorVal = SGF.B.createLoad(E, alloc, LoadOwnershipQualifier::Trivial);
4638+
vectorVal = SGF.B.createTrivialLoadOr(E, alloc, LoadOwnershipQualifier::Take);
46394639
}
46404640

46414641
auto vectorMV = SGF.emitManagedRValueWithCleanup(vectorVal);

lib/Sema/CSDiagnostics.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9525,3 +9525,8 @@ bool InvalidTypeAsKeyPathSubscriptIndex::diagnoseAsError() {
95259525
emitDiagnostic(diag::cannot_convert_type_to_keypath_subscript_index, ArgType);
95269526
return true;
95279527
}
9528+
9529+
bool IncorrectVectorLiteralCount::diagnoseAsError() {
9530+
emitDiagnostic(diag::vector_literal_incorrect_count, lhsCount, rhsCount);
9531+
return true;
9532+
}

lib/Sema/CSDiagnostics.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3239,6 +3239,24 @@ class InvalidTypeAsKeyPathSubscriptIndex final : public FailureDiagnostic {
32393239
bool diagnoseAsError() override;
32403240
};
32413241

3242+
/// Diagnose when a vector literal has an incorrect number of elements for the
3243+
/// contextual vector type it's initializing.
3244+
///
3245+
/// \code
3246+
/// let x: Vector<4, Int> = [1, 2] // expected '4' elements but got '2'
3247+
/// \endcode
3248+
class IncorrectVectorLiteralCount final : public FailureDiagnostic {
3249+
Type lhsCount, rhsCount;
3250+
3251+
public:
3252+
IncorrectVectorLiteralCount(const Solution &solution, Type lhsCount,
3253+
Type rhsCount, ConstraintLocator *locator)
3254+
: FailureDiagnostic(solution, locator), lhsCount(resolveType(lhsCount)),
3255+
rhsCount(resolveType(rhsCount)) {}
3256+
3257+
bool diagnoseAsError() override;
3258+
};
3259+
32423260
} // end namespace constraints
32433261
} // end namespace swift
32443262

lib/Sema/CSFix.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2721,3 +2721,17 @@ IgnoreKeyPathSubscriptIndexMismatch::create(ConstraintSystem &cs, Type argType,
27212721
return new (cs.getAllocator())
27222722
IgnoreKeyPathSubscriptIndexMismatch(cs, argType, locator);
27232723
}
2724+
2725+
AllowVectorLiteralCountMismatch *
2726+
AllowVectorLiteralCountMismatch::create(ConstraintSystem &cs, Type lhsCount,
2727+
Type rhsCount,
2728+
ConstraintLocator *locator) {
2729+
return new (cs.getAllocator())
2730+
AllowVectorLiteralCountMismatch(cs, lhsCount, rhsCount, locator);
2731+
}
2732+
2733+
bool AllowVectorLiteralCountMismatch::diagnose(const Solution &solution,
2734+
bool asNote) const {
2735+
IncorrectVectorLiteralCount failure(solution, lhsCount, rhsCount, getLocator());
2736+
return failure.diagnose(asNote);
2737+
}

lib/Sema/CSGen.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,7 +1085,7 @@ namespace {
10851085
baseObjTy = baseObjTy->getWithoutSpecifierType();
10861086
}
10871087

1088-
if (baseObjTy->isArrayType()) {
1088+
if (auto elementTy = baseObjTy->isArrayType()) {
10891089

10901090
if (auto arraySliceTy =
10911091
dyn_cast<ArraySliceType>(baseObjTy.getPointer())) {
@@ -1095,7 +1095,7 @@ namespace {
10951095
if (argList->isUnlabeledUnary() &&
10961096
isa<IntegerLiteralExpr>(argList->getExpr(0))) {
10971097

1098-
outputTy = baseObjTy->getAs<BoundGenericType>()->getGenericArgs()[0];
1098+
outputTy = elementTy;
10991099

11001100
if (isLValueBase)
11011101
outputTy = LValueType::get(outputTy);
@@ -2125,8 +2125,7 @@ namespace {
21252125
};
21262126

21272127
// If a contextual type exists for this expression, apply it directly.
2128-
if (contextualType &&
2129-
(contextualType->isArrayType() || contextualType->isVector())) {
2128+
if (contextualType && contextualType->isArrayType()) {
21302129
// Now that we know we're actually going to use the type, get the
21312130
// version for use in a constraint.
21322131
contextualType = CS.getContextualType(expr, /*forConstraint=*/true);

lib/Sema/CSSimplify.cpp

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7619,7 +7619,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
76197619
// Special implicit nominal conversions.
76207620
if (!type1->is<LValueType>() && kind >= ConstraintKind::Subtype) {
76217621
// Array -> Array.
7622-
if (desugar1->isArrayType() && desugar2->isArrayType()) {
7622+
if (desugar1->isArray() && desugar2->isArray()) {
76237623
conversionsOrFixes.push_back(ConversionRestrictionKind::ArrayUpcast);
76247624
// Dictionary -> Dictionary.
76257625
} else if (isDictionaryType(desugar1) && isDictionaryType(desugar2)) {
@@ -7937,6 +7937,39 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
79377937
return getTypeMatchSuccess();
79387938
}
79397939

7940+
// Vector literals will attempt to convert element types to
7941+
// ExpressibleByArrayLiteral.ArrayLiteralElement.
7942+
if (kind == ConstraintKind::Conversion) {
7943+
if (auto dmt = desugar2->getAs<DependentMemberType>()) {
7944+
auto &ctx = getASTContext();
7945+
auto arrayProto =
7946+
ctx.getProtocol(KnownProtocolKind::ExpressibleByArrayLiteral);
7947+
if (!arrayProto)
7948+
return getTypeMatchFailure(locator);
7949+
7950+
// Assume that ExpressibleByArrayLiteral contains a single associated type.
7951+
auto *elementAssocTy = arrayProto->getAssociatedTypeMembers()[0];
7952+
if (!elementAssocTy)
7953+
return getTypeMatchFailure(locator);
7954+
7955+
// If this is the case, then just eagerly bind whatever our lhs type is
7956+
// to the element type of the Vector.
7957+
if (dmt->getAssocType() == elementAssocTy &&
7958+
dmt->getBase()->isVector()) {
7959+
auto vectorTy = dmt->getBase()->castTo<BoundGenericStructType>();
7960+
auto elementTy = vectorTy->getGenericArgs()[1];
7961+
assert(elementTy->isTypeVariableOrMember());
7962+
7963+
desugar1->dump();
7964+
desugar2->dump();
7965+
vectorTy->dump();
7966+
7967+
addConstraint(ConstraintKind::Bind, elementTy, desugar1, locator);
7968+
return getTypeMatchSuccess();
7969+
}
7970+
}
7971+
}
7972+
79407973
// Attempt fixes iff it's allowed, both types are concrete and
79417974
// we are not in the middle of attempting one already.
79427975
if (shouldAttemptFixes() && !flags.contains(TMF_ApplyingFix)) {
@@ -8578,7 +8611,12 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyConformsToConstraint(
85788611
}
85798612

85808613
// Otherwise this is an error and the counts aren't equal to each other.
8581-
// We'll make a fix to diagnose it as such.
8614+
if (!shouldAttemptFixes())
8615+
return SolutionKind::Error;
8616+
8617+
auto fix = AllowVectorLiteralCountMismatch::create(*this, contextualCount,
8618+
literalCount, loc);
8619+
return recordFix(fix) ? SolutionKind::Error : SolutionKind::Solved;
85828620
}
85838621
} break;
85848622

@@ -15518,6 +15556,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
1551815556
case FixKind::IgnoreInvalidPlaceholder:
1551915557
case FixKind::IgnoreOutOfPlaceThenStmt:
1552015558
case FixKind::IgnoreMissingEachKeyword:
15559+
case FixKind::AllowVectorLiteralCountMismatch:
1552115560
llvm_unreachable("handled elsewhere");
1552215561
}
1552315562

stdlib/public/core/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,6 @@ list(APPEND swift_stdlib_compile_flags "-enable-experimental-feature" "Macros")
316316
list(APPEND swift_stdlib_compile_flags "-enable-experimental-feature" "FreestandingMacros")
317317
list(APPEND swift_stdlib_compile_flags "-enable-experimental-feature" "Extern")
318318
list(APPEND swift_stdlib_compile_flags "-enable-experimental-feature" "BitwiseCopyable")
319-
list(APPEND swift_stdlib_compile_flags "-enable-experimental-feature" "RawLayout")
320319
list(APPEND swift_stdlib_compile_flags "-enable-experimental-feature" "ValueGenerics")
321320

322321
if("${SWIFT_NATIVE_SWIFT_TOOLS_PATH}" STREQUAL "")

0 commit comments

Comments
 (0)