Skip to content

Commit 213ccf8

Browse files
authored
Merge pull request #28535 from gottesmm/pr-58a7ab90baf353a5eb3b6ce3f10345d410d8b323
2 parents c036ea0 + a31d550 commit 213ccf8

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

include/swift/AST/DiagnosticsSIL.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,11 @@ ERROR(polymorphic_builtin_passed_type_without_static_overload, none, "Static"
541541
"overload implied by passing argument of type %2",
542542
(Identifier, StringRef, Type))
543543

544+
ERROR(box_to_stack_cannot_promote_box_to_stack_due_to_escape_alloc, none,
545+
"Can not promote value from heap to stack due to value escaping", ())
546+
NOTE(box_to_stack_cannot_promote_box_to_stack_due_to_escape_location, none,
547+
"value escapes here", ())
548+
544549
#ifndef DIAG_NO_UNDEF
545550
# if defined(DIAG)
546551
# undef DIAG

lib/SILOptimizer/Transforms/AllocBoxToStack.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#define DEBUG_TYPE "allocbox-to-stack"
14+
#include "swift/AST/DiagnosticsSIL.h"
1415
#include "swift/SIL/ApplySite.h"
1516
#include "swift/SIL/Dominance.h"
1617
#include "swift/SIL/SILArgument.h"
@@ -354,6 +355,12 @@ findUnexpectedBoxUse(SILValue Box, bool examinePartialApply,
354355
return nullptr;
355356
}
356357

358+
template <typename... T, typename... U>
359+
static InFlightDiagnostic diagnose(ASTContext &Context, SourceLoc loc,
360+
Diag<T...> diag, U &&... args) {
361+
return Context.Diags.diagnose(loc, diag, std::forward<U>(args)...);
362+
}
363+
357364
/// canPromoteAllocBox - Can we promote this alloc_box to an alloc_stack?
358365
static bool canPromoteAllocBox(AllocBoxInst *ABI,
359366
SmallVectorImpl<Operand *> &PromotedOperands) {
@@ -369,6 +376,21 @@ static bool canPromoteAllocBox(AllocBoxInst *ABI,
369376
<< ABI->getFunction()->getName() << ": " << *ABI
370377
<< " Due to user: " << *User << "\n");
371378

379+
// Check if the vardecl has a "boxtostack.mustbeonstack" attribute. If so,
380+
// emit a diagnostic.
381+
if (auto *decl = ABI->getDecl()) {
382+
if (decl->hasSemanticsAttr("boxtostack.mustbeonstack")) {
383+
auto allocDiag =
384+
diag::box_to_stack_cannot_promote_box_to_stack_due_to_escape_alloc;
385+
diagnose(ABI->getModule().getASTContext(), ABI->getLoc().getSourceLoc(),
386+
allocDiag);
387+
auto escapeNote = diag::
388+
box_to_stack_cannot_promote_box_to_stack_due_to_escape_location;
389+
diagnose(ABI->getModule().getASTContext(),
390+
User->getLoc().getSourceLoc(), escapeNote);
391+
}
392+
}
393+
372394
return false;
373395
}
374396

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: %target-swift-frontend %s -o /dev/null -verify -emit-sil
2+
3+
struct Box<T> {
4+
var value: T
5+
}
6+
7+
class Klass {}
8+
9+
func myPrint(_ x: inout Box<Klass>) -> () { print(x) }
10+
11+
func testError() -> (() -> ()) {
12+
@_semantics("boxtostack.mustbeonstack")
13+
var x = Box<Klass>(value: Klass()) // expected-error {{Can not promote value from heap to stack due to value escaping}}
14+
let result = { // expected-note {{value escapes here}}
15+
myPrint(&x)
16+
}
17+
return result
18+
}
19+
20+
func main() {
21+
testError()()
22+
}
23+
24+
main()

0 commit comments

Comments
 (0)