Skip to content

Commit

Permalink
Don't instrument "self" in a constructor.
Browse files Browse the repository at this point in the history
Instrumenting "self" might make the constructor
use "self" before calling its parent's constructor,
causing a SIL error.

Added a matching testcase.

<rdar://problem/17765562>


Swift SVN r20660
  • Loading branch information
scallanan committed Jul 29, 2014
1 parent f22ad4c commit 0159a14
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/Sema/PlaygroundTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,12 @@ class Instrumenter {
// log*() functions return a newly-created log expression to be inserted
// after or instead of the expression they're looking at.
Expr *logVarDecl(VarDecl *VD) {
if (llvm::dyn_cast<ConstructorDecl>(TypeCheckDC) &&
VD->getNameStr().equals("self")) {
// Don't log "self" in a constructor
return nullptr;
}

return buildLoggerCall(
new (Context) DeclRefExpr(ConcreteDeclRef(VD),
SourceLoc(),
Expand All @@ -586,6 +592,13 @@ class Instrumenter {

Expr *logDeclRef(DeclRefExpr *DRE) {
VarDecl *VD = llvm::cast<VarDecl>(DRE->getDecl());

if (llvm::dyn_cast<ConstructorDecl>(TypeCheckDC) &&
VD->getNameStr().equals("self")) {
// Don't log "self" in a constructor
return nullptr;
}

return buildLoggerCall(
new (Context) DeclRefExpr(ConcreteDeclRef(VD),
SourceLoc(),
Expand Down
26 changes: 26 additions & 0 deletions test/PlaygroundTransform/init.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// RUN: rm -rf %t
// RUN: mkdir %t
// RUN: cp %s %t/main.swift
// RUN: %target-build-swift -Xfrontend -playground -Xfrontend -debugger-support -o %t/main %S/Inputs/PlaygroundsRuntime.swift %t/main.swift
// RUN: %target-run %t/main | FileCheck %s
class B {
init() {
}
}

class C : B {
var i : Int
var j : Int
override init() {
i = 3
j = 5
i + j
}
}
let c = C()
// CHECK: [{{.*}}] $builtin_log_scope_entry
// CHECK-NEXT: [{{.*}}] $builtin_log[='8']
// CHECK-NEXT: [{{.*}}] $builtin_log_scope_exit
// CHECK-NEXT: [{{.*}}] $builtin_log_scope_entry
// CHECK-NEXT: [{{.*}}] $builtin_log_scope_exit
// CHECK-NEXT: [{{.*}}] $builtin_log[c='main.C']

0 comments on commit 0159a14

Please sign in to comment.