Skip to content

Commit e7e45fd

Browse files
committed
[SyntaxParse] Attach 'inout' to the type in tuple type parsing
instead of using that as a deprecated use of 'inout'.
1 parent dc3f110 commit e7e45fd

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

lib/Parse/ParseType.cpp

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,22 +1084,28 @@ ParsedSyntaxResult<ParsedTypeSyntax> Parser::parseTypeTupleBody() {
10841084
return ty.getStatus();
10851085
}
10861086

1087-
if (IsInOutObsoleted) {
1088-
bool IsTypeAlreadyAttributed = false;
1089-
if (auto AttributedType = ty.getAs<ParsedAttributedTypeSyntax>()) {
1090-
IsTypeAlreadyAttributed =
1091-
AttributedType->getDeferredSpecifier().hasValue();
1092-
ty = makeParsedResult(std::move(*AttributedType), ty.getStatus());
1093-
}
1094-
1095-
if (IsTypeAlreadyAttributed) {
1096-
// If the parsed type is already attributed, suggest removing `inout`.
1097-
diagnose(Tok, diag::parameter_specifier_repeated)
1098-
.fixItRemove(InOutLoc);
1087+
if (InOut) {
1088+
if (IsInOutObsoleted) {
1089+
bool IsTypeAlreadyAttributed = false;
1090+
if (auto AttributedType = ty.getAs<ParsedAttributedTypeSyntax>()) {
1091+
IsTypeAlreadyAttributed =
1092+
AttributedType->getDeferredSpecifier().hasValue();
1093+
ty = makeParsedResult(std::move(*AttributedType), ty.getStatus());
1094+
}
1095+
if (IsTypeAlreadyAttributed) {
1096+
// If the parsed type is already attributed, suggest removing `inout`.
1097+
diagnose(Tok, diag::parameter_specifier_repeated)
1098+
.fixItRemove(InOutLoc);
1099+
} else {
1100+
diagnose(InOutLoc, diag::parameter_specifier_as_attr_disallowed, "inout")
1101+
.fixItRemove(InOutLoc)
1102+
.fixItInsert(TypeLoc, "inout ");
1103+
}
10991104
} else {
1100-
diagnose(InOutLoc, diag::parameter_specifier_as_attr_disallowed, "inout")
1101-
.fixItRemove(InOutLoc)
1102-
.fixItInsert(TypeLoc, "inout ");
1105+
// Apply 'inout' to the parsed type.
1106+
ParsedAttributedTypeSyntaxBuilder builder(*SyntaxContext);
1107+
ty = applyAttributeToTypeSyntax(std::move(ty), std::move(InOut), None);
1108+
InOut.reset();
11031109
}
11041110
}
11051111

test/Syntax/Outputs/round_trip_parse_gen.swift.withkinds

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,10 @@ typealias G <TypeInitializerClause>= <FunctionType>(<TupleTypeElement>a x: <Simp
142142
typealias H <TypeInitializerClause>= <FunctionType>() rethrows -> <TupleType>()</TupleType></FunctionType></TypeInitializerClause></TypealiasDecl><TypealiasDecl>
143143
typealias I <TypeInitializerClause>= <FunctionType>(<TupleTypeElement><CompositionType><CompositionTypeElement><SimpleTypeIdentifier>A </SimpleTypeIdentifier>& </CompositionTypeElement><CompositionTypeElement><SimpleTypeIdentifier>B<GenericArgumentClause><<GenericArgument><SimpleTypeIdentifier>C</SimpleTypeIdentifier></GenericArgument>></GenericArgumentClause></SimpleTypeIdentifier></CompositionTypeElement></CompositionType></TupleTypeElement>) -> <CompositionType><CompositionTypeElement><SimpleTypeIdentifier>C </SimpleTypeIdentifier>& </CompositionTypeElement><CompositionTypeElement><SimpleTypeIdentifier>D</SimpleTypeIdentifier></CompositionTypeElement></CompositionType></FunctionType></TypeInitializerClause></TypealiasDecl><TypealiasDecl>
144144
typealias J <TypeInitializerClause>= <AttributedType>inout <Attribute>@autoclosure </Attribute><FunctionType>() -> <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></FunctionType></AttributedType></TypeInitializerClause></TypealiasDecl><TypealiasDecl>
145-
typealias K <TypeInitializerClause>= <FunctionType>(<TupleTypeElement><AttributedType><Attribute>@invalidAttr </Attribute><SimpleTypeIdentifier>Int</SimpleTypeIdentifier></AttributedType>, </TupleTypeElement><TupleTypeElement>inout <SimpleTypeIdentifier>Int</SimpleTypeIdentifier>, </TupleTypeElement><TupleTypeElement><AttributedType>__shared <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></AttributedType>, </TupleTypeElement><TupleTypeElement><AttributedType>__owned <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></AttributedType></TupleTypeElement>) -> <TupleType>()</TupleType></FunctionType></TypeInitializerClause></TypealiasDecl><TypealiasDecl><Attribute>
145+
typealias K <TypeInitializerClause>= <FunctionType>(<TupleTypeElement><AttributedType><Attribute>@invalidAttr </Attribute><SimpleTypeIdentifier>Int</SimpleTypeIdentifier></AttributedType>, </TupleTypeElement><TupleTypeElement><AttributedType>inout <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></AttributedType>, </TupleTypeElement><TupleTypeElement><AttributedType>__shared <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></AttributedType>, </TupleTypeElement><TupleTypeElement><AttributedType>__owned <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></AttributedType></TupleTypeElement>) -> <TupleType>()</TupleType></FunctionType></TypeInitializerClause></TypealiasDecl><TypealiasDecl>
146+
typealias L <TypeInitializerClause>= <FunctionType>(<TupleTypeElement><AttributedType>inout <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></AttributedType></TupleTypeElement>) -> <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></FunctionType></TypeInitializerClause></TypealiasDecl><TypealiasDecl>
147+
typealias M <TypeInitializerClause>= <FunctionType>(<TupleTypeElement>inout arg: <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></TupleTypeElement>) -> <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></FunctionType></TypeInitializerClause></TypealiasDecl><TypealiasDecl>
148+
typealias N <TypeInitializerClause>= <FunctionType>(<TupleTypeElement>inout _ arg: <AttributedType><Attribute>@objc </Attribute><SimpleTypeIdentifier>Int</SimpleTypeIdentifier></AttributedType></TupleTypeElement>) -> <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></FunctionType></TypeInitializerClause></TypealiasDecl><TypealiasDecl><Attribute>
146149

147150
@objc </Attribute><DeclModifier>private </DeclModifier>typealias T<GenericParameterClause><<GenericParameter>a,</GenericParameter><GenericParameter>b</GenericParameter>> </GenericParameterClause><TypeInitializerClause>= <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></TypeInitializerClause></TypealiasDecl><TypealiasDecl><Attribute>
148151
@objc </Attribute><DeclModifier>private </DeclModifier>typealias T<GenericParameterClause><<GenericParameter>a,</GenericParameter><GenericParameter>b</GenericParameter>></GenericParameterClause></TypealiasDecl><ClassDecl>

test/Syntax/round_trip_parse_gen.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ typealias H = () rethrows -> ()
143143
typealias I = (A & B<C>) -> C & D
144144
typealias J = inout @autoclosure () -> Int
145145
typealias K = (@invalidAttr Int, inout Int, __shared Int, __owned Int) -> ()
146+
typealias L = (inout Int) -> Int
147+
typealias M = (inout arg: Int) -> Int
148+
typealias N = (inout _ arg: @objc Int) -> Int
146149

147150
@objc private typealias T<a,b> = Int
148151
@objc private typealias T<a,b>

0 commit comments

Comments
 (0)