Skip to content

[Compile Time Constant Extraction] Add extraction of paren expressions #62816

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
merged 1 commit into from
Jan 4, 2023
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
12 changes: 9 additions & 3 deletions lib/ConstExtract/ConstExtract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@ static std::shared_ptr<CompileTimeValue> extractCompileTimeValue(Expr *expr) {
return extractCompileTimeValue(erasureExpr->getSubExpr());
}

case ExprKind::Paren: {
auto parenExpr = cast<ParenExpr>(expr);
return extractCompileTimeValue(parenExpr->getSubExpr());
}

default: {
break;
}
Expand Down Expand Up @@ -254,9 +259,10 @@ extractTypePropertyInfo(VarDecl *propertyDecl) {

if (auto accessorDecl = propertyDecl->getAccessor(AccessorKind::Get)) {
auto node = accessorDecl->getTypecheckedBody()->getFirstElement();
if (node.is<Stmt *>()) {
if (auto returnStmt = dyn_cast<ReturnStmt>(node.get<Stmt *>())) {
return {propertyDecl, extractCompileTimeValue(returnStmt->getResult())};
if (auto *stmt = node.dyn_cast<Stmt *>()) {
if (stmt->getKind() == StmtKind::Return) {
return {propertyDecl,
extractCompileTimeValue(cast<ReturnStmt>(stmt)->getResult())};
}
}
}
Expand Down
35 changes: 35 additions & 0 deletions test/ConstExtraction/ExtractLiterals.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@
// CHECK-NEXT: "isComputed": "false",
// CHECK-NEXT: "valueKind": "RawLiteral",
// CHECK-NEXT: "value": "42.2"
// CHECK-NEXT: },
// CHECK-NEXT: {
// CHECK-NEXT: "label": "float2",
// CHECK-NEXT: "type": "Swift.Float",
// CHECK-NEXT: "isStatic": "true",
// CHECK-NEXT: "isComputed": "true",
// CHECK-NEXT: "valueKind": "RawLiteral",
// CHECK-NEXT: "value": "6"
// CHECK-NEXT: },
// CHECK-NEXT: {
// CHECK-NEXT: "label": "float3",
// CHECK-NEXT: "type": "Swift.Float",
// CHECK-NEXT: "isStatic": "true",
// CHECK-NEXT: "isComputed": "true",
// CHECK-NEXT: "valueKind": "Runtime"
// CHECK-NEXT: }
// CHECK-NEXT: ]
// CHECK-NEXT: },
Expand All @@ -82,6 +97,22 @@
// CHECK-NEXT: "isComputed": "false",
// CHECK-NEXT: "valueKind": "RawLiteral",
// CHECK-NEXT: "value": "\"Hello, World\""
// CHECK-NEXT: },
// CHECK-NEXT: {
// CHECK-NEXT: "label": "string2",
// CHECK-NEXT: "type": "Swift.String",
// CHECK-NEXT: "isStatic": "false",
// CHECK-NEXT: "isComputed": "false",
// CHECK-NEXT: "valueKind": "RawLiteral",
// CHECK-NEXT: "value": "\"Hi\""
// CHECK-NEXT: },
// CHECK-NEXT: {
// CHECK-NEXT: "label": "string3",
// CHECK-NEXT: "type": "Swift.String",
// CHECK-NEXT: "isStatic": "false",
// CHECK-NEXT: "isComputed": "true",
// CHECK-NEXT: "valueKind": "RawLiteral",
// CHECK-NEXT: "value": "\"Hey\""
// CHECK-NEXT: }
// CHECK-NEXT: ]
// CHECK-NEXT: },
Expand Down Expand Up @@ -215,10 +246,14 @@ public struct Ints : MyProto {

public struct Floats : MyProto {
static let float1: Float = 42.2
static var float2: Float { return (6) }
static var float3: Float { return (1 + 2) }
}

public struct Strings : MyProto {
let string1: String = "Hello, World"
let string2: String = (("Hi"))
var string3: String { ("Hey") }
}

public struct PropertyWrappers : MyProto {
Expand Down