Skip to content

Commit 223f689

Browse files
committed
reflect: add word-sized type intw that is int on most platforms, but uintptr on AVR platforms
1 parent 8eaa9bd commit 223f689

File tree

5 files changed

+65
-3
lines changed

5 files changed

+65
-3
lines changed

src/reflect/intw.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//go:build !avr
2+
3+
package reflect
4+
5+
// intw is an integer type, used in places where an int is typically required,
6+
// except architectures where the size of an int != word size.
7+
// See https://github.com/tinygo-org/tinygo/issues/1284.
8+
type intw = int

src/reflect/intw_avr.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//go:build avr
2+
3+
package reflect
4+
5+
// intw is an integer type, used in places where an int is typically required,
6+
// except architectures where the size of an int != word size.
7+
// See https://github.com/tinygo-org/tinygo/issues/1284.
8+
type intw = uintptr

src/reflect/intw_avr_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//go:build avr
2+
3+
package reflect_test
4+
5+
import (
6+
"reflect"
7+
"testing"
8+
)
9+
10+
// TestSliceHeaderIntegerSize verifies that SliceHeader.Len and Cap are type uintptr on AVR platforms.
11+
// See https://github.com/tinygo-org/tinygo/issues/1284.
12+
func TestSliceHeaderIntegerSize(t *testing.T) {
13+
var h reflect.SliceHeader
14+
h.Len = uintptr(0)
15+
h.Cap = uintptr(0)
16+
}
17+
18+
// TestStringHeaderIntegerSize verifies that StringHeader.Len and Cap are type uintptr on AVR platforms.
19+
// See https://github.com/tinygo-org/tinygo/issues/1284.
20+
func TestStringHeaderIntegerSize(t *testing.T) {
21+
var h reflect.StringHeader
22+
h.Len = uintptr(0)
23+
}

src/reflect/intw_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//go:build !avr
2+
3+
package reflect_test
4+
5+
import (
6+
"reflect"
7+
"testing"
8+
)
9+
10+
// TestSliceHeaderIntegerSize verifies that SliceHeader.Len and Cap are type int on non-AVR platforms.
11+
// See https://github.com/tinygo-org/tinygo/issues/1284.
12+
func TestSliceHeaderIntegerSize(t *testing.T) {
13+
var h reflect.SliceHeader
14+
h.Len = int(0)
15+
h.Cap = int(0)
16+
}
17+
18+
// TestStringHeaderIntegerSize verifies that StringHeader.Len and Cap are type int on non-AVR platforms.
19+
// See https://github.com/tinygo-org/tinygo/issues/1284.
20+
func TestStringHeaderIntegerSize(t *testing.T) {
21+
var h reflect.StringHeader
22+
h.Len = int(0)
23+
}

src/reflect/value.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,8 +1578,8 @@ type funcHeader struct {
15781578

15791579
type SliceHeader struct {
15801580
Data uintptr
1581-
Len uintptr
1582-
Cap uintptr
1581+
Len intw
1582+
Cap intw
15831583
}
15841584

15851585
// Slice header that matches the underlying structure. Used for when we switch
@@ -1592,7 +1592,7 @@ type sliceHeader struct {
15921592

15931593
type StringHeader struct {
15941594
Data uintptr
1595-
Len uintptr
1595+
Len intw
15961596
}
15971597

15981598
// Like sliceHeader, this type is used internally to make sure pointer and

0 commit comments

Comments
 (0)