Skip to content

Commit f4f60f4

Browse files
committed
Remove Value requirement Add GenericTypeParamKind
1 parent 0df42e9 commit f4f60f4

File tree

100 files changed

+835
-1074
lines changed

Some content is hidden

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

100 files changed

+835
-1074
lines changed

docs/ABI/Mangling.rst

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,11 +1012,18 @@ now codified into the ABI; the index 0 is therefore reserved.
10121012

10131013
::
10141014

1015-
generic-signature ::= requirement* generic-param-pack-marker* 'l' // one generic parameter
1016-
generic-signature ::= requirement* generic-param-pack-marker* 'r' GENERIC-PARAM-COUNT* 'l'
1015+
generic-signature ::= requirement* generic-param-marker 'l' // one generic parameter
1016+
generic-signature ::= requirement* generic-param-marker* 'r' GENERIC-PARAM-COUNT* 'l'
1017+
1018+
generic-param-marker ::= generic-param-pack-marker
1019+
generic-param-marker ::= generic-param-value-marker
10171020

10181021
generic-param-pack-marker ::= 'Rv' GENERIC_PARAM-INDEX // generic parameter pack marker
10191022

1023+
#if SWIFT_RUNTIME_VERSION >= 6.TBD
1024+
generic-param-value-marker ::= type 'RV' GENERIC-PARAM-INDEX // generic parameter value marker
1025+
#endif
1026+
10201027
GENERIC-PARAM-COUNT ::= 'z' // zero parameters
10211028
GENERIC-PARAM-COUNT ::= INDEX // N+1 parameters
10221029

@@ -1045,10 +1052,6 @@ now codified into the ABI; the index 0 is therefore reserved.
10451052

10461053
requirement ::= type 'Rh' GENERIC-PARAM-INDEX // same-shape requirement (only supported on a generic parameter)
10471054

1048-
#if SWIFT_RUNTIME_VERSION >= 6.TBD
1049-
requirement ::= type 'RV' GENERIC-PARAM-INDEX // value requirement
1050-
#endif
1051-
10521055
GENERIC-PARAM-INDEX ::= 'z' // depth = 0, idx = 0
10531056
GENERIC-PARAM-INDEX ::= INDEX // depth = 0, idx = N+1
10541057
GENERIC-PARAM-INDEX ::= 'd' INDEX INDEX // depth = M+1, idx = N

include/swift/AST/ASTBridging.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,11 +1896,11 @@ BridgedGenericParamList BridgedGenericParamList_createParsed(
18961896
BridgedSourceLoc cRightAngleLoc);
18971897

18981898
SWIFT_NAME(
1899-
"BridgedGenericTypeParamDecl.createParsed(_:declContext:eachKeywordLoc:"
1900-
"letKeywordLoc:name:nameLoc:inheritedType:index:)")
1899+
"BridgedGenericTypeParamDecl.createParsed(_:declContext:specifierLoc:"
1900+
"name:nameLoc:inheritedType:index:)")
19011901
BridgedGenericTypeParamDecl BridgedGenericTypeParamDecl_createParsed(
19021902
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
1903-
BridgedSourceLoc cEachLoc, BridgedSourceLoc cLetLoc, BridgedIdentifier cName,
1903+
BridgedSourceLoc cSpecifierLoc, BridgedIdentifier cName,
19041904
BridgedSourceLoc cNameLoc, BridgedNullableTypeRepr opaqueInheritedType,
19051905
size_t index);
19061906

include/swift/AST/Decl.h

Lines changed: 63 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -587,13 +587,10 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
587587

588588
SWIFT_INLINE_BITFIELD_EMPTY(TypeDecl, ValueDecl);
589589

