Skip to content

Commit c231330

Browse files
committed
[AST] Remove ParenType
Today ParenType is used: 1. As the type of ParenExpr 2. As the payload type of an unlabeled single associated value enum case (and the type of ParenPattern). 3. As the type for an `(X)` TypeRepr For 1, this leads to some odd behavior, e.g the type of `(5.0 * 5).squareRoot()` is `(Double)`. For 2, we should be checking the arity of the enum case constructor parameters and the presence of ParenPattern respectively. Eventually we ought to consider replacing Paren/TuplePattern with a PatternList node, similar to ArgumentList. 3 is one case where it could be argued that there's some utility in preserving the sugar of the type that the user wrote. However it's really not clear to me that this is particularly desirable since a bunch of diagnostic logic is already stripping ParenTypes. In cases where we care about how the type was written in source, we really ought to be consulting the TypeRepr.
1 parent a1e31c0 commit c231330

File tree

72 files changed

+135
-383
lines changed

Some content is hidden

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

72 files changed

+135
-383
lines changed

docs/ABI/Mangling.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,6 @@ productions:
933933
type ::= base-type "XSq" // sugared Optional type
934934
type ::= base-type "XSa" // sugared Array type
935935
type ::= key-type value-type "XSD" // sugared Dictionary type
936-
type ::= base-type "XSp" // sugared Paren type
937936

938937
Generics
939938
~~~~~~~~

