Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ASTGen] Assorted fixes #79544

Merged
merged 3 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions include/swift/AST/ASTBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -1568,14 +1568,15 @@ BridgedImportDecl BridgedImportDecl_createParsed(

SWIFT_NAME("BridgedSubscriptDecl.createParsed(_:declContext:staticLoc:"
"staticSpelling:subscriptKeywordLoc:genericParamList:parameterList:"
"arrowLoc:returnType:)")
"arrowLoc:returnType:genericWhereClause:)")
BridgedSubscriptDecl BridgedSubscriptDecl_createParsed(
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
BridgedSourceLoc cStaticLoc, BridgedStaticSpelling cStaticSpelling,
BridgedSourceLoc cSubscriptKeywordLoc,
BridgedNullableGenericParamList cGenericParamList,
BridgedParameterList cParamList, BridgedSourceLoc cArrowLoc,
BridgedTypeRepr returnType);
BridgedTypeRepr returnType,
BridgedNullableTrailingWhereClause genericWhereClause);

SWIFT_NAME("BridgedTopLevelCodeDecl.create(_:declContext:)")
BridgedTopLevelCodeDecl
Expand Down Expand Up @@ -2509,6 +2510,10 @@ BridgedDictionaryTypeRepr BridgedDictionaryTypeRepr_createParsed(
BridgedTypeRepr keyType, BridgedSourceLoc cColonloc,
BridgedTypeRepr valueType, BridgedSourceLoc cRSquareLoc);

SWIFT_NAME("BridgedErrorTypeRepr.create(_:range:)")
BridgedErrorTypeRepr BridgedErrorTypeRepr_create(BridgedASTContext cContext,
BridgedSourceRange cRange);

SWIFT_NAME("BridgedFunctionTypeRepr.createParsed(_:argsType:asyncLoc:throwsLoc:"
"thrownType:arrowLoc:resultType:)")
BridgedFunctionTypeRepr BridgedFunctionTypeRepr_createParsed(
Expand Down
2 changes: 2 additions & 0 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -7459,6 +7459,8 @@ class SubscriptDecl : public GenericContext, public AbstractStorageDecl {
/// operation.
Type getElementInterfaceType() const;

std::optional<Type> getCachedElementInterfaceType() const;

TypeRepr *getElementTypeRepr() const { return ElementTy.getTypeRepr(); }
SourceRange getElementTypeSourceRange() const {
return ElementTy.getSourceRange();
Expand Down
3 changes: 3 additions & 0 deletions lib/AST/ASTDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2192,6 +2192,7 @@ namespace {
Label::optional("generic_signature"));
} else {
printParsedGenericParams(GC->getParsedGenericParams());
printWhereRequirements(GC);
}
}

Expand Down Expand Up @@ -2562,6 +2563,8 @@ namespace {
printCommon(SD, "subscript_decl", label);
printStorageImpl(SD);
printAttributes(SD);
printTypeOrTypeRepr(SD->getCachedElementInterfaceType(),
SD->getElementTypeRepr(), Label::always("element"));
printAccessors(SD);
printFoot();
}
Expand Down
7 changes: 5 additions & 2 deletions lib/AST/Bridging/DeclBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -615,12 +615,15 @@ BridgedSubscriptDecl BridgedSubscriptDecl_createParsed(
BridgedSourceLoc cSubscriptKeywordLoc,
BridgedNullableGenericParamList cGenericParamList,
BridgedParameterList cParamList, BridgedSourceLoc cArrowLoc,
BridgedTypeRepr returnType) {
return SubscriptDecl::createParsed(
BridgedTypeRepr returnType,
BridgedNullableTrailingWhereClause genericWhereClause) {
auto *decl = SubscriptDecl::createParsed(
cContext.unbridged(), cStaticLoc.unbridged(), unbridged(cStaticSpelling),
cSubscriptKeywordLoc.unbridged(), cParamList.unbridged(),
cArrowLoc.unbridged(), returnType.unbridged(), cDeclContext.unbridged(),
cGenericParamList.unbridged());
decl->setTrailingWhereClause(genericWhereClause.unbridged());
return decl;
}

BridgedTopLevelCodeDecl
Expand Down
5 changes: 5 additions & 0 deletions lib/AST/Bridging/TypeReprBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ BridgedDictionaryTypeRepr BridgedDictionaryTypeRepr_createParsed(
SourceRange{lSquareLoc, rSquareLoc});
}

BridgedErrorTypeRepr BridgedErrorTypeRepr_create(BridgedASTContext cContext,
BridgedSourceRange cRange) {
return ErrorTypeRepr::create(cContext.unbridged(), cRange.unbridged());
}

BridgedInverseTypeRepr
BridgedInverseTypeRepr_createParsed(BridgedASTContext cContext,
BridgedSourceLoc cTildeLoc,
Expand Down
5 changes: 5 additions & 0 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9343,6 +9343,11 @@ Type SubscriptDecl::getElementInterfaceType() const {
return ErrorType::get(ctx);
}

std::optional<Type> SubscriptDecl::getCachedElementInterfaceType() const {
auto mutableThis = const_cast<SubscriptDecl *>(this);
return ResultTypeRequest{mutableThis}.getCachedResult();
}

ObjCSubscriptKind SubscriptDecl::getObjCSubscriptKind() const {
// If the index type is an integral type, we have an indexed
// subscript.
Expand Down
3 changes: 2 additions & 1 deletion lib/ASTGen/Sources/ASTGen/Decls.swift
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,8 @@ extension ASTGenVisitor {
genericParamList: self.generate(genericParameterClause: node.genericParameterClause),
parameterList: self.generate(functionParameterClause: node.parameterClause, for: .subscript),
arrowLoc: self.generateSourceLoc(node.returnClause.arrow),
returnType: self.generate(type: node.returnClause.type)
returnType: self.generate(type: node.returnClause.type),
genericWhereClause: self.generate(genericWhereClause: node.genericWhereClause)
)
subscriptDecl.asDecl.attachParsedAttrs(attrs.attributes)

Expand Down
25 changes: 16 additions & 9 deletions lib/ASTGen/Sources/ASTGen/Stmts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,19 +148,26 @@ extension ASTGenVisitor {
var pat = self.generate(pattern: node.pattern)
let keywordLoc = self.generateSourceLoc(node.bindingSpecifier)
let isLet = node.bindingSpecifier.keywordKind == .let
pat =
BridgedBindingPattern.createParsed(
self.ctx,
keywordLoc: keywordLoc,
isLet: isLet,
subPattern: pat
).asPattern
pat = BridgedBindingPattern.createParsed(
self.ctx,
keywordLoc: keywordLoc,
isLet: isLet,
subPattern: pat
).asPattern

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

if let typeAnnotation = node.typeAnnotation {
pat = BridgedTypedPattern.createParsed(
self.ctx,
pattern: pat,
type: self.generate(type: typeAnnotation.type)
).asPattern
}

let initializer: BridgedExpr
if let initNode = node.initializer {
initializer = self.generate(expr: initNode.value)
Expand Down
11 changes: 9 additions & 2 deletions lib/ASTGen/Sources/ASTGen/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ extension ASTGenVisitor {
return self.generate(memberType: node).asTypeRepr
case .metatypeType(let node):
return self.generate(metatypeType: node)
case .missingType:
fatalError("unimplemented")
case .missingType(let node):
return self.generate(missingType: node)
case .namedOpaqueReturnType(let node):
return self.generate(namedOpaqueReturnType: node).asTypeRepr
case .optionalType(let node):
Expand Down Expand Up @@ -164,6 +164,13 @@ extension ASTGenVisitor {
}
}

func generate(missingType node: MissingTypeSyntax) -> BridgedTypeRepr {
return BridgedErrorTypeRepr.create(
self.ctx,
range: self.generateSourceRange(node)
).asTypeRepr
}

func generate(
implicitlyUnwrappedOptionalType node: ImplicitlyUnwrappedOptionalTypeSyntax
) -> BridgedImplicitlyUnwrappedOptionalTypeRepr {
Expand Down
4 changes: 4 additions & 0 deletions test/ASTGen/decls.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ struct TestSubscripts {
}
set(x) {}
}

subscript<I: Proto3, J: Proto3>(i: I, j: J) -> Int where I.A == J.B {
1
}
}

protocol Proto1 {}
Expand Down
5 changes: 5 additions & 0 deletions test/ASTGen/diagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,8 @@ func dummy() {}

@_silgen_name("whatever", extra) // expected-error@:27 {{unexpected arguments in '_silgen_name' attribute}}
func _whatever()

struct S {
subscript(x: Int) { _ = 1 } // expected-error@:23 {{expected '->' and return type in subscript}}
// expected-note@-1:23 {{insert '->' and return type}}
}
11 changes: 11 additions & 0 deletions test/ASTGen/stmts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,17 @@ func testThen() {
}
}

func intOrString() -> Int? { 1 }
func intOrString() -> String? { "" }
func testIf() {
if
let i: Int = intOrString(),
case let str? = intOrString() as String?
{
_ = (i, str)
}
}

struct GenericTypeWithYields<T> {
var storedProperty: T?

Expand Down