590-
SWIFT_INLINE_BITFIELD_FULL(GenericTypeParamDecl, TypeDecl, 16+16+1+1+1,
591-
: NumPadBits,
592-
590+
SWIFT_INLINE_BITFIELD_FULL(GenericTypeParamDecl, TypeDecl, 16+16+8+1,
593591
Depth : 16,
594592
Index : 16,
595-
ParameterPack : 1,
596-
Value : 1,
593+
ParamKind : 8,
597594

598595
/// Whether this generic parameter represents an opaque type.
599596
IsOpaqueType : 1
@@ -3772,17 +3769,11 @@ class GenericTypeParamDecl final
37723769
}
37733770

37743771
size_t numTrailingObjects(OverloadToken<SourceLoc>) const {
3775-
auto numSourceLocs = 0;
3776-
3777-
if (isParameterPack()) {
3778-
numSourceLocs += 1;
3779-
}
3780-
3781-
if (isValue()) {
3782-
numSourceLocs += 1;
3772+
if (isParameterPack() || isValue()) {
3773+
return 1;
37833774
}
37843775

3785-
return numSourceLocs;
3776+
return 0;
37863777
}
37873778

37883779
/// Construct a new generic type parameter.
@@ -3793,24 +3784,16 @@ class GenericTypeParamDecl final
37933784
///
37943785
/// \param name The name of the generic parameter.
37953786
/// \param nameLoc The location of the name.
3796-
/// \param eachLoc The location of the 'each' keyword for a type parameter
3797-
/// pack.
3798-
/// \param letLoc The location of the 'let' keyword for a variable type
3799-
/// parameter.
3787+
/// \param specifierLoc The location of the 'each' or 'let' keyword for pack
3788+
/// or value parameters.
38003789
/// \param depth The generic signature depth.
38013790
/// \param index The index of the parameter in the generic signature.
3802-
/// \param isParameterPack Whether the generic parameter is for a type
3803-
/// parameter pack, denoted by \c <each T>.
3804-
/// \param isValue Whether the generic parameter is for a value type
3805-
/// parameter, denoted by \c <let N: Int>.
3806-
/// \param isOpaqueType Whether the generic parameter is written as an opaque
3807-
/// parameter e.g 'some Collection'.
3791+
/// \param paramKind The kind of generic type parameter this is.
38083792
/// \param opaqueTypeRepr The TypeRepr of an opaque generic parameter.
38093793
///
38103794
GenericTypeParamDecl(DeclContext *dc, Identifier name, SourceLoc nameLoc,
3811-
SourceLoc eachLoc, SourceLoc letLoc, unsigned depth,
3812-
unsigned index, bool isParameterPack, bool isValue,
3813-
bool isOpaqueType, TypeRepr *opaqueTypeRepr);
3795+
SourceLoc specifierLoc, unsigned depth, unsigned index,
3796+
GenericTypeParamKind paramKind, TypeRepr *opaqueTypeRepr);
38143797

38153798
/// Construct a new generic type parameter.
38163799
///
@@ -3820,25 +3803,17 @@ class GenericTypeParamDecl final
38203803
///
38213804
/// \param name The name of the generic parameter.
38223805
/// \param nameLoc The location of the name.
3823-
/// \param eachLoc The location of the 'each' keyword for a type parameter
3824-
/// pack.
3825-
/// \param letLoc The location of the 'let' keyword for a variable type
3826-
/// parameter.
3806+
/// \param specifierLoc The location of the 'each' or 'let' keyword for pack
3807+
/// or value parameters.
38273808
/// \param depth The generic signature depth.
38283809
/// \param index The index of the parameter in the generic signature.
3829-
/// \param isParameterPack Whether the generic parameter is for a type
3830-
/// parameter pack, denoted by \c <each T>.
3831-
/// \param isValue Whether the generic parameter is for a value type
3832-
/// parameter, denoted by \c <let N: Int>.
3833-
/// \param isOpaqueType Whether the generic parameter is written as an opaque
3834-
/// parameter e.g 'some Collection'.
3810+
/// \param paramKind The kind of generic type parameter this is.
38353811
/// \param opaqueTypeRepr The TypeRepr of an opaque generic parameter.
38363812
///
38373813
static GenericTypeParamDecl *create(DeclContext *dc, Identifier name,
3838-
SourceLoc nameLoc, SourceLoc eachLoc,
3839-
SourceLoc letLoc, unsigned depth,
3840-
unsigned index, bool isParameterPack,
3841-
bool isValue, bool isOpaqueType,
3814+
SourceLoc nameLoc, SourceLoc specifierLoc,
3815+
unsigned depth, unsigned index,
3816+
GenericTypeParamKind paramKind,
38423817
TypeRepr *opaqueTypeRepr);
38433818

38443819
public:
@@ -3847,11 +3822,10 @@ class GenericTypeParamDecl final
38473822
/// Construct a new generic type parameter. This should only be used by the
38483823
/// ClangImporter, use \c GenericTypeParamDecl::create[...] instead.
38493824
GenericTypeParamDecl(DeclContext *dc, Identifier name, SourceLoc nameLoc,
3850-
SourceLoc eachLoc, SourceLoc letLoc, unsigned depth,
3851-
unsigned index, bool isParameterPack, bool isValue)
3852-
: GenericTypeParamDecl(dc, name, nameLoc, eachLoc, letLoc, depth, index,
3853-
isParameterPack, isValue, /*isOpaqueType*/ false,
3854-
nullptr) {
3825+
SourceLoc specifierLoc, unsigned depth, unsigned index,
3826+
GenericTypeParamKind paramKind)
3827+
: GenericTypeParamDecl(dc, name, nameLoc, specifierLoc, depth, index,
3828+
paramKind, nullptr) {
38553829
}
38563830

38573831
/// Construct a deserialized generic type parameter.
@@ -3863,17 +3837,11 @@ class GenericTypeParamDecl final
38633837
/// \param name The name of the generic parameter.
38643838
/// \param depth The generic signature depth.
38653839
/// \param index The index of the parameter in the generic signature.
3866-
/// \param isParameterPack Whether the generic parameter is for a type
3867-
/// parameter pack, denoted by \c <each T>.
3868-
/// \param isValue Whether the generic parameter is for a variable type
3869-
/// parameter, denoted by \c <let N: Int>.
3870-
/// \param isOpaqueType Whether the generic parameter is written as an opaque
3871-
/// parameter e.g 'some Collection'.
3840+
/// \param paramKind The kind of generic type parameter this is.
38723841
///
38733842
static GenericTypeParamDecl *
38743843
createDeserialized(DeclContext *dc, Identifier name, unsigned depth,
3875-
unsigned index, bool isParameterPack, bool isValue,
3876-
bool isOpaqueType);
3844+
unsigned index, GenericTypeParamKind paramKind);
38773845

