Skip to content

Commit 9ecc3ee

Browse files
committed
[dev.typealias] cmd/compile: avoid false positive cycles from type aliases
For #18130. Fixes #18640. Change-Id: I26cf1d1b78cca6ef207cc4333f30a9011ef347c9 Reviewed-on: https://go-review.googlesource.com/35831 Run-TryBot: Matthew Dempsky <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent 49b7af8 commit 9ecc3ee

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -339,13 +339,15 @@ func Main() {
339339
// Phase 1: const, type, and names and types of funcs.
340340
// This will gather all the information about types
341341
// and methods but doesn't depend on any of it.
342+
// We also defer type alias declarations until phase 2
343+
// to avoid cycles like #18640.
342344
defercheckwidth()
343345

344346
// Don't use range--typecheck can add closures to xtop.
345347
timings.Start("fe", "typecheck", "top1")
346348
for i := 0; i < len(xtop); i++ {
347349
n := xtop[i]
348-
if op := n.Op; op != ODCL && op != OAS && op != OAS2 {
350+
if op := n.Op; op != ODCL && op != OAS && op != OAS2 && (op != ODCLTYPE || !n.Left.Name.Param.Alias) {
349351
xtop[i] = typecheck(n, Etop)
350352
}
351353
}
@@ -357,7 +359,7 @@ func Main() {
357359
timings.Start("fe", "typecheck", "top2")
358360
for i := 0; i < len(xtop); i++ {
359361
n := xtop[i]
360-
if op := n.Op; op == ODCL || op == OAS || op == OAS2 {
362+
if op := n.Op; op == ODCL || op == OAS || op == OAS2 || op == ODCLTYPE && n.Left.Name.Param.Alias {
361363
xtop[i] = typecheck(n, Etop)
362364
}
363365
}

test/fixedbugs/issue18640.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// compile
2+
3+
// Copyright 2017 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package p
8+
9+
type (
10+
a = b
11+
b struct {
12+
*a
13+
}
14+
15+
c struct {
16+
*d
17+
}
18+
d = c
19+
20+
e = f
21+
f = g
22+
g = []h
23+
h i
24+
i = j
25+
j = e
26+
)

0 commit comments

Comments
 (0)