Skip to content

Commit 3d7f28b

Browse files
committed
Introduce type sugar for InlineArray
Parse e.g `[3 x Int]` as type sugar for InlineArray. Gated behind an experimental feature flag for now.
1 parent 8da8558 commit 3d7f28b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+658
-6
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2681,6 +2681,11 @@ BridgedImplicitlyUnwrappedOptionalTypeRepr_createParsed(
26812681
BridgedASTContext cContext, BridgedTypeRepr base,
26822682
BridgedSourceLoc cExclamationLoc);
26832683

2684+
SWIFT_NAME("BridgedInlineArrayTypeRepr.createParsed(_:count:element:brackets:)")
2685+
BridgedInlineArrayTypeRepr BridgedInlineArrayTypeRepr_createParsed(
2686+
BridgedASTContext cContext, BridgedTypeRepr cCountType,
2687+
BridgedTypeRepr cElementType, BridgedSourceRange cBracketsRange);
2688+
26842689
SWIFT_NAME("BridgedInverseTypeRepr.createParsed(_:tildeLoc:constraint:)")
26852690
BridgedInverseTypeRepr
26862691
BridgedInverseTypeRepr_createParsed(BridgedASTContext cContext,

include/swift/AST/DiagnosticsParse.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,10 @@ NOTE(subscript_array_element_fix_it_add_comma, none, "add a separator between "
878878
NOTE(subscript_array_element_fix_it_remove_space, none,
879879
"remove the space between the elements to silence this warning", ())
880880

881+
// Inline Array
882+
ERROR(expected_rsquare_inline_array,none,
883+
"expected ']' in inline array type", ())
884+
881885
// Tuple Types
882886
ERROR(expected_rparen_tuple_type_list,none,
883887
"expected ')' at end of tuple list", ())

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6059,7 +6059,7 @@ NOTE(note_recursive_enum_case_here,none,
60596059

60606060
ERROR(sugar_type_not_found,none,
60616061
"broken standard library: cannot find "
6062-
"%select{Array|Optional|ImplicitlyUnwrappedOptional|Dictionary|"
6062+
"%select{Array|Optional|InlineArray|Dictionary|"
60636063
"Error}0 type", (unsigned))
60646064
ERROR(optional_intrinsics_not_found,none,
60656065
"broken standard library: cannot find intrinsic operations on "
@@ -8283,6 +8283,9 @@ ERROR(invalid_value_for_type_same_type,none,
82838283
ERROR(inlinearray_literal_incorrect_count,none,
82848284
"expected %0 elements in inline array literal, but got %1", (Type, Type))
82858285

8286+
ERROR(inline_array_type_backwards,none,
8287+
"element count must precede inline array element type", ())
8288+
82868289
//===----------------------------------------------------------------------===//
82878290
// MARK: @abi Attribute
82888291
//===----------------------------------------------------------------------===//

include/swift/AST/PrintOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,10 @@ struct PrintOptions {
590590
/// Whether to always desugar array types from `[base_type]` to `Array<base_type>`
591591
bool AlwaysDesugarArraySliceTypes = false;
592592

593+
/// Whether to always desugar inline array types from
594+
/// `[<count> x <element>]` to `InlineArray<count, element>`
595+
bool AlwaysDesugarInlineArrayTypes = false;
596+
593597
/// Whether to always desugar dictionary types
594598
/// from `[key_type:value_type]` to `Dictionary<key_type,value_type>`
595599
bool AlwaysDesugarDictionaryTypes = false;

include/swift/AST/TypeNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ ABSTRACT_SUGARED_TYPE(Sugar, Type)
217217
SUGARED_TYPE(Optional, UnarySyntaxSugarType)
218218
SUGARED_TYPE(VariadicSequence, UnarySyntaxSugarType)
219219
TYPE_RANGE(UnarySyntaxSugar, ArraySlice, VariadicSequence)
220+
SUGARED_TYPE(InlineArray, SyntaxSugarType)
220221
SUGARED_TYPE(Dictionary, SyntaxSugarType)
221222
TYPE_RANGE(SyntaxSugar, ArraySlice, Dictionary)
222223
TYPE_RANGE(Sugar, TypeAlias, Dictionary)

include/swift/AST/TypeRepr.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,35 @@ class ArrayTypeRepr : public TypeRepr {
641641
friend class TypeRepr;
642642
};
643643

644+
/// An InlineArray type e.g `[2 x Foo]`, sugar for `InlineArray<2, Foo>`.
645+
class InlineArrayTypeRepr : public TypeRepr {
646+
TypeRepr *Count;
647+
TypeRepr *Element;
648+
SourceRange Brackets;
649+
650+
InlineArrayTypeRepr(TypeRepr *count, TypeRepr *element, SourceRange brackets)
651+
: TypeRepr(TypeReprKind::InlineArray), Count(count), Element(element),
652+
Brackets(brackets) {}
653+
654+
public:
655+
static InlineArrayTypeRepr *create(ASTContext &ctx, TypeRepr *count,
656+
TypeRepr *element, SourceRange brackets);
657+
658+
TypeRepr *getCount() const { return Count; }
659+
TypeRepr *getElement() const { return Element; }
660+
SourceRange getBrackets() const { return Brackets; }
661+
662+
static bool classof(const TypeRepr *T) {
663+
return T->getKind() == TypeReprKind::InlineArray;
664+
}
665+
666+
private:
667+
SourceLoc getStartLocImpl() const { return Brackets.Start; }
668+
SourceLoc getEndLocImpl() const { return Brackets.End; }
669+
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
670+
friend class TypeRepr;
671+
};
672+
644673
/// A dictionary type.
645674
/// \code
646675
/// [K : V]
@@ -1626,6 +1655,7 @@ inline bool TypeRepr::isSimple() const {
16261655
case TypeReprKind::Fixed:
16271656
case TypeReprKind::Self:
16281657
case TypeReprKind::Array:
1658+
case TypeReprKind::InlineArray:
16291659
case TypeReprKind::SILBox:
16301660
case TypeReprKind::Isolated:
16311661
case TypeReprKind::Sending:

include/swift/AST/TypeReprNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ ABSTRACT_TYPEREPR(DeclRef, TypeRepr)
4949
TYPEREPR(UnqualifiedIdent, DeclRefTypeRepr)
5050
TYPEREPR(QualifiedIdent, DeclRefTypeRepr)
5151
TYPEREPR(Function, TypeRepr)
52+
TYPEREPR(InlineArray, TypeRepr)
5253
TYPEREPR(Array, TypeRepr)
5354
TYPEREPR(Dictionary, TypeRepr)
5455
TYPEREPR(Optional, TypeRepr)

include/swift/AST/TypeTransform.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,25 @@ case TypeKind::Id:
894894
return ArraySliceType::get(baseTy);
895895
}
896896

897+
case TypeKind::InlineArray: {
898+
auto ty = cast<InlineArrayType>(base);
899+
auto countTy = doIt(ty->getCountType(), TypePosition::Invariant);
900+
if (!countTy)
901+
return Type();
902+
903+
// Currently the element type is invariant for InlineArray.
904+
// FIXME: Should we allow covariance?
905+
auto eltTy = doIt(ty->getElementType(), TypePosition::Invariant);
906+
if (!eltTy)
907+
return Type();
908+
909+
if (countTy.getPointer() == ty->getCountType().getPointer() &&
910+
eltTy.getPointer() == ty->getElementType().getPointer())
911+
return t;
912+
913+
return InlineArrayType::get(countTy, eltTy);
914+
}
915+
897916
case TypeKind::Optional: {
898917
auto optional = cast<OptionalType>(base);
899918
auto baseTy = doIt(optional->getBaseType(), pos);

include/swift/AST/Types.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6191,6 +6191,27 @@ class ArraySliceType : public UnarySyntaxSugarType {
61916191
}
61926192
};
61936193

6194+
/// An InlineArray type e.g `[2 x Foo]`, sugar for `InlineArray<2, Foo>`.
6195+
class InlineArrayType : public SyntaxSugarType {
6196+
Type Count;
6197+
Type Elt;
6198+
6199+
InlineArrayType(const ASTContext &ctx, Type count, Type elt,
6200+
RecursiveTypeProperties properties)
6201+
: SyntaxSugarType(TypeKind::InlineArray, ctx, properties), Count(count),
6202+
Elt(elt) {}
6203+
6204+
public:
6205+
static InlineArrayType *get(Type count, Type elt);
6206+
6207+
Type getCountType() const { return Count; }
6208+
Type getElementType() const { return Elt; }
6209+
6210+
static bool classof(const TypeBase *T) {
6211+
return T->getKind() == TypeKind::InlineArray;
6212+
}
6213+
};
6214+
61946215
/// The type T?, which is always sugar for a library type.
61956216
class OptionalType : public UnarySyntaxSugarType {
61966217
OptionalType(const ASTContext &ctx,Type base,

include/swift/Basic/Features.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,9 @@ EXPERIMENTAL_FEATURE(SwiftSettings, false)
508508
/// Syntax sugar features for concurrency.
509509
EXPERIMENTAL_FEATURE(ConcurrencySyntaxSugar, true)
510510

511+
/// Enable syntax sugar type '[3 x Int]' for Inline Array
512+
EXPERIMENTAL_FEATURE(InlineArrayTypeSugar, false)
513+
511514
/// Allow declaration of compile-time values
512515
EXPERIMENTAL_FEATURE(CompileTimeValues, true)
513516

0 commit comments

Comments
 (0)