38783846
/// Construct a new parsed generic type parameter.
38793847
///
@@ -3883,22 +3851,16 @@ class GenericTypeParamDecl final
38833851
///
38843852
/// \param name The name of the generic parameter.
38853853
/// \param nameLoc The location of the name.
3886-
/// \param eachLoc The location of the 'each' keyword for a type parameter
3887-
/// pack.
3888-
/// \param letLoc The location of the 'let' keyword for a variable type
3889-
/// parameter.
3854+
/// \param specifierLoc The location of the 'each' or 'let' keyword for pack
3855+
/// or value parameters.
38903856
/// \param index The index of the parameter in the generic signature.
3891-
/// \param isParameterPack Whether the generic parameter is for a type
3892-
/// parameter pack, denoted by \c <each T>.
3893-
/// \param isValue Whether the generic parameter is for a variable type
3894-
/// parameter, denoted by \c <let N: Int>.
3857+
/// \param paramKind The kind of generic type parameter this is.
38953858
///
38963859
static GenericTypeParamDecl *createParsed(DeclContext *dc, Identifier name,
38973860
SourceLoc nameLoc,
3898-
SourceLoc eachLoc, SourceLoc letLoc,
3861+
SourceLoc specifierLoc,
38993862
unsigned index,
3900-
bool isParameterPack,
3901-
bool isValue);
3863+
GenericTypeParamKind paramKind);
39023864

