From 9bf5a3135127c3d3cdca6c60f7cd9f4c1cdff155 Mon Sep 17 00:00:00 2001 From: Max Schaefer Date: Wed, 26 Feb 2020 10:10:47 +0000 Subject: [PATCH] Clarify field identity. Like-named fields declared in identical types are identical. This can be a little confusing, since such fields will have multiple declarations and multiple locations, so it's worth calling out explicitly in the documentation. --- ql/src/semmle/go/Scopes.qll | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/ql/src/semmle/go/Scopes.qll b/ql/src/semmle/go/Scopes.qll index ec0f929fe17..efe4871515f 100644 --- a/ql/src/semmle/go/Scopes.qll +++ b/ql/src/semmle/go/Scopes.qll @@ -283,7 +283,24 @@ class ResultVariable extends DeclaredVariable { FuncDef getFunction() { result = fn } } -/** A struct field. */ +/** + * A struct field. + * + * Note that field identity is determined by type identity: if two struct types are identical in + * the sense of the Go language specification (https://golang.org/ref/spec#Type_identity), then + * any of their fields that have the same name are also identical. This, in turn, means that a + * field can have two or more declarations. + * + * For example, consider the following two type declarations: + * + * ```go + * type T1 struct { x int } + * type T2 struct { x int } + * ``` + * + * Types `T1` and `T2` are different, but their underlying struct types are identical. Hence + * the two declarations of `x` refer to the same field. + */ class Field extends Variable { StructType declaringType;