Smart contracts frequently encounter data structures that demand management across multiple variants. For example, a payment mechanism might need to accommodate native ETH, ERC20 tokens, or NFT transfers, whereas an auction contract might transition through states such as unstarted, active, ended, or cancelled. Each unique variant traditionally requires dedicated handling, yet the logic governing these states is typically scattered across numerous contract functions. When developers introduce a new variant and inadvertently fail to update one of these distributed functions, the legacy compiler issues no warnings. Once the contract is deployed to a blockchain network, discovering and patching such oversights can result in severe financial losses and permanently locked funds.
To address this systemic vulnerability, Core Solidity has introduced algebraic data types (ADTs) and static pattern matching, moving safety guarantees from runtime execution checks directly into the compiler. Designed to eliminate an entire class of smart contract bugs before deployment, this architectural shift re-evaluates how state management and dispatch logic are handled in decentralized applications. By forcing exhaustive handling of data variants at compile time, Core Solidity seeks to alleviate the heavy reliance on memory, code review checklists, and manual security audits that have long defined smart contract development.
Why Classic Solidity Needs Better Data Modeling
Classic Solidity has historically struggled with data modeling when applications must deal with values existing in one of several mutually exclusive states. Consider a multi-asset payment processor designed to handle native ETH transfers, ERC20 token transfers, and ERC721 NFT transfers. In standard Solidity, developers typically implement this by coupling an enumeration tag with a generalized struct containing fields for every conceivable parameter across all supported variants.
This conventional approach introduces several critical safety problems that the legacy compiler cannot catch. First, invalid states are easily representable. A single struct can be constructed with contradictory field combinations, such as a native payment configured with a non-zero token address or an ERC721 transfer missing a unique token identifier. Because nothing in the type system prevents developers from populating these structures with incoherent values, developers are forced to write repetitive require statements across every function that processes the payment data.
Second, Classic Solidity offers no compile-time totality guarantees. If a project expands to support a new standard, such as ERC1155, the compiler will not flag the functions that require updates. Payment processing logic may silently fail to execute for the new asset type, while fee calculation functions might return zero without alerting the developer. Every function that dispatches logic based on an enumeration type risks silent failures if a new variant is introduced later in the development lifecycle.
Finally, the reliance on redundant runtime validation degrades efficiency and increases attack surfaces. Because the type system cannot express structural constraints, every function must repeatedly execute safety checks on-chain. This consumes valuable gas while remaining error-prone, as a developer might easily add a new function and forget to validate a specific field. Furthermore, because standard structs separate the type tag from its corresponding payload, nothing stops a caller from pairing them incorrectly.
Algebraic Data Types Make Invalid States Unrepresentable
Core Solidity solves these foundational architectural flaws by implementing algebraic data types, allowing developers to define custom types that express variants precisely and concisely. Utilizing Core Solidity’s primitive 256-bit word type, developers can construct sum types where each constructor carries exclusively the data it requires—no more and no less.
Under this model, a native payment holds a recipient address and an amount, whereas an ERC20 payment holds a token contract address, a sender, a recipient, and an amount. An ERC721 payment similarly encapsulates a token contract, a sender, a recipient, and a token identifier. It is structurally impossible to construct a payment value that carries an ERC721 token identifier while simultaneously claiming to be a native transaction, because the underlying type system rejects such contradictions outright.
When processing these structured types, dispatch functions become declarative and self-documenting through pattern matching. The match statement binds exactly the fields relevant to each variant, removing the need for repetitive structural require checks. Because the type system enforces data cohesion, distinct fields like the amount in a native payment and the token identifier in an NFT transfer cannot be confused or misattributed. Furthermore, if a developer expands the system by adding a new variant, the compiler immediately halts compilation and reports every match statement that fails to handle the new case, preventing any function from silently missing the update.
Totality and Exhaustiveness as Core Safety Properties
The cornerstone of this architectural enhancement is exhaustiveness checking, often referred to as totality. A pattern match is considered exhaustive when every possible value of the scrutinized expression is accounted for by at least one branch. The Core Solidity compiler enforces this requirement statically, rejecting any program containing an incomplete match statement before it can be deployed.
Smart contract code is notoriously difficult to modify once launched, as immutability prevents fixes unless complex upgradeable proxy patterns are utilized. Any funds locked inside a buggy contract risk permanent inaccessibility. In traditional Solidity implementations, developers often rely on fallback clauses or default returns to satisfy compiler requirements, which can mask missing logic if new variants are added later. Exhaustiveness checking transforms these latent hazards into definitive compile-time errors.
When a match statement is incomplete, the compiler identifies a specific missing case, providing a concrete witness that guides the developer directly to the unhandled scenario. In addition to exhaustiveness, the compiler performs redundancy checking, issuing warnings when a pattern branch can never be reached because an earlier branch has already processed its inputs. Redundant branches often signal copy-paste errors or remnant code left behind after a refactoring process.
For smart contract security audits, this capability changes the verification workflow. Auditing enum-heavy contracts traditionally requires security researchers to manually trace extensive conditional blocks across every function to verify that all edge cases are handled without falling through to silent defaults. Pattern matching with exhaustiveness checking renders this entire category of audit findings obsolete. If a contract successfully compiles, auditors have an automated guarantee that all variants are handled, allowing them to redirect their focus toward higher-value security concerns.
The Pattern Match Compiler and Efficient Lowering
To make exhaustiveness checking computationally tractable while generating highly efficient execution paths, the Core Solidity prototype incorporates a dedicated compilation pass based on decision-tree algorithms. Traditional pattern-matching engines relying on backtracking automata often struggle to cleanly extract exhaustiveness information. In contrast, decision-tree-based compilation produces exhaustiveness and redundancy metrics as natural byproducts of building the tree structure, eliminating the need for separate analysis passes.
Operating between type inference and final code generation, this compilation pass transforms match statements over complex nested patterns into flat decision trees where each node tests a single scrutinee against constructor patterns. The resulting tree is subsequently simplified into structures that the backend can translate directly into efficient Yul code.
A common concern among developers transitioning to advanced type systems is whether such abstractions introduce runtime overhead. In Core Solidity, pattern matching over algebraic data types introduces no performance penalty. The generated Yul output matches the optimized switch statements that a meticulous developer would write manually.
During the lower-level translation phases, sum types are flattened directly onto the Ethereum Virtual Machine stack. Binary sums utilize boolean values to differentiate between constructors, while N-ary sums assign sequential integer tags alongside payload slots padded to accommodate the widest variant. Single-constructor wrapper types incur zero overhead, erasing the constructor entirely to occupy a single stack slot identically to raw primitive variables.
Ultimately, Core Solidity’s integration of algebraic data types and pattern matching provides developers with robust tools to eliminate entire categories of structural bugs. By shifting state verification and variant exhaustiveness from runtime checks and manual checklists to compile-time guarantees, the language achieves enhanced contract safety without sacrificing execution efficiency or incurring additional gas costs.






