@@ -1438,25 +1438,28 @@ next:
1438
1438
// a binding decl or when using the literalization
1439
1439
// strategy.
1440
1440
1441
- // Check if the types are identical to eliminate redundant type conversion.
1441
+ // If the types are identical, we can eliminate
1442
+ // redundant type conversions such as this:
1442
1443
//
1443
- // As trivialConversion is unable to distinguish between typed and untyped constants,
1444
- // it leads to redundant conversions:
1445
- // A callee:
1446
- // func f1(i int32) { print(i) }
1447
- // Called from:
1448
- // func f() { f1(int32(1)) }
1449
- // Will be inlined as:
1450
- // func f() { print(int32(int32(1)))
1444
+ // Callee:
1445
+ // func f(i int32) { print(i) }
1446
+ // Caller:
1447
+ // func g() { f(int32(1)) }
1448
+ // Inlined as:
1449
+ // func g() { print(int32(int32(1)))
1451
1450
//
1452
- // However, at this point arg.typ includes information regarding constants being typed or untyped.
1453
- // This happens due to state.arguments() performing re-typechecking.
1454
- // Hence, the redundant conversion can be eliminated by making a check if types are identical at this stage.
1455
- typesIdentical := types .Identical (arg .typ , param .obj .Type ())
1456
- if len (param .info .Refs ) > 0 && ! typesIdentical && ! trivialConversion (args [i ].constant , args [i ].typ , params [i ].obj .Type ()) {
1457
- arg .expr = convert (params [i ].fieldType , arg .expr )
1451
+ // Recall that non-trivial does not imply non-identical
1452
+ // for constant conversions; however, at this point state.arguments
1453
+ // has already re-typechecked the constant and set arg.type to
1454
+ // its (possibly "untyped") inherent type, so
1455
+ // the conversion from untyped 1 to int32 is non-trivial even
1456
+ // though both arg and param have identical types (int32).
1457
+ if len (param .info .Refs ) > 0 &&
1458
+ ! types .Identical (arg .typ , param .obj .Type ()) &&
1459
+ ! trivialConversion (arg .constant , arg .typ , param .obj .Type ()) {
1460
+ arg .expr = convert (param .fieldType , arg .expr )
1458
1461
logf ("param %q: adding explicit %s -> %s conversion around argument" ,
1459
- param .info .Name , args [ i ] .typ , params [ i ] .obj .Type ())
1462
+ param .info .Name , arg .typ , param .obj .Type ())
1460
1463
}
1461
1464
1462
1465
// It is safe to substitute param and replace it with arg.
0 commit comments