Skip to content

Commit e12788e

Browse files
owen-mcCopilot
andcommitted
Go: reduce intermediate tuples in embedded method resolution
Materialize embedded fields and their types before joining them with methods. Keep the helper non-inline and non-magic to avoid expanding field-type lookups across all fields. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 1a8dbb2 commit e12788e

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

‎go/ql/lib/semmle/go/Types.qll‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -517,17 +517,24 @@ class StructType extends @structtype, CompositeType {
517517
.getOwnField(name, isEmbedded)
518518
}
519519

520+
// Materialize the embedded fields and their types before joining them with methods.
521+
pragma[noinline]
522+
pragma[nomagic]
523+
private Type getEmbeddedFieldType(Field embeddedParent, int depth) {
524+
this.hasFieldCand(_, embeddedParent, depth, true) and
525+
result = embeddedParent.getType()
526+
}
527+
520528
/**
521529
* Gets a method of `embeddedParent`, which is then embedded into this struct type.
522530
*/
523531
Method getMethodOfEmbedded(Field embeddedParent, string name, int depth) {
524-
// embeddedParent is a field of 'this' at depth 'depth - 1'
525-
this.hasFieldCand(_, embeddedParent, depth - 1, true) and
526532
result.getName() = name and
527-
(
528-
result.getReceiverBaseType() = lookThroughPointerType(embeddedParent.getType())
533+
// embeddedParent is a field of 'this' at depth 'depth - 1'
534+
exists(Type embeddedType | embeddedType = this.getEmbeddedFieldType(embeddedParent, depth - 1) |
535+
result.getReceiverBaseType() = lookThroughPointerType(embeddedType)
529536
or
530-
methodhosts(result, embeddedParent.getType())
537+
methodhosts(result, embeddedType)
531538
)
532539
}
533540

0 commit comments

Comments
 (0)