Source file src/cmd/compile/internal/types2/format.go

     1  // Copyright 2024 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  // This file implements (error and trace) message formatting support.
     6  
     7  package types2
     8  
     9  import (
    10  	"bytes"
    11  	"cmd/compile/internal/syntax"
    12  	"fmt"
    13  	"strconv"
    14  	"strings"
    15  )
    16  
    17  // quote encloses s in `' quotes, as in `foo', except for _,
    18  // which is left alone.
    19  //
    20  // Use to prevent confusion when user supplied names alter the
    21  // meaning of an error message.
    22  //
    23  // For instance, report
    24  //
    25  //	duplicate method `wanted'
    26  //
    27  // rather than
    28  //
    29  //	duplicate method wanted
    30  //
    31  // Exceptions:
    32  //
    33  //   - don't quote _:
    34  //     `_' is ugly and not necessary
    35  //   - don't quote after a ":" as there's no need for it:
    36  //     undefined name: foo
    37  //   - don't quote if the name is used correctly in a statement:
    38  //     goto L jumps over variable declaration
    39  //
    40  // quote encloses s in `' quotes, as in `foo',
    41  // except for _ which is left alone.
    42  func quote(s string) string {
    43  	if s == "_" {
    44  		// `_' is ugly and not necessary
    45  		return s
    46  	}
    47  	return "`" + s + "'"
    48  }
    49  
    50  func sprintf(qf Qualifier, tpSubscripts bool, format string, args ...any) string {
    51  	for i, arg := range args {
    52  		switch a := arg.(type) {
    53  		case nil:
    54  			arg = "<nil>"
    55  		case operand:
    56  			panic("got operand instead of *operand")
    57  		case *operand:
    58  			arg = operandString(a, qf)
    59  		case syntax.Pos:
    60  			arg = a.String()
    61  		case syntax.Expr:
    62  			arg = ExprString(a)
    63  		case []syntax.Expr:
    64  			var buf strings.Builder
    65  			buf.WriteByte('[')
    66  			for i, x := range a {
    67  				if i > 0 {
    68  					buf.WriteString(", ")
    69  				}
    70  				buf.WriteString(ExprString(x))
    71  			}
    72  			buf.WriteByte(']')
    73  			arg = buf.String()
    74  		case Object:
    75  			arg = ObjectString(a, qf)
    76  		case Type:
    77  			var buf bytes.Buffer
    78  			w := newTypeWriter(&buf, qf)
    79  			w.tpSubscripts = tpSubscripts
    80  			w.typ(a)
    81  			arg = buf.String()
    82  		case []Type:
    83  			var buf bytes.Buffer
    84  			w := newTypeWriter(&buf, qf)
    85  			w.tpSubscripts = tpSubscripts
    86  			buf.WriteByte('[')
    87  			for i, x := range a {
    88  				if i > 0 {
    89  					buf.WriteString(", ")
    90  				}
    91  				w.typ(x)
    92  			}
    93  			buf.WriteByte(']')
    94  			arg = buf.String()
    95  		case []*TypeParam:
    96  			var buf bytes.Buffer
    97  			w := newTypeWriter(&buf, qf)
    98  			w.tpSubscripts = tpSubscripts
    99  			buf.WriteByte('[')
   100  			for i, x := range a {
   101  				if i > 0 {
   102  					buf.WriteString(", ")
   103  				}
   104  				w.typ(x)
   105  			}
   106  			buf.WriteByte(']')
   107  			arg = buf.String()
   108  		}
   109  		args[i] = arg
   110  	}
   111  	return fmt.Sprintf(format, args...)
   112  }
   113  
   114  // check may be nil.
   115  func (check *Checker) sprintf(format string, args ...any) string {
   116  	var qf Qualifier
   117  	if check != nil {
   118  		qf = check.qualifier
   119  	}
   120  	return sprintf(qf, false, format, args...)
   121  }
   122  
   123  func (check *Checker) trace(pos syntax.Pos, format string, args ...any) {
   124  	fmt.Printf("%s:\t%s%s\n",
   125  		pos,
   126  		strings.Repeat(".  ", check.indent),
   127  		sprintf(check.qualifier, true, format, args...),
   128  	)
   129  }
   130  
   131  // dump is only needed for debugging
   132  func (check *Checker) dump(format string, args ...any) {
   133  	fmt.Println(sprintf(check.qualifier, true, format, args...))
   134  }
   135  
   136  func (check *Checker) qualifier(pkg *Package) string {
   137  	// Qualify the package unless it's the package being type-checked.
   138  	if pkg != check.pkg {
   139  		if check.pkgPathMap == nil {
   140  			check.pkgPathMap = make(map[string]map[string]bool)
   141  			check.seenPkgMap = make(map[*Package]bool)
   142  			check.markImports(check.pkg)
   143  		}
   144  		// If the same package name was used by multiple packages, display the full path.
   145  		if len(check.pkgPathMap[pkg.name]) > 1 {
   146  			return strconv.Quote(pkg.path)
   147  		}
   148  		return pkg.name
   149  	}
   150  	return ""
   151  }
   152  
   153  // markImports recursively walks pkg and its imports, to record unique import
   154  // paths in pkgPathMap.
   155  func (check *Checker) markImports(pkg *Package) {
   156  	if check.seenPkgMap[pkg] {
   157  		return
   158  	}
   159  	check.seenPkgMap[pkg] = true
   160  
   161  	forName, ok := check.pkgPathMap[pkg.name]
   162  	if !ok {
   163  		forName = make(map[string]bool)
   164  		check.pkgPathMap[pkg.name] = forName
   165  	}
   166  	forName[pkg.path] = true
   167  
   168  	for _, imp := range pkg.imports {
   169  		check.markImports(imp)
   170  	}
   171  }
   172  
   173  // stripAnnotations removes internal (type) annotations from s.
   174  func stripAnnotations(s string) string {
   175  	var buf strings.Builder
   176  	for _, r := range s {
   177  		// strip #'s and subscript digits
   178  		if r < '₀' || '₀'+10 <= r { // '₀' == U+2080
   179  			buf.WriteRune(r)
   180  		}
   181  	}
   182  	if buf.Len() < len(s) {
   183  		return buf.String()
   184  	}
   185  	return s
   186  }
   187  

View as plain text