@@ -3177,6 +3177,51 @@ func TestIssue69606b(t *testing.T) {
3177
3177
}
3178
3178
}
3179
3179
3180
+ // TestIssue70394 tests materializing an alias type defined in a package (m/a)
3181
+ // in another package (m/b) where the types for m/b are coming from the compiler,
3182
+ // e.g. `go list -compiled=true ... m/b`.
3183
+ func TestIssue70394 (t * testing.T ) {
3184
+ // TODO(taking): backport https://go.dev/cl/604099 so that this works on 23.
3185
+ testenv .NeedsGo1Point (t , 24 )
3186
+ testenv .NeedsTool (t , "go" ) // requires go list.
3187
+ testenv .NeedsGoBuild (t ) // requires the compiler for export data.
3188
+
3189
+ t .Setenv ("GODEBUG" , "gotypesalias=1" )
3190
+
3191
+ dir := t .TempDir ()
3192
+ overlay := map [string ][]byte {
3193
+ filepath .Join (dir , "go.mod" ): []byte ("module m" ), // go version of the module does not matter.
3194
+ filepath .Join (dir , "a/a.go" ): []byte (`package a; type A = int32` ),
3195
+ filepath .Join (dir , "b/b.go" ): []byte (`package b; import "m/a"; var V a.A` ),
3196
+ }
3197
+ cfg := & packages.Config {
3198
+ Dir : dir ,
3199
+ Mode : packages .NeedTypes , // just NeedsTypes allows for loading export data.
3200
+ Overlay : overlay ,
3201
+ Env : append (os .Environ (), "GOFLAGS=-mod=vendor" , "GOWORK=off" ),
3202
+ }
3203
+ pkgs , err := packages .Load (cfg , "m/b" )
3204
+ if err != nil {
3205
+ t .Fatal (err )
3206
+ }
3207
+ if errs := packages .PrintErrors (pkgs ); errs > 0 {
3208
+ t .Fatalf ("Got %d errors while loading packages." , errs )
3209
+ }
3210
+ if len (pkgs ) != 1 {
3211
+ t .Fatalf ("Loaded %d packages. expected 1" , len (pkgs ))
3212
+ }
3213
+
3214
+ pkg := pkgs [0 ]
3215
+ scope := pkg .Types .Scope ()
3216
+ obj := scope .Lookup ("V" )
3217
+ if obj == nil {
3218
+ t .Fatalf ("Failed to find object %q in package %q" , "V" , pkg )
3219
+ }
3220
+ if _ , ok := obj .Type ().(* types.Alias ); ! ok {
3221
+ t .Errorf ("Object %q has type %q. expected an alias" , obj , obj .Type ())
3222
+ }
3223
+ }
3224
+
3180
3225
// TestNeedTypesInfoOnly tests when NeedTypesInfo was set and NeedSyntax & NeedTypes were not,
3181
3226
// Load should include the TypesInfo of packages properly
3182
3227
func TestLoadTypesInfoWithoutSyntaxOrTypes (t * testing.T ) {
0 commit comments