Skip to content
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

unsafe.Sizeof and unsafe.Alignof on numeric constants #799

Closed
edsrzf opened this issue May 23, 2010 · 4 comments
Closed

unsafe.Sizeof and unsafe.Alignof on numeric constants #799

edsrzf opened this issue May 23, 2010 · 4 comments

Comments

@edsrzf
Copy link

edsrzf commented May 23, 2010

unsafe.Sizeof gives a strange result on a numeric constant. For example:
println(unsafe.Sizeof(3))
Output: -1000000000

unsafe.Alignof on a numeric constant doesn't compile and produces:
<epoch>: internal compiler error: dowidth: unknown type: ideal

For comparison, unsafe.Sizeof and unsafe.Alignof on a string literal
produce 16 and 8, respectively.

I think the expected behavior would be a reasonable compile error in both
cases. It might also be acceptable to treat the numeric constant as
whatever type it would become in a short variable declaration. (That is, 12
is an int, 2.5 is a float, 5i is a complex.)

What is your $GOOS?  $GOARCH?
$GOOS: linux
$GOARCH: amd64

Which revision are you using?  (hg identify)
baf75e1c5625+ tip
@peterGo
Copy link
Contributor

peterGo commented May 23, 2010

Comment 1:

"A constant may be given a type explicitly by a constant declaration or conversion,
or implicitly when used in a variable declaration or an assignment or as an operand
in an expression. ... For instance, 3.0 can be given any integer or any
floating-point type, while 2147483648.0 (equal to 1<<31) can be given the types
float32, float64, or uint32 but not int32 or string."
Constants, The Go Programming Language Specification.
http://golang.org/doc/go_spec.html#Constants
Aren't you are asking for the size of and alignment of an untyped constant, which are
undefined? The constant 3 can be given any integer or any floating-point type. Give
it a type e.g. float(3), int64(3), etc. More meaningful error reporting would be nice.
"Sizeof returns the size in bytes occupied by the value v. The size is that of the
"top level" of the value only. For instance, if v is a slice, it returns the size of
the slice descriptor, not the size of the memory referenced by the slice."
func Sizeof. http://golang.org/pkg/unsafe/#Sizeof 
String literals are typed, they are strings. Aren't the size of and alignment of a
string returned by unsafe actually the size of and alignment of the string descriptor?

@edsrzf
Copy link
Author

edsrzf commented May 23, 2010

Comment 2:

The size of an untyped constant is undefined, I agree. If no one does anything about
this part, I can't complain too much.
The internal compiler error definitely seems like a bug to me, though.
Also note that string literals are not typed. I can say:
type S string
var s S = "A string"
If string literals were typed, that would fail to compile. I think it's inconsistent
that I get a somewhat reasonable value for the size of a string literal, but not for
a numeric literal.

@rsc
Copy link
Contributor

rsc commented May 23, 2010

Comment 3:

Owner changed to r...@golang.org.

Status changed to Accepted.

@rsc
Copy link
Contributor

rsc commented May 24, 2010

Comment 4:

changeset:   5525:330d5e8777eb
user:        Russ Cox <rsc@golang.org>
date:        Mon May 24 14:18:15 2010 -0700
summary:     gc: fix unsafe.Sizeof on ideal constants

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

4 participants