Skip to content

reflect behavior varies depending on the presence of fmt.Println! #1728

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mark-summerfield opened this issue Apr 22, 2011 · 4 comments
Closed

Comments

@mark-summerfield
Copy link

The following program should output:

-2
-2

But actually outputs

5
-2

But if you uncomment the commented fmt.Println statement it does produce the right
answers (-2, -2)!

So I guess there's a weird reflect bug.

: hg identify
d6903b7fbff4+ weekly/weekly.2011-04-13

I'm on debian testing 64-bit.

package main

import (
    "fmt"
    "reflect"
    )

func main() {
    i := M(5, 1, 2, 3, -2, 4).Int()
    fmt.Println(i)
    f := M(5.0, 1.0, 2.0, 3.0, -2.0, 4.0).Float()
    fmt.Println(f)
}

func M(x interface{}, ys ...interface{}) reflect.Value {
    m := reflect.NewValue(x)
    k := m.Kind()
    for _, y := range ys {
        //fmt.Println(m) // Uncomment this and it gives the right answer!
        v := reflect.NewValue(y)
        switch k {
        case reflect.Int: if v.Int() < m.Int() { m.Set(v) }
        case reflect.Float64: if v.Float() < m.Float() { m.Set(v) }
        }
    }
    return m
}
@rsc
Copy link
Contributor

rsc commented Apr 22, 2011

Comment 1:

Owner changed to @rsc.

Status changed to Accepted.

@gopherbot
Copy link
Contributor

Comment 2 by qyzhai:

reflect: more efficient; cannot Set result of NewValue anymore
http://code.google.com/p/go/source/detail?r=47bd15b1505695064d61a4d9fa6533065f5d5e74

@rsc
Copy link
Contributor

rsc commented Apr 23, 2011

Comment 3:

This doesn't have to do with reflect, although the new
reflect changes make the program not run anymore.
I checked out the version that Mark is running on Linux
and can reproduce the problem.  It is a compiler optimization bug.
I have a workaround but I want to spend a bit more time looking
for the root cause to make sure I fix the real problem.

Status changed to Started.

@rsc
Copy link
Contributor

rsc commented Apr 26, 2011

Comment 5:

This issue was closed by revision bac8f18.

Status changed to Fixed.

@golang golang locked and limited conversation to collaborators Jun 24, 2016
@rsc rsc removed their assignment Jun 22, 2022
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants