Skip to content

Conversation

@cyberthirst
Copy link
Collaborator

@cyberthirst cyberthirst commented Mar 13, 2025

What I did

implements #4515

How I did it

How to verify it

Commit message

this commit moves the `sqrt()` builtin to a new, pure vyper stdlib
implementation.

it includes machinery to handle extension of the stdlib in the
future. the benefits of having machinery to handle code written in pure
vyper in the stdlib is that it will be easier for external contributors
to contribute and audit. it also uses the native import machinery,
rather than the existing ad-hoc mechanism for writing vyper from inside
the compiler, which simplifies the implementation. that mechanism for
writing pure vyper builtins is also removed in this commit (previously,
this machinery had been used specifically to handle "pure vyper"
builtins, the only one of which was `sqrt()`).

Description for the changelog

Cute Animal Picture

Put a link to a cute animal picture inside the parenthesis-->

@codecov
Copy link

codecov bot commented Mar 13, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.63%. Comparing base (9486a41) to head (74c40e1).
Report is 81 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4520      +/-   ##
==========================================
- Coverage   92.71%   92.63%   -0.09%     
==========================================
  Files         123      122       -1     
  Lines       17546    17518      -28     
  Branches     2977     2976       -1     
==========================================
- Hits        16268    16228      -40     
- Misses        879      892      +13     
+ Partials      399      398       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@cyberthirst
Copy link
Collaborator Author

addresses the sqrt part of #4520
after discussion with @charles-cooper isqrt won't be ported because it would introduce a gas regression as it is hand-rolled via IR

ZeroDivisionException,
)
from vyper.semantics.analysis.base import Modifiability, VarInfo
from vyper.semantics.analysis.base import Modifiability

Check notice

Code scanning / CodeQL

Cyclic import Note

Import of module
vyper.semantics.analysis.base
begins an import cycle.
@cyberthirst cyberthirst added this to the v0.4.2 milestone Mar 13, 2025
@cyberthirst cyberthirst marked this pull request as ready for review March 17, 2025 06:47
@charles-cooper charles-cooper added the release - must release blocker label Mar 17, 2025
remapped_module = module_str
if remapped_module.startswith("ethereum.ercs"):
if is_erc_interface:
remapped_module = remapped_module.removeprefix("ethereum.ercs")
Copy link
Member

Choose a reason for hiding this comment

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

maybe we should physically move these to the stdlib/ directory

Copy link
Member

Choose a reason for hiding this comment

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

(to fuse the two branches)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

won't it still require a condition?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

updated the remapping logic a bit to use a dict: 828d3ea


def test_sqrt_literal(get_contract):
code = """
import stdlib.math as math
Copy link
Member

Choose a reason for hiding this comment

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

i prefer just import math

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

hmm, I don't. I think that stdlib clearly establishes that it comes from the compiler. I think that it's reasonable to assume that math module could be from user-space

Copy link
Member

Choose a reason for hiding this comment

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

How about vyper.math

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

import math was added in b5e3825

@cyberthirst cyberthirst closed this Apr 3, 2025
@cyberthirst
Copy link
Collaborator Author

trying to close&reopen to reflect the newest changes, due to gh bug the new commits weren't reflected

@cyberthirst cyberthirst reopened this Apr 3, 2025
@cyberthirst cyberthirst changed the title feat[lang]: move sqrt to stdlib feat[lang]!: move sqrt to stdlib Apr 10, 2025


def _load_builtin_import(level: int, module_str: str) -> tuple[CompilerInput, vy_ast.Module]:
if not _is_builtin(module_str): # pragma: nocover
Copy link
Member

Choose a reason for hiding this comment

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

i don't think the pragma should be removed

for prefix in BUILTIN_PREFIXES:
if module_str.startswith(prefix):
return prefix
return None
Copy link
Member

Choose a reason for hiding this comment

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

panic here, unreachable

Suggested change
return None
raise CompilerPanic("unreachable") # pragma: nocover



def _get_builtin_prefix(module_str: str):
for prefix in BUILTIN_PREFIXES:
Copy link
Member

Choose a reason for hiding this comment

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

nit:

Suggested change
for prefix in BUILTIN_PREFIXES:
for prefix in BUILTIN_PREFIXES.keys():

Copy link
Member

@charles-cooper charles-cooper left a comment

Choose a reason for hiding this comment

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

couple nits, but looks good overall

@cyberthirst
Copy link
Collaborator Author

looked at the recent commits by @charles-cooper, lgtm

@charles-cooper charles-cooper changed the title feat[lang]!: move sqrt to stdlib feat[lang]!: move sqrt to new stdlib math module Apr 12, 2025
@charles-cooper charles-cooper enabled auto-merge (squash) April 12, 2025 12:49
@charles-cooper charles-cooper merged commit 1591a12 into vyperlang:master Apr 12, 2025
159 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release - must release blocker

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants