-
Notifications
You must be signed in to change notification settings - Fork 419
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
Add TypeSyntax util functions #2886
base: main
Are you sure you want to change the base?
Conversation
This moves isVoid from SwiftRefactor to be publicly accessible in SwiftSyntax. Also adds isSwiftCoreModule and isSwiftInt and calls isSwiftCoreModule in isVoid and isSwiftInt to properly handle the qualified cases of Swift.Void and Swift.Int.
@swift-ci Please smoke test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like these APIs are in an unfortunate middle ground where they aren't just one-offs we should add and live with, nor are they along the path to incrementally grow into the full solution. I think a full solution needs to be able to incorporate many (most?) of the Swift standard library types, so we can use it in various source-to-source translation tools. As an example, we have something similar to these APIs in the swift-java project... but there we handle all of the integer and floating-point types that have Java primitive equivalents, as well as things like unsafe (buffer) pointers.
@@ -102,3 +102,29 @@ extension RawUnexpectedNodesSyntax { | |||
self.init(raw: raw) | |||
} | |||
} | |||
|
|||
extension TypeSyntax { | |||
public var isVoid: Bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide documentation comments for all public APIs. Once we settle on them, please add an entry to the release notes.
Sources/SwiftSyntax/Utils.swift
Outdated
public var isSwiftInt: Bool { | ||
switch self.as(TypeSyntaxEnum.self) { | ||
case .identifierType(let identifierType) where identifierType.name.text == "Int": return true | ||
case .memberType(let memberType) where memberType.name.text == "Int": return memberType.baseType.isSwiftCoreModule | ||
default: return false | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is largely a copy of the isVoid
implementation above. I'm concerned that this approach to the API doesn't really scale: are we going to end up growing isSwiftUInt
, isSwiftBool
, isSwiftSet
, and others as separate properties? That doesn't seem like a desirable end state.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found a way to generalise it to types where the canonical name only contains MemberTypeSyntax
or IdentifierTypeSyntax
. This doesn't include Void
since it's a tuple, and there doesn't seem to be a good way to generalise meta types for tuples; I couldn't find anything like Tuple.Type
, so handling tuple types would involve enumerating every arity. Not sure how to handle type shadowing either, since there doesn't seem to be a way to reify module<Swift>.self
.
canRepresentBasicType() can match against any canonical type named by only dots and identifiers.
@swift-ci Please smoke test |
This moves isVoid from SwiftRefactor to be publicly accessible in SwiftSyntax. Also adds isSwiftCoreModule and isSwiftInt and calls isSwiftCoreModule in isVoid and isSwiftInt to properly handle the qualified cases of Swift.Void and Swift.Int.