Skip to content

const fn support, #[selector = sol("foo()")] attribute#1216

Merged
cburgdorf merged 29 commits intoargotorg:masterfrom
sbillig:const-extern
Jan 30, 2026
Merged

const fn support, #[selector = sol("foo()")] attribute#1216
cburgdorf merged 29 commits intoargotorg:masterfrom
sbillig:const-extern

Conversation

@sbillig
Copy link
Collaborator

@sbillig sbillig commented Jan 16, 2026

This adds support for extern and fe-defined const fns, with support for basic code.

For example:

pub const fn sol<T: AsBytes>(sig: T) -> u32 {
    (keccak(sig) >> 224) as u32
}

The rhs of attributes can now be a const expr, so we can do this:

msg Erc20 {
    #[selector = sol("transfer(address,uint256)")]
    Transfer { to: Address, amount: u256 } -> bool,

Limitations (for now):

  • const fn must be effect-free (no uses (...))
  • supported: literals, if/else, unary/binary ops, indexing, casts, field access, tuple/array/record construction, calls to const functions and methods (including trait-method dispatch when resolvable).
  • not supported: loops, match, assignment/+=, with, mutable bindings/patterns
  • Array repetition [elem; N] requires N to be fully resolved during type checking (so it fails for abstract consts and const-generic parameters)
  • Some extern const fns remain abstract (not evaluated), so they’re rejected in places that require a fully-resolved value (e.g. const items)
  • core::keccak can only be used with statically known values for now; it can appear in a normal function body and will evaluate to the statically-computed keccak if possible. Otherwise, it'll result in a codegen error. (This should of course be fixed, so that it does the reasonable thing when faced with a runtime value)

@micahscopes
Copy link
Collaborator

Could this be relevant to the optimization stuff happening in #1211 ?

@sbillig sbillig marked this pull request as ready for review January 26, 2026 05:10
@sbillig
Copy link
Collaborator Author

sbillig commented Jan 29, 2026

@codex review

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 52584dc36d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@sbillig sbillig force-pushed the const-extern branch 2 times, most recently from 56972dd to 3fcbdd2 Compare January 29, 2026 23:28
@sbillig sbillig changed the title [WIP] const fn stuff; nicer selector attribute? const fn support, #[selector = sol("foo()")] attribute Jan 29, 2026
@sbillig
Copy link
Collaborator Author

sbillig commented Jan 30, 2026

@codex review

@sbillig sbillig requested a review from cburgdorf January 30, 2026 04:25
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 48c71fce9d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@sbillig
Copy link
Collaborator Author

sbillig commented Jan 30, 2026

@codex review

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a71e322001

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +952 to +956

match name.data(self.db).as_str() {
"__as_bytes" => Ok(Some(self.eval_intrinsic_as_bytes(expr, ret_ty, args)?)),
"__keccak256" => Ok(Some(self.eval_intrinsic_keccak(expr, ret_ty, args)?)),
_ => Ok(None),

Choose a reason for hiding this comment

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

P2 Badge Evaluate const size_of calls in CTFE

core::intrinsic::size_of was made a const fn, but eval_extern_const_fn only handles __as_bytes/__keccak256, so a const item like const N: u256 = core::size_of<u256>() (or a const generic/array length) now type-checks yet fails CTFE and gets reported as “const value must be known.” Since size-of is always computable at compile time, this is a regression for const contexts; consider adding a size_of case here that returns the layout size (similar to the MIR lowering) so CTFE can resolve it.

Useful? React with 👍 / 👎.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Deferring until later: #1225

@sbillig sbillig changed the title const fn support, #[selector = sol("foo()")] attribute const fn support, #[selector = sol("foo()")] attribute Jan 30, 2026
Copy link
Collaborator

@cburgdorf cburgdorf left a comment

Choose a reason for hiding this comment

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

Looks good! Let's merge that thang!

@cburgdorf cburgdorf merged commit 2ed2202 into argotorg:master Jan 30, 2026
5 checks passed
@sbillig sbillig mentioned this pull request Jan 30, 2026
19 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants