Skip to content

[5.9🍒] improve noncopyable closure diags #66588

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
7 changes: 4 additions & 3 deletions include/swift/AST/DiagnosticsSIL.def
Original file line number Diff line number Diff line change
Expand Up @@ -766,9 +766,10 @@ ERROR(sil_movechecking_borrowed_parameter_captured_by_closure, none,
"parameter",
(StringRef))
ERROR(sil_movechecking_capture_consumed, none,
"noncopyable '%0' cannot be consumed when captured by a closure", (StringRef))
ERROR(sil_movechecking_inout_not_reinitialized_before_end_of_function, none,
"missing reinitialization of inout parameter '%0' after consume", (StringRef))
"noncopyable '%0' cannot be consumed when captured by an escaping closure", (StringRef))
ERROR(sil_movechecking_not_reinitialized_before_end_of_function, none,
"missing reinitialization of %select{inout parameter|closure capture}1 '%0' "
"after consume", (StringRef, bool))
ERROR(sil_movechecking_value_consumed_in_a_loop, none,
"'%0' consumed in a loop", (StringRef))
ERROR(sil_movechecking_use_after_partial_consume, none,
Expand Down
21 changes: 17 additions & 4 deletions lib/SILOptimizer/Mandatory/MoveOnlyDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,19 @@ void DiagnosticEmitter::emitObjectDiagnosticsForPartialApplyUses(
// MARK: Address Diagnostics
//===----------------------------------------------------------------------===//

static bool isClosureCapture(MarkMustCheckInst *markedValue) {
SILValue val = markedValue->getOperand();

// look past any project-box
if (auto *pbi = dyn_cast<ProjectBoxInst>(val))
val = pbi->getOperand();

if (auto *fArg = dyn_cast<SILFunctionArgument>(val))
return fArg->isClosureCapture();

return false;
}

void DiagnosticEmitter::emitAddressExclusivityHazardDiagnostic(
MarkMustCheckInst *markedValue, SILInstruction *consumingUser) {
if (!useWithDiagnostic.insert(consumingUser).second)
Expand Down Expand Up @@ -596,8 +609,8 @@ void DiagnosticEmitter::emitAddressDiagnostic(MarkMustCheckInst *markedValue,
diagnose(
astContext, markedValue,
diag::
sil_movechecking_inout_not_reinitialized_before_end_of_function,
varName);
sil_movechecking_not_reinitialized_before_end_of_function,
varName, isClosureCapture(markedValue));
diagnose(astContext, violatingUser,
diag::sil_movechecking_consuming_use_here);
return;
Expand Down Expand Up @@ -647,8 +660,8 @@ void DiagnosticEmitter::emitInOutEndOfFunctionDiagnostic(
// consuming message:
diagnose(
astContext, markedValue,
diag::sil_movechecking_inout_not_reinitialized_before_end_of_function,
varName);
diag::sil_movechecking_not_reinitialized_before_end_of_function,
varName, isClosureCapture(markedValue));
diagnose(astContext, violatingUser,
diag::sil_movechecking_consuming_use_here);
}
Expand Down
112 changes: 56 additions & 56 deletions test/SILGen/moveonly_escaping_closure.swift

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ bb3:
%23 = apply %22(%21) : $@convention(thin) (@guaranteed S) -> ()
destroy_value %21 : $S
%25 = mark_must_check [assignable_but_not_consumable] %3 : $*S
// expected-error @-1 {{noncopyable 's' cannot be consumed when captured by a closure}}
// expected-error @-1 {{noncopyable 's' cannot be consumed when captured by an escaping closure}}
%26 = load [take] %25 : $*S
%27 = function_ref @consumeVal : $@convention(thin) (@owned S) -> ()
%28 = apply %27(%26) : $@convention(thin) (@owned S) -> ()
Expand Down
2 changes: 1 addition & 1 deletion test/SILOptimizer/discard_checking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ struct Basics: ~Copyable {

// FIXME move checker is treating the defer like a closure capture (rdar://100468597)
// not expecting any errors here
consuming func brokenPositiveTest(_ i: Int) { // expected-error {{missing reinitialization of inout parameter 'self' after consume}}
consuming func brokenPositiveTest(_ i: Int) { // expected-error {{missing reinitialization of closure capture 'self' after consume}}
defer { discard self } // expected-note {{consumed here}}
switch i {
case 0, 1, 2, 3: return
Expand Down
4 changes: 2 additions & 2 deletions test/SILOptimizer/moveonly_addresschecker_diagnostics.sil
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ bb0(%0 : @closureCapture @guaranteed $<τ_0_0 where τ_0_0 : P> { var AddressOnl
end_access %12 : $*AddressOnlyGeneric<T>
%17 = begin_access [deinit] [dynamic] %1 : $*AddressOnlyGeneric<T>
%18 = mark_must_check [assignable_but_not_consumable] %17 : $*AddressOnlyGeneric<T>
// expected-error @-1 {{noncopyable 'x' cannot be consumed when captured by a closure}}
// expected-error @-1 {{noncopyable 'x' cannot be consumed when captured by an escaping closure}}
%19 = function_ref @addressOnlyGenericConsume : $@convention(thin) <τ_0_0 where τ_0_0 : P> (@in AddressOnlyGeneric<τ_0_0>) -> ()
%20 = apply %19<T>(%18) : $@convention(thin) <τ_0_0 where τ_0_0 : P> (@in AddressOnlyGeneric<τ_0_0>) -> ()
end_access %17 : $*AddressOnlyGeneric<T>
Expand Down Expand Up @@ -528,4 +528,4 @@ bb0(%0 : @owned $NonTrivialStruct):
dealloc_stack %1 : $*NonTrivialStruct
%9999 = tuple()
return %9999 : $()
}
}
Loading