Skip to content

Commit af489ea

Browse files
authored
Merge pull request #79544 from rintaro/astgen-small-fixes
2 parents 09003d6 + efea164 commit af489ea

File tree

12 files changed

+74
-16
lines changed

12 files changed

+74
-16
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,14 +1568,15 @@ BridgedImportDecl BridgedImportDecl_createParsed(
15681568

15691569
SWIFT_NAME("BridgedSubscriptDecl.createParsed(_:declContext:staticLoc:"
15701570
"staticSpelling:subscriptKeywordLoc:genericParamList:parameterList:"
1571-
"arrowLoc:returnType:)")
1571+
"arrowLoc:returnType:genericWhereClause:)")
15721572
BridgedSubscriptDecl BridgedSubscriptDecl_createParsed(
15731573
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
15741574
BridgedSourceLoc cStaticLoc, BridgedStaticSpelling cStaticSpelling,
15751575
BridgedSourceLoc cSubscriptKeywordLoc,
15761576
BridgedNullableGenericParamList cGenericParamList,
15771577
BridgedParameterList cParamList, BridgedSourceLoc cArrowLoc,
1578-
BridgedTypeRepr returnType);
1578+
BridgedTypeRepr returnType,
1579+
BridgedNullableTrailingWhereClause genericWhereClause);
15791580

15801581
SWIFT_NAME("BridgedTopLevelCodeDecl.create(_:declContext:)")
15811582
BridgedTopLevelCodeDecl
@@ -2509,6 +2510,10 @@ BridgedDictionaryTypeRepr BridgedDictionaryTypeRepr_createParsed(
25092510
BridgedTypeRepr keyType, BridgedSourceLoc cColonloc,
25102511
BridgedTypeRepr valueType, BridgedSourceLoc cRSquareLoc);
25112512

