Skip to content

[Constant Evaluator] Add support for displaying assertion/precondition failure messages and fatalError messages #29521

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
12 changes: 9 additions & 3 deletions lib/SILOptimizer/Utils/ConstExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,13 @@ extractStaticStringValue(SymbolicValue staticString) {
return staticStringProps[0].getStringValue();
}

static Optional<StringRef>
extractStringOrStaticStringValue(SymbolicValue stringValue) {
if (stringValue.getKind() == SymbolicValue::String)
return stringValue.getStringValue();
return extractStaticStringValue(stringValue);
}

/// If the specified type is a Swift.Array of some element type, then return the
/// element type. Otherwise, return a null Type.
static Type getArrayElementType(Type ty) {
Expand Down Expand Up @@ -829,8 +836,8 @@ ConstExprFunctionState::computeWellKnownCallResult(ApplyInst *apply,
for (unsigned i = 0; i < apply->getNumArguments(); i++) {
SILValue argument = apply->getArgument(i);
SymbolicValue argValue = getConstantValue(argument);
Optional<StringRef> stringOpt = extractStaticStringValue(argValue);

Optional<StringRef> stringOpt =
extractStringOrStaticStringValue(argValue);
// The first argument is a prefix that specifies the kind of failure
// this is.
if (i == 0) {
Expand All @@ -842,7 +849,6 @@ ConstExprFunctionState::computeWellKnownCallResult(ApplyInst *apply,
}
continue;
}

if (stringOpt) {
message += ": ";
message += stringOpt.getValue();
Expand Down
32 changes: 32 additions & 0 deletions test/SILOptimizer/constant_evaluable_subset_test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -925,3 +925,35 @@ func interpretBinaryIntegerDescription() -> String {
str += testBinaryIntegerDescription(UInt(20))
return str
}

// CHECK-LABEL: @testPreconditionFailure
// CHECK: error: not constant evaluable
@_semantics("constant_evaluable")
func testPreconditionFailure(_ x: Int) -> Int {
precondition(x > 0, "argument must be positive")
return x + 1
// CHECK: note: operation traps
// Note that the message displayed depends on the assert configuration,
// therefore it is not checked here. For debug stdlib, the full message
// must be displayed.
}

@_semantics("test_driver")
func interpretPreconditionFailure() -> Int {
return testPreconditionFailure(-10)
}

// CHECK-LABEL: @testFatalError
// CHECK: error: not constant evaluable
@_semantics("constant_evaluable")
func testFatalError() -> Int {
fatalError("invoked an uncallable function")
return 0
// CHECK: note: Fatal error: invoked an uncallable function
// CHECK: note: operation traps
}

@_semantics("test_driver")
func interpretFatalError() -> Int {
return testFatalError()
}