Skip to content

Change InlineArray sugar separator x -> of #81868

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 1 commit into from
May 31, 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 include/swift/AST/PrintOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ struct PrintOptions {
bool AlwaysDesugarArraySliceTypes = false;

/// Whether to always desugar inline array types from
/// `[<count> x <element>]` to `InlineArray<count, element>`
/// `[<count> of <element>]` to `InlineArray<count, element>`
bool AlwaysDesugarInlineArrayTypes = false;

/// Whether to always desugar dictionary types
Expand Down
2 changes: 1 addition & 1 deletion include/swift/AST/TypeRepr.h
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ class ArrayTypeRepr : public TypeRepr {
friend class TypeRepr;
};

/// An InlineArray type e.g `[2 x Foo]`, sugar for `InlineArray<2, Foo>`.
/// An InlineArray type e.g `[2 of Foo]`, sugar for `InlineArray<2, Foo>`.
class InlineArrayTypeRepr : public TypeRepr {
TypeRepr *Count;
TypeRepr *Element;
Expand Down
2 changes: 1 addition & 1 deletion include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -6299,7 +6299,7 @@ class ArraySliceType : public UnarySyntaxSugarType {
}
};

/// An InlineArray type e.g `[2 x Foo]`, sugar for `InlineArray<2, Foo>`.
/// An InlineArray type e.g `[2 of Foo]`, sugar for `InlineArray<2, Foo>`.
class InlineArrayType : public SyntaxSugarType {
Type Count;
Type Elt;
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Basic/Features.def
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ EXPERIMENTAL_FEATURE(CustomAvailability, true)
/// Syntax sugar features for concurrency.
EXPERIMENTAL_FEATURE(ConcurrencySyntaxSugar, true)

/// Enable syntax sugar type '[3 x Int]' for Inline Array
/// Enable syntax sugar type '[3 of Int]' for Inline Array
EXPERIMENTAL_FEATURE(InlineArrayTypeSugar, false)

/// Allow declaration of compile-time values
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7115,7 +7115,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
} else {
Printer << "[";
visit(T->getCountType());
Printer << " x ";
Printer << " of ";
visit(T->getElementType());
Printer << "]";
}
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/TypeRepr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ void InlineArrayTypeRepr::printImpl(ASTPrinter &Printer,
const PrintOptions &Opts) const {
Printer << "[";
printTypeRepr(getCount(), Printer, Opts);
Printer << " x ";
Printer << " of ";
printTypeRepr(getElement(), Printer, Opts);
Printer << "]";
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Demangling/NodePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3316,7 +3316,7 @@ NodePointer NodePrinter::print(NodePointer Node, unsigned depth,
case Node::Kind::SugaredInlineArray: {
Printer << "[";
print(Node->getChild(0), depth + 1);
Printer << " x ";
Printer << " of ";
print(Node->getChild(1), depth + 1);
Printer << "]";
return nullptr;
Expand Down
14 changes: 7 additions & 7 deletions lib/Parse/ParseType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1322,13 +1322,13 @@ ParserResult<TypeRepr> Parser::parseTypeInlineArray(SourceLoc lSquare) {

ParserStatus status;

// 'isStartOfInlineArrayTypeBody' means we should at least have a type and 'x'
// to start with.
// 'isStartOfInlineArrayTypeBody' means we should at least have a type and
// 'of' to start with.
auto count = parseTypeOrValue();
auto *countTy = count.get();
status |= count;

// 'x'
// 'of'
consumeToken(tok::identifier);

// Allow parsing a value for better recovery, Sema will diagnose any
Expand Down Expand Up @@ -1827,16 +1827,16 @@ bool Parser::canParseStartOfInlineArrayType() {
if (!Context.LangOpts.hasFeature(Feature::InlineArrayTypeSugar))
return false;

// We must have at least '[<type> x', which cannot be any other kind of
// We must have at least '[<type> of', which cannot be any other kind of
// expression or type. We specifically look for any type, not just integers
// for better recovery in e.g cases where the user writes '[Int x 2]'. We
// only do type-scalar since variadics would be ambiguous e.g 'Int...x'.
// for better recovery in e.g cases where the user writes '[Int of 2]'. We
// only do type-scalar since variadics would be ambiguous e.g 'Int...of'.
if (!canParseTypeScalar())
return false;

// For now we don't allow multi-line since that would require
// disambiguation.
if (Tok.isAtStartOfLine() || !Tok.isContextualKeyword("x"))
if (Tok.isAtStartOfLine() || !Tok.isContextualKeyword("of"))
return false;

consumeToken();
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5379,7 +5379,7 @@ TypeResolver::resolveInlineArrayType(InlineArrayTypeRepr *repr,
auto argOptions = options.withoutContext().withContext(
TypeResolverContext::ValueGenericArgument);

// It's possible the user accidentally wrote '[Int x 4]', correct that here.
// It's possible the user accidentally wrote '[Int of 4]', correct that here.
auto *countRepr = repr->getCount();
auto *eltRepr = repr->getElement();
if (!isa<IntegerTypeRepr>(countRepr) && isa<IntegerTypeRepr>(eltRepr)) {
Expand Down
8 changes: 4 additions & 4 deletions test/ASTGen/types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ let optionalIntArray: Array<_> = [42]

@available(SwiftStdlib 9999, *)
func testInlineArray() {
let _: [3 x Int] = [1, 2, 3]
let _: [_ x _] = [1, 2]
let _ = [3 x Int](repeating: 0)
let _ = [3 x _](repeating: 0)
let _: [3 of Int] = [1, 2, 3]
let _: [_ of _] = [1, 2]
let _ = [3 of Int](repeating: 0)
let _ = [3 of _](repeating: 0)
}

func testNamedOpaqueReturnTy() -> <T> T { return () }
8 changes: 4 additions & 4 deletions test/DebugInfo/sugar_inline_array.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

// REQUIRES: swift_feature_InlineArrayTypeSugar

let a: ([3 x Int], InlineArray<3, Int>) = ([1, 2, 3], [1, 2, 3])
let b: ([3 x [1 x String]], InlineArray<3, InlineArray<1, String>>) = ([[""], [""], [""]], [[""], [""], [""]])
let a: ([3 of Int], InlineArray<3, Int>) = ([1, 2, 3], [1, 2, 3])
let b: ([3 of [1 of String]], InlineArray<3, InlineArray<1, String>>) = ([[""], [""], [""]], [[""], [""], [""]])

// CHECK: !DICompositeType(tag: DW_TAG_structure_type, name: "$s$2_SiXSA_s11InlineArrayVy$2_SiGtD", {{.*}})
// CHECK: !DICompositeType(tag: DW_TAG_structure_type, name: "$s$2_$0_SSXSAXSA_s11InlineArrayVy$2_ABy$0_SSGGtD", {{.*}})

// RUN: swift-demangle 's$2_SiXSA_s11InlineArrayVy$2_SiGtD' | %FileCheck %s --check-prefix SIMPLE
// SIMPLE: ([3 x Swift.Int], Swift.InlineArray<3, Swift.Int>)
// SIMPLE: ([3 of Swift.Int], Swift.InlineArray<3, Swift.Int>)

// RUN: swift-demangle 's$2_$0_SSXSAXSA_s11InlineArrayVy$2_ABy$0_SSGGtD' | %FileCheck %s --check-prefix NESTED
// NESTED: ([3 x [1 x Swift.String]], Swift.InlineArray<3, Swift.InlineArray<1, Swift.String>>)
// NESTED: ([3 of [1 of Swift.String]], Swift.InlineArray<3, Swift.InlineArray<1, Swift.String>>)
4 changes: 2 additions & 2 deletions test/IDE/complete_inline_array.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

struct FooBar {}

[3 x #^COMPLETE_TOPLEVEL?check=COMPLETE^#
let _: [3 x #^COMPLETE_TYPE?check=COMPLETE^#
[3 of #^COMPLETE_TOPLEVEL?check=COMPLETE^#
let _: [3 of #^COMPLETE_TYPE?check=COMPLETE^#
// COMPLETE: Decl[Struct]/CurrModule: FooBar[#FooBar#]; name=FooBar
4 changes: 2 additions & 2 deletions test/Sema/inline_array_availability.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ func foo(x: InlineArray<3, Int>) {}
// expected-error@-1 {{'InlineArray' is only available in}}
// expected-note@-2 {{add '@available' attribute to enclosing global function}}

func bar(x: [3 x Int]) {}
func bar(x: [3 of Int]) {}
// expected-error@-1 {{'InlineArray' is only available in}}
// expected-note@-2 {{add '@available' attribute to enclosing global function}}

@available(SwiftStdlib 9999, *)
func baz(x: InlineArray<3, Int>) {}

@available(SwiftStdlib 9999, *)
func qux(x: [3 x Int]) {}
func qux(x: [3 of Int]) {}
60 changes: 30 additions & 30 deletions test/Sema/inlinearray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@ let f: InlineArray<_, Int> = ["hello"] // expected-error {{cannot convert value

let g: InlineArray<1, 1> // expected-error {{cannot use value type '1' for generic argument 'Element'}}

let _: [3 x Int] = [1, 2, 3] // Ok, InlineArray<3, Int>
let _: [_ x Int] = [1, 2, 3] // Ok, InlineArray<3, Int>
let _: [3 x _] = [1, 2, 3] // Ok, InlineArray<3, Int>
let _: [_ x _] = ["", "", ""] // Ok, InlineArray<3, String>
let _: [3 of Int] = [1, 2, 3] // Ok, InlineArray<3, Int>
let _: [_ of Int] = [1, 2, 3] // Ok, InlineArray<3, Int>
let _: [3 of _] = [1, 2, 3] // Ok, InlineArray<3, Int>
let _: [_ of _] = ["", "", ""] // Ok, InlineArray<3, String>

let _: [3 x [3 x Int]] = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
let _: [3 x [3 x Int]] = [[1, 2], [3, 4, 5, 6]]
let _: [3 of [3 of Int]] = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
let _: [3 of [3 of Int]] = [[1, 2], [3, 4, 5, 6]]
// expected-error@-1 {{'3' elements in inline array literal, but got '2'}}
// expected-error@-2 2{{cannot convert value of type '[Int]' to expected element type '[3 x Int]'}}
// expected-error@-2 2{{cannot convert value of type '[Int]' to expected element type '[3 of Int]'}}

let _ = [3 x [3 x Int]](repeating: [1, 2]) // expected-error {{expected '3' elements in inline array literal, but got '2'}}
let _ = [3 x [_ x Int]](repeating: [1, 2])
let _ = [3 of [3 of Int]](repeating: [1, 2]) // expected-error {{expected '3' elements in inline array literal, but got '2'}}
let _ = [3 of [_ of Int]](repeating: [1, 2])

let _: [Int x 10] = [1, 2] // expected-error {{element count must precede inline array element type}} {{15-17=Int}} {{9-12=10}}
let _: [Int of 10] = [1, 2] // expected-error {{element count must precede inline array element type}} {{16-18=Int}} {{9-12=10}}
// expected-error@-1 {{expected '10' elements in inline array literal, but got '2'}}

let _: [4 x _] = [1, 2, 3] // expected-error {{expected '4' elements in inline array literal, but got '3'}}
let _: [3 x Int] = [1, 2, 3, 4] // expected-error {{expected '3' elements in inline array literal, but got '4'}}
let _: [3 x String] = [1, 2, 3] // expected-error 3{{cannot convert value of type 'Int' to expected element type 'String'}}
let _: [3 x String] = [1] // expected-error {{cannot convert value of type 'Int' to expected element type 'String'}}
let _: [4 of _] = [1, 2, 3] // expected-error {{expected '4' elements in inline array literal, but got '3'}}
let _: [3 of Int] = [1, 2, 3, 4] // expected-error {{expected '3' elements in inline array literal, but got '4'}}
let _: [3 of String] = [1, 2, 3] // expected-error 3{{cannot convert value of type 'Int' to expected element type 'String'}}
let _: [3 of String] = [1] // expected-error {{cannot convert value of type 'Int' to expected element type 'String'}}
// expected-error@-1 {{expected '3' elements in inline array literal, but got '1'}}

func takeVectorOf2<T>(_: InlineArray<2, T>) {}
Expand Down Expand Up @@ -63,10 +63,10 @@ takeVectorOf2Int(["hello", "world"]) // expected-error {{cannot convert value of

takeVectorOf2Int(["hello", "world", "!"]) // expected-error {{cannot convert value of type '[String]' to expected argument type 'InlineArray<2, Int>'}}

func takeSugarVectorOf2<T>(_: [2 x T], ty: T.Type = T.self) {}
func takeSugarVectorOf2<T>(_: [2 of T], ty: T.Type = T.self) {}
takeSugarVectorOf2([1, 2])
takeSugarVectorOf2(["hello"]) // expected-error {{expected '2' elements in inline array literal, but got '1'}}
takeSugarVectorOf2(["hello"], ty: Int.self) // expected-error {{cannot convert value of type '[String]' to expected argument type '[2 x Int]'}}
takeSugarVectorOf2(["hello"], ty: Int.self) // expected-error {{cannot convert value of type '[String]' to expected argument type '[2 of Int]'}}
takeSugarVectorOf2(["hello", "hi"], ty: Int.self) // expected-error 2{{cannot convert value of type 'String' to expected element type 'Int'}}


Expand Down Expand Up @@ -112,15 +112,15 @@ extension InlineArray where Element: ~Copyable {
}
}

extension [3 x Int] { // expected-note 2{{where 'count' = '2'}} expected-note {{where 'Element' = 'String'}}
extension [3 of Int] { // expected-note 2{{where 'count' = '2'}} expected-note {{where 'Element' = 'String'}}
func methodOnSugar() {}
}

func testExtension(
_ a: [3 x Int],
_ a: [3 of Int],
_ b: InlineArray<3, Int>,
_ c: [2 x Int],
_ d: [2 x String]
_ c: [2 of Int],
_ d: [2 of String]
) {
a.enumerated { _, _ in }
a.methodOnSugar()
Expand All @@ -133,24 +133,24 @@ func testExtension(
}

func redecl(_ x: InlineArray<2, Int>) {} // expected-note {{'redecl' previously declared here}}
func redecl(_ x: [2 x Int]) {} // expected-error {{invalid redeclaration of 'redecl'}}
func redecl(_ x: [2 of Int]) {} // expected-error {{invalid redeclaration of 'redecl'}}

func noRedecl(_ x: InlineArray<2, Int>) {}
func noRedecl(_ x: [3 x Int]) {}
func noRedecl(_ x: [2 x String]) {}
func noRedecl(_ x: [3 x String]) {}
func noRedecl(_ x: [3 of Int]) {}
func noRedecl(_ x: [2 of String]) {}
func noRedecl(_ x: [3 of String]) {}

func testMismatches(_ x: [3 x Int], _ y: InlineArray<3, Int>) {
func testMismatches(_ x: [3 of Int], _ y: InlineArray<3, Int>) {
let _: InlineArray<3, Int> = x
let _: InlineArray<4, Int> = x // expected-error {{cannot assign value of type '[3 x Int]' to type 'InlineArray<4, Int>'}}
let _: InlineArray<4, Int> = x // expected-error {{cannot assign value of type '[3 of Int]' to type 'InlineArray<4, Int>'}}
// expected-note@-1 {{arguments to generic parameter 'count' ('3' and '4') are expected to be equal}}
let _: InlineArray<3, String> = x // expected-error {{cannot assign value of type '[3 x Int]' to type 'InlineArray<3, String>'}}
let _: InlineArray<3, String> = x // expected-error {{cannot assign value of type '[3 of Int]' to type 'InlineArray<3, String>'}}
// expected-note@-1 {{arguments to generic parameter 'Element' ('Int' and 'String') are expected to be equal}}

let _: [3 x Int] = y
let _: [4 x Int] = y // expected-error {{cannot assign value of type 'InlineArray<3, Int>' to type '[4 x Int]'}}
let _: [3 of Int] = y
let _: [4 of Int] = y // expected-error {{cannot assign value of type 'InlineArray<3, Int>' to type '[4 of Int]'}}
// expected-note@-1 {{arguments to generic parameter 'count' ('3' and '4') are expected to be equal}}
let _: [3 x String] = y // expected-error {{cannot assign value of type 'InlineArray<3, Int>' to type '[3 x String]'}}
let _: [3 of String] = y // expected-error {{cannot assign value of type 'InlineArray<3, Int>' to type '[3 of String]'}}
// expected-note@-1 {{arguments to generic parameter 'Element' ('Int' and 'String') are expected to be equal}}
}

Expand Down
4 changes: 2 additions & 2 deletions test/Serialization/value_generics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ extension Vector where Count == 2 {
// CHECK: func something<let N : Int>(_: borrowing Vector<Int, N>)
func something<let N: Int>(_: borrowing Vector<Int, N>) {}

// CHECK: func hello(_: [4 x Int])
func hello(_: [4 x Int]) {}
// CHECK: func hello(_: [4 of Int])
func hello(_: [4 of Int]) {}
Loading