2513+
SWIFT_NAME("BridgedErrorTypeRepr.create(_:range:)")
2514+
BridgedErrorTypeRepr BridgedErrorTypeRepr_create(BridgedASTContext cContext,
2515+
BridgedSourceRange cRange);
2516+
25122517
SWIFT_NAME("BridgedFunctionTypeRepr.createParsed(_:argsType:asyncLoc:throwsLoc:"
25132518
"thrownType:arrowLoc:resultType:)")
25142519
BridgedFunctionTypeRepr BridgedFunctionTypeRepr_createParsed(

include/swift/AST/Decl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7459,6 +7459,8 @@ class SubscriptDecl : public GenericContext, public AbstractStorageDecl {
74597459
/// operation.
74607460
Type getElementInterfaceType() const;
74617461

7462+
std::optional<Type> getCachedElementInterfaceType() const;
7463+
74627464
TypeRepr *getElementTypeRepr() const { return ElementTy.getTypeRepr(); }
74637465
SourceRange getElementTypeSourceRange() const {
74647466
return ElementTy.getSourceRange();

lib/AST/ASTDumper.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2192,6 +2192,7 @@ namespace {
21922192
Label::optional("generic_signature"));
21932193
} else {
21942194
printParsedGenericParams(GC->getParsedGenericParams());
2195+
printWhereRequirements(GC);
21952196
}
21962197
}
21972198

@@ -2562,6 +2563,8 @@ namespace {
25622563
printCommon(SD, "subscript_decl", label);
25632564
printStorageImpl(SD);
25642565
printAttributes(SD);
2566+
printTypeOrTypeRepr(SD->getCachedElementInterfaceType(),
2567+
SD->getElementTypeRepr(), Label::always("element"));
25652568
printAccessors(SD);
25662569
printFoot();
25672570
}

lib/AST/Bridging/DeclBridging.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -615,12 +615,15 @@ BridgedSubscriptDecl BridgedSubscriptDecl_createParsed(
615615
BridgedSourceLoc cSubscriptKeywordLoc,
616616
BridgedNullableGenericParamList cGenericParamList,
617617
BridgedParameterList cParamList, BridgedSourceLoc cArrowLoc,
618-
BridgedTypeRepr returnType) {
619-
return SubscriptDecl::createParsed(
618+
BridgedTypeRepr returnType,
619+
BridgedNullableTrailingWhereClause genericWhereClause) {
620+
auto *decl = SubscriptDecl::createParsed(
620621
cContext.unbridged(), cStaticLoc.unbridged(), unbridged(cStaticSpelling),
621622
cSubscriptKeywordLoc.unbridged(), cParamList.unbridged(),
622623
cArrowLoc.unbridged(), returnType.unbridged(), cDeclContext.unbridged(),
623624
cGenericParamList.unbridged());
625+
decl->setTrailingWhereClause(genericWhereClause.unbridged());
626+
return decl;
624627
}
625628

626629
BridgedTopLevelCodeDecl

lib/AST/Bridging/TypeReprBridging.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ BridgedDictionaryTypeRepr BridgedDictionaryTypeRepr_createParsed(
9595
SourceRange{lSquareLoc, rSquareLoc});
9696
}
9797

98+
BridgedErrorTypeRepr BridgedErrorTypeRepr_create(BridgedASTContext cContext,
99+
BridgedSourceRange cRange) {
100+
return ErrorTypeRepr::create(cContext.unbridged(), cRange.unbridged());
101+
}
102+
98103
BridgedInverseTypeRepr
99104
BridgedInverseTypeRepr_createParsed(BridgedASTContext cContext,
100105
BridgedSourceLoc cTildeLoc,

lib/AST/Decl.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9343,6 +9343,11 @@ Type SubscriptDecl::getElementInterfaceType() const {
93439343
return ErrorType::get(ctx);
93449344
}
93459345

9346+
std::optional<Type> SubscriptDecl::getCachedElementInterfaceType() const {
9347+
auto mutableThis = const_cast<SubscriptDecl *>(this);
9348+
return ResultTypeRequest{mutableThis}.getCachedResult();
9349+
}
9350+
93469351
ObjCSubscriptKind SubscriptDecl::getObjCSubscriptKind() const {
93479352
// If the index type is an integral type, we have an indexed
93489353
// subscript.

lib/ASTGen/Sources/ASTGen/Decls.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,8 @@ extension ASTGenVisitor {
630630
genericParamList: self.generate(genericParameterClause: node.genericParameterClause),
631631
parameterList: self.generate(functionParameterClause: node.parameterClause, for: .subscript),
632632
arrowLoc: self.generateSourceLoc(node.returnClause.arrow),
633-
returnType: self.generate(type: node.returnClause.type)
633+
returnType: self.generate(type: node.returnClause.type),
634+
genericWhereClause: self.generate(genericWhereClause: node.genericWhereClause)
634635
)
635636
subscriptDecl.asDecl.attachParsedAttrs(attrs.attributes)
636637

lib/ASTGen/Sources/ASTGen/Stmts.swift

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,19 +148,26 @@ extension ASTGenVisitor {
148148
var pat = self.generate(pattern: node.pattern)
149149
let keywordLoc = self.generateSourceLoc(node.bindingSpecifier)
150150
let isLet = node.bindingSpecifier.keywordKind == .let
151-
pat =
152-
BridgedBindingPattern.createParsed(
153-
self.ctx,
154-
keywordLoc: keywordLoc,
155-
isLet: isLet,
156-
subPattern: pat
157-
).asPattern
151+
pat = BridgedBindingPattern.createParsed(
152+
self.ctx,
153+
keywordLoc: keywordLoc,
154+
isLet: isLet,
155+
subPattern: pat
156+
).asPattern
158157

159158
// NOTE: (From the comment in libParse) The let/var pattern is part of the
160-
// statement. But since the statement doesn't have the information. But,
161-
// I'm not sure this should really be implicit.
159+
// statement. But since the statement doesn't have the information, I'm not
160+
// sure this should really be implicit.
162161
pat.setImplicit()
163162

163+
if let typeAnnotation = node.typeAnnotation {
164+
pat = BridgedTypedPattern.createParsed(
165+
self.ctx,
166+
pattern: pat,
167+
type: self.generate(type: typeAnnotation.type)
168+
).asPattern
169+
}
170+
164171
let initializer: BridgedExpr
165172
if let initNode = node.initializer {
166173
initializer = self.generate(expr: initNode.value)

lib/ASTGen/Sources/ASTGen/Types.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ extension ASTGenVisitor {
4444
return self.generate(memberType: node).asTypeRepr
4545
case .metatypeType(let node):
4646
return self.generate(metatypeType: node)
47-
case .missingType:
48-
fatalError("unimplemented")
47+
case .missingType(let node):
48+
return self.generate(missingType: node)
4949
case .namedOpaqueReturnType(let node):
5050
return self.generate(namedOpaqueReturnType: node).asTypeRepr
5151
case .optionalType(let node):
@@ -164,6 +164,13 @@ extension ASTGenVisitor {
164164
}
165165
}
166166

167+
func generate(missingType node: MissingTypeSyntax) -> BridgedTypeRepr {
168+
return BridgedErrorTypeRepr.create(
169+
self.ctx,
170+
range: self.generateSourceRange(node)
171+
).asTypeRepr
172+
}
173+
167174
func generate(
168175
implicitlyUnwrappedOptionalType node: ImplicitlyUnwrappedOptionalTypeSyntax
169176
) -> BridgedImplicitlyUnwrappedOptionalTypeRepr {

test/ASTGen/decls.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ struct TestSubscripts {
174174
}
175175
set(x) {}
176176
}
177+
178+
subscript<I: Proto3, J: Proto3>(i: I, j: J) -> Int where I.A == J.B {
179+
1
180+
}
177181
}
178182

179183
protocol Proto1 {}

test/ASTGen/diagnostics.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,8 @@ func dummy() {}
2828

2929
@_silgen_name("whatever", extra) // expected-error@:27 {{unexpected arguments in '_silgen_name' attribute}}
3030
func _whatever()
31+
32+
struct S {
33+
subscript(x: Int) { _ = 1 } // expected-error@:23 {{expected '->' and return type in subscript}}
34+
// expected-note@-1:23 {{insert '->' and return type}}
35+
}

test/ASTGen/stmts.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,17 @@ func testThen() {
116116
}
117117
}
118118

119+
func intOrString() -> Int? { 1 }
120+
func intOrString() -> String? { "" }
121+
func testIf() {
122+
if
123+
let i: Int = intOrString(),
124+
case let str? = intOrString() as String?
125+
{
126+
_ = (i, str)
127+
}
128+
}
129+
119130
struct GenericTypeWithYields<T> {
120131
var storedProperty: T?
121132

0 commit comments

Comments
 (0)