Skip to content

DefiniteInitialization: Error when noncopyable types are conditionally initialized. #66726

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
5 changes: 5 additions & 0 deletions include/swift/AST/DiagnosticsSIL.def
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ ERROR(ivar_not_initialized_by_init_accessor,none,
"property %0 not initialized by init accessor",
(DeclName))

ERROR(noncopyable_dynamic_lifetime_unsupported,none,
"conditional initialization or destruction of noncopyable types is not "
"supported; this variable must be consistently in an initialized or "
"uninitialized state through every code path", ())

ERROR(self_use_before_fully_init,none,
"'self' used in %select{method call|property access}1 %0 before "
"%select{all stored properties are initialized|"
Expand Down
5 changes: 5 additions & 0 deletions lib/SILOptimizer/Mandatory/DefiniteInitialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,11 @@ void LifetimeChecker::doIt() {
} else if (auto *ABI = dyn_cast<AllocBoxInst>(memAddr)) {
ABI->setDynamicLifetime();
}
// We don't support noncopyable types with dynamic lifetimes currently.
if (TheMemory.getType().isMoveOnly()) {
diagnose(Module, TheMemory.getUninitializedValue()->getLoc(),
diag::noncopyable_dynamic_lifetime_unsupported);
}
}
if (!ConditionalDestroys.empty())
handleConditionalDestroys(ControlVariable);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// RUN: %target-swift-frontend -emit-sil -verify %s

enum E: Error { case err }

struct NC: ~Copyable {
let x = 0

deinit { print("deinit") }
}

func chk(_ cond: Bool) throws {
let y: NC // expected-error{{not supported}} expected-warning{{never used}}
if cond {
y = NC()
}
throw E.err
}

try? chk(true)