2323package types_test
2424
2525import (
26+ "bytes"
2627 "flag"
2728 "fmt"
2829 "go/ast"
@@ -185,25 +186,58 @@ func eliminate(t *testing.T, errmap map[string][]string, errlist []error) {
185186 }
186187}
187188
188- // goVersionRx matches a Go version string using '_', e.g. "go1_12".
189- var goVersionRx = regexp .MustCompile (`^go[1-9][0-9]*_(0|[1-9][0-9]*)$` )
189+ // parseFlags parses flags from the first line of the given source
190+ // (from src if present, or by reading from the file) if the line
191+ // starts with "//" (line comment) followed by "-" (possiby with
192+ // spaces between). Otherwise the line is ignored.
193+ func parseFlags (filename string , src []byte , flags * flag.FlagSet ) error {
194+ // If there is no src, read from the file.
195+ const maxLen = 256
196+ if len (src ) == 0 {
197+ f , err := os .Open (filename )
198+ if err != nil {
199+ return err
200+ }
190201
191- // asGoVersion returns a regular Go language version string
192- // if s is a Go version string using '_' rather than '.' to
193- // separate the major and minor version numbers (e.g. "go1_12").
194- // Otherwise it returns the empty string.
195- func asGoVersion (s string ) string {
196- if goVersionRx .MatchString (s ) {
197- return strings .Replace (s , "_" , "." , 1 )
202+ var buf [maxLen ]byte
203+ n , err := f .Read (buf [:])
204+ if err != nil {
205+ return err
206+ }
207+ src = buf [:n ]
198208 }
199- return ""
209+
210+ // we must have a line comment that starts with a "-"
211+ const prefix = "//"
212+ if ! bytes .HasPrefix (src , []byte (prefix )) {
213+ return nil // first line is not a line comment
214+ }
215+ src = src [len (prefix ):]
216+ if i := bytes .Index (src , []byte ("-" )); i < 0 || len (bytes .TrimSpace (src [:i ])) != 0 {
217+ return nil // comment doesn't start with a "-"
218+ }
219+ end := bytes .Index (src , []byte ("\n " ))
220+ if end < 0 || end > maxLen {
221+ return fmt .Errorf ("flags comment line too long" )
222+ }
223+
224+ return flags .Parse (strings .Fields (string (src [:end ])))
200225}
201226
202227func testFiles (t * testing.T , sizes Sizes , filenames []string , srcs [][]byte , manual bool , imp Importer ) {
203228 if len (filenames ) == 0 {
204229 t .Fatal ("no source files" )
205230 }
206231
232+ var conf Config
233+ conf .Sizes = sizes
234+ flags := flag .NewFlagSet ("" , flag .PanicOnError )
235+ flags .StringVar (& conf .GoVersion , "lang" , "" , "" )
236+ if err := parseFlags (filenames [0 ], srcs [0 ], flags ); err != nil {
237+ t .Fatal (err )
238+ }
239+
240+ // TODO(gri) remove this or use flag mechanism to set mode if still needed
207241 if strings .HasSuffix (filenames [0 ], ".go1" ) {
208242 // TODO(rfindley): re-enable this test by using GoVersion.
209243 t .Skip ("type params are enabled" )
@@ -222,12 +256,6 @@ func testFiles(t *testing.T, sizes Sizes, filenames []string, srcs [][]byte, man
222256 pkgName = files [0 ].Name .Name
223257 }
224258
225- // if no Go version is given, consider the package name
226- goVersion := * goVersion
227- if goVersion == "" {
228- goVersion = asGoVersion (pkgName )
229- }
230-
231259 listErrors := manual && ! * verifyErrors
232260 if listErrors && len (errlist ) > 0 {
233261 t .Errorf ("--- %s:" , pkgName )
@@ -237,10 +265,6 @@ func testFiles(t *testing.T, sizes Sizes, filenames []string, srcs [][]byte, man
237265 }
238266
239267 // typecheck and collect typechecker errors
240- var conf Config
241- conf .Sizes = sizes
242- conf .GoVersion = goVersion
243-
244268 // special case for importC.src
245269 if len (filenames ) == 1 {
246270 if strings .HasSuffix (filenames [0 ], "importC.src" ) {
0 commit comments