Source file src/simd/archsimd/_gen/specdoc/fill_test.go

     1  // Copyright 2026 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package specdoc
     6  
     7  import (
     8  	"errors"
     9  	"testing"
    10  
    11  	"simd/archsimd/_gen/specgen"
    12  	"simd/archsimd/_gen/specgen/specexpr"
    13  )
    14  
    15  func typeDecl(t *testing.T, name, doc string) *specgen.TypeDecl {
    16  	shape, err := specexpr.ParseTypeName(name)
    17  	if err != nil {
    18  		t.Fatal(err)
    19  	}
    20  	return &specgen.TypeDecl{
    21  		Name:  name,
    22  		Shape: shape.(specexpr.Vector),
    23  		Doc:   []specgen.Paragraph{{Kind: specgen.SpecOwned, Text: doc}},
    24  	}
    25  }
    26  
    27  func TestFillTypeDocs(t *testing.T) {
    28  	idx := specgen.NewIndex(nil, []*specgen.TypeDecl{
    29  		typeDecl(t, "Int32x4", "Int32x4 is a vector."),
    30  		typeDecl(t, "Mask32x4", "Mask32x4 is a mask."),
    31  		typeDecl(t, "Float64s", "Float64s is a scalable vector."),
    32  	})
    33  
    34  	src := `package p
    35  
    36  // Int32x4 has an old doc.
    37  //
    38  // CPU Feature: AVX
    39  type Int32x4 struct{}
    40  
    41  type (
    42  	// Mask32x4 has an old doc.
    43  	Mask32x4 struct{}
    44  
    45  	Float64s struct{}
    46  )
    47  
    48  // X86Features is not described by spec.
    49  type X86Features struct{}
    50  
    51  // int32x4 is unexported.
    52  type int32x4 struct{}
    53  `
    54  	want := `package p
    55  
    56  // Int32x4 is a vector.
    57  //
    58  // CPU Feature: AVX
    59  type Int32x4 struct{}
    60  
    61  type (
    62  	// Mask32x4 is a mask.
    63  	Mask32x4 struct{}
    64  
    65  	// Float64s is a scalable vector.
    66  	Float64s struct{}
    67  )
    68  
    69  // X86Features is not described by spec.
    70  type X86Features struct{}
    71  
    72  // int32x4 is unexported.
    73  type int32x4 struct{}
    74  `
    75  	got, err := Fill([]byte(src), idx, Options{AllowDocRewrite: true})
    76  	if err != nil {
    77  		t.Fatalf("Fill: %v", err)
    78  	}
    79  	if string(got) != want {
    80  		t.Errorf("Fill:\n%s\nwant:\n%s", got, want)
    81  	}
    82  
    83  	// Filling again is idempotent.
    84  	got2, err := Fill(got, idx, Options{AllowDocRewrite: true})
    85  	if err != nil {
    86  		t.Fatalf("second Fill: %v", err)
    87  	}
    88  	if string(got2) != string(got) {
    89  		t.Errorf("second Fill changed source:\n%s", got2)
    90  	}
    91  
    92  	// Without AllowDocRewrite, existing spec-owned type docs are reported.
    93  	_, err = Fill([]byte(src), idx, Options{})
    94  	var rep *Report
    95  	if !errors.As(err, &rep) || len(rep.UnexpectedDocs) != 2 {
    96  		t.Errorf("want 2 UnexpectedDocs, got %v", err)
    97  	}
    98  
    99  	// With RejectUnknown, a generally available archsimd file can't declare
   100  	// vector types missing from spec, but can declare CPU feature types.
   101  	_, err = Fill([]byte("//go:build goexperiment.simdstable && amd64\n\npackage archsimd\n\ntype Int8x16 struct{}\n\ntype X86Features struct{}\n"), idx, Options{RejectUnknown: true})
   102  	if !errors.As(err, &rep) || len(rep.UnknownDecls) != 1 || rep.UnknownDecls[0].D.Name != "Int8x16" {
   103  		t.Errorf("want UnknownDecl for Int8x16, got %v", err)
   104  	}
   105  }
   106  

View as plain text