Skip to content

Fixes crash instrumenting generic func body nested in another func #78564

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
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 lib/Sema/PCMacro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,12 @@ class Instrumenter : InstrumenterBase {
if (auto *FD = dyn_cast<FuncDecl>(D)) {
if (BraceStmt *B = FD->getTypecheckedBody()) {
const ParameterList *PL = FD->getParameters();

// Use FD's DeclContext as TypeCheckDC for transforms in func body
// then swap back TypeCheckDC at end of scope.
llvm::SaveAndRestore<DeclContext *> localDC(TypeCheckDC, FD);
BraceStmt *NB = transformBraceStmt(B, PL);

// Since it would look strange going straight to the first line in a
// function body, we throw in a before/after pointing at the function
// decl at the start of the transformed body
Expand Down
5 changes: 5 additions & 0 deletions lib/Sema/PlaygroundTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,11 @@ class Instrumenter : InstrumenterBase {
if (BraceStmt *B = FD->getTypecheckedBody()) {
const ParameterList *PL = FD->getParameters();
TargetKindSetter TKS(BracePairs, BracePair::TargetKinds::Return);

// Use FD's DeclContext as TypeCheckDC for transforms in func body
// then swap back TypeCheckDC at end of scope.
llvm::SaveAndRestore<DeclContext *> localDC(TypeCheckDC, FD);

BraceStmt *NB = transformBraceStmt(B, PL);
if (NB != B) {
FD->setBody(NB, AbstractFunctionDecl::BodyKind::TypeChecked);
Expand Down
33 changes: 33 additions & 0 deletions test/PlaygroundTransform/generic_func_in_local_scope.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// RUN: %empty-directory(%t)

// Build PlaygroundSupport module
// RUN: %target-build-swift -whole-module-optimization -module-name PlaygroundSupport -emit-module-path %t/PlaygroundSupport.swiftmodule -parse-as-library -c -o %t/PlaygroundSupport.o %S/Inputs/SilentPCMacroRuntime.swift %S/Inputs/PlaygroundsRuntime.swift

// RUN: %target-build-swift -swift-version 5 -emit-module -Xfrontend -playground -I=%t %s
// RUN: %target-build-swift -swift-version 6 -emit-module -Xfrontend -playground -I=%t %s

// RUN: %target-build-swift -swift-version 5 -emit-module -Xfrontend -playground -Xfrontend -pc-macro -I=%t %s
// RUN: %target-build-swift -swift-version 6 -emit-module -Xfrontend -playground -Xfrontend -pc-macro -I=%t %s

// REQUIRES: executable_test

import PlaygroundSupport

func test1() {
func buildBlock<Content>(content: Content) {
content
}
}

func test2() {
func buildBlock<Content>(content: Content) -> Content {
content
return content
}
}

func test3<Content>(_ content: Content) {
func buildBlock<Content2>(_ content2: Content2) -> Content2 {
return content2
}
}