Skip to content

[Compiler] Fix compiling inherited code from same contract - #4520

Merged
SupunS merged 1 commit into
masterfrom
supun/fix-inherited-code-from-same-contract
Jul 28, 2026
Merged

[Compiler] Fix compiling inherited code from same contract#4520
SupunS merged 1 commit into
masterfrom
supun/fix-inherited-code-from-same-contract

Conversation

@SupunS

@SupunS SupunS commented Jul 28, 2026

Copy link
Copy Markdown
Member

Work towards #3804

Description

Problem

Compiling inherited code (e.g: pre/post condition) always treated contract variables as references (i.e: Foo in an expression Foo.hello(), where Foo is a contract). This was correct if the contract Foo is an import.

However, this wasn't correct for scenarios where the inherited code refers to the same contract, and those should not be treat as references.

Fix

Treat the contract variable as a reference only if it's an imported contract. Otherwise (it's the enclosing contract of the current code) treat it as the concrete type.


  • Targeted PR against master branch
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work
  • Code follows the standards mentioned here
  • Updated relevant documentation
  • Re-reviewed Files changed in the Github PR explorer
  • Added appropriate labels

@SupunS SupunS self-assigned this Jul 28, 2026
@SupunS SupunS added the Bugfix label Jul 28, 2026
@github-actions

Copy link
Copy Markdown

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

Scanned Files

None

@github-actions

Copy link
Copy Markdown

Benchstat comparison

  • Base branch: onflow:master
  • Base commit: 0fb572e
Results

old.txtnew.txt
time/opdelta
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
ByteArrayTransfer-41.62µs ± 0%1.58µs ± 0%~(p=1.000 n=1+1)
ByteArrayValueToByteSlice-485.2ns ± 0%79.1ns ± 0%~(p=1.000 n=1+1)
ByteSliceToByteArrayValue-41.40µs ± 0%1.37µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/bbq/compiler goos:linux goarch:amd64
CompileFungibleTokenTransferTransaction-4116µs ± 0%118µs ± 0%~(p=1.000 n=1+1)
CompileTime-431.1µs ± 0%29.6µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
ContractFunctionInvocation-4369µs ± 0%351µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
ContractImport-4219µs ± 0%222µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
EMVAddressTransfer-44.18µs ± 0%4.52µs ± 0%~(p=1.000 n=1+1)
Emit-45.12ms ± 0%5.06ms ± 0%~(p=1.000 n=1+1)
EnumTransfer-41.69µs ± 0%1.61µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
ExportType/composite_type-4239ns ± 0%222ns ± 0%~(p=1.000 n=1+1)
ExportType/simple_type-441.2ns ± 0%41.3ns ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
FTTransfer-4150µs ± 0%148µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
ImperativeFib-422.7µs ± 0%23.0µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
ImperativeFib-426.5µs ± 0%27.5µs ± 0%~(p=1.000 n=1+1)
ImperativeFibNewCompilerNewVM-451.3µs ± 0%50.2µs ± 0%~(p=1.000 n=1+1)
ImperativeFibNewVM-429.3µs ± 0%29.0µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
InterpretRecursionFib-42.14ms ± 0%2.16ms ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
InterpreterFTTransfer-4121µs ± 0%116µs ± 0%~(p=1.000 n=1+1)
InterpreterImperativeFib-421.3µs ± 0%23.1µs ± 0%~(p=1.000 n=1+1)
InterpreterNewStruct-471.9µs ± 0%71.2µs ± 0%~(p=1.000 n=1+1)
MethodCall/concrete_type_method_call-446.2µs ± 0%47.8µs ± 0%~(p=1.000 n=1+1)
MethodCall/interface_method_call-462.8µs ± 0%64.5µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
NewInterpreter/new_interpreter-4882ns ± 0%881ns ± 0%~(p=1.000 n=1+1)
NewInterpreter/new_sub-interpreter-4322ns ± 0%320ns ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
NewStruct-454.3µs ± 0%51.1µs ± 0%~(p=1.000 n=1+1)
NewStructRaw-43.93µs ± 0%3.78µs ± 0%~(p=1.000 n=1+1)
RecursionFib-41.61ms ± 0%1.71ms ± 0%~(p=1.000 n=1+1)
RuntimeFungibleTokenTransfer-4776µs ± 0%748µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
RuntimeFungibleTokenTransferInterpreter-4642µs ± 0%661µs ± 0%~(p=1.000 n=1+1)
RuntimeResourceDictionaryValues-42.85ms ± 0%2.60ms ± 0%~(p=1.000 n=1+1)
RuntimeResourceTracking-411.4ms ± 0%10.1ms ± 0%~(p=1.000 n=1+1)
RuntimeScriptNoop-414.9µs ± 0%14.6µs ± 0%~(p=1.000 n=1+1)
RuntimeVMInvokeContractImperativeFib-441.6µs ± 0%42.9µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
ValueIsSubtypeOfSemaType-458.8ns ± 0%58.6ns ± 0%~(p=1.000 n=1+1)
 
