@@ -4723,10 +4723,10 @@ func (c *Compiler[_, _]) emitConvert(valueType, targetType sema.Type) {
47234723
47244724func (c * Compiler [_ , _ ]) getOrAddType (ty sema.Type ) uint16 {
47254725 // When compiling an inherited code, if we come across a concrete contract type,
4726- // then use a reference-type to it.
4727- // An inherited code can never refer to the currently compiling contract (i.e: circular imports),
4728- // so it's always safe to treat an inherited contract-variable type as a reference type .
4729- if c .isInheritedCode {
4726+ // then use a reference-type to it if the contract is imported .
4727+ // Non-imported contracts (i.e: the enclosing contract of the current program) should be
4728+ // used as the concrete type, not as a reference.
4729+ if c .isInheritedCode && c . isImportedContract ( ty ) {
47304730 ty = sema .ImportedType (c .Config .MemoryGauge , ty )
47314731 }
47324732
@@ -4745,6 +4745,16 @@ func (c *Compiler[_, _]) getOrAddType(ty sema.Type) uint16 {
47454745 return index
47464746}
47474747
4748+ func (c * Compiler [_ , _ ]) isImportedContract (ty sema.Type ) bool {
4749+ compositeType , isCompositeType := ty .(* sema.CompositeType )
4750+ if ! isCompositeType || compositeType .Kind != common .CompositeKindContract {
4751+ return false
4752+ }
4753+
4754+ _ , isImported := c .addedImports [compositeType .Location ]
4755+ return isImported
4756+ }
4757+
47484758func (c * Compiler [_ , T ]) addCompiledType (ty sema.Type , data T ) uint16 {
47494759 count := len (c .compiledTypes )
47504760 if count >= math .MaxUint16 {
0 commit comments