Skip to content

Commit 68c461d

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 57fefb9 commit 68c461d

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

+662
-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 "
@@ -8281,6 +8281,9 @@ ERROR(invalid_value_for_type_same_type,none,
82818281
ERROR(inlinearray_literal_incorrect_count,none,
82828282
"expected %0 elements in inline array literal, but got %1", (Type, Type))
82838283

8284+
ERROR(inline_array_type_backwards,none,
8285+
"element count must precede inline array element type", ())
8286+
82848287
//===----------------------------------------------------------------------===//
82858288
// MARK: @abi Attribute
82868289
//===----------------------------------------------------------------------===//

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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,38 @@ class ArrayTypeRepr : public TypeRepr {
641641
friend class TypeRepr;
642642
};
643643

644+
/// An InlineArray type.
645+
/// \code
646+
/// [2 x Foo]
647+
/// \endcode
648+
class InlineArrayTypeRepr : public TypeRepr {
649+
TypeRepr *Count;
650+
TypeRepr *Element;
651+
SourceRange Brackets;
652+
653+
InlineArrayTypeRepr(TypeRepr *count, TypeRepr *element, SourceRange brackets)
654+
: TypeRepr(TypeReprKind::InlineArray), Count(count), Element(element),
655+
Brackets(brackets) {}
656+
657+
public:
658+
static InlineArrayTypeRepr *create(ASTContext &ctx, TypeRepr *count,
659+
TypeRepr *element, SourceRange brackets);
660+
661+
TypeRepr *getCount() const { return Count; }
662+
TypeRepr *getElement() const { return Element; }
663+
SourceRange getBrackets() const { return Brackets; }
664+
665+
static bool classof(const TypeRepr *T) {
666+
return T->getKind() == TypeReprKind::InlineArray;
667+
}
668+
669+
private:
670+
SourceLoc getStartLocImpl() const { return Brackets.Start; }
671+
SourceLoc getEndLocImpl() const { return Brackets.End; }
672+
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
673+
friend class TypeRepr;
674+
};
675+
644676
/// A dictionary type.
645677
/// \code
646678
/// [K : V]
@@ -1626,6 +1658,7 @@ inline bool TypeRepr::isSimple() const {
16261658
case TypeReprKind::Fixed:
16271659
case TypeReprKind::Self:
16281660
case TypeReprKind::Array:
1661+
case TypeReprKind::InlineArray:
16291662
case TypeReprKind::SILBox:
16301663
case TypeReprKind::Isolated:
16311664
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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6191,6 +6191,28 @@ class ArraySliceType : public UnarySyntaxSugarType {
61916191
}
61926192
};
61936193

6194+
/// The type [<count> x <element>], which is syntax sugar for
6195+
/// InlineArray<count, element>.
6196+
class InlineArrayType : public SyntaxSugarType {
6197+
Type Count;
6198+
Type Elt;
6199+
6200+
InlineArrayType(const ASTContext &ctx, Type count, Type elt,
6201+
RecursiveTypeProperties properties)
6202+
: SyntaxSugarType(TypeKind::InlineArray, ctx, properties), Count(count),
6203+
Elt(elt) {}
6204+
6205+
public:
6206+
static InlineArrayType *get(Type count, Type elt);
6207+
6208+
Type getCountType() const { return Count; }
6209+
Type getElementType() const { return Elt; }
6210+
6211+
static bool classof(const TypeBase *T) {
6212+
return T->getKind() == TypeKind::InlineArray;
6213+
}
6214+
};
6215+
61946216
/// The type T?, which is always sugar for a library type.
61956217
class OptionalType : public UnarySyntaxSugarType {
61966218
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)