feat(diagnostics): diagnose duplicate virtual specifiers#1832
Conversation
Reject functions, fallback/receive functions, and modifiers that carry more than one `virtual` keyword, matching solc's ParserError 6879/2662. Closes the `virtual` part of the SDR[1730] validation TODO in the IR builder.
|
There was a problem hiding this comment.
Pull request overview
Adds a new v2 syntax diagnostic to match solc behavior when virtual is specified more than once on the same definition, emitting syntax/multiple-virtual-specifiers for each virtual after the first.
Changes:
- Introduces
MultipleVirtualSpecifiersdiagnostic kind (syntax/multiple-virtual-specifiers) with an error severity and message aligned to the PR description. - Updates the v2 CST→IR builder to extract the first
virtualkeyword and emit a diagnostic on each subsequentvirtualfor functions, fallback/receive functions, and modifiers. - Adds new diagnostics snapshot fixtures and test wiring covering functions, fallback, receive, and modifiers.
Reviewed changes
Copilot reviewed 8 out of 17 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| crates/solidity-v2/testing/snapshots/diagnostics_output/syntax/multiple_virtual_specifiers/receive_functions/input.sol | New snapshot input covering duplicate virtual on receive(). |
| crates/solidity-v2/testing/snapshots/diagnostics_output/syntax/multiple_virtual_specifiers/receive_functions/generated/solc/0.8.0-failure.txt | Expected solc failure output for duplicate virtual on receive(). |
| crates/solidity-v2/testing/snapshots/diagnostics_output/syntax/multiple_virtual_specifiers/receive_functions/generated/slang/0.8.0-failure.txt | Expected Slang diagnostic output for duplicate virtual on receive(). |
| crates/solidity-v2/testing/snapshots/diagnostics_output/syntax/multiple_virtual_specifiers/modifiers/input.sol | New snapshot input covering duplicate virtual on a modifier. |
| crates/solidity-v2/testing/snapshots/diagnostics_output/syntax/multiple_virtual_specifiers/modifiers/generated/solc/0.8.0-failure.txt | Expected solc failure output for duplicate virtual on a modifier. |
| crates/solidity-v2/testing/snapshots/diagnostics_output/syntax/multiple_virtual_specifiers/modifiers/generated/slang/0.8.0-failure.txt | Expected Slang diagnostic output for duplicate virtual on a modifier. |
| crates/solidity-v2/testing/snapshots/diagnostics_output/syntax/multiple_virtual_specifiers/functions/input.sol | New snapshot inputs covering duplicate virtual on regular functions (including multiple duplicates). |
| crates/solidity-v2/testing/snapshots/diagnostics_output/syntax/multiple_virtual_specifiers/functions/generated/solc/0.8.0-failure.txt | Expected solc failure outputs for duplicate virtual on functions (multiple diagnostics). |
| crates/solidity-v2/testing/snapshots/diagnostics_output/syntax/multiple_virtual_specifiers/functions/generated/slang/0.8.0-failure.txt | Expected Slang diagnostic outputs for duplicate virtual on functions (multiple diagnostics). |
| crates/solidity-v2/testing/snapshots/diagnostics_output/syntax/multiple_virtual_specifiers/fallback_functions/input.sol | New snapshot input covering duplicate virtual on fallback(). |
| crates/solidity-v2/testing/snapshots/diagnostics_output/syntax/multiple_virtual_specifiers/fallback_functions/generated/solc/0.8.0-failure.txt | Expected solc failure output for duplicate virtual on fallback(). |
| crates/solidity-v2/testing/snapshots/diagnostics_output/syntax/multiple_virtual_specifiers/fallback_functions/generated/slang/0.8.0-failure.txt | Expected Slang diagnostic output for duplicate virtual on fallback(). |
| crates/solidity-v2/outputs/cargo/tests/src/diagnostics_output/snapshots.generated.rs | Registers the new snapshot test suite under syntax/multiple_virtual_specifiers. |
| crates/solidity-v2/outputs/cargo/ir/src/ir/builder/mod.rs | Implements duplicate-virtual detection via a shared extract_first_virtual helper and emits the new diagnostic. |
| crates/solidity-v2/outputs/cargo/common/src/diagnostics/kinds/syntax/multiple_virtual_specifiers.rs | Adds the new MultipleVirtualSpecifiers diagnostic kind (code + message + severity). |
| crates/solidity-v2/outputs/cargo/common/src/diagnostics/kinds/syntax/mod.rs | Wires the new diagnostic kind into the syntax diagnostics module and enum. |
| crates/solidity-v2/outputs/cargo/common/generated/public_api.txt | Updates the public API surface to include the new diagnostic type/enum variant. |
| let mutability = self | ||
| .extract_mutability_specifier(&source.attributes.elements) | ||
| .unwrap_or(output::FunctionMutability::NonPayable); | ||
| let virtual_keyword = source.attributes.elements.iter().find_map(|attribute| { | ||
| if let input::FunctionAttribute::VirtualKeyword(virtual_keyword) = attribute { | ||
| Some(self.build_virtual_keyword(virtual_keyword)) | ||
| } else { | ||
| None | ||
| } | ||
| }); | ||
|
|
||
| let virtual_keyword = | ||
| self.extract_first_virtual(source.attributes.elements.iter().filter_map(|attribute| { | ||
| if let input::FunctionAttribute::VirtualKeyword(virtual_keyword) = attribute { | ||
| Some(virtual_keyword) | ||
| } else { | ||
| None | ||
| } | ||
| })); |
There was a problem hiding this comment.
The reason why it's not as clean as the mutability check is that in order to build an output::VirtualKeyword we need a reference to CstToIrBuilder (to get the node id), making it difficult to implement the TryFrom trait.
We could implement smarter transformations, but I don't think is needed for now.
There was a problem hiding this comment.
Is that a requirement?
I wonder why does this keyword get a NodeId and the bunch of utilities that come with it, compared to other attributes that we only record their existence/absence (FunctionVisibility or FunctionMutability). AFAICT, it has no other associated metadata to record.
There was a problem hiding this comment.
I'm not sure, maybe @ggiraldez can chime in here. I'll go ahead and merge since I don't think it's related to the PR itself.
There was a problem hiding this comment.
FunctionVisibility and FunctionMutability are "synthetic" enums, that's why they don't register any traceability back to the CST. The main reason is that in some (most?) cases we want a default value, even in the absence of a language attribute. Eg. function mutability is NonPayable by default, but there's no associated attribute; or free functions have Internal visibility, but they cannot have an explicit visibility attribute.
As to why this attribute records the NodeId and other information, I'm a bit fuzzy on the details, but the change happened in #1751 and I think one of the reasons (maybe the main reason) was to allow more precise diagnostics that may need to reference the keywords. This is actually a shortcoming for visibility/mutability in that area.
There was a problem hiding this comment.
Thanks for the context.
So, if validation happens at this stage (where we have the IR text ranges), we might be able to simplify the AST for something like this to have just an is_virtual: bool property.
There was a problem hiding this comment.
There's no way to add a boolean field type to the IR model right now. Previously that was automatic with unique terminals used as fields, but as part of the change mentioned above we changed it to hold the terminal.
There are a couple of cases where this happens in the language: abstract keyword for contracts and indexed keyword for event parameters (off the top of my head, I may be missing some).
We could change the representation of sequence fields of unique terminals back to a bool, or we can add it as a feature to the IR model mutator, to selectively do it for some fields. The second alternative would be preferable if we want to customize the name of the field, eg. to is_virtual, as otherwise it would be derived from the terminal name, so that would be virtual_keyword: bool in this case.
If we take the mutator feature route, we can probably use the synthetic terminal type External for that, and generate a bool field in those cases.
|
| Branch | teofr/diag-multiple-virtual |
| Testbed | ci |
⚠️ WARNING: Truncated view!The full continuous benchmarking report exceeds the maximum length allowed on this platform.
⚠️ WARNING: No Threshold found!Without a Threshold, no Alerts will ever be generated.
Rejects definitions carrying more than one
virtualkeyword, matching solc's behavior. Emits a newsyntax/multiple-virtual-specifiersdiagnostic ("Only a single virtual specifier can be provided.") on everyvirtualkeyword after the first.Implements solc error codes 6879 and 2662