Skip to content

[Compile Time Constant Extraction] Extract Nil Values for Optionals #78511

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
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
17 changes: 17 additions & 0 deletions include/swift/AST/ConstTypeInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class CompileTimeValue {
StaticFunctionCall,
MemberReference,
InterpolatedString,
NilLiteral,
Runtime
};

Expand Down Expand Up @@ -72,6 +73,22 @@ class RawLiteralValue : public CompileTimeValue {
std::string Value;
};

/// A representation of an Optional<Wrapped> value declared as nil
/// or left undeclared.
///
/// Nil values were previously represented as RawLiteralValue with
/// value "nil". This caused ambiguous values when extracting values,
/// such as an Optional<String> of value "nil".

class NilLiteralValue : public CompileTimeValue {
public:
NilLiteralValue() : CompileTimeValue(ValueKind::NilLiteral) {}

static bool classof(const CompileTimeValue *T) {
return T->getKind() == ValueKind::NilLiteral;
}
};

struct FunctionParameter {
std::string Label;
swift::Type Type;
Expand Down
13 changes: 10 additions & 3 deletions lib/ConstExtract/ConstExtract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,7 @@ static std::optional<std::string> extractRawLiteral(Expr *expr) {
switch (expr->getKind()) {
case ExprKind::BooleanLiteral:
case ExprKind::FloatLiteral:
case ExprKind::IntegerLiteral:
case ExprKind::NilLiteral: {
case ExprKind::IntegerLiteral: {
std::string literalOutput;
llvm::raw_string_ostream OutputStream(literalOutput);
expr->printConstExprValue(&OutputStream, nullptr);
Expand Down Expand Up @@ -230,7 +229,6 @@ static std::shared_ptr<CompileTimeValue> extractCompileTimeValue(Expr *expr) {
case ExprKind::BooleanLiteral:
case ExprKind::FloatLiteral:
case ExprKind::IntegerLiteral:
case ExprKind::NilLiteral:
case ExprKind::StringLiteral: {
auto rawLiteral = extractRawLiteral(expr);
if (rawLiteral.has_value()) {
Expand All @@ -240,6 +238,10 @@ static std::shared_ptr<CompileTimeValue> extractCompileTimeValue(Expr *expr) {
break;
}

case ExprKind::NilLiteral: {
return std::make_shared<NilLiteralValue>();
}

case ExprKind::Array: {
auto arrayExpr = cast<ArrayExpr>(expr);
std::vector<std::shared_ptr<CompileTimeValue>> elementValues;
Expand Down Expand Up @@ -710,6 +712,11 @@ void writeValue(llvm::json::OStream &JSON,
break;
}

case CompileTimeValue::ValueKind::NilLiteral: {
JSON.attribute("valueKind", "NilLiteral");
break;
}

case CompileTimeValue::ValueKind::InitCall: {
auto initCallValue = cast<InitCallValue>(value);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extension ImplClass: MyProto {

// CHECK: "label": "defaultNilProperty",
// CHECK: "type": "Swift.Optional<AnyObject>",
// CHECK: "value": "nil"
// CHECK: "valueKind": "NilLiteral"

// CHECK: "label": "notStoredProperty",
// CHECK: "type": "Swift.Bool",
Expand Down
58 changes: 56 additions & 2 deletions test/ConstExtraction/ExtractLiterals.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ public struct PropertyWrappers : MyProto {
var projectedValue: (V, V?) { (self.value, self.lastValue) }
}

public struct Optionals: MyProto {
let int1: Bool? = nil
let string1: String?
static var float1: Float?
}

// CHECK: [
// CHECK-NEXT: {
// CHECK-NEXT: "typeName": "ExtractLiterals.Bools",
Expand Down Expand Up @@ -131,8 +137,7 @@ public struct PropertyWrappers : MyProto {
// CHECK-NEXT: "isComputed": "false",
// CHECK-NEXT: "file": "{{.*}}test{{/|\\\\}}ConstExtraction{{/|\\\\}}ExtractLiterals.swift",
// CHECK-NEXT: "line": 11,
// CHECK-NEXT: "valueKind": "RawLiteral",
// CHECK-NEXT: "value": "nil"
// CHECK-NEXT: "valueKind": "NilLiteral"
// CHECK-NEXT: }
// CHECK-NEXT: ]
// CHECK-NEXT: },
Expand Down Expand Up @@ -617,5 +622,54 @@ public struct PropertyWrappers : MyProto {
// CHECK-NEXT: "valueKind": "Runtime"
// CHECK-NEXT: }
// CHECK-NEXT: ]
// CHECK-NEXT: },
// CHECK-NEXT: {
// CHECK-NEXT: "typeName": "ExtractLiterals.Optionals",
// CHECK-NEXT: "mangledTypeName": "15ExtractLiterals9OptionalsV",
// CHECK-NEXT: "kind": "struct",
// CHECK-NEXT: "file": "{{.*}}test{{/|\\\\}}ConstExtraction{{/|\\\\}}ExtractLiterals.swift",
// CHECK-NEXT: "line": 97,
// CHECK-NEXT: "conformances": [
// CHECK-NEXT: "ExtractLiterals.MyProto"
// CHECK-NEXT: ],
// CHECK-NEXT: "allConformances": [
// CHECK-NEXT: {
// CHECK-NEXT: "protocolName": "ExtractLiterals.MyProto",
// CHECK-NEXT: "conformanceDefiningModule": "ExtractLiterals"
// CHECK-NEXT: }
// CHECK-NEXT: ],
// CHECK-NEXT: "associatedTypeAliases": [],
// CHECK-NEXT: "properties": [
// CHECK-NEXT: {
// CHECK-NEXT: "label": "int1",
// CHECK-NEXT: "type": "Swift.Optional<Swift.Bool>",
// CHECK-NEXT: "mangledTypeName": "n/a - deprecated",
// CHECK-NEXT: "isStatic": "false",
// CHECK-NEXT: "isComputed": "false",
// CHECK-NEXT: "file": "{{.*}}test{{/|\\\\}}ConstExtraction{{/|\\\\}}ExtractLiterals.swift",
// CHECK-NEXT: "line": 98,
// CHECK-NEXT: "valueKind": "NilLiteral"
// CHECK-NEXT: },
// CHECK-NEXT: {
// CHECK-NEXT: "label": "string1",
// CHECK-NEXT: "type": "Swift.Optional<Swift.String>",
// CHECK-NEXT: "mangledTypeName": "n/a - deprecated",
// CHECK-NEXT: "isStatic": "false",
// CHECK-NEXT: "isComputed": "false",
// CHECK-NEXT: "file": "{{.*}}test{{/|\\\\}}ConstExtraction{{/|\\\\}}ExtractLiterals.swift",
// CHECK-NEXT: "line": 99,
// CHECK-NEXT: "valueKind": "Runtime"
// CHECK-NEXT: },
// CHECK-NEXT: {
// CHECK-NEXT: "label": "float1",
// CHECK-NEXT: "type": "Swift.Optional<Swift.Float>",
// CHECK-NEXT: "mangledTypeName": "n/a - deprecated",
// CHECK-NEXT: "isStatic": "true",
// CHECK-NEXT: "isComputed": "false",
// CHECK-NEXT: "file": "{{.*}}test{{/|\\\\}}ConstExtraction{{/|\\\\}}ExtractLiterals.swift",
// CHECK-NEXT: "line": 100,
// CHECK-NEXT: "valueKind": "NilLiteral"
// CHECK-NEXT: }
// CHECK-NEXT: ]
// CHECK-NEXT: }
// CHECK-NEXT:]