Skip to content

Commit 9023f30

Browse files
committed
WIP
1 parent 9013032 commit 9023f30

15 files changed

+171
-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: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7622,7 +7622,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
76227622
// Special implicit nominal conversions.
76237623
if (!type1->is<LValueType>() && kind >= ConstraintKind::Subtype) {
76247624
// Array -> Array.
7625-
if (desugar1->isArrayType() && desugar2->isArrayType()) {
7625+
if (desugar1->isArray() && desugar2->isArray()) {
76267626
conversionsOrFixes.push_back(ConversionRestrictionKind::ArrayUpcast);
76277627
// Dictionary -> Dictionary.
76287628
} else if (isDictionaryType(desugar1) && isDictionaryType(desugar2)) {
@@ -8581,7 +8581,12 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyConformsToConstraint(
85818581
}
85828582

85838583
// Otherwise this is an error and the counts aren't equal to each other.
8584-
// We'll make a fix to diagnose it as such.
8584+
if (!shouldAttemptFixes())
8585+
return SolutionKind::Error;
8586+
8587+
auto fix = AllowVectorLiteralCountMismatch::create(*this, contextualCount,
8588+
literalCount, loc);
8589+
return recordFix(fix) ? SolutionKind::Error : SolutionKind::Solved;
85858590
}
85868591
} break;
85878592

@@ -15521,6 +15526,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
1552115526
case FixKind::IgnoreInvalidPlaceholder:
1552215527
case FixKind::IgnoreOutOfPlaceThenStmt:
1552315528
case FixKind::IgnoreMissingEachKeyword:
15529+
case FixKind::AllowVectorLiteralCountMismatch:
1552415530
llvm_unreachable("handled elsewhere");
1552515531
}
1552615532

lib/Sema/ConstraintSystem.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1652,6 +1652,20 @@ struct TypeSimplifier {
16521652
auto *proto = assocType->getProtocol();
16531653
auto conformance = CS.lookupConformance(lookupBaseType, proto);
16541654
if (!conformance) {
1655+
// Special case: When building vector literals, we go through the same
1656+
// array literal machinery, so there will be a conversion constraint
1657+
// for the element to ExpressibleByArrayLiteral.ArrayLiteralType.
1658+
if (lookupBaseType->isVector()) {
1659+
auto &ctx = CS.getASTContext();
1660+
auto arrayProto =
1661+
ctx.getProtocol(KnownProtocolKind::ExpressibleByArrayLiteral);
1662+
auto elementAssocTy = arrayProto->getAssociatedTypeMembers()[0];
1663+
1664+
if (proto == arrayProto && assocType == elementAssocTy) {
1665+
return lookupBaseType->isArrayType();
1666+
}
1667+
}
1668+
16551669
// If the base type doesn't conform to the associatedtype's protocol,
16561670
// there will be a missing conformance fix applied in diagnostic mode,
16571671
// so the concrete dependent member type is considered a "hole" in

stdlib/public/core/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,6 @@ list(APPEND swift_stdlib_compile_flags "-enable-experimental-feature" "Macros")
318318
list(APPEND swift_stdlib_compile_flags "-enable-experimental-feature" "FreestandingMacros")
319319
list(APPEND swift_stdlib_compile_flags "-enable-experimental-feature" "Extern")
320320
list(APPEND swift_stdlib_compile_flags "-enable-experimental-feature" "BitwiseCopyable")
321-
list(APPEND swift_stdlib_compile_flags "-enable-experimental-feature" "RawLayout")
322321
list(APPEND swift_stdlib_compile_flags "-enable-experimental-feature" "ValueGenerics")
323322

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

0 commit comments

Comments
 (0)