Skip to content

Commit b3e75dd

Browse files
committed
Eliminate the stored "size" field in ArrayTypeRepr since it is always null, saving a word. NFC.
We should also eliminate the isOldSyntax as well, and just do the error recovery in the parser (there is no need to retain the sugar here anymore), but I'll do that as a separate change since it could be behavior changing.
1 parent 62e0680 commit b3e75dd

File tree

8 files changed

+24
-44
lines changed

8 files changed

+24
-44
lines changed

include/swift/AST/DiagnosticsCommon.def

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,6 @@ ERROR(class_var_not_in_class,common,none,
7373
// FIXME: Used by both the parser and the type-checker.
7474
ERROR(func_decl_without_brace,decl_parsing,PointsToFirstBadToken,
7575
"expected '{' in body of function declaration", ())
76-
ERROR(unsupported_fixed_length_array,type_parsing,none,
77-
"fixed-length arrays are not yet supported", ())
7876

7977
ERROR(new_array_syntax,type_parsing,none,
8078
"array types are now written with the brackets around the element type",

include/swift/AST/DiagnosticsParse.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,8 @@ ERROR(nonliteral_enum_case_raw_value,type_parsing,PointsToFirstBadToken,
598598
"raw value for enum case must be a literal", ())
599599

600600
// Collection Types
601+
ERROR(unsupported_fixed_length_array,type_parsing,none,
602+
"fixed-length arrays are not yet supported", ())
601603
ERROR(expected_expr_array_type,expr_parsing,PointsToFirstBadToken,
602604
"expected expression for size of array type", ())
603605
ERROR(expected_rbracket_array_type,type_parsing,PointsToFirstBadToken,

include/swift/AST/TypeRepr.h

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -394,21 +394,18 @@ class FunctionTypeRepr : public TypeRepr {
394394
class ArrayTypeRepr : public TypeRepr {
395395
// FIXME: Tail allocation. Use bits to determine whether Base/Size are
396396
// available.
397-
TypeRepr *Base;
398-
llvm::PointerIntPair<ExprHandle *, 1, bool> SizeAndOldSyntax;
397+
llvm::PointerIntPair<TypeRepr *, 1, bool> BaseAndIsOldSyntax;
399398
SourceRange Brackets;
400399

401400
public:
402-
ArrayTypeRepr(TypeRepr *Base, ExprHandle *Size, SourceRange Brackets,
403-
bool OldSyntax)
404-
: TypeRepr(TypeReprKind::Array), Base(Base),
405-
SizeAndOldSyntax(Size, OldSyntax), Brackets(Brackets) { }
401+
ArrayTypeRepr(TypeRepr *Base, SourceRange Brackets, bool OldSyntax)
402+
: TypeRepr(TypeReprKind::Array), BaseAndIsOldSyntax(Base, OldSyntax),
403+
Brackets(Brackets) { }
406404

407-
TypeRepr *getBase() const { return Base; }
408-
ExprHandle *getSize() const { return SizeAndOldSyntax.getPointer(); }
405+
TypeRepr *getBase() const { return BaseAndIsOldSyntax.getPointer(); }
409406
SourceRange getBrackets() const { return Brackets; }
410407

411-
bool usesOldSyntax() const { return SizeAndOldSyntax.getInt(); }
408+
bool usesOldSyntax() const { return BaseAndIsOldSyntax.getInt(); }
412409

413410
static bool classof(const TypeRepr *T) {
414411
return T->getKind() == TypeReprKind::Array;
@@ -418,16 +415,16 @@ class ArrayTypeRepr : public TypeRepr {
418415
private:
419416
SourceLoc getStartLocImpl() const {
420417
if (usesOldSyntax())
421-
return Base->getStartLoc();
418+
return getBase()->getStartLoc();
422419

423420
return Brackets.Start;
424421
}
425422
SourceLoc getEndLocImpl() const {
426-
// This test is necessary because the type Int[4][2] is represented as
427-
// ArrayTypeRepr(ArrayTypeRepr(Int, 2), 4), so the range needs to cover both
423+
// This test is necessary because the type Int[][] is represented as
424+
// ArrayTypeRepr(ArrayTypeRepr(Int)), so the range needs to cover both
428425
// sets of brackets.
429-
if (usesOldSyntax() && isa<ArrayTypeRepr>(Base))
430-
return Base->getEndLoc();
426+
if (usesOldSyntax() && isa<ArrayTypeRepr>(getBase()))
427+
return getBase()->getEndLoc();
431428
return Brackets.End;
432429
}
433430
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;

lib/AST/ASTDumper.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2314,10 +2314,6 @@ class PrintTypeRepr : public TypeReprVisitor<PrintTypeRepr> {
23142314
void visitArrayTypeRepr(ArrayTypeRepr *T) {
23152315
printCommon(T, "type_array") << '\n';
23162316
printRec(T->getBase());
2317-
if (T->getSize()) {
2318-
OS << '\n';
2319-
printRec(T->getSize()->getExpr());
2320-
}
23212317
OS << ')';
23222318
}
23232319

lib/AST/TypeRepr.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ TypeRepr *CloneVisitor::visitFunctionTypeRepr(FunctionTypeRepr *T) {
158158
}
159159

160160
TypeRepr *CloneVisitor::visitArrayTypeRepr(ArrayTypeRepr *T) {
161-
return new (Ctx) ArrayTypeRepr(visit(T->getBase()), T->getSize(),
162-
T->getBrackets(), T->usesOldSyntax());
161+
return new (Ctx) ArrayTypeRepr(visit(T->getBase()), T->getBrackets(),
162+
T->usesOldSyntax());
163163
}
164164

165165
TypeRepr *CloneVisitor::visitDictionaryTypeRepr(DictionaryTypeRepr *T) {
@@ -339,14 +339,11 @@ void FunctionTypeRepr::printImpl(ASTPrinter &Printer,
339339
void ArrayTypeRepr::printImpl(ASTPrinter &Printer,
340340
const PrintOptions &Opts) const {
341341
if (usesOldSyntax()) {
342-
printTypeRepr(Base, Printer, Opts);
343-
Printer << "[";
344-
if (auto size = getSize())
345-
size->getExpr()->print(Printer, Opts);
346-
Printer << "]";
342+
printTypeRepr(getBase(), Printer, Opts);
343+
Printer << "[]";
347344
} else {
348345
Printer << "[";
349-
printTypeRepr(Base, Printer, Opts);
346+
printTypeRepr(getBase(), Printer, Opts);
350347
Printer << "]";
351348
}
352349
}

lib/Parse/ParseType.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -586,8 +586,8 @@ ParserResult<ArrayTypeRepr> Parser::parseTypeArray(TypeRepr *Base) {
586586
}
587587

588588
// Just build a normal array slice type.
589-
ATR = new (Context) ArrayTypeRepr(NestedType.get(), nullptr,
590-
SourceRange(lsquareLoc, rsquareLoc),
589+
ATR = new (Context) ArrayTypeRepr(NestedType.get(),
590+
SourceRange(lsquareLoc, rsquareLoc),
591591
/*OldSyntax=*/true);
592592

593593
if (NestedType.isParseError())
@@ -623,7 +623,6 @@ ParserResult<ArrayTypeRepr> Parser::parseTypeArray(TypeRepr *Base) {
623623
.highlight(sizeEx.get()->getSourceRange());
624624

625625
ATR = new (Context) ArrayTypeRepr(NestedType.get(),
626-
nullptr,
627626
SourceRange(lsquareLoc,
628627
getEndOfPreviousLoc()),
629628
/*OldSyntax=*/true);
@@ -643,9 +642,8 @@ ParserResult<ArrayTypeRepr> Parser::parseTypeArray(TypeRepr *Base) {
643642

644643
// Create an array slice type for the malformed array type specification
645644
NestedType = makeParserErrorResult(Base);
646-
ATR = new (Context) ArrayTypeRepr(NestedType.get(), nullptr,
647-
SourceRange(lsquareLoc,
648-
PreviousLoc),
645+
ATR = new (Context) ArrayTypeRepr(NestedType.get(),
646+
SourceRange(lsquareLoc, PreviousLoc),
649647
/*OldSyntax=*/true);
650648
return makeParserErrorResult(ATR);
651649
}
@@ -693,7 +691,6 @@ ParserResult<TypeRepr> Parser::parseTypeCollection() {
693691
// Form the array type.
694692
return makeParserResult(firstTy,
695693
new (Context) ArrayTypeRepr(firstTy.get(),
696-
nullptr,
697694
brackets,
698695
/*OldSyntax=*/false));
699696
}

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -861,8 +861,7 @@ TypeExpr *PreCheckExpression::simplifyTypeExpr(Expr *E) {
861861
"the TypeExpr should have been built correctly in the first place");
862862

863863
auto *NewTypeRepr =
864-
new (TC.Context) ArrayTypeRepr(InnerTypeRepr, nullptr,
865-
Indexes->getSourceRange(),
864+
new (TC.Context) ArrayTypeRepr(InnerTypeRepr, Indexes->getSourceRange(),
866865
/*OldSyntax=*/true);
867866

868867
TC.diagnose(Indexes->getStartLoc(), diag::new_array_syntax)
@@ -997,7 +996,7 @@ TypeExpr *PreCheckExpression::simplifyTypeExpr(Expr *E) {
997996
return nullptr;
998997

999998
auto *NewTypeRepr =
1000-
new (TC.Context) ArrayTypeRepr(TyExpr->getTypeRepr(), nullptr,
999+
new (TC.Context) ArrayTypeRepr(TyExpr->getTypeRepr(),
10011000
SourceRange(AE->getLBracketLoc(),
10021001
AE->getRBracketLoc()),
10031002
/*OldSyntax=*/false);

lib/Sema/TypeCheckType.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2097,12 +2097,6 @@ Type TypeResolver::resolveArrayType(ArrayTypeRepr *repr,
20972097
// FIXME: diagnose non-materializability of element type!
20982098
Type baseTy = resolveType(repr->getBase(), withoutContext(options));
20992099
if (!baseTy || baseTy->is<ErrorType>()) return baseTy;
2100-
2101-
if (ExprHandle *sizeEx = repr->getSize()) {
2102-
TC.diagnose(repr->getBrackets().Start, diag::unsupported_fixed_length_array)
2103-
.highlight(sizeEx->getExpr()->getSourceRange());
2104-
return ErrorType::get(Context);
2105-
}
21062100

21072101
auto sliceTy = TC.getArraySliceType(repr->getBrackets().Start, baseTy);
21082102
if (!sliceTy)

0 commit comments

Comments
 (0)