Skip to content

Commit e68b398

Browse files
committed
Rename isArrayType and split the InlineArray portion
1 parent ea74546 commit e68b398

File tree

13 files changed

+71
-38
lines changed

13 files changed

+71
-38
lines changed

include/swift/AST/Types.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -987,9 +987,13 @@ class alignas(1 << TypeAlignInBits) TypeBase
987987
/// type) from `DistributedActor`.
988988
bool isDistributedActor();
989989

990-
/// Determine if the type in question is an Array<T> and, if so, provide the
990+
/// Determine if this type is an Array<T> and, if so, provide the element type
991+
/// of the array.
992+
Type getArrayElementType();
993+
994+
/// Determine if this type is an InlineArray<n, T> and, if so, provide the
991995
/// element type of the array.
992-
Type isArrayType();
996+
Type getInlineArrayElementType();
993997

994998
/// Determines the element type of a known
995999
/// [Autoreleasing]Unsafe[Mutable][Raw]Pointer variant, or returns null if the

lib/AST/Type.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -829,15 +829,22 @@ CanType CanType::wrapInOptionalTypeImpl(CanType type) {
829829
return type->wrapInOptionalType()->getCanonicalType();
830830
}
831831

832-
Type TypeBase::isArrayType() {
833-
if (auto boundStruct = getAs<BoundGenericStructType>()) {
834-
if (isArray())
835-
return boundStruct->getGenericArgs()[0];
832+
Type TypeBase::getArrayElementType() {
833+
if (!isArray())
834+
return Type();
836835

837-
if (isInlineArray())
838-
return boundStruct->getGenericArgs()[1];
839-
}
840-
return Type();
836+
// Array<T>
837+
auto boundStruct = castTo<BoundGenericStructType>();
838+
return boundStruct->getGenericArgs()[0];
839+
}
840+
841+
Type TypeBase::getInlineArrayElementType() {
842+
if (!isInlineArray())
843+
return Type();
844+
845+
// InlineArray<n, T>
846+
auto boundStruct = castTo<BoundGenericStructType>();
847+
return boundStruct->getGenericArgs()[1];
841848
}
842849

843850
Type TypeBase::getAnyPointerElementType(PointerTypeKind &PTK) {

lib/SILGen/SILGenExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6680,7 +6680,7 @@ static void diagnoseImplicitRawConversion(Type sourceTy, Type pointerTy,
66806680
// Array conversion does not always go down the ArrayConverter
66816681
// path. Recognize the Array source type here both for ArrayToPointer and
66826682
// InoutToPointer cases and diagnose on the element type.
6683-
Type eltTy = sourceTy->isArrayType();
6683+
Type eltTy = sourceTy->getArrayElementType();
66846684
if (!eltTy)
66856685
eltTy = sourceTy;
66866686

lib/Sema/CSApply.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6891,7 +6891,7 @@ bool ExprRewriter::peepholeCollectionUpcast(Expr *expr, Type toType,
68916891

68926892
// Array literals.
68936893
if (auto arrayLiteral = dyn_cast<ArrayExpr>(expr)) {
6894-
if (Type elementType = toType->isArrayType()) {
6894+
if (auto elementType = toType->getArrayElementType()) {
68956895
peepholeArrayUpcast(arrayLiteral, toType, bridged, elementType, locator);
68966896
return true;
68976897
}

lib/Sema/CSBindings.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1385,7 +1385,7 @@ static bool hasConversions(Type type) {
13851385
return true;
13861386

13871387
if (auto *structTy = type->getAs<BoundGenericStructType>()) {
1388-
if (auto eltTy = structTy->isArrayType()) {
1388+
if (auto eltTy = structTy->getArrayElementType()) {
13891389
return hasConversions(eltTy);
13901390
} else if (auto pair = ConstraintSystem::isDictionaryType(structTy)) {
13911391
return hasConversions(pair->second);

lib/Sema/CSDiagnostics.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1979,7 +1979,7 @@ bool RValueTreatedAsLValueFailure::diagnoseAsError() {
19791979
auto argType = getType(inoutExpr)->getWithoutSpecifierType();
19801980

19811981
PointerTypeKind ptr;
1982-
if (argType->isArrayType() && paramType->getAnyPointerElementType(ptr)
1982+
if (argType->isArray() && paramType->getAnyPointerElementType(ptr)
19831983
&& (ptr == PTK_UnsafePointer || ptr == PTK_UnsafeRawPointer)) {
19841984
emitDiagnosticAt(inoutExpr->getLoc(),
19851985
diag::extra_address_of_unsafepointer, paramType)

lib/Sema/CSFix.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ TreatArrayLiteralAsDictionary *
219219
TreatArrayLiteralAsDictionary::attempt(ConstraintSystem &cs, Type dictionaryTy,
220220
Type arrayTy,
221221
ConstraintLocator *locator) {
222-
if (!arrayTy->isArrayType())
222+
if (!arrayTy->isArray())
223223
return nullptr;
224224

225225
// Determine the ArrayExpr from the locator.
@@ -1814,7 +1814,7 @@ ExpandArrayIntoVarargs::attempt(ConstraintSystem &cs, Type argType,
18141814
if (!(argLoc && argLoc->getParameterFlags().isVariadic()))
18151815
return nullptr;
18161816

1817-
auto elementType = argType->isArrayType();
1817+
auto elementType = argType->getArrayElementType();
18181818
if (!elementType)
18191819
return nullptr;
18201820

lib/Sema/CSGen.cpp

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

1088-
if (auto elementTy = baseObjTy->isArrayType()) {
1088+
auto elementTy = baseObjTy->getArrayElementType();
1089+
1090+
if (!elementTy)
1091+
elementTy = baseObjTy->getInlineArrayElementType();
1092+
1093+
if (elementTy) {
10891094

10901095
if (auto arraySliceTy =
10911096
dyn_cast<ArraySliceType>(baseObjTy.getPointer())) {
@@ -2151,18 +2156,27 @@ namespace {
21512156
};
21522157

21532158
// If a contextual type exists for this expression, apply it directly.
2154-
if (contextualType && contextualType->isArrayType()) {
2159+
if (contextualType &&
2160+
(contextualType->isArray() || contextualType->isInlineArray())) {
21552161
// Now that we know we're actually going to use the type, get the
21562162
// version for use in a constraint.
21572163
contextualType = CS.getContextualType(expr, /*forConstraint=*/true);
21582164
// FIXME: This is the wrong place to be opening the opaque type.
21592165
contextualType = CS.openOpaqueType(
21602166
contextualType, contextualPurpose, locator, /*ownerDecl=*/nullptr);
2161-
Type arrayElementType = contextualType->isArrayType();
2167+
2168+
Type eltType;
2169+
2170+
if (contextualType->isArray())
2171+
eltType = contextualType->getArrayElementType();
2172+
2173+
if (contextualType->isInlineArray())
2174+
eltType = contextualType->getInlineArrayElementType();
2175+
21622176
CS.addConstraint(ConstraintKind::LiteralConformsTo, contextualType,
21632177
arrayProto->getDeclaredInterfaceType(),
21642178
locator);
2165-
joinElementTypes(arrayElementType);
2179+
joinElementTypes(eltType);
21662180
return contextualType;
21672181
}
21682182

lib/Sema/CSSimplify.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6018,7 +6018,7 @@ bool ConstraintSystem::repairFailures(
60186018
// ```
60196019
if (rhs->isKnownStdlibCollectionType()) {
60206020
std::function<Type(Type)> getArrayOrSetType = [&](Type type) -> Type {
6021-
if (auto eltTy = type->isArrayType())
6021+
if (auto eltTy = type->getArrayElementType())
60226022
return getArrayOrSetType(eltTy);
60236023

60246024
if (auto eltTy = isSetType(type))
@@ -7992,7 +7992,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
79927992
if (inoutBaseType->isTypeVariableOrMember())
79937993
return formUnsolvedResult();
79947994

7995-
auto baseIsArray = inoutBaseType->isArrayType();
7995+
auto baseIsArray = inoutBaseType->isArray();
79967996

79977997
if (baseIsArray)
79987998
conversionsOrFixes.push_back(
@@ -8061,7 +8061,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
80618061
if (pointerKind == PTK_UnsafePointer
80628062
|| pointerKind == PTK_UnsafeRawPointer) {
80638063
if (!isAutoClosureArgument) {
8064-
if (type1->isArrayType()) {
8064+
if (type1->isArray()) {
80658065
conversionsOrFixes.push_back(
80668066
ConversionRestrictionKind::ArrayToPointer);
80678067

@@ -9263,7 +9263,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyTransitivelyConformsTo(
92639263
}
92649264

92659265
// Array<T> -> Unsafe{Raw}Pointer<T>
9266-
if (auto elt = resolvedTy->isArrayType()) {
9266+
if (auto elt = resolvedTy->getArrayElementType()) {
92679267
typesToCheck.push_back(getPointerFor(PTK_UnsafePointer, elt));
92689268
typesToCheck.push_back(getPointerFor(PTK_UnsafeRawPointer, elt));
92699269
}
@@ -9294,7 +9294,7 @@ static CheckedCastKind getCheckedCastKind(ConstraintSystem *cs,
92949294
Type fromType,
92959295
Type toType) {
92969296
// Array downcasts are handled specially.
9297-
if (fromType->isArrayType() && toType->isArrayType()) {
9297+
if (fromType->isArray() && toType->isArray()) {
92989298
return CheckedCastKind::ArrayDowncast;
92999299
}
93009300

@@ -9558,8 +9558,8 @@ ConstraintSystem::simplifyCheckedCastConstraint(
95589558
auto kind = getCheckedCastKind(this, fromType, toType);
95599559
switch (kind) {
95609560
case CheckedCastKind::ArrayDowncast: {
9561-
auto fromBaseType = fromType->isArrayType();
9562-
auto toBaseType = toType->isArrayType();
9561+
auto fromBaseType = fromType->getArrayElementType();
9562+
auto toBaseType = toType->getArrayElementType();
95639563

95649564
auto elementLocator =
95659565
locator.withPathElement(LocatorPathElt::GenericArgument(0));
@@ -12558,8 +12558,8 @@ ConstraintSystem::simplifyBridgingConstraint(Type type1,
1255812558
};
1255912559

1256012560
// Bridging the elements of an array.
12561-
if (auto fromElement = unwrappedFromType->isArrayType()) {
12562-
if (auto toElement = unwrappedToType->isArrayType()) {
12561+
if (auto fromElement = unwrappedFromType->getArrayElementType()) {
12562+
if (auto toElement = unwrappedToType->getArrayElementType()) {
1256312563
countOptionalInjections();
1256412564
auto result = simplifyBridgingConstraint(
1256512565
fromElement, toElement, subflags,
@@ -14838,7 +14838,7 @@ ConstraintSystem::simplifyRestrictedConstraintImpl(
1483814838

1483914839
auto t2 = type2->getDesugaredType();
1484014840

14841-
auto baseType1 = getFixedTypeRecursive(obj1->isArrayType(), false);
14841+
auto baseType1 = getFixedTypeRecursive(obj1->getArrayElementType(), false);
1484214842
auto ptr2 = getBaseTypeForPointer(t2);
1484314843

1484414844
increaseScore(SK_ValueToOptional, locator, ptr2.getInt());
@@ -14941,7 +14941,7 @@ ConstraintSystem::simplifyRestrictedConstraintImpl(
1494114941

1494214942
increaseScore(SK_ValueToPointerConversion, locator);
1494314943

14944-
type1 = getFixedTypeRecursive(type1->getInOutObjectType()->isArrayType(),
14944+
type1 = getFixedTypeRecursive(type1->getInOutObjectType()->getArrayElementType(),
1494514945
/*wantRValue=*/false);
1494614946
LLVM_FALLTHROUGH;
1494714947
}
@@ -14977,8 +14977,8 @@ ConstraintSystem::simplifyRestrictedConstraintImpl(
1497714977

1497814978
// T < U or T is bridged to V where V < U ===> Array<T> <c Array<U>
1497914979
case ConversionRestrictionKind::ArrayUpcast: {
14980-
Type baseType1 = type1->isArrayType();
14981-
Type baseType2 = type2->isArrayType();
14980+
Type baseType1 = type1->getArrayElementType();
14981+
Type baseType2 = type2->getArrayElementType();
1498214982

1498314983
increaseScore(SK_CollectionUpcastConversion, locator);
1498414984
return matchTypes(baseType1,
@@ -15774,7 +15774,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
1577415774
auto dictionaryKeyTy = DependentMemberType::get(valueBaseTy, keyAssocTy);
1577515775

1577615776
// Extract the array element type.
15777-
auto elemTy = type1->isArrayType();
15777+
auto elemTy = type1->getArrayElementType();
1577815778

1577915779
ConstraintLocator *elemLoc = getConstraintLocator(AE->getElement(0));
1578015780
ConstraintKind kind = isDictionaryType(dictTy)

lib/Sema/ConstraintSystem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1692,7 +1692,7 @@ struct TypeSimplifier {
16921692
auto elementAssocTy = arrayProto->getAssociatedTypeMembers()[0];
16931693

16941694
if (proto == arrayProto && assocType == elementAssocTy) {
1695-
return lookupBaseType->isArrayType();
1695+
return lookupBaseType->getInlineArrayElementType();
16961696
}
16971697
}
16981698

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1860,8 +1860,8 @@ TypeChecker::typeCheckCheckedCast(Type fromType, Type toType,
18601860
};
18611861

18621862
// Check for casts between specific concrete types that cannot succeed.
1863-
if (auto toElementType = toType->isArrayType()) {
1864-
if (auto fromElementType = fromType->isArrayType()) {
1863+
if (auto toElementType = toType->getArrayElementType()) {
1864+
if (auto fromElementType = fromType->getArrayElementType()) {
18651865
return checkElementCast(fromElementType, toElementType,
18661866
CheckedCastKind::ArrayDowncast);
18671867
}

test/Sema/inlinearray.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,11 @@ func testMismatches(_ x: [3 x Int], _ y: InlineArray<3, Int>) {
153153
let _: [3 x String] = y // expected-error {{cannot assign value of type 'InlineArray<3, Int>' to type '[3 x String]'}}
154154
// expected-note@-1 {{arguments to generic parameter 'Element' ('Int' and 'String') are expected to be equal}}
155155
}
156+
157+
func testPointerConversion() {
158+
var inlineArray = InlineArray<1, Int>(repeating: 0)
159+
acceptPointer(&inlineArray) // expected-error {{cannot convert value of type 'UnsafeMutablePointer<InlineArray<1, Int>>' to expected argument type 'UnsafeMutablePointer<Int>'}}
160+
// expected-note@-1 {{arguments to generic parameter 'Pointee' ('InlineArray<1, Int>' and 'Int') are expected to be equal}}
161+
}
162+
163+
func acceptPointer(_ pointer: UnsafeMutablePointer<Int>) {}

unittests/Sema/PlaceholderTypeInferenceTests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ TEST_F(SemaTest, TestPlaceholderInferenceForArrayLiteral) {
4949

5050
auto &solution = solutions[0];
5151

52-
auto eltTy = solution.simplifyType(solution.getType(arrayExpr))->isArrayType();
52+
auto eltTy = solution.simplifyType(solution.getType(arrayExpr))->getArrayElementType();
5353
ASSERT_TRUE(eltTy);
5454
ASSERT_TRUE(eltTy->is<StructType>());
5555
ASSERT_EQ(eltTy->getAs<StructType>()->getDecl(), intTypeDecl);

0 commit comments

Comments
 (0)