Skip to content

Commit 380ef6b

Browse files
committed
cmd/compile: simplify {defer,resume}checkwidth logic
This CL extends {defer,resume}checkwidth to support nesting, which simplifies usage. Updates #33658. Change-Id: Ib3ffb8a7cabfae2cbeba74e21748c228436f4726 Reviewed-on: https://go-review.googlesource.com/c/go/+/192721 Run-TryBot: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: Robert Griesemer <gri@golang.org>
1 parent adf20ee commit 380ef6b

File tree

6 files changed

+19
-48
lines changed

6 files changed

+19
-48
lines changed

src/cmd/compile/internal/gc/align.go

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ func dowidth(t *types.Type) {
217217
}
218218

219219
// defer checkwidth calls until after we're done
220-
defercalc++
220+
defercheckwidth()
221221

222222
lno := lineno
223223
if asNode(t.Nod) != nil {
@@ -391,11 +391,7 @@ func dowidth(t *types.Type) {
391391

392392
lineno = lno
393393

394-
if defercalc == 1 {
395-
resumecheckwidth()
396-
} else {
397-
defercalc--
398-
}
394+
resumecheckwidth()
399395
}
400396

401397
// when a type's width should be known, we call checkwidth
@@ -440,24 +436,18 @@ func checkwidth(t *types.Type) {
440436
}
441437

442438
func defercheckwidth() {
443-
// we get out of sync on syntax errors, so don't be pedantic.
444-
if defercalc != 0 && nerrors == 0 {
445-
Fatalf("defercheckwidth")
446-
}
447-
defercalc = 1
439+
defercalc++
448440
}
449441

450442
func resumecheckwidth() {
451-
if defercalc == 0 {
452-
Fatalf("resumecheckwidth")
453-
}
454-
455-
for len(deferredTypeStack) > 0 {
456-
t := deferredTypeStack[len(deferredTypeStack)-1]
457-
deferredTypeStack = deferredTypeStack[:len(deferredTypeStack)-1]
458-
t.SetDeferwidth(false)
459-
dowidth(t)
443+
if defercalc == 1 {
444+
for len(deferredTypeStack) > 0 {
445+
t := deferredTypeStack[len(deferredTypeStack)-1]
446+
deferredTypeStack = deferredTypeStack[:len(deferredTypeStack)-1]
447+
t.SetDeferwidth(false)
448+
dowidth(t)
449+
}
460450
}
461451

462-
defercalc = 0
452+
defercalc--
463453
}

src/cmd/compile/internal/gc/iimport.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -298,21 +298,10 @@ func (r *importReader) doDecl(n *Node) {
298298

299299
// We also need to defer width calculations until
300300
// after the underlying type has been assigned.
301-
//
302-
// TODO(mdempsky): Add nesting support directly to
303-
// {defer,resume}checkwidth? Width calculations are
304-
// already deferred during initial typechecking, but
305-
// not when we're expanding inline function bodies, so
306-
// we currently need to handle both cases here.
307-
deferring := defercalc != 0
308-
if !deferring {
309-
defercheckwidth()
310-
}
301+
defercheckwidth()
311302
underlying := r.typ()
312303
copytype(typenod(t), underlying)
313-
if !deferring {
314-
resumecheckwidth()
315-
}
304+
resumecheckwidth()
316305

317306
if underlying.IsInterface() {
318307
break

src/cmd/compile/internal/gc/main.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,6 @@ func Main(archInit func(*Arch)) {
527527
// We also defer type alias declarations until phase 2
528528
// to avoid cycles like #18640.
529529
// TODO(gri) Remove this again once we have a fix for #25838.
530-
defercheckwidth()
531530

532531
// Don't use range--typecheck can add closures to xtop.
533532
timings.Start("fe", "typecheck", "top1")
@@ -549,7 +548,6 @@ func Main(archInit func(*Arch)) {
549548
xtop[i] = typecheck(n, ctxStmt)
550549
}
551550
}
552-
resumecheckwidth()
553551

554552
// Phase 3: Type check function bodies.
555553
// Don't use range--typecheck can add closures to xtop.
@@ -1035,7 +1033,6 @@ func loadsys() {
10351033

10361034
inimport = true
10371035
typecheckok = true
1038-
defercheckwidth()
10391036

10401037
typs := runtimeTypes()
10411038
for _, d := range runtimeDecls {
@@ -1052,7 +1049,6 @@ func loadsys() {
10521049
}
10531050

10541051
typecheckok = false
1055-
resumecheckwidth()
10561052
inimport = false
10571053
}
10581054

src/cmd/compile/internal/gc/typecheck.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3669,9 +3669,7 @@ func typecheckdef(n *Node) {
36693669
}
36703670

36713671
// regular type declaration
3672-
if Curfn != nil {
3673-
defercheckwidth()
3674-
}
3672+
defercheckwidth()
36753673
n.SetWalkdef(1)
36763674
setTypeNode(n, types.New(TFORW))
36773675
n.Type.Sym = n.Sym
@@ -3682,9 +3680,7 @@ func typecheckdef(n *Node) {
36823680
// but it was reported. Silence future errors.
36833681
n.Type.SetBroke(true)
36843682
}
3685-
if Curfn != nil {
3686-
resumecheckwidth()
3687-
}
3683+
resumecheckwidth()
36883684
}
36893685

36903686
ret:

test/fixedbugs/bug195.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ type I4 interface { // GC_ERROR "invalid recursive type"
1818
I4 // GCCGO_ERROR "interface"
1919
}
2020

21-
type I5 interface {
21+
type I5 interface { // GC_ERROR "invalid recursive type"
2222
I6 // GCCGO_ERROR "interface"
2323
}
2424

25-
type I6 interface { // GC_ERROR "invalid recursive type"
25+
type I6 interface {
2626
I5 // GCCGO_ERROR "interface"
2727
}

test/fixedbugs/issue23823.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
package p
88

9-
type I1 = interface { // ERROR "invalid recursive type"
9+
type I1 = interface {
1010
I2
1111
}
1212

13-
type I2 interface {
13+
type I2 interface { // ERROR "invalid recursive type"
1414
I1
1515
}

0 commit comments

Comments
 (0)