diff --git a/src/cmd/compile/internal/gc/asm_test.go b/src/cmd/compile/internal/gc/asm_test.go index 1ab32f6e244..bac09ef2957 100644 --- a/src/cmd/compile/internal/gc/asm_test.go +++ b/src/cmd/compile/internal/gc/asm_test.go @@ -364,7 +364,19 @@ var linuxAMD64Tests = []*asmTest{ `, []string{"\tMOVQ\t\\$0, \\(.*\\)", "\tMOVQ\t\\$0, 8\\(.*\\)", "\tMOVQ\t\\$0, 16\\(.*\\)"}, }, - // TODO: add a test for *t = T{3,4,5} when we fix that. + // SSA-able composite literal initialization. Issue 18872. + { + ` + type T18872 struct { + a, b, c, d int + } + + func f18872(p *T18872) { + *p = T18872{1, 2, 3, 4} + } + `, + []string{"\tMOVQ\t[$]1", "\tMOVQ\t[$]2", "\tMOVQ\t[$]3", "\tMOVQ\t[$]4"}, + }, // Also test struct containing pointers (this was special because of write barriers). { ` diff --git a/src/cmd/compile/internal/gc/walk.go b/src/cmd/compile/internal/gc/walk.go index 293e18eef09..557293b9f01 100644 --- a/src/cmd/compile/internal/gc/walk.go +++ b/src/cmd/compile/internal/gc/walk.go @@ -1624,7 +1624,7 @@ opswitch: n = cmp case OARRAYLIT, OSLICELIT, OMAPLIT, OSTRUCTLIT, OPTRLIT: - if isStaticCompositeLiteral(n) { + if isStaticCompositeLiteral(n) && !canSSAType(n.Type) { // n can be directly represented in the read-only data section. // Make direct reference to the static data. See issue 12841. vstat := staticname(n.Type) diff --git a/test/writebarrier.go b/test/writebarrier.go index f3149e1b496..55ba81e764e 100644 --- a/test/writebarrier.go +++ b/test/writebarrier.go @@ -238,11 +238,14 @@ var i23 int // f23x: zeroing global needs write barrier for the hybrid barrier. func f23a() { t23 = T23{} // ERROR "write barrier" +} + +func f23b() { // also test partial assignments t23 = T23{a: 1} // ERROR "write barrier" } -func f23b() { +func f23c() { t23 = T23{} // no barrier (dead store) // also test partial assignments t23 = T23{p: &i23} // ERROR "write barrier"