infer_language_versions api#1265
Merged
mjoerussell merged 56 commits intoNomicFoundation:mainfrom Apr 10, 2025
Merged
Conversation
added 13 commits
February 26, 2025 11:28
This parser is trying to stick to npm's flavor of semver, which Solidity also uses for its version pragma
…g common syntax forms, and they are all passing. The biggest thing that's missing right now is handling Solidity's specific semver quirks and edge cases, which are definitely not handled.
…nts of a Solidity source file as input and shallowly parses it to pull out any version pragmas. It thencompares all of the pragmas to the full list of available versions and returns a list of all the versions which pass.
…urce files against the actual version used to compile them according to Sanctuary. I'm marking two kinds of version errors here: one for when we can't infer a version from a source file, and one for when the actual version is not included in the inferred versions list.
…d of Sanctuary testing. At first this was extacting the version pragmas by just going line-by-line and finding lines that start with `pragma solidity`. This is obviously an overly simplistic way to do this. One glaring issue here is that if a source file has a version pragma inside a block comment, then that would still be included in the list of pragmas that must be matched. This update uses slang's parser instead to extract all of the real version pragmas, and then the custom semver parser to do the rest. After making this change we have 0 version inference errors from Sanctuary.
…ators (`^` and `~`) in hyphen ranges. These two things don't make sense to be included together, and most likely a correct parser would fail to parse this range. However, Solidity does/has just ignored these operators in the case of a hyphen range, so we will do that too.
I copied the semver parsing/matching test cases from solc and recreated them here. From there I fixed any weird behaviors that weren't already handled. Most importantly, solc allows users to specify operators in front of partial version ranges, which leads to some unintuitive behavior.
…rly in some circumstances * Refactored the parser code to move the types and base implementations to `semver/mod.rs`, and all the parsing code in `semver/parser.rs`.
…ype of thing is getting parsed. Also trying to simplify/consolidate some of the higher-level parsing bits.
🦋 Changeset detectedLatest commit: cf5ad63 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
…otes are included
ggiraldez
reviewed
Mar 5, 2025
Contributor
ggiraldez
left a comment
There was a problem hiding this comment.
Left a couple of suggestions, but the implementation looks good in general. I have two questions though:
- Re: the internal version types, wouldn't it be possible to reuse the semver crate types? It's not too much code, but it seems redundant. What is preventing you from reusing them?
- The parsing code also somewhat duplicates the main language parser, and IIUC, there are some cases accepted by this new parser that the language will not parse. Maybe we should not try to parse versions from the language definition at all. Or, on the opposite side, try to build the version ranges from the parsed CST nodes. WDYT?
| return Ok(()); | ||
| } else if !inferred_versions.contains(&version) { | ||
| events.version_inference_error(format!( | ||
| "[{version}] Did not find correct version for {path}", |
Contributor
There was a problem hiding this comment.
Suggested change
| "[{version}] Did not find correct version for {path}", | |
| "[{version}] Inferred versions do not match with reported for {path}", |
added 4 commits
March 5, 2025 11:37
* Add `has_known_version_mismatch` check to the sanctuary tests so that we skip files where the version in sanctuary is known to not match the version pragmas
… our purposes they're interchangable, all of the custom semver parsing/handling happens in `PartialVersion`, `Comparator`, and `ComparatorSet` - once we reduce the versions to concrete versions then we don't need anything special. This results in a bit less custom code.
OmarTawfik
reviewed
Mar 7, 2025
OmarTawfik
reviewed
Mar 7, 2025
OmarTawfik
reviewed
Mar 7, 2025
OmarTawfik
reviewed
Mar 7, 2025
OmarTawfik
reviewed
Mar 7, 2025
OmarTawfik
reviewed
Mar 7, 2025
added 5 commits
March 17, 2025 11:29
…anguage_versions` stubs
…he custom `VersionIterator`
ggiraldez
reviewed
Mar 17, 2025
ggiraldez
reviewed
Mar 17, 2025
OmarTawfik
reviewed
Mar 18, 2025
stefanoban
reviewed
Mar 19, 2025
| latest-version: static func() -> string; | ||
|
|
||
| /// Analyze the version pragmas within a source file, and return a list of supported language versions that match these pragmas. | ||
| /// If the source file contains no pragmas, all supported versions are returned. |
Contributor
There was a problem hiding this comment.
Suggested change
| /// If the source file contains no pragmas, all supported versions are returned. | |
| /// If the source file contains no pragmas, all versions supported by Slang are returned. |
stefanoban
reviewed
Mar 19, 2025
|
|
||
| ## Inferring Compatible Solidity Versions | ||
|
|
||
| For cases where you don't know in advance which version of Solidity to use, the `LanguageFacts` API provides a utility to generate a list of compatible versions. It uses the [version pragmas](https://docs.soliditylang.org/en/develop/layout-of-source-files.html#version-pragma) defined in your Solidity source file to filter the list of versions supported by Slang, returning only the compatible ones. |
Contributor
There was a problem hiding this comment.
Suggested change
| For cases where you don't know in advance which version of Solidity to use, the `LanguageFacts` API provides a utility to generate a list of compatible versions. It uses the [version pragmas](https://docs.soliditylang.org/en/develop/layout-of-source-files.html#version-pragma) defined in your Solidity source file to filter the list of versions supported by Slang, returning only the compatible ones. | |
| For cases where you don't know in advance which version of Solidity to use, the `LanguageFacts` API provides a utility to generate a list of compatible versions. It uses the [version pragmas](https://docs.soliditylang.org/en/develop/layout-of-source-files.html#version-pragma) defined in your Solidity source file to filter the list of versions supported by Slang, returning only the compatible ones. If the source file contains no pragmas, all versions supported by Slang are returned. |
stefanoban
reviewed
Mar 19, 2025
Co-authored-by: Omar Tawfik <15987992+OmarTawfik@users.noreply.github.com>
OmarTawfik
reviewed
Mar 20, 2025
|
|
||
| /// Analyze the version pragmas within a source file, and return a list of supported language versions that match these pragmas. | ||
| /// If the source file contains no pragmas, all versions supported by Slang are returned. | ||
| infer-language-versions: static func(src: string) -> list<string>; |
Contributor
There was a problem hiding this comment.
nit: suggest using the same parameter names as the Rust API:
Suggested change
| infer-language-versions: static func(src: string) -> list<string>; | |
| infer-language-versions: static func(input: string) -> list<string>; |
OmarTawfik
reviewed
Mar 20, 2025
OmarTawfik
approved these changes
Mar 20, 2025
Contributor
OmarTawfik
left a comment
There was a problem hiding this comment.
Left a couple of suggestions, but LGTM otherwise.
Thank you!
Merged
github-merge-queue Bot
pushed a commit
that referenced
this pull request
Apr 10, 2025
This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and publish to npm yourself or [setup this action to publish automatically](https://github.com/changesets/action#with-publishing). If you're not ready to do a release yet, that's fine, whenever you add more changesets to main, this PR will be updated. # Releases ## @nomicfoundation/slang@1.1.0 ### Minor Changes - [#1288](#1288) [`2090ab8`](2090ab8) Thanks [@OmarTawfik](https://github.com/OmarTawfik)! - support Solidity `0.8.29` and [Custom Storage Layouts](https://docs.soliditylang.org/en/v0.8.29/contracts.html#custom-storage-layout): - `ContractDefinition` nodes will no longer have an optional `InheritanceSpecifier` child directly, but will hold a list of `ContractSpecifier` children - `ContractSpecifier` nodes have either `InheritanceSpecifier` or `StorageLayoutSpecifier` children - [#1265](#1265) [`2312260`](2312260) Thanks [@mjoerussell](https://github.com/mjoerussell)! - Add `LanguageUtils::infer_language_versions(source_code) -> Version[]` API, which will analyze version pragmas inside a source file, and return a list of supported language versions that they allow. This can be used to select a valid language version to use with the rest of Slang APIs. Please see the [Choosing a Solidity Version](https://nomicfoundation.github.io/slang/1.1.0/user-guide/04-getting-started/02-choosing-a-solidity-version/) guide for more information. ### Patch Changes - [#1291](#1291) [`da1f863`](da1f863) Thanks [@ggiraldez](https://github.com/ggiraldez)! - Resolve arguments to inheritance specifiers and expressions in storage layout specifiers using the contract's parent scope. Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
rollup-smithbm0p
added a commit
to rollup-smithbm0p/slang
that referenced
this pull request
Dec 26, 2025
This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and publish to npm yourself or [setup this action to publish automatically](https://github.com/changesets/action#with-publishing). If you're not ready to do a release yet, that's fine, whenever you add more changesets to main, this PR will be updated. # Releases ## @nomicfoundation/slang@1.1.0 ### Minor Changes - [#1288](NomicFoundation/slang#1288) [`2090ab8`](NomicFoundation/slang@2090ab8) Thanks [@OmarTawfik](https://github.com/OmarTawfik)! - support Solidity `0.8.29` and [Custom Storage Layouts](https://docs.soliditylang.org/en/v0.8.29/contracts.html#custom-storage-layout): - `ContractDefinition` nodes will no longer have an optional `InheritanceSpecifier` child directly, but will hold a list of `ContractSpecifier` children - `ContractSpecifier` nodes have either `InheritanceSpecifier` or `StorageLayoutSpecifier` children - [#1265](NomicFoundation/slang#1265) [`2312260`](NomicFoundation/slang@2312260) Thanks [@mjoerussell](https://github.com/mjoerussell)! - Add `LanguageUtils::infer_language_versions(source_code) -> Version[]` API, which will analyze version pragmas inside a source file, and return a list of supported language versions that they allow. This can be used to select a valid language version to use with the rest of Slang APIs. Please see the [Choosing a Solidity Version](https://nomicfoundation.github.io/slang/1.1.0/user-guide/04-getting-started/02-choosing-a-solidity-version/) guide for more information. ### Patch Changes - [#1291](NomicFoundation/slang#1291) [`da1f863`](NomicFoundation/slang@da1f863) Thanks [@ggiraldez](https://github.com/ggiraldez)! - Resolve arguments to inheritance specifiers and expressions in storage layout specifiers using the contract's parent scope. Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
wvalencs
pushed a commit
to tirixa-hub/solidity-toolkit
that referenced
this pull request
Apr 18, 2026
This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and publish to npm yourself or [setup this action to publish automatically](https://github.com/changesets/action#with-publishing). If you're not ready to do a release yet, that's fine, whenever you add more changesets to main, this PR will be updated. # Releases ## @nomicfoundation/slang@1.1.0 ### Minor Changes - [#1288](NomicFoundation/slang#1288) [`2090ab8`](NomicFoundation/slang@2090ab8) Thanks [@OmarTawfik](https://github.com/OmarTawfik)! - support Solidity `0.8.29` and [Custom Storage Layouts](https://docs.soliditylang.org/en/v0.8.29/contracts.html#custom-storage-layout): - `ContractDefinition` nodes will no longer have an optional `InheritanceSpecifier` child directly, but will hold a list of `ContractSpecifier` children - `ContractSpecifier` nodes have either `InheritanceSpecifier` or `StorageLayoutSpecifier` children - [#1265](NomicFoundation/slang#1265) [`2312260`](NomicFoundation/slang@2312260) Thanks [@mjoerussell](https://github.com/mjoerussell)! - Add `LanguageUtils::infer_language_versions(source_code) -> Version[]` API, which will analyze version pragmas inside a source file, and return a list of supported language versions that they allow. This can be used to select a valid language version to use with the rest of Slang APIs. Please see the [Choosing a Solidity Version](https://nomicfoundation.github.io/slang/1.1.0/user-guide/04-getting-started/02-choosing-a-solidity-version/) guide for more information. ### Patch Changes - [#1291](NomicFoundation/slang#1291) [`da1f863`](NomicFoundation/slang@da1f863) Thanks [@ggiraldez](https://github.com/ggiraldez)! - Resolve arguments to inheritance specifiers and expressions in storage layout specifiers using the contract's parent scope. Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Added a new API which generates a list of valid language versions for a Solidity source file by parsing the
pragma soliditydeclarations.Here's a basic example of how this API might be used:
As stated in the Solidity documentation, the semver parsing is based on npm's semver implementation. However, solc's implementation has had several bugs/undocumented features currently and in the past which we need to support.
I've added 30 unit tests to the parser, each of which tests various positive and negative cases for different types of semvers. I've also included some of solc's test cases from here to ensure that we're passing/failing the same matches that they are.