Skip to content

[lldb] Diagnose expressions that evaluate to ~Copyable types #8680

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 lldb/source/Plugins/ExpressionParser/Swift/SwiftASTManipulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,23 @@ void SwiftASTManipulator::InsertError(swift::VarDecl *error_var,
m_catch_stmt->setBody(body_stmt);
}


bool SwiftASTManipulator::IsExpressionResultNonCopyable() {
VariableInfo *result_var =
llvm::find_if(m_variables, [&](const VariableInfo &var) {
return var.GetName().str() == GetResultName();
});
if (result_var == m_variables.end())
return false;

swift::VarDecl *decl = result_var->GetDecl();
if (!decl)
return false;

swift::Type type = decl->getTypeInContext();
return type->isNoncopyable();
}

llvm::Error SwiftASTManipulator::FixupResultAfterTypeChecking() {
if (!IsValid())
return llvm::createStringError(llvm::inconvertibleErrorCode(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,12 @@ class SwiftASTManipulator : public SwiftASTManipulatorBase {
bool make_private = true,
swift::DeclContext *decl_ctx = nullptr);

/// Returns true if $__lldb_result is a non copyable type.
/// Currently LLDB cannot deal with expressions whose result is a non copyable
/// type, this function exists so LLDB can diagnose this situation and provide
/// a proper error message.
bool IsExpressionResultNonCopyable();

llvm::Error FixupResultAfterTypeChecking();

static const char *GetArgumentName() { return "$__lldb_arg"; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1843,6 +1843,17 @@ SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager,
m_options.GetPlaygroundTransformHighPerformance());
}

/// Currently LLDB cannot deal with expressions whose result is a non copyable
/// type, because there's no easy way to assign $__lldb_result to the result
/// of the expression.
if (parsed_expr->code_manipulator &&
parsed_expr->code_manipulator->IsExpressionResultNonCopyable()) {
diagnostic_manager.PutString(
eSeverityError,
"Cannot evaluate an expression that results in a ~Copyable type");
return ParseResult::unrecoverable_error;
}

// FIXME: We now should have to do the name binding and type
// checking again, but there should be only the result
// variable to bind up at this point.
Expand Down
3 changes: 3 additions & 0 deletions lldb/test/API/lang/swift/noncopyable/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SWIFT_SOURCES := main.swift

include Makefile.rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import lldb
from lldbsuite.test.lldbtest import *
from lldbsuite.test.decorators import *
import lldbsuite.test.lldbutil as lldbutil
import unittest2


class TestSwiftNonCopyableTypeError(TestBase):
@skipUnlessDarwin
@swiftTest
def test(self):
self.build()
target, process, thread, _ = lldbutil.run_to_source_breakpoint(
self, "break here", lldb.SBFileSpec("main.swift")
)

self.expect("expression s", substrs=["Cannot evaluate an expression that results in a ~Copyable type"], error=True)

10 changes: 10 additions & 0 deletions lldb/test/API/lang/swift/noncopyable/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
struct S: ~Copyable {
let i = 42
}

func f() {
let s = S()
print("break here")
}

f()