Skip to content

Commit 9dcd042

Browse files
committed
update
Change-Id: Ifad568a8bf9b9da7aadcd2efea27e979740ca428
1 parent 7efcf29 commit 9dcd042

File tree

2 files changed

+14
-52
lines changed

2 files changed

+14
-52
lines changed

src/cmd/compile/internal/devirtualize/devirtualize.go

Lines changed: 7 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@ import (
1616
"cmd/compile/internal/ir"
1717
"cmd/compile/internal/typecheck"
1818
"cmd/compile/internal/types"
19-
"cmd/internal/src"
2019
)
2120

22-
// TODO: update tests to include full errors.
23-
2421
const go125ImprovedConcreteTypeAnalysis = true
2522

2623
// StaticCall devirtualizes the given call if possible when the concrete callee
@@ -154,7 +151,6 @@ func StaticCall(call *ir.CallExpr) {
154151
if base.Flag.LowerM != 0 {
155152
base.WarnfAt(call.Pos(), "partially devirtualizing %v to %v", sel, typ)
156153
}
157-
// TODO: with interleaved inlining and devirtualization we migth get here multiple times for the same call.
158154
call.SetOp(ir.OCALLINTER)
159155
call.Fun = x
160156
default:
@@ -180,8 +176,6 @@ func StaticCall(call *ir.CallExpr) {
180176
typecheck.FixMethodCall(call)
181177
}
182178

183-
const concreteTypeDebug = false
184-
185179
// concreteType determines the concrete type of n, following OCONVIFACEs and type asserts.
186180
// Returns nil when the concrete type could not be determined, or when there are multiple
187181
// (different) types assigned to an interface.
@@ -214,39 +208,19 @@ func concreteType(n ir.Node) (typ *types.Type) {
214208
}
215209

216210
func concreteType1(n ir.Node, analyzed map[*ir.Name]*types.Type, getAssignements func(*ir.Name) []valOrTyp) (out *types.Type, isNil bool) {
217-
nn := n
218-
219-
if concreteTypeDebug {
220-
defer func() {
221-
base.WarnfAt(nn.Pos(), "concreteType1(%v) -> (typ: %v; isNil: %v)", nn, out, isNil)
222-
}()
223-
}
224-
225211
for {
226-
if concreteTypeDebug {
227-
base.WarnfAt(n.Pos(), "concreteType1(%v) current iteration node: %v", nn, n)
228-
}
229-
230212
if !n.Type().IsInterface() {
231-
if n, ok := n.(*ir.CallExpr); ok {
232-
if n.Fun != nil {
233-
results := n.Fun.Type().Results()
234-
base.Assert(len(results) == 1)
235-
retTyp := results[0].Type
236-
if !retTyp.IsInterface() {
237-
// TODO: isnt n.Type going to return that?
238-
return retTyp, false
239-
}
240-
}
241-
}
242213
return n.Type(), false
243214
}
244215

245216
switch n1 := n.(type) {
246217
case *ir.ConvExpr:
247-
// OCONVNOP might change the type, thus check whether they are identical.
248-
// TODO: can this happen for iface?
249-
if n1.Op() == ir.OCONVNOP && types.Identical(n1.Type(), n1.X.Type()) {
218+
if n1.Op() == ir.OCONVNOP {
219+
if !n1.Type().IsInterface() || !types.Identical(n1.Type(), n1.X.Type()) {
220+
// As we check (directly before this switch) wheter n is an interface, thus we should only reach
221+
// here for iface conversions where both operands are the same.
222+
base.Fatalf("not identical/interface types found n1.Type = %v; n1.X.Type = %v", n1.Type(), n1.X.Type())
223+
}
250224
n = n1.X
251225
continue
252226
}
@@ -292,10 +266,6 @@ func concreteType1(n ir.Node, analyzed map[*ir.Name]*types.Type, getAssignements
292266
return nil, false // conservatively assume it's reassigned with a different type indirectly
293267
}
294268

295-
if concreteTypeDebug {
296-
base.WarnfAt(src.NoXPos, "concreteType1(%v) name: %v, analyzing assignements", nn, name)
297-
}
298-
299269
if typ, ok := analyzed[name]; ok {
300270
return typ, false
301271
}
@@ -310,9 +280,6 @@ func concreteType1(n ir.Node, analyzed map[*ir.Name]*types.Type, getAssignements
310280
if len(assignements) == 0 {
311281
// Variable either declared with zero value, or only assigned
312282
// with nil (getAssignements does not return such assignements).
313-
if concreteTypeDebug {
314-
base.WarnfAt(src.NoXPos, "concreteType1(%v) name: %v assigned with only nil values", nn, name)
315-
}
316283
return nil, true
317284
}
318285

@@ -321,23 +288,14 @@ func concreteType1(n ir.Node, analyzed map[*ir.Name]*types.Type, getAssignements
321288
t := v.typ
322289
if v.node != nil {
323290
var isNil bool
324-
if concreteTypeDebug {
325-
base.WarnfAt(src.NoXPos, "concreteType1(%v) name: %v assigned with node %v", nn, name, v.node)
326-
}
327291
t, isNil = concreteType1(v.node, analyzed, getAssignements)
328292
if isNil {
329-
if concreteTypeDebug {
330-
base.WarnfAt(src.NoXPos, "concreteType1(%v) name: %v assigned with nil", nn, name)
331-
}
332293
if t != nil {
333294
base.Fatalf("t = %v; want = <nil>", t)
334295
}
335296
continue
336297
}
337298
}
338-
if concreteTypeDebug {
339-
base.WarnfAt(src.NoXPos, "concreteType1(%v) name: %v assigned with %v", nn, name, t)
340-
}
341299
if t == nil || (typ != nil && !types.Identical(typ, t)) {
342300
return nil, false
343301
}
@@ -375,7 +333,6 @@ func ifaceAssignments(fun *ir.Func) map[*ir.Name][]valOrTyp {
375333
return
376334
}
377335

378-
// TODO: is this needed?
379336
n, ok := ir.OuterValue(name).(*ir.Name)
380337
if !ok {
381338
return
@@ -450,7 +407,7 @@ func ifaceAssignments(fun *ir.Func) map[*ir.Name][]valOrTyp {
450407
} else if call, ok := rhs.(*ir.InlinedCallExpr); ok {
451408
assign(p, valOrTyp{node: call.Result(i)})
452409
} else {
453-
// TODO: can we reach here? Fatal?
410+
// TODO: can we reach here?
454411
assign(p, valOrTyp{})
455412
}
456413
}

test/devirtualization.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ func typeAsserts() {
6262
c.(A).A() // ERROR "devirtualizing" "inlining call"
6363
c.(M).M() // ERROR "devirtualizing" "inlining call"
6464

65+
M(a).M() // ERROR "devirtualizing" "inlining call"
66+
M(M(a)).M() // ERROR "devirtualizing" "inlining call"
67+
68+
a2 := a.(A)
69+
A(a2).A() // ERROR "devirtualizing" "inlining call"
70+
A(A(a2)).A() // ERROR "devirtualizing" "inlining call"
71+
6572
{
6673
var a C = &CImpl{} // ERROR "does not escape"
6774
a.(any).(C).C() // ERROR "devirtualizing" "inlining"
@@ -564,8 +571,6 @@ var (
564571

565572
func globals() {
566573
{
567-
// TODO: why no panic?
568-
// .CurFunc is nil
569574
globalA.A()
570575
globalA.(M).M()
571576
globalM.M()

0 commit comments

Comments
 (0)