-
Notifications
You must be signed in to change notification settings - Fork 346
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update unsafe2 for go1.21, skip faky tests
In go1.21, the reflect rtype definition has been move to internal/abi. We follow this change for maintainability, even if there is no layout change (the go1.20 unsafe2 is compatible with go1.21). We have isolated a few problematic tests which are failing sometimes in go1.21, but work in go1.20, and also in go1.22. Those tests are skipped if in go1.21. A preliminary investigation can not confirm that something is wrong in yaegi, and the problem disappears with go1.22.
- Loading branch information
Showing
4 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
internal/unsafe2/unsafe.go → internal/unsafe2/go1_20_unsafe.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
//go:build go1.21 | ||
// +build go1.21 | ||
|
||
// Package unsafe2 provides helpers to generate recursive struct types. | ||
package unsafe2 | ||
|
||
import ( | ||
"reflect" | ||
"unsafe" | ||
) | ||
|
||
type dummy struct{} | ||
|
||
// DummyType represents a stand-in for a recursive type. | ||
var DummyType = reflect.TypeOf(dummy{}) | ||
|
||
// The following type sizes must match their original definition in Go src/internal/abi/type.go. | ||
type abiType struct { | ||
_ uintptr | ||
_ uintptr | ||
_ uint32 | ||
_ uint8 | ||
_ uint8 | ||
_ uint8 | ||
_ uint8 | ||
_ uintptr | ||
_ uintptr | ||
_ int32 | ||
_ int32 | ||
} | ||
|
||
type abiName struct { | ||
Bytes *byte | ||
} | ||
|
||
type abiStructField struct { | ||
Name abiName | ||
Typ *abiType | ||
Offset uintptr | ||
} | ||
|
||
type abiStructType struct { | ||
abiType | ||
PkgPath abiName | ||
Fields []abiStructField | ||
} | ||
|
||
type emptyInterface struct { | ||
typ *abiType | ||
_ unsafe.Pointer | ||
} | ||
|
||
// SetFieldType sets the type of the struct field at the given index, to the given type. | ||
// | ||
// The struct type must have been created at runtime. This is very unsafe. | ||
func SetFieldType(s reflect.Type, idx int, t reflect.Type) { | ||
if s.Kind() != reflect.Struct || idx >= s.NumField() { | ||
return | ||
} | ||
|
||
rtyp := unpackType(s) | ||
styp := (*abiStructType)(unsafe.Pointer(rtyp)) | ||
f := styp.Fields[idx] | ||
f.Typ = unpackType(t) | ||
styp.Fields[idx] = f | ||
} | ||
|
||
func unpackType(t reflect.Type) *abiType { | ||
v := reflect.New(t).Elem().Interface() | ||
eface := *(*emptyInterface)(unsafe.Pointer(&v)) | ||
return eface.typ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters