Skip to content

[Compile Time Constant Extraction] Extract Interpolated String Literals #74491

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

Merged

Conversation

venkatesh5789
Copy link
Contributor

Added support for extracting Interpolated String literals:

Example:

public struct Internal: MyProto {
    static var internalTitle: String = "Inner title"
}

public struct External: MyProto {
    var interpolatedTitle = "Interpolation Start: \(Internal.internalTitle)."
}

@venkatesh5789 venkatesh5789 force-pushed the interpolated-string-extraction branch 2 times, most recently from b6c2d47 to c0295bf Compare June 18, 2024 22:20
Copy link
Contributor

@jPaolantonio jPaolantonio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it might make sense to both store segments and interpolations together in a single array, as well as extract out any static information that may be available. Something like this:

"segments": [
          {
            "valueKind": "RawLiteral",
            "value": "Start Interpolation with Member Reference: ",
          },
          {
            "valueKind": "Runtime",
            // Any interpolation information
            "interpolatedSegment": "Internal.internalTitle",
            "baseTypeName": "ExtractInterpolatedStringLiterals.Internal",
            "mangledBaseTypeName": "33ExtractInterpolatedStringLiterals8InternalV",
            "label": "internalTitle"
          },
          {
            "valueKind": "RawLiteral",
            "value": ". Followed By Function Call: ",
          },
          {
            "valueKind": "FunctionCall",
            // Any interpolation information
          },
          {
            "valueKind": "RawLiteral",
            "value": ". End with KeyPath: ",
          },
          {
            "valueKind": "KeyPath",
            "value:": "nested.foo", // new keypath type
            // Any interpolation information
            "interpolatedSegment": "Internal.internalTitle",
            "baseTypeName": "ExtractInterpolatedStringLiterals.MyType",
            "mangledBaseTypeName": "33ExtractInterpolatedStringLiterals6MyTypeV",
          },
          {
            "valueKind": "RawLiteral",
            "value": ".",
          }
      ]

I think leveraging the current types might make this more adaptable to future changes,

@venkatesh5789 venkatesh5789 force-pushed the interpolated-string-extraction branch from c0295bf to ade3977 Compare June 21, 2024 16:34
@venkatesh5789
Copy link
Contributor Author

@jPaolantonio I changed this up to just call extractCompileTimeValue(Expr *expr) for each segment of the interpolated string. I then added the necessary cases to extractCompileTimeValue(). So the resulting segments look like this:

Input:

var interpolatedTitle = "Start Interpolation with Member Reference: \(Internal.internalTitle). Followed By Function Call: \(generateString(input: "test")). End with KeyPath: \(\MyType.nested.foo)."

Output:

        "valueKind": "InterpolatedStringLiteral",
        "value": {
          "segments": [
            {
              "valueKind": "RawLiteral",
              "value": "Start Interpolation with Member Reference: "
            },
            {
              "valueKind": "MemberReference",
              "value": {
                "baseType": "ExtractInterpolatedStringLiterals.Internal",
                "memberLabel": "internalTitle"
              }
            },
            {
              "valueKind": "RawLiteral",
              "value": ". Followed By Function Call: "
            },
            {
              "valueKind": "FunctionCall",
              "value": {
                "name": "generateString",
                "arguments": [
                  {
                    "label": "input",
                    "type": "Swift.String",
                    "valueKind": "RawLiteral",
                    "value": "test"
                  }
                ]
              }
            },
            {
              "valueKind": "RawLiteral",
              "value": ". End with KeyPath: "
            },
            {
              "valueKind": "KeyPath",
              "value": {
                "path": "nested.foo",
                "rootType": "ExtractInterpolatedStringLiterals.MyType",
                "components": [
                  {
                    "label": "nested",
                    "type": "ExtractInterpolatedStringLiterals.MyType.NestedOne"
                  },
                  {
                    "label": "foo",
                    "type": "Swift.String"
                  }
                ]
              }
            },
            {
              "valueKind": "RawLiteral",
              "value": "."
            }
          ]
        }

@venkatesh5789 venkatesh5789 force-pushed the interpolated-string-extraction branch from ade3977 to 62de52a Compare June 21, 2024 17:14
@venkatesh5789
Copy link
Contributor Author

@swift-ci test

@venkatesh5789 venkatesh5789 force-pushed the interpolated-string-extraction branch from 62de52a to 9820865 Compare June 25, 2024 18:11
@venkatesh5789
Copy link
Contributor Author

@swift-ci test

@xedin xedin removed their request for review June 26, 2024 15:41
@venkatesh5789 venkatesh5789 merged commit 465634c into swiftlang:main Jun 26, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants