summaryrefslogtreecommitdiff
path: root/test/bugs/bug132.go
blob: 58ebfcb4487fc2370daa04a40bab162723d7d463 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// errchk $G $D/$F.go

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

type T struct {
	x, x int  // this should be a compile-time error
}

/*
Accessing obj.x for obj of type T will lead to an error so this cannot
be used in a program, but I would argue that this should be a compile-
tume error at the declaration point.
*/

/* Condensed e-mail thread:

---------- Russ Cox	
I don't think this is an error as long as you don't refer to x. I like the fact that you could name
multiple elements in the struct "pad".


---------- Rob 'Commander' Pike to Russ, me, go-dev, reviewlog2
the real question is whether this program matches the spec and if not, which is in error.


---------- Russ Cox to Rob, me, go-dev, reviewlog2
true enough.  the spec disagrees with 6g.
when we discussed the disambiguation
rules for anonymous structs i thought we'd
mentioned this issue too and decided the
opposite, but i'm happy to make 6g agree
with the spec.


---------- Robert Griesemer to Russ, Rob, go-dev, reviewlog2
I think the spec could perhaps be more definitive. Note that 6g also accepts:

type T struct {
	x int
}

func (p *T) x() {
}

func (p *T) x() {
}

The spec says that the scope of methods and fields is selectors of the form obj.selector. In a scope an identifier can be declared only once. I'd conclude that in the scope of fields and selectors of T, there are multiple x. But it's somewhat indirect. From a programmer's point of view making this an error seems less surprising, at least to me.


---------- Ken Thompson to me, Russ, Rob, go-dev, reviewlog2
obviously i dont think this is an error, or
i would have made it an error. it seems like
a small point if the error comes up at
declaration or use.


---------- Robert Griesemer to Ken, Russ, Rob, go-dev, reviewlog2
I don't really care too much, but I think it should be consistent. The following code:

type T struct {
	x int;
}

func (p *T) x() {
}

func (p *T) x(a, b, c int) {
}

does result in an error (method redeclared). I don't quite see why this should behave any different then if both x() had the same parameter list.

PS: I agree that the spec is saying two different things here - or at least should be more precise; the section on selectors (which arguably is the newer section), would allow such a declaration. I suspect that the case below leads to an early error possibly due to some interaction with code that checks for forward declaration of functions/methods (this not having looked at 6g).

I am happy to go either way. It's a small item and we can close it once we all agree.
*/