39033865
/// Construct a new implicit generic type parameter.
39043866
///
@@ -3909,25 +3871,17 @@ class GenericTypeParamDecl final
39093871
/// \param name The name of the generic parameter.
39103872
/// \param depth The generic signature depth.
39113873
/// \param index The index of the parameter in the generic signature.
3912-
/// \param isParameterPack Whether the generic parameter is for a type
3913-
/// parameter pack, denoted by \c <each T>.
3914-
/// \param isValue Whether the generic parameter is for a variable type
3915-
/// parameter, denoted by \c <let N: Int>.
3916-
/// \param isOpaqueType Whether the generic parameter is written as an opaque
3917-
/// parameter e.g 'some Collection'.
3874+
/// \param paramKind The kind of generic type parameter this is.
39183875
/// \param opaqueTypeRepr The TypeRepr of an opaque generic parameter.
39193876
/// \param nameLoc The location of the name.
3920-
/// \param eachLoc The location of the 'each' keyword for a type parameter
3921-
/// pack.
3922-
/// \param letLoc The location of the 'let' keyword for a variable type
3923-
/// parameter.
3877+
/// \param specifierLoc The location of the 'each' or 'let' keyword for pack
3878+
/// or value parameters.
39243879
///
39253880
static GenericTypeParamDecl *
39263881
createImplicit(DeclContext *dc, Identifier name, unsigned depth,
3927-
unsigned index, bool isParameterPack = false,
3928-
bool isValue = false, bool isOpaqueType = false,
3882+
unsigned index, GenericTypeParamKind paramKind,
39293883
TypeRepr *opaqueTypeRepr = nullptr, SourceLoc nameLoc = {},
3930-
SourceLoc eachLoc = {}, SourceLoc letLoc = {});
3884+
SourceLoc specifierLoc = {});
39313885

39323886
/// The depth of this generic type parameter, i.e., the number of outer
39333887
/// levels of generic parameter lists that enclose this type parameter.
@@ -3949,14 +3903,21 @@ class GenericTypeParamDecl final
39493903
assert(Bits.GenericTypeParamDecl.Depth == depth && "Truncation");
39503904
}
39513905

3906+
/// The kind of generic type parameter this is.
3907+
GenericTypeParamKind getParamKind() const {
3908+
return (GenericTypeParamKind) Bits.GenericTypeParamDecl.ParamKind;
3909+
}
3910+
39523911
/// Returns \c true if this generic type parameter is declared as a type
39533912
/// parameter pack.
39543913
///
39553914
/// \code
39563915
/// func foo<each T>(_ : for each T) { }
39573916
/// struct Foo<each T> { }
39583917
/// \endcode
3959-
bool isParameterPack() const { return Bits.GenericTypeParamDecl.ParameterPack; }
3918+
bool isParameterPack() const {
3919+
return getParamKind() == GenericTypeParamKind::Pack;
3920+
}
39603921

39613922
/// Returns \c true if this generic type parameter is declared as a value
39623923
/// type parameter.
@@ -3965,9 +3926,13 @@ class GenericTypeParamDecl final
39653926
/// struct Vector<Element, let N: Int>
39663927
/// \endcode
39673928
bool isValue() const {
3968-
return Bits.GenericTypeParamDecl.Value;
3929+
return getParamKind() == GenericTypeParamKind::Value;
39693930
}
39703931

3932+
/// Returns the underlying value type associated with this generic value
3933+
/// parameter. E.g. if this is 'let N: Int' returns 'Int'.
3934+
Type getValueType() const;
3935+
39713936
/// Determine whether this generic parameter represents an opaque type.
39723937
///
39733938
/// \code
@@ -4004,14 +3969,31 @@ class GenericTypeParamDecl final
40043969
/// Here 'T' and 'U' have indexes 0 and 1, respectively. 'V' has index 0.
40053970
unsigned getIndex() const { return Bits.GenericTypeParamDecl.Index; }
40063971

