From 591d58a3bb9ea3afea0c898564d972b822212674 Mon Sep 17 00:00:00 2001 From: Dmitriy Vyukov Date: Thu, 13 Jun 2013 16:03:58 +0400 Subject: cmd/gc: properly race-instrument for loops Instrumentation of ntest expression should go to ntest->init. Same for nincr. Fixes #5340. R=golang-dev, daniel.morsing CC=golang-dev https://golang.org/cl/10026046 --- src/pkg/runtime/race/testdata/mop_test.go | 88 ++++++++++++++++++++++++ src/pkg/runtime/race/testdata/regression_test.go | 17 +++++ 2 files changed, 105 insertions(+) (limited to 'src/pkg/runtime') diff --git a/src/pkg/runtime/race/testdata/mop_test.go b/src/pkg/runtime/race/testdata/mop_test.go index d221f444e3..620b7ab6e4 100644 --- a/src/pkg/runtime/race/testdata/mop_test.go +++ b/src/pkg/runtime/race/testdata/mop_test.go @@ -306,6 +306,94 @@ func TestRaceRange(t *testing.T) { } } +func TestRaceForInit(t *testing.T) { + c := make(chan int) + x := 0 + go func() { + c <- x + }() + for x = 42; false; { + } + <-c +} + +func TestNoRaceForInit(t *testing.T) { + done := make(chan bool) + c := make(chan bool) + x := 0 + go func() { + for { + _, ok := <-c + if !ok { + done <- true + return + } + x++ + } + }() + i := 0 + for x = 42; i < 10; i++ { + c <- true + } + close(c) + <-done +} + +func TestRaceForTest(t *testing.T) { + done := make(chan bool) + c := make(chan bool) + stop := false + go func() { + for { + _, ok := <-c + if !ok { + done <- true + return + } + stop = true + } + }() + for !stop { + c <- true + } + close(c) + <-done +} + +func TestRaceForIncr(t *testing.T) { + done := make(chan bool) + c := make(chan bool) + x := 0 + go func() { + for { + _, ok := <-c + if !ok { + done <- true + return + } + x++ + } + }() + for i := 0; i < 10; x++ { + i++ + c <- true + } + close(c) + <-done +} + +func TestNoRaceForIncr(t *testing.T) { + done := make(chan bool) + x := 0 + go func() { + x++ + done <- true + }() + for i := 0; i < 0; x++ { + } + <-done +} + func TestRacePlus(t *testing.T) { var x, y, z int ch := make(chan int, 2) diff --git a/src/pkg/runtime/race/testdata/regression_test.go b/src/pkg/runtime/race/testdata/regression_test.go index 49e03d9082..d461269d98 100644 --- a/src/pkg/runtime/race/testdata/regression_test.go +++ b/src/pkg/runtime/race/testdata/regression_test.go @@ -175,3 +175,20 @@ type inltype struct { func inlinetest(p **inltype) *inltype { return *p } + +type iface interface { + Foo() *struct{ b bool } +} + +type Int int + +func (i Int) Foo() *struct{ b bool } { + return &struct{ b bool }{false} +} + +func TestNoRaceForInfiniteLoop(t *testing.T) { + var x Int + // interface conversion causes nodes to be put on init list + for iface(x).Foo().b { + } +} -- cgit v1.3-5-g9baa