include/swift/AST/ASTDemangler.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,6 @@ class ASTBuilder {
229229

230230
Type createDictionaryType(Type key, Type value);
231231

232-
Type createParenType(Type base);
233-
234232
Type createIntegerType(intptr_t value);
235233

236234
Type createNegativeIntegerType(intptr_t value);

include/swift/AST/TypeNodes.def

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ UNCHECKED_TYPE(TypeVariable, Type)
191191
UNCHECKED_TYPE(ErrorUnion, Type)
192192
ALWAYS_CANONICAL_TYPE(Integer, Type)
193193
ABSTRACT_SUGARED_TYPE(Sugar, Type)
194-
SUGARED_TYPE(Paren, SugarType)
195194
SUGARED_TYPE(TypeAlias, SugarType)
196195
ABSTRACT_SUGARED_TYPE(SyntaxSugar, SugarType)
197196
ABSTRACT_SUGARED_TYPE(UnarySyntaxSugar, SyntaxSugarType)
@@ -201,7 +200,7 @@ ABSTRACT_SUGARED_TYPE(Sugar, Type)
201200
TYPE_RANGE(UnarySyntaxSugar, ArraySlice, VariadicSequence)
202201
SUGARED_TYPE(Dictionary, SyntaxSugarType)
203202
TYPE_RANGE(SyntaxSugar, ArraySlice, Dictionary)
204-
TYPE_RANGE(Sugar, Paren, Dictionary)
203+
TYPE_RANGE(Sugar, TypeAlias, Dictionary)
205204
LAST_TYPE(Dictionary) // Sugared types are last to make isa<SugarType>() fast.
206205

207206
#endif

include/swift/AST/TypeTransform.h

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,10 @@ class TypeTransform {
9494

9595
public:
9696
Type doIt(Type t, TypePosition pos) {
97-
if (!isa<ParenType>(t.getPointer())) {
98-
// Transform this type node.
99-
if (std::optional<Type> transformed = asDerived().transform(t.getPointer(), pos))
100-
return *transformed;
101-
102-
// Recur.
103-
}
97+
// Transform this type node.
98+
if (std::optional<Type> transformed =
99+
asDerived().transform(t.getPointer(), pos))
100+
return *transformed;
104101

105102
// Recur into children of this type.
106103
TypeBase *base = t.getPointer();
@@ -511,18 +508,6 @@ case TypeKind::Id:
511508
newUnderlyingTy);
512509
}
513510

514-
case TypeKind::Paren: {
515-
auto paren = cast<ParenType>(base);
516-
Type underlying = doIt(paren->getUnderlyingType(), pos);
517-
if (!underlying)
518-
return Type();
519-
520-
if (underlying.getPointer() == paren->getUnderlyingType().getPointer())
521-
return t;
522-
523-
return ParenType::get(ctx, underlying);
524-
}
525-
526511
case TypeKind::ErrorUnion: {
527512
auto errorUnion = cast<ErrorUnionType>(base);
528513
bool anyChanged = false;

include/swift/AST/Types.h

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,6 @@ class alignas(1 << TypeAlignInBits) TypeBase
425425
HasCachedType : 1
426426
);
427427

428-
SWIFT_INLINE_BITFIELD_EMPTY(ParenType, SugarType);
429-
430428
SWIFT_INLINE_BITFIELD_FULL(AnyFunctionType, TypeBase, NumAFTExtInfoBits+1+1+1+1+16,
431429
/// Extra information which affects how the function is called, like
432430
/// regparm and the calling convention.
@@ -687,9 +685,6 @@ class alignas(1 << TypeAlignInBits) TypeBase
687685
/// Returns true if this contextual type is (Escapable && !isNoEscape).
688686
bool mayEscape() { return !isNoEscape() && isEscapable(); }
689687

690-
/// Does the type have outer parenthesis?
691-
bool hasParenSugar() const { return getKind() == TypeKind::Paren; }
692-
693688
/// Are values of this type essentially just class references,
694689
/// possibly with some sort of additional information?
695690
///
@@ -1287,9 +1282,6 @@ class alignas(1 << TypeAlignInBits) TypeBase
12871282
/// argument labels removed.
12881283
Type removeArgumentLabels(unsigned numArgumentLabels);
12891284

1290-
/// Retrieve the type without any parentheses around it.
1291-
Type getWithoutParens();
1292-
12931285
/// Replace the base type of the result type of the given function
12941286
/// type with a new result type, as per a DynamicSelf or other
12951287
/// covariant return transformation. The optionality of the
@@ -2477,21 +2469,6 @@ class YieldTypeFlags {
24772469
uint8_t toRaw() const { return value.toRaw(); }
24782470
};
24792471

2480-
/// ParenType - A paren type is a type that's been written in parentheses.
2481-
class ParenType : public SugarType {
2482-
ParenType(Type UnderlyingType, RecursiveTypeProperties properties);
2483-
2484-
public:
2485-
static ParenType *get(const ASTContext &C, Type underlying);
2486-
2487-
Type getUnderlyingType() const { return getSinglyDesugaredType(); }
2488-
2489-
// Implement isa/cast/dyncast/etc.
2490-
static bool classof(const TypeBase *T) {
2491-
return T->getKind() == TypeKind::Paren;
2492-
}
2493-
};
2494-
24952472
/// TupleTypeElt - This represents a single element of a tuple.
24962473
class TupleTypeElt {
24972474
/// An optional name for the field.

include/swift/Demangling/DemangleNodes.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,6 @@ NODE(AssociatedTypeGenericParamRef)
327327
NODE(SugaredOptional)
328328
NODE(SugaredArray)
329329
NODE(SugaredDictionary)
330-
NODE(SugaredParen)
331330

332331
// Added in Swift 5.1
333332
NODE(AccessorFunctionReference)

include/swift/Demangling/TypeDecoder.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,16 +1470,6 @@ class TypeDecoder {
14701470

14711471
return Builder.createDictionaryType(key.getType(), value.getType());
14721472
}
1473-
case NodeKind::SugaredParen: {
1474-
if (Node->getNumChildren() < 1)
1475-
return MAKE_NODE_TYPE_ERROR0(Node, "no children");
1476-
1477-
auto base = decodeMangledType(Node->getChild(0), depth + 1);
1478-
if (base.isError())
1479-
return base;
1480-
1481-
return Builder.createParenType(base.getType());
1482-
}
14831473
case NodeKind::OpaqueType: {
14841474
if (Node->getNumChildren() < 3)
14851475
return MAKE_NODE_TYPE_ERROR(Node,

include/swift/Sema/CSBindings.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ struct PotentialBinding {
9494
public:
9595
PotentialBinding(Type type, AllowedBindingKind kind, Constraint *source)
9696
: PotentialBinding(
97-
type->getWithoutParens(), kind,
97+
type, kind,
9898
PointerUnion<Constraint *, ConstraintLocator *>(source)) {}
9999

100100
bool isDefaultableBinding() const {

lib/APIDigester/ModuleAnalyzerNodes.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,13 +1065,12 @@ static StringRef getPrintedName(SDKContext &Ctx, Type Ty,
10651065
if (IsImplicitlyUnwrappedOptional)
10661066
PO.PrintOptionalAsImplicitlyUnwrapped = true;
10671067

1068-
Ty->getWithoutParens().print(OS, PO);
1068+
Ty.print(OS, PO);
10691069
return Ctx.buffer(OS.str());
10701070
}
10711071

10721072
static StringRef getTypeName(SDKContext &Ctx, Type Ty,
10731073
bool IsImplicitlyUnwrappedOptional) {
1074-
Ty = Ty->getWithoutParens();
10751074
if (Ty->isVoid()) {
10761075
return Ctx.buffer("Void");
10771076
}

lib/AST/ASTContext.cpp

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,6 @@ struct ASTContext::Implementation {
542542
llvm::DenseMap<Type, VariadicSequenceType*> VariadicSequenceTypes;
543543
llvm::DenseMap<std::pair<Type, Type>, DictionaryType *> DictionaryTypes;
544544
llvm::DenseMap<Type, OptionalType*> OptionalTypes;
545-
llvm::DenseMap<Type, ParenType*> ParenTypes;
546545
llvm::DenseMap<uintptr_t, ReferenceStorageType*> ReferenceStorageTypes;
547546
llvm::DenseMap<Type, LValueType*> LValueTypes;
548547
llvm::DenseMap<Type, InOutType*> InOutTypes;
@@ -3238,7 +3237,6 @@ size_t ASTContext::Implementation::Arena::getTotalMemory() const {
32383237
llvm::capacity_in_bytes(DictionaryTypes) +
32393238
llvm::capacity_in_bytes(OptionalTypes) +
32403239
llvm::capacity_in_bytes(VariadicSequenceTypes) +
3241-
llvm::capacity_in_bytes(ParenTypes) +
32423240
llvm::capacity_in_bytes(ReferenceStorageTypes) +
32433241
llvm::capacity_in_bytes(LValueTypes) +
32443242
llvm::capacity_in_bytes(InOutTypes) +
@@ -3277,7 +3275,6 @@ void ASTContext::Implementation::Arena::dump(llvm::raw_ostream &os) const {
32773275
SIZE_AND_BYTES(VariadicSequenceTypes);
32783276
SIZE_AND_BYTES(DictionaryTypes);
32793277
SIZE_AND_BYTES(OptionalTypes);
3280-
SIZE_AND_BYTES(ParenTypes);
32813278
SIZE_AND_BYTES(ReferenceStorageTypes);
32823279
SIZE_AND_BYTES(LValueTypes);
32833280
SIZE_AND_BYTES(InOutTypes);
@@ -3595,18 +3592,6 @@ BuiltinVectorType *BuiltinVectorType::get(const ASTContext &context,
35953592
return vecTy;
35963593
}
35973594

3598-
ParenType *ParenType::get(const ASTContext &C, Type underlying) {
3599-
auto properties = underlying->getRecursiveProperties();
3600-
auto arena = getArena(properties);
3601-
ParenType *&Result = C.getImpl().getArena(arena).ParenTypes[underlying];
3602-
if (Result == nullptr) {
3603-
Result = new (C, arena) ParenType(underlying, properties);
3604-
assert((C.hadError() || !underlying->is<InOutType>()) &&
3605-
"Cannot wrap InOutType");
3606-
}
3607-
return Result;
3608-
}
3609-
36103595
CanTupleType TupleType::getEmpty(const ASTContext &C) {
36113596
return cast<TupleType>(CanType(C.TheEmptyTupleType));
36123597
}
@@ -4533,7 +4518,7 @@ Type AnyFunctionType::composeTuple(ASTContext &ctx, ArrayRef<Param> params,
45334518
elements.emplace_back(param.getParameterType(), param.getLabel());
45344519
}
45354520
if (elements.size() == 1 && !elements[0].hasName())
4536-
return ParenType::get(ctx, elements[0].getType());
4521+
return elements[0].getType();
45374522
return TupleType::get(elements, ctx);
45384523
}
45394524

lib/AST/ASTDemangler.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,10 +1037,6 @@ Type ASTBuilder::createDictionaryType(Type key, Type value) {
10371037
return DictionaryType::get(key, value);
10381038
}
10391039

1040-
Type ASTBuilder::createParenType(Type base) {
1041-
return ParenType::get(Ctx, base);
1042-
}
1043-
10441040
Type ASTBuilder::createIntegerType(intptr_t value) {
10451041
return IntegerType::get(std::to_string(value), /*isNegative*/ false, Ctx);
10461042
}

lib/AST/ASTDumper.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4125,14 +4125,6 @@ namespace {
41254125
printFoot();
41264126
}
41274127

4128-
void visitParenType(ParenType *T, StringRef label) {
4129-
printCommon("paren_type", label);
4130-
4131-
printRec(T->getUnderlyingType());
4132-
4133-
printFoot();
4134-
}
4135-
41364128
void visitTupleType(TupleType *T, StringRef label) {
41374129
printCommon("tuple_type", label);
41384130

lib/AST/ASTMangler.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,12 +1387,6 @@ void ASTMangler::appendType(Type type, GenericSignature sig,
13871387
return;
13881388
}
13891389

1390-
case TypeKind::Paren:
1391-
assert(DWARFMangling && "sugared types are only legal for the debugger");
1392-
appendType(cast<ParenType>(tybase)->getUnderlyingType(), sig, forDecl);
1393-
appendOperator("XSp");
1394-
return;
1395-
13961390
case TypeKind::ArraySlice:
13971391
assert(DWARFMangling && "sugared types are only legal for the debugger");
13981392
appendType(cast<ArraySliceType>(tybase)->getBaseType(), sig, forDecl);

lib/AST/ASTPrinter.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6100,12 +6100,6 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
61006100
}
61016101
}
61026102

6103-
void visitParenType(ParenType *T) {
6104-
Printer << "(";
6105-
visit(T->getUnderlyingType()->getInOutObjectType());
6106-
Printer << ")";
6107-
}
6108-
61096103
void visitPackType(PackType *T) {
61106104
if (Options.PrintExplicitPackTypes || Options.PrintTypesForDebugging)
61116105
Printer << "Pack{";
@@ -6183,7 +6177,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
61836177
Printer << ": ";
61846178
} else if (e == 1 && !EltType->is<PackExpansionType>()) {
61856179
// Unlabeled one-element tuples always print the empty label to
6186-
// distinguish them from the older syntax for ParenType.
6180+
// distinguish them from the older syntax for parens.
61876181
Printer << "_: ";
61886182
}
61896183
visit(EltType);

lib/AST/ASTVerifier.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2135,16 +2135,6 @@ class Verifier : public ASTWalker {
21352135
verifyCheckedBase(E);
21362136
}
21372137

2138-
void verifyChecked(ParenExpr *E) {
2139-
PrettyStackTraceExpr debugStack(Ctx, "verifying ParenExpr", E);
2140-
auto ty = dyn_cast<ParenType>(E->getType().getPointer());
2141-
if (!ty) {
2142-
Out << "ParenExpr not of ParenType\n";
2143-
abort();
2144-
}
2145-
verifyCheckedBase(E);
2146-
}
2147-
21482138
void verifyChecked(AnyTryExpr *E) {
21492139
PrettyStackTraceExpr debugStack(Ctx, "verifying AnyTryExpr", E);
21502140

lib/AST/ArgumentList.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ Expr *ArgumentList::packIntoImplicitTupleOrParen(
239239
if (auto *unary = getUnlabeledUnaryExpr()) {
240240
auto *paren = new (ctx) ParenExpr(getLParenLoc(), unary, getRParenLoc());
241241
if (auto ty = getType(unary))
242-
paren->setType(ParenType::get(ctx, ty));
242+
paren->setType(ty);
243243
paren->setImplicit();
244244
return paren;
245245
}

lib/AST/DiagnosticEngine.cpp

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -700,8 +700,8 @@ static bool typeSpellingIsAmbiguous(Type type,
700700
for (auto arg : Args) {
701701
if (arg.getKind() == DiagnosticArgumentKind::Type) {
702702
auto argType = arg.getAsType();
703-
if (argType && argType->getWithoutParens().getPointer() != type.getPointer() &&
704-
argType->getWithoutParens().getString(PO) == type.getString(PO)) {
703+
if (argType && argType.getPointer() != type.getPointer() &&
704+
argType.getString(PO) == type.getString(PO)) {
705705
// Currently, existential types are spelled the same way
706706
// as protocols and compositions. We can remove this once
707707
// existenials are printed with 'any'.
@@ -893,41 +893,20 @@ static void formatDiagnosticArgument(StringRef Modifier,
893893
// Compute the appropriate print options for this argument.
894894
auto printOptions = PrintOptions::forDiagnosticArguments();
895895
if (Arg.getKind() == DiagnosticArgumentKind::Type) {
896-
type = Arg.getAsType()->getWithoutParens();
897-
if (type.isNull()) {
898-
// FIXME: We should never receive a nullptr here, but this is causing
899-
// crashes (rdar://75740683). Remove once ParenType never contains
900-
// nullptr as the underlying type.
901-
Out << "<null>";
902-
break;
903-
}
896+
type = Arg.getAsType();
904897
if (type->getASTContext().TypeCheckerOpts.PrintFullConvention)
905898
printOptions.PrintFunctionRepresentationAttrs =
906899
PrintOptions::FunctionRepresentationMode::Full;
907900
needsQualification = typeSpellingIsAmbiguous(type, Args, printOptions);
908901
} else if (Arg.getKind() == DiagnosticArgumentKind::FullyQualifiedType) {
909-
type = Arg.getAsFullyQualifiedType().getType()->getWithoutParens();
910-
if (type.isNull()) {
911-
// FIXME: We should never receive a nullptr here, but this is causing
912-
// crashes (rdar://75740683). Remove once ParenType never contains
913-
// nullptr as the underlying type.
914-
Out << "<null>";
915-
break;
916-
}
902+
type = Arg.getAsFullyQualifiedType().getType();
917903
if (type->getASTContext().TypeCheckerOpts.PrintFullConvention)
918904
printOptions.PrintFunctionRepresentationAttrs =
919905
PrintOptions::FunctionRepresentationMode::Full;
920906
needsQualification = true;
921907
} else {
922908
assert(Arg.getKind() == DiagnosticArgumentKind::WitnessType);
923-
type = Arg.getAsWitnessType().getType()->getWithoutParens();
924-
if (type.isNull()) {
925-
// FIXME: We should never receive a nullptr here, but this is causing
926-
// crashes (rdar://75740683). Remove once ParenType never contains
927-
// nullptr as the underlying type.
928-
Out << "<null>";
929-
break;
930-
}
909+
type = Arg.getAsWitnessType().getType();
931910
printOptions.PrintGenericRequirements = false;
932911
printOptions.PrintInverseRequirements = false;
933912
needsQualification = typeSpellingIsAmbiguous(type, Args, printOptions);

0 commit comments

Comments
 (0)