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

spec: legal conversions from string constant to byte slice are not covered by convertability rules #25991

Open
marat-rkh opened this issue Jun 21, 2018 · 2 comments
Labels
Documentation NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@marat-rkh
Copy link

According to Conversions (as of 21.06.2018), the following programs should be illegal, but it compiles without errors:

package main

func main() {
	_ = []byte("s") // no compile time error
}

Here we have constant value "s" converted to []byte. According to spec, "s" must be Representable by []byte. The only way to explain the absence of error is to suppose that "s" is in the set of values determined by []byte.

The assumption above contradicts with Assignability rules. Consider the following program that fails to compile:

package main

func main() {
	var b []byte
	b = "s" // compile time error
}

So, "s" is not assignable to []byte. As a consequence, "s" is not representable by []byte and "s" is not in the set of values determined by []byte.

@bradfitz
Copy link
Contributor

@marat-rkh
Copy link
Author

marat-rkh commented Jun 21, 2018

Yes, this is the part that covers it, thanks.

However, this is what Conversions section says for constant values:

A constant value x can be converted to type T if x is representable by a value of T. As a special case, an integer constant x can be converted to a string type using the same rule as for non-constant x.

As you can see, conversion from integer constant to string is noted as a special case and is governed by the rules for non-constant x. These rules are described in the section that I missed (Conversions to and from a string type).

But that makes me think that the Conversions to and from a string type is for non-constant values. The section itself does not state explicitly what values the rules are applied to. As a result, it is not obvious that I should consult that section when working with constant values.

Moreover, Conversions section contains the following note:

Specific rules apply to (non-constant) conversions between numeric types or to and from a string type. These conversions may change the representation of x and incur a run-time cost. All other conversions only change the type but not the representation of x.

Again, as far as I can see it is about Conversions to and from a string type.

@ianlancetaylor ianlancetaylor added this to the Unplanned milestone Jun 21, 2018
@ianlancetaylor ianlancetaylor added the NeedsFix The path to resolution is known, but the work has not been done. label Jun 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

4 participants