alloc/opdelta
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
ByteArrayTransfer-41.15kB ± 0%1.17kB ± 0%~(p=1.000 n=1+1)
ByteArrayValueToByteSlice-432.0B ± 0%32.0B ± 0%~(all equal)
ByteSliceToByteArrayValue-4943B ± 0%961B ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/bbq/compiler goos:linux goarch:amd64
CompileFungibleTokenTransferTransaction-483.4kB ± 0%83.5kB ± 0%~(p=1.000 n=1+1)
CompileTime-417.2kB ± 0%17.2kB ± 0%~(all equal)
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
ContractFunctionInvocation-4149kB ± 0%149kB ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
ContractImport-479.9kB ± 0%79.9kB ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
EMVAddressTransfer-42.77kB ± 0%2.84kB ± 0%~(p=1.000 n=1+1)
Emit-41.70MB ± 0%1.70MB ± 0%~(p=1.000 n=1+1)
EnumTransfer-4992B ± 0%984B ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
ExportType/composite_type-4112B ± 0%112B ± 0%~(all equal)
ExportType/simple_type-40.00B 0.00B ~(all equal)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
FTTransfer-446.8kB ± 0%46.8kB ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
ImperativeFib-48.30kB ± 0%8.30kB ± 0%~(all equal)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
ImperativeFib-46.30kB ± 0%6.30kB ± 0%~(all equal)
ImperativeFibNewCompilerNewVM-427.0kB ± 0%27.0kB ± 0%~(all equal)
ImperativeFibNewVM-49.03kB ± 0%9.03kB ± 0%~(all equal)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
InterpretRecursionFib-41.17MB ± 0%1.17MB ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
InterpreterFTTransfer-446.2kB ± 0%46.2kB ± 0%~(p=1.000 n=1+1)
InterpreterImperativeFib-48.29kB ± 0%8.29kB ± 0%~(all equal)
InterpreterNewStruct-426.1kB ± 0%26.2kB ± 0%~(p=1.000 n=1+1)
MethodCall/concrete_type_method_call-411.4kB ± 0%11.4kB ± 0%~(p=1.000 n=1+1)
MethodCall/interface_method_call-418.3kB ± 0%18.3kB ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
NewInterpreter/new_interpreter-41.02kB ± 0%1.02kB ± 0%~(all equal)
NewInterpreter/new_sub-interpreter-4232B ± 0%232B ± 0%~(all equal)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
NewStruct-415.6kB ± 0%15.8kB ± 0%~(p=1.000 n=1+1)
NewStructRaw-41.98kB ± 0%1.95kB ± 0%~(p=1.000 n=1+1)
RecursionFib-4342kB ± 0%342kB ± 0%~(all equal)
RuntimeFungibleTokenTransfer-4252kB ± 0%251kB ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
RuntimeFungibleTokenTransferInterpreter-4174kB ± 0%175kB ± 0%~(p=1.000 n=1+1)
RuntimeResourceDictionaryValues-41.81MB ± 0%1.80MB ± 0%~(p=1.000 n=1+1)
RuntimeResourceTracking-47.42MB ± 0%7.50MB ± 0%~(p=1.000 n=1+1)
RuntimeScriptNoop-48.18kB ± 0%8.17kB ± 0%~(p=1.000 n=1+1)
RuntimeVMInvokeContractImperativeFib-415.0kB ± 0%15.0kB ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
ValueIsSubtypeOfSemaType-432.0B ± 0%32.0B ± 0%~(all equal)
 
