Skip to content

Commit fb49cca

Browse files
committed
clang: remove defer in bare blocks (#76)
1 parent b660bea commit fb49cca

10 files changed

Lines changed: 48 additions & 265 deletions

File tree

doc/spec.md

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -919,9 +919,7 @@ When `--track-source` is enabled, the reported source location may be off by a f
919919

920920
## Defer
921921

922-
`defer` schedules a function or method call to run at the end of the enclosing scope.
923-
924-
This scope can be either a function:
922+
`defer` schedules a function or method call to run at the end of the function:
925923

926924
```go
927925
func main() {
@@ -932,22 +930,9 @@ func main() {
932930
}
933931
```
934932

935-
Or a bare block:
936-
937-
```go
938-
func example() {
939-
{
940-
xopen(&state)
941-
defer xclose(&state)
942-
// xclose(&state) runs here, at block end
943-
}
944-
// state is already closed here
945-
}
946-
```
947-
948-
Deferred calls are emitted inline (before returns, panics, and scope end) in LIFO order. The return value is evaluated before the deferred calls run.
933+
Deferred calls are emitted inline (before returns, panics, and function end) in LIFO order. The return value is evaluated before the deferred calls run.
949934

950-
Defer is not supported inside other scopes like `for` or `if`.
935+
Defer can only use variables declared at the top level of a function, not inside nested scopes like bare blocks, `for`, or `if`.
951936

952937
## C interop
953938

internal/clang/visit.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,8 @@ func (g *Generator) walkAST(w io.Writer, root ast.Node) {
9494

9595
// emitBlockStmt emits a bare block statement (scoping block inside a function body).
9696
func (g *Generator) emitBlockStmt(w io.Writer, stmt *ast.BlockStmt) {
97-
defers := g.state.defers // stash outer defers
98-
g.state.defers = nil
9997
fmt.Fprintf(w, "%s{\n", g.indent())
10098
g.emitBlock(w, stmt)
101-
g.state.indent++
102-
g.emitDeferredCalls(w)
103-
g.state.indent--
104-
g.state.defers = defers // restore outer defers
10599
fmt.Fprintf(w, "%s}\n", g.indent())
106100
}
107101

testdata/lang/defer/dst/main.c

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ static so_int funcWithReturn(void);
88
static so_R_int_err funcReturnCall(void);
99
static so_int funcReturnVar(void);
1010
static so_R_int_err funcCalc(void);
11-
static void blockScope(void);
1211

1312
// -- Variables and constants --
1413
static so_int state = 0;
@@ -64,31 +63,6 @@ static so_R_int_err funcCalc(void) {
6463
return (so_R_int_err){.val = 42, .err = (so_Error){0}};
6564
}
6665

67-
static void blockScope(void) {
68-
{
69-
xopen(&state);
70-
if (state != 1) {
71-
xclose(&state);
72-
so_panic("unexpected state");
73-
}
74-
xclose(&state);
75-
}
76-
if (state != 0) {
77-
so_panic("unexpected state");
78-
}
79-
{
80-
xopen(&state);
81-
if (state != 1) {
82-
xclose(&state);
83-
so_panic("unexpected state");
84-
}
85-
xclose(&state);
86-
}
87-
if (state != 0) {
88-
so_panic("unexpected state");
89-
}
90-
}
91-
9266
int main(void) {
9367
funcScope();
9468
if (state != 0) {
@@ -108,9 +82,5 @@ int main(void) {
10882
if (state != 0) {
10983
so_panic("unexpected state");
11084
}
111-
blockScope();
112-
if (state != 0) {
113-
so_panic("unexpected state");
114-
}
11585
return 0;
11686
}

testdata/lang/defer/src/main.go

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -47,29 +47,6 @@ func funcCalc() (int, error) {
4747
return 42, nil
4848
}
4949

50-
func blockScope() {
51-
{
52-
xopen(&state)
53-
defer xclose(&state)
54-
if state != 1 {
55-
panic("unexpected state")
56-
}
57-
}
58-
if state != 0 {
59-
panic("unexpected state")
60-
}
61-
{
62-
xopen(&state)
63-
defer xclose(&state)
64-
if state != 1 {
65-
panic("unexpected state")
66-
}
67-
}
68-
if state != 0 {
69-
panic("unexpected state")
70-
}
71-
}
72-
7350
func main() {
7451
funcScope()
7552
if state != 0 {
@@ -89,8 +66,4 @@ func main() {
8966
if state != 0 {
9067
panic("unexpected state")
9168
}
92-
blockScope()
93-
if state != 0 {
94-
panic("unexpected state")
95-
}
9669
}

0 commit comments

Comments
 (0)