Source file test/fixedbugs/issue54542.go

     1  // run
     2  
     3  //go:build !js && !wasip1
     4  
     5  // Copyright 2024 The Go Authors. All rights reserved.
     6  // Use of this source code is governed by a BSD-style
     7  // license that can be found in the LICENSE file.
     8  
     9  package main
    10  
    11  import (
    12  	"bytes"
    13  	"fmt"
    14  	"os"
    15  	"os/exec"
    16  	"path/filepath"
    17  )
    18  
    19  const aSrc = `package a
    20  
    21  func A() { println("a") }
    22  `
    23  
    24  const mainSrc = `package main
    25  
    26  import "a"
    27  
    28  func main() { a.A() }
    29  `
    30  
    31  var srcs = map[string]string{
    32  	"a.go":    aSrc,
    33  	"main.go": mainSrc,
    34  }
    35  
    36  func main() {
    37  	dir, err := os.MkdirTemp("", "issue54542")
    38  	if err != nil {
    39  		panic(err)
    40  	}
    41  	defer os.RemoveAll(dir)
    42  
    43  	for fn, src := range srcs {
    44  		if err := os.WriteFile(filepath.Join(dir, fn), []byte(src), 0644); err != nil {
    45  			panic(err)
    46  		}
    47  	}
    48  
    49  	if _, err := runInDir(dir, "tool", "compile", "-p=lie", "a.go"); err != nil {
    50  		panic(err)
    51  	}
    52  
    53  	out, err := runInDir(dir, "tool", "compile", "-I=.", "-p=main", "main.go")
    54  	if err == nil {
    55  		panic("compiling succeed unexpectedly")
    56  	}
    57  
    58  	if bytes.Contains(out, []byte("internal compiler error:")) {
    59  		panic(fmt.Sprintf("unexpected ICE:\n%s", string(out)))
    60  	}
    61  }
    62  
    63  func runInDir(dir string, args ...string) ([]byte, error) {
    64  	cmd := exec.Command("go", args...)
    65  	cmd.Dir = dir
    66  	return cmd.CombinedOutput()
    67  }
    68  

View as plain text