-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
reflect: bad test cases in TestCallPanic? #22053
Comments
Change https://golang.org/cl/66331 mentions this issue: |
I think we've worked our way into a confusing and unfortunate state. CC @crawshaw |
If that's the case, that's confusing and unfortunate indeed, but I believe to be a separate issue than what I'm reporting here. |
Looking at your play example, I think that program should run. The method F is exported. If you return the type it is declared on as an interface{}, you can, without reflect, cast that to an interface{ F() } and call the method. The same logic follows for your inline example. The type t0 could be returned as an interface{}, and via a new interface{ W() } declared in the calling package, we can access W. I need to think harder about Ian's comment, which looks unrelated. Without digging, I think neither reflect.Type.Method nor reflect.Value.Method should enumerate unexported methods. |
@crawshaw Without reflection or package-local access, you can't access the embedded field |
Got it. But I still don't follow why your play example should fail. If I have RO access to |
The reflect API appears to consider calls to be write operations. I suppose because they can't be proven safe (e.g., maybe it's an internal helper function that isn't safe for arbitrary user calls). Here's a corresponding example except without embedding that also panics because Call was used on a value obtained via unexported fields: https://play.golang.org/p/zUbHj0-_YF |
Oh I see, I mistakenly thought we were calling the method on a copy of the value in the unexported field. Yes, I agree with your change. |
@ianlancetaylor Following up on your earlier comment, was there other evidence that Type.Method and Value.Method are behaving inconsistently? (I just took a quick look at the code, and I don't think that to be the case.) |
When I run the following program, printing
|
In
TestCallPanic
, there are these two related test cases that are expected to execute without panicking:The first case makes sense to me: it's calling the method
W
that has been promoted toT
via embeddingt0
.However, the second case seems iffy to me: after the Elem() call, we're accessing the dynamic value and
Method
is indexing its complete method set, so we're able to call methods that wouldn't be accessible otherwise.For example, this program currently runs ok, though I think it should fail: https://play.golang.org/p/QHa7dTEIzJ
Related to #22031. Easily fixable at the same time, assuming others agree that this is an issue.
The text was updated successfully, but these errors were encountered: