Skip to content

[stdlib] Rename Slab to InlineArray #79294

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 6 commits into from
Feb 12, 2025
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
2 changes: 1 addition & 1 deletion Runtimes/Core/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ add_library(swiftCore
HashTable.swift
Identifiable.swift
Indices.swift
InlineArray.swift
InputStream.swift
IntegerParsing.swift
Integers.swift
Expand Down Expand Up @@ -145,7 +146,6 @@ add_library(swiftCore
SetVariant.swift
ShadowProtocols.swift
Shims.swift
Slab.swift
Slice.swift
SmallString.swift
Sort.swift
Expand Down
2 changes: 1 addition & 1 deletion include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -3994,7 +3994,7 @@ class GenericTypeParamDecl final
/// type parameter.
///
/// \code
/// struct Slab<let count: Int, Element: ~Copyable>
/// struct InlineArray<let count: Int, Element: ~Copyable>
/// \endcode
bool isValue() const {
return getParamKind() == GenericTypeParamKind::Value;
Expand Down
6 changes: 3 additions & 3 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -8212,11 +8212,11 @@ ERROR(invalid_value_for_type_same_type,none,
"cannot constrain type parameter %0 to be integer %1", (Type, Type))

//===----------------------------------------------------------------------===//
// MARK: Slab
// MARK: InlineArray
//===----------------------------------------------------------------------===//

ERROR(slab_literal_incorrect_count,none,
"expected %0 elements in slab literal, but got %1", (Type, Type))
ERROR(inlinearray_literal_incorrect_count,none,
"expected %0 elements in inline array literal, but got %1", (Type, Type))

//===----------------------------------------------------------------------===//
// MARK: @abi Attribute
Expand Down
2 changes: 1 addition & 1 deletion include/swift/AST/KnownStdlibTypes.def
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,6 @@ KNOWN_STDLIB_TYPE_DECL(DecodingError, NominalTypeDecl, 0)

KNOWN_STDLIB_TYPE_DECL(Result, NominalTypeDecl, 2)

KNOWN_STDLIB_TYPE_DECL(Slab, NominalTypeDecl, 2)
KNOWN_STDLIB_TYPE_DECL(InlineArray, NominalTypeDecl, 2)

#undef KNOWN_STDLIB_TYPE_DECL
2 changes: 1 addition & 1 deletion include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -7246,7 +7246,7 @@ class GenericTypeParamType : public SubstitutableType,
/// Returns \c true if this type parameter is declared as a value.
///
/// \code
/// struct Slab<let count: Int, Element: ~Copyable>
/// struct InlineArray<let count: Int, Element: ~Copyable>
/// \endcode
bool isValue() const {
return ParamKind == GenericTypeParamKind::Value;
Expand Down
16 changes: 8 additions & 8 deletions include/swift/Sema/CSFix.h
Original file line number Diff line number Diff line change
Expand Up @@ -484,9 +484,9 @@ enum class FixKind : uint8_t {
/// result.
AllowSendingMismatch,

/// Ignore when a 'Slab' literal has mismatched number of elements to the
/// type it's attempting to bind to.
AllowSlabLiteralCountMismatch,
/// Ignore when an 'InlineArray' literal has mismatched number of elements to
/// the type it's attempting to bind to.
AllowInlineArrayLiteralCountMismatch,
};

class ConstraintFix {
Expand Down Expand Up @@ -3841,12 +3841,12 @@ class IgnoreKeyPathSubscriptIndexMismatch final : public ConstraintFix {
}
};

class AllowSlabLiteralCountMismatch final : public ConstraintFix {
class AllowInlineArrayLiteralCountMismatch final : public ConstraintFix {
Type lhsCount, rhsCount;

AllowSlabLiteralCountMismatch(ConstraintSystem &cs, Type lhsCount,
AllowInlineArrayLiteralCountMismatch(ConstraintSystem &cs, Type lhsCount,
Type rhsCount, ConstraintLocator *locator)
: ConstraintFix(cs, FixKind::AllowSlabLiteralCountMismatch, locator),
: ConstraintFix(cs, FixKind::AllowInlineArrayLiteralCountMismatch, locator),
lhsCount(lhsCount), rhsCount(rhsCount) {}

public:
Expand All @@ -3856,12 +3856,12 @@ class AllowSlabLiteralCountMismatch final : public ConstraintFix {

bool diagnose(const Solution &solution, bool asNote = false) const override;

static AllowSlabLiteralCountMismatch *
static AllowInlineArrayLiteralCountMismatch *
create(ConstraintSystem &cs, Type lhsCount, Type rhsCount,
ConstraintLocator *locator);

static bool classof(const ConstraintFix *fix) {
return fix->getKind() == FixKind::AllowSlabLiteralCountMismatch;
return fix->getKind() == FixKind::AllowInlineArrayLiteralCountMismatch;
}
};

Expand Down
2 changes: 1 addition & 1 deletion lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ Type TypeBase::isArrayType() {
if (isArray())
return boundStruct->getGenericArgs()[0];

if (isSlab())
if (isInlineArray())
return boundStruct->getGenericArgs()[1];
}
return Type();
Expand Down
2 changes: 1 addition & 1 deletion lib/Parse/ParseType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ ParserStatus Parser::parseGenericArguments(SmallVectorImpl<TypeRepr *> &Args,
// variadic generic types.
if (!startsWithGreater(Tok)) {
while (true) {
// Note: This can be a value type, e.g. 'Slab<3, Int>'.
// Note: This can be a value type, e.g. 'InlineArray<3, Int>'.
ParserResult<TypeRepr> Ty = parseTypeOrValue(diag::expected_type);
if (Ty.isNull() || Ty.hasCodeCompletion()) {
// Skip until we hit the '>'.
Expand Down
38 changes: 19 additions & 19 deletions lib/SILGen/SILGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4600,17 +4600,17 @@ visitMagicIdentifierLiteralExpr(MagicIdentifierLiteralExpr *E, SGFContext C) {
llvm_unreachable("Unhandled MagicIdentifierLiteralExpr in switch.");
}

static RValue emitSlabLiteral(SILGenFunction &SGF, CollectionExpr *E,
static RValue emitInlineArrayLiteral(SILGenFunction &SGF, CollectionExpr *E,
SGFContext C) {
ArgumentScope scope(SGF, E);

auto slabType = E->getType()->castTo<BoundGenericType>();
auto loweredSlabType = SGF.getLoweredType(slabType);
auto elementType = slabType->getGenericArgs()[1]->getCanonicalType();
auto iaTy = E->getType()->castTo<BoundGenericType>();
auto loweredIAType = SGF.getLoweredType(iaTy);
auto elementType = iaTy->getGenericArgs()[1]->getCanonicalType();
auto loweredElementType = SGF.getLoweredType(elementType);
auto &eltTL = SGF.getTypeLowering(AbstractionPattern::getOpaque(), elementType);

SILValue alloc = SGF.emitTemporaryAllocation(E, loweredSlabType);
SILValue alloc = SGF.emitTemporaryAllocation(E, loweredIAType);
SILValue addr = SGF.B.createUncheckedAddrCast(E, alloc,
loweredElementType.getAddressType());

Expand Down Expand Up @@ -4643,31 +4643,31 @@ static RValue emitSlabLiteral(SILGenFunction &SGF, CollectionExpr *E,
.forwardInto(SGF, AbstractionPattern::getOpaque(), &init, eltTL);
}

// Kill the per-element cleanups. The slab will take ownership of them.
// Kill the per-element cleanups. The inline array will take ownership of them.
for (auto destCleanup : cleanups)
SGF.Cleanups.setCleanupState(destCleanup, CleanupState::Dead);

SILValue slabVal;
SILValue iaVal;

// If the slab is naturally address-only, then we can adopt the stack slot
// as the value directly.
if (loweredSlabType.isAddressOnly(SGF.F)) {
slabVal = SGF.B.createUncheckedAddrCast(E, alloc, loweredSlabType);
// If the inline array is naturally address-only, then we can adopt the stack
// slot as the value directly.
if (loweredIAType.isAddressOnly(SGF.F)) {
iaVal = SGF.B.createUncheckedAddrCast(E, alloc, loweredIAType);
} else {
// Otherwise, this slab is loadable. Load it.
slabVal = SGF.B.createTrivialLoadOr(E, alloc, LoadOwnershipQualifier::Take);
// Otherwise, this inline array is loadable. Load it.
iaVal = SGF.B.createTrivialLoadOr(E, alloc, LoadOwnershipQualifier::Take);
}

auto slabMV = SGF.emitManagedRValueWithCleanup(slabVal);
auto slab = RValue(SGF, E, slabMV);
auto iaMV = SGF.emitManagedRValueWithCleanup(iaVal);
auto ia = RValue(SGF, E, iaMV);

return scope.popPreservingValue(std::move(slab));
return scope.popPreservingValue(std::move(ia));
}

RValue RValueEmitter::visitCollectionExpr(CollectionExpr *E, SGFContext C) {
// Handle 'Slab' literals separately.
if (E->getType()->isSlab()) {
return emitSlabLiteral(SGF, E, C);
// Handle 'InlineArray' literals separately.
if (E->getType()->isInlineArray()) {
return emitInlineArrayLiteral(SGF, E, C);
}

auto loc = SILLocation(E);
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3804,7 +3804,7 @@ namespace {
Type arrayTy = cs.getType(expr);
Type elementType;

if (arrayTy->isSlab()) {
if (arrayTy->isInlineArray()) {
// <let count: Int, Element>
elementType = arrayTy->castTo<BoundGenericStructType>()->getGenericArgs()[1];
} else {
Expand Down
4 changes: 2 additions & 2 deletions lib/Sema/CSDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9572,7 +9572,7 @@ bool InvalidTypeAsKeyPathSubscriptIndex::diagnoseAsError() {
return true;
}

bool IncorrectSlabLiteralCount::diagnoseAsError() {
emitDiagnostic(diag::slab_literal_incorrect_count, lhsCount, rhsCount);
bool IncorrectInlineArrayLiteralCount::diagnoseAsError() {
emitDiagnostic(diag::inlinearray_literal_incorrect_count, lhsCount, rhsCount);
return true;
}
10 changes: 5 additions & 5 deletions lib/Sema/CSDiagnostics.h
Original file line number Diff line number Diff line change
Expand Up @@ -3234,17 +3234,17 @@ class InvalidTypeAsKeyPathSubscriptIndex final : public FailureDiagnostic {
bool diagnoseAsError() override;
};

/// Diagnose when a slab literal has an incorrect number of elements for the
/// contextual slab type it's initializing.
/// Diagnose when an inline array literal has an incorrect number of elements
/// for the contextual inline array type it's initializing.
///
/// \code
/// let x: Slab<4, Int> = [1, 2] // expected '4' elements but got '2'
/// let x: InlineArray<4, Int> = [1, 2] // expected '4' elements but got '2'
/// \endcode
class IncorrectSlabLiteralCount final : public FailureDiagnostic {
class IncorrectInlineArrayLiteralCount final : public FailureDiagnostic {
Type lhsCount, rhsCount;

public:
IncorrectSlabLiteralCount(const Solution &solution, Type lhsCount,
IncorrectInlineArrayLiteralCount(const Solution &solution, Type lhsCount,
Type rhsCount, ConstraintLocator *locator)
: FailureDiagnostic(solution, locator), lhsCount(resolveType(lhsCount)),
rhsCount(resolveType(rhsCount)) {}
Expand Down
17 changes: 9 additions & 8 deletions lib/Sema/CSFix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2723,16 +2723,17 @@ IgnoreKeyPathSubscriptIndexMismatch::create(ConstraintSystem &cs, Type argType,
IgnoreKeyPathSubscriptIndexMismatch(cs, argType, locator);
}

AllowSlabLiteralCountMismatch *
AllowSlabLiteralCountMismatch::create(ConstraintSystem &cs, Type lhsCount,
Type rhsCount,
ConstraintLocator *locator) {
AllowInlineArrayLiteralCountMismatch *
AllowInlineArrayLiteralCountMismatch::create(ConstraintSystem &cs, Type lhsCount,
Type rhsCount,
ConstraintLocator *locator) {
return new (cs.getAllocator())
AllowSlabLiteralCountMismatch(cs, lhsCount, rhsCount, locator);
AllowInlineArrayLiteralCountMismatch(cs, lhsCount, rhsCount, locator);
}

bool AllowSlabLiteralCountMismatch::diagnose(const Solution &solution,
bool asNote) const {
IncorrectSlabLiteralCount failure(solution, lhsCount, rhsCount, getLocator());
bool AllowInlineArrayLiteralCountMismatch::diagnose(const Solution &solution,
bool asNote) const {
IncorrectInlineArrayLiteralCount failure(solution, lhsCount, rhsCount,
getLocator());
return failure.diagnose(asNote);
}
21 changes: 11 additions & 10 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8709,30 +8709,30 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyConformsToConstraint(
auto anchor = loc->getAnchor();
auto arrayLiteral = getAsExpr<ArrayExpr>(anchor);

// If we're attempting to bind an array literal to a 'Slab' parameter,
// then check if the counts are equal and solve.
// If we're attempting to bind an array literal to an 'InlineArray'
// parameter, then check if the counts are equal and solve.
if (kind == ConstraintKind::LiteralConformsTo &&
protocol == arrayLiteralProto &&
type->isSlab() &&
type->isInlineArray() &&
arrayLiteral) {
auto slabTy = type->castTo<BoundGenericStructType>();
auto iaTy = type->castTo<BoundGenericStructType>();

// <let count: Int, Element>
// Attempt to bind the number of elements in the literal with the
// contextual count. This will diagnose if the literal does not enough
// or too many elements.
auto contextualCount = slabTy->getGenericArgs()[0];
auto contextualCount = iaTy->getGenericArgs()[0];
auto literalCount = IntegerType::get(
std::to_string(arrayLiteral->getNumElements()),
/* isNegative */ false,
slabTy->getASTContext());
iaTy->getASTContext());

// If the counts are already equal, '2' == '2', then we're done.
if (contextualCount->isEqual(literalCount)) {
return SolutionKind::Solved;
}

// If our contextual count is not known, e.g., Slab<_, Int> = [1, 2],
// If our contextual count is not known, e.g., InlineArray<_, Int> = [1, 2],
// then just eagerly bind the count to what the literal count is.
if (contextualCount->isTypeVariableOrMember()) {
addConstraint(ConstraintKind::Bind, contextualCount, literalCount,
Expand All @@ -8744,8 +8744,9 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyConformsToConstraint(
if (!shouldAttemptFixes())
return SolutionKind::Error;

auto fix = AllowSlabLiteralCountMismatch::create(*this, contextualCount,
literalCount, loc);
auto fix = AllowInlineArrayLiteralCountMismatch::create(*this,
contextualCount,
literalCount, loc);
return recordFix(fix) ? SolutionKind::Error : SolutionKind::Solved;
}
} break;
Expand Down Expand Up @@ -15640,7 +15641,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
case FixKind::IgnoreInvalidPlaceholder:
case FixKind::IgnoreOutOfPlaceThenStmt:
case FixKind::IgnoreMissingEachKeyword:
case FixKind::AllowSlabLiteralCountMismatch:
case FixKind::AllowInlineArrayLiteralCountMismatch:
llvm_unreachable("handled elsewhere");
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1669,7 +1669,7 @@ struct TypeSimplifier {
// Special case: When building slab literals, we go through the same
// array literal machinery, so there will be a conversion constraint
// for the element to ExpressibleByArrayLiteral.ArrayLiteralType.
if (lookupBaseType->isSlab()) {
if (lookupBaseType->isInlineArray()) {
auto &ctx = CS.getASTContext();
auto arrayProto =
ctx.getProtocol(KnownProtocolKind::ExpressibleByArrayLiteral);
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ split_embedded_sources(
EMBEDDED HashTable.swift
EMBEDDED Identifiable.swift
EMBEDDED Indices.swift
EMBEDDED InlineArray.swift
EMBEDDED InputStream.swift
EMBEDDED IntegerParsing.swift
EMBEDDED Integers.swift
Expand Down Expand Up @@ -151,7 +152,6 @@ split_embedded_sources(
EMBEDDED SetVariant.swift
EMBEDDED ShadowProtocols.swift
NORMAL Shims.swift
EMBEDDED Slab.swift
EMBEDDED Slice.swift
EMBEDDED SmallString.swift
EMBEDDED Sort.swift
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/GroupInfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@
"EmbeddedRuntime.swift",
"EmbeddedStubs.swift",
"EmbeddedPrint.swift",
"Slab.swift"
"InlineArray.swift"
],
"Result": [
"Result.swift"
Expand Down
Loading