Source file src/cmd/compile/internal/types/identity.go

     1  // Copyright 2018 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 types
     6  
     7  const (
     8  	identIgnoreTags = 1 << iota
     9  	identStrict
    10  )
    11  
    12  // Identical reports whether t1 and t2 are identical types, following the spec rules.
    13  // Receiver parameter types are ignored. Named (defined) types are only equal if they
    14  // are pointer-equal - i.e. there must be a unique types.Type for each specific named
    15  // type. Also, a type containing a shape type is considered identical to another type
    16  // (shape or not) if their underlying types are the same, or they are both pointers.
    17  func Identical(t1, t2 *Type) bool {
    18  	return identical(t1, t2, 0, nil)
    19  }
    20  
    21  // IdenticalIgnoreTags is like Identical, but it ignores struct tags
    22  // for struct identity.
    23  func IdenticalIgnoreTags(t1, t2 *Type) bool {
    24  	return identical(t1, t2, identIgnoreTags, nil)
    25  }
    26  
    27  // IdenticalStrict is like Identical, but matches types exactly, without the
    28  // exception for shapes.
    29  func IdenticalStrict(t1, t2 *Type) bool {
    30  	return identical(t1, t2, identStrict, nil)
    31  }
    32  
    33  type typePair struct {
    34  	t1 *Type
    35  	t2 *Type
    36  }
    37  
    38  func identical(t1, t2 *Type, flags int, assumedEqual map[typePair]struct{}) bool {
    39  	if t1 == t2 {
    40  		return true
    41  	}
    42  	if t1 == nil || t2 == nil || t1.kind != t2.kind {
    43  		return false
    44  	}
    45  	if t1.obj != nil || t2.obj != nil {
    46  		if flags&identStrict == 0 && (t1.HasShape() || t2.HasShape()) {
    47  			switch t1.kind {
    48  			case TINT8, TUINT8, TINT16, TUINT16, TINT32, TUINT32, TINT64, TUINT64, TINT, TUINT, TUINTPTR, TCOMPLEX64, TCOMPLEX128, TFLOAT32, TFLOAT64, TBOOL, TSTRING, TPTR, TUNSAFEPTR:
    49  				return true
    50  			}
    51  			// fall through to unnamed type comparison for complex types.
    52  			goto cont
    53  		}
    54  		// Special case: we keep byte/uint8 and rune/int32
    55  		// separate for error messages. Treat them as equal.
    56  		switch t1.kind {
    57  		case TUINT8:
    58  			return (t1 == Types[TUINT8] || t1 == ByteType) && (t2 == Types[TUINT8] || t2 == ByteType)
    59  		case TINT32:
    60  			return (t1 == Types[TINT32] || t1 == RuneType) && (t2 == Types[TINT32] || t2 == RuneType)
    61  		case TINTER:
    62  			// Make sure named any type matches any unnamed empty interface
    63  			// (but not a shape type, if identStrict).
    64  			isUnnamedEface := func(t *Type) bool { return t.IsEmptyInterface() && t.Sym() == nil }
    65  			if flags&identStrict != 0 {
    66  				return t1 == AnyType && isUnnamedEface(t2) && !t2.HasShape() || t2 == AnyType && isUnnamedEface(t1) && !t1.HasShape()
    67  			}
    68  			return t1 == AnyType && isUnnamedEface(t2) || t2 == AnyType && isUnnamedEface(t1)
    69  		default:
    70  			return false
    71  		}
    72  	}
    73  cont:
    74  
    75  	// Any cyclic type must go through a named type, and if one is
    76  	// named, it is only identical to the other if they are the
    77  	// same pointer (t1 == t2), so there's no chance of chasing
    78  	// cycles ad infinitum, so no need for a depth counter.
    79  	if assumedEqual == nil {
    80  		assumedEqual = make(map[typePair]struct{})
    81  	} else if _, ok := assumedEqual[typePair{t1, t2}]; ok {
    82  		return true
    83  	}
    84  	assumedEqual[typePair{t1, t2}] = struct{}{}
    85  
    86  	switch t1.kind {
    87  	case TIDEAL:
    88  		// Historically, cmd/compile used a single "untyped
    89  		// number" type, so all untyped number types were
    90  		// identical. Match this behavior.
    91  		// TODO(mdempsky): Revisit this.
    92  		return true
    93  
    94  	case TINTER:
    95  		if len(t1.AllMethods()) != len(t2.AllMethods()) {
    96  			return false
    97  		}
    98  		for i, f1 := range t1.AllMethods() {
    99  			f2 := t2.AllMethods()[i]
   100  			if f1.Sym != f2.Sym || !identical(f1.Type, f2.Type, flags, assumedEqual) {
   101  				return false
   102  			}
   103  		}
   104  		return true
   105  
   106  	case TSTRUCT:
   107  		if t1.NumFields() != t2.NumFields() {
   108  			return false
   109  		}
   110  		for i, f1 := range t1.Fields() {
   111  			f2 := t2.Field(i)
   112  			if f1.Sym != f2.Sym || f1.Embedded != f2.Embedded || !identical(f1.Type, f2.Type, flags, assumedEqual) {
   113  				return false
   114  			}
   115  			if (flags&identIgnoreTags) == 0 && f1.Note != f2.Note {
   116  				return false
   117  			}
   118  		}
   119  		return true
   120  
   121  	case TFUNC:
   122  		// Check parameters and result parameters for type equality.
   123  		// We intentionally ignore receiver parameters for type
   124  		// equality, because they're never relevant.
   125  		if t1.NumParams() != t2.NumParams() ||
   126  			t1.NumResults() != t2.NumResults() ||
   127  			t1.IsVariadic() != t2.IsVariadic() {
   128  			return false
   129  		}
   130  
   131  		fs1 := t1.ParamsResults()
   132  		fs2 := t2.ParamsResults()
   133  		for i, f1 := range fs1 {
   134  			if !identical(f1.Type, fs2[i].Type, flags, assumedEqual) {
   135  				return false
   136  			}
   137  		}
   138  		return true
   139  
   140  	case TARRAY:
   141  		if t1.NumElem() != t2.NumElem() {
   142  			return false
   143  		}
   144  
   145  	case TCHAN:
   146  		if t1.ChanDir() != t2.ChanDir() {
   147  			return false
   148  		}
   149  
   150  	case TMAP:
   151  		if !identical(t1.Key(), t2.Key(), flags, assumedEqual) {
   152  			return false
   153  		}
   154  	}
   155  
   156  	return identical(t1.Elem(), t2.Elem(), flags, assumedEqual)
   157  }
   158  

View as plain text