go spec: restrictions for index and slice expressions
At the moment, gc and gccgo report compile-
time errors for certain constant indexes that
are out of bounds. The spec however requests
a run-time panic for out-of-bounds indexes
(http://tip.golang.org/ref/spec#Indexes).
Document the status quo.
Fixes issue 4231.
https://codereview.appspot.com/6699048/diff/4001/doc/go_spec.html File doc/go_spec.html (right): https://codereview.appspot.com/6699048/diff/4001/doc/go_spec.html#newcode2504 doc/go_spec.html:2504: <li>if <code>x</code> is a <a href="#Constant_expressions">constant expression</a> it must ...
12 years, 4 months ago
(2012-10-15 23:41:58 UTC)
#2
https://codereview.appspot.com/6699048/diff/4001/doc/go_spec.html File doc/go_spec.html (right): https://codereview.appspot.com/6699048/diff/4001/doc/go_spec.html#newcode2504 doc/go_spec.html:2504: <li>if <code>x</code> is a <a href="#Constant_expressions">constant expression</a> it must ...
12 years, 4 months ago
(2012-10-16 03:33:26 UTC)
#3
https://codereview.appspot.com/6699048/diff/4001/doc/go_spec.html
File doc/go_spec.html (right):
https://codereview.appspot.com/6699048/diff/4001/doc/go_spec.html#newcode2504
doc/go_spec.html:2504: <li>if <code>x</code> is a <a
href="#Constant_expressions">constant expression</a> it must be in range
On 2012/10/15 23:41:59, iant wrote:
> This doesn't work, because in general the compiler can not know the length of
a
> slice. That is, the compiler can not whether a non-negative constant
expression
> is in range or not.
Sigh, of course not. I meant to separate the two (after all, I just coded this
in the type checker), but in the rush I missed that this is both for arrays and
slices. Update forthcoming.
The semantics look good to me. Some wording nits. "run time" is a noun, "run-time" ...
12 years, 4 months ago
(2012-10-16 19:04:20 UTC)
#7
The semantics look good to me. Some wording nits.
"run time" is a noun, "run-time" is an adjective.
So "at run time" but "run-time error".
The spec does not have any mention of "compile time" before this CL. It talks
about programs being valid or invalid. Perhaps we should do the same here.
LGTM although i share gri's nervousness. we should probably have a serious talk about how ...
12 years, 4 months ago
(2012-10-16 22:01:25 UTC)
#9
LGTM
although i share gri's nervousness.
we should probably have a serious talk about how much we want to specify what
the compiler catches vs. what the execution catches. consider
var x []int
x[0] = 1
or the even likelier
var m map[int]int
m[0] = 1
the current spec defines that the compiler cannot report this mistake. do we
want to be so prescriptive?
http://codereview.appspot.com/6699048/diff/22002/doc/go_spec.html
File doc/go_spec.html (right):
http://codereview.appspot.com/6699048/diff/22002/doc/go_spec.html#newcode2634
doc/go_spec.html:2634: A <a href="#Constant_expressions">constant</a> index must
not be negative, and if both indexes
s/indexes/indices/
i looked into it because i was unsure. according to chanmbers, indexes is the
plural for the thing in the back of the book, indices for all other uses
including math.
there are a couple of other appearances that should be changed too, to be
consistent
PTAL. I wouldn't want to push it much farther in the spec. We already have ...
12 years, 4 months ago
(2012-10-16 23:23:18 UTC)
#10
PTAL.
I wouldn't want to push it much farther in the spec.
We already have the notion of a constant and of a constant expressions and they
are important for the language as they define what can be used in types (array
lengths), const declarations, and untyped values. For instance, we already say
that array lengths must be constant and >= 0. It seems reasonable to restrict
other related constructs (like indices and make arguments).
There's much more that a good compiler could report at compile-time, and I think
perhaps it should. But I would be hesitant to make it part of the language spec.
As suggested by r, perhaps a serious discussion is warranted.
https://codereview.appspot.com/6699048/diff/22002/doc/go_spec.html
File doc/go_spec.html (right):
https://codereview.appspot.com/6699048/diff/22002/doc/go_spec.html#newcode2634
doc/go_spec.html:2634: A <a href="#Constant_expressions">constant</a> index must
not be negative, and if both indexes
On 2012/10/16 22:01:25, r wrote:
> s/indexes/indices/
> i looked into it because i was unsure. according to chanmbers, indexes is the
> plural for the thing in the back of the book, indices for all other uses
> including math.
> there are a couple of other appearances that should be changed too, to be
> consistent
Done.
Issue 6699048: code review 6699048: go spec: restrictions for index and slice expressions
(Closed)
Created 12 years, 4 months ago by gri
Modified 12 years, 4 months ago
Reviewers:
Base URL:
Comments: 6