4007-
/// Retrieve the 'each' keyword location for a type parameter pack \c each T
3972+
/// Retrieve either the 'each' keyword or the 'let' keyword location for this
3973+
/// generic parameter depending on if it's a parameter pack or a value.
3974+
SourceLoc getSpecifierLoc() const {
3975+
if (!isParameterPack() && !isValue())
3976+
return SourceLoc();
3977+
3978+
return *getTrailingObjects<SourceLoc>();
3979+
}
3980+
3981+
/// Retrieve the 'each' keyword location for a type parameter pack \c each T.
40083982
SourceLoc getEachLoc() const {
40093983
if (!isParameterPack())
40103984
return SourceLoc();
40113985

40123986
return *getTrailingObjects<SourceLoc>();
40133987
}
40143988

3989+
/// Retrieve the 'let' keyword location for a value parameter \c let N.
3990+
SourceLoc getLetLoc() const {
3991+
if (!isValue())
3992+
return SourceLoc();
3993+
3994+
return *getTrailingObjects<SourceLoc>();
3995+
}
3996+
40153997
SourceLoc getStartLoc() const { return getNameLoc(); }
40163998
SourceRange getSourceRange() const;
40173999

include/swift/AST/DiagnosticsSema.def

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8009,8 +8009,7 @@ NOTE(invalid_value_value_generic_requirement,none,
80098009
"requirement specified as %0 == %1%2",
80108010
(Type, Type, StringRef))
80118011
ERROR(cannot_pass_type_for_value_generic,none,
8012-
"cannot pass type %1 as a value for generic value of %2",
8013-
(Type, Type, Type))
8012+
"cannot pass type %0 as a value for generic value %1", (Type, Type))
80148013
ERROR(value_type_used_in_type_parameter,none,
80158014
"cannot use value type %0 for generic argument %1", (Type, Type))
80168015
ERROR(cannot_self_value_generic,none,
@@ -8019,6 +8018,10 @@ ERROR(invalid_value_type_value_generic,none,
80198018
"%0 is not a supported value type for %1", (Type, Type))
80208019
ERROR(invalid_value_generic_conformance,none,
80218020
"value generic type %0 cannot conform to protocol %1", (Type, Type))
8021+
ERROR(invalid_value_generic_same_type,none,
8022+
"cannot constrain value parameter %0 to be type %1", (Type, Type))
8023+
ERROR(integer_type_not_accepted,none,
8024+
"integer unexpectedly used in a type position", ())
80228025

80238026
#define UNDEFINE_DIAGNOSTIC_MACROS
80248027
#include "DefineDiagnosticMacros.h"

include/swift/AST/GenericSignature.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,6 @@ class GenericSignature {
179179
LayoutConstraint layout;
180180

181181
Type packShape;
182-
183-
Type valueType;
184182
};
185183

186184
private:
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===--- GenericTypeParamKind.h ---------------------------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// This file defines the GenericTypeParamKind enum.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
#ifndef SWIFT_AST_GENERICTYPEPARAMKIND_H
18+
#define SWIFT_AST_GENERICTYPEPARAMKIND_H
19+
20+
namespace swift {
21+
/// Describes the kind of a generic type parameter that occurs within generic
22+
/// parameter lists.
23+
enum class GenericTypeParamKind: uint8_t {
24+
/// A regular generic type parameter: 'T'
25+
Type,
26+
/// A generic parameter pack: 'each T'
27+
Pack,
28+
/// A generic value parameter: 'let T'
29+
Value
30+
};
31+
32+
} // namespace swift
33+
#endif // #ifndef SWIFT_AST_GENERICTYPEPARAMKIND_H

0 commit comments

Comments
 (0)