allocs/opdelta
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
ByteArrayTransfer-48.00 ± 0%8.00 ± 0%~(all equal)
ByteArrayValueToByteSlice-41.00 ± 0%1.00 ± 0%~(all equal)
ByteSliceToByteArrayValue-46.00 ± 0%6.00 ± 0%~(all equal)
pkg:github.com/onflow/cadence/bbq/compiler goos:linux goarch:amd64
CompileFungibleTokenTransferTransaction-4581 ± 0%581 ± 0%~(all equal)
CompileTime-4212 ± 0%212 ± 0%~(all equal)
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
ContractFunctionInvocation-42.28k ± 0%2.28k ± 0%~(all equal)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
ContractImport-41.40k ± 0%1.40k ± 0%~(all equal)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
EMVAddressTransfer-431.0 ± 0%31.0 ± 0%~(all equal)
Emit-442.0k ± 0%42.0k ± 0%~(p=1.000 n=1+1)
EnumTransfer-414.0 ± 0%14.0 ± 0%~(all equal)
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
ExportType/composite_type-42.00 ± 0%2.00 ± 0%~(all equal)
ExportType/simple_type-40.00 0.00 ~(all equal)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
FTTransfer-41.49k ± 0%1.49k ± 0%~(all equal)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
ImperativeFib-4176 ± 0%176 ± 0%~(all equal)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
ImperativeFib-4322 ± 0%322 ± 0%~(all equal)
ImperativeFibNewCompilerNewVM-4534 ± 0%534 ± 0%~(all equal)
ImperativeFibNewVM-4365 ± 0%365 ± 0%~(all equal)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
InterpretRecursionFib-416.5k ± 0%16.5k ± 0%~(all equal)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
InterpreterFTTransfer-41.01k ± 0%1.01k ± 0%~(p=1.000 n=1+1)
InterpreterImperativeFib-4175 ± 0%175 ± 0%~(all equal)
InterpreterNewStruct-4418 ± 0%418 ± 0%~(all equal)
MethodCall/concrete_type_method_call-4404 ± 0%404 ± 0%~(all equal)
MethodCall/interface_method_call-4514 ± 0%514 ± 0%~(all equal)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
NewInterpreter/new_interpreter-416.0 ± 0%16.0 ± 0%~(all equal)
NewInterpreter/new_sub-interpreter-44.00 ± 0%4.00 ± 0%~(all equal)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
NewStruct-4432 ± 0%432 ± 0%~(all equal)
NewStructRaw-432.0 ± 0%32.0 ± 0%~(all equal)
RecursionFib-419.5k ± 0%19.5k ± 0%~(all equal)
RuntimeFungibleTokenTransfer-44.22k ± 0%4.22k ± 0%~(all equal)
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
RuntimeFungibleTokenTransferInterpreter-43.22k ± 0%3.22k ± 0%~(all equal)
RuntimeResourceDictionaryValues-436.7k ± 0%36.7k ± 0%~(p=1.000 n=1+1)
RuntimeResourceTracking-4131k ± 0%131k ± 0%~(p=1.000 n=1+1)
RuntimeScriptNoop-4117 ± 0%117 ± 0%~(all equal)
RuntimeVMInvokeContractImperativeFib-4489 ± 0%489 ± 0%~(all equal)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
ValueIsSubtypeOfSemaType-41.00 ± 0%1.00 ± 0%~(all equal)
 

@SupunS
SupunS marked this pull request as ready for review July 28, 2026 18:46
@SupunS
SupunS requested a review from turbolent as a code owner July 28, 2026 18:46

@turbolent turbolent left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

@SupunS
SupunS merged commit 2c89314 into master Jul 28, 2026
16 of 17 checks passed
@SupunS
SupunS deleted the supun/fix-inherited-code-from-same-contract branch July 28, 2026 19:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants