Skip to content

Commit

Permalink
Merge pull request #78564 from swiftlang/eng/chrismiles/playground-tr…
Browse files Browse the repository at this point in the history
…ansform-crash-parsing-nested-generic-func

Fixes crash instrumenting generic func body nested in another func.

The crash was caused by attempting to add logging expressions that capture generic variables while using the outer func decl context that was not the generic decl context of the inner (generic) func.

The fix "pushes" the current func decl context while instrumenting the body. Rather than always using the top-level func decl context for all nested func bodies.
  • Loading branch information
chrismiles authored Jan 15, 2025
2 parents 44a4a67 + f2846ce commit 73b184d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
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
}
}

0 comments on commit 73b184d

Please sign in to comment.