-
Notifications
You must be signed in to change notification settings - Fork 271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
At-attributes - new language feature #5795
Labels
kind: enhancement
Enhancements, feature requests, etc. These are NOT bugs, but ways we can improve Dafny
Comments
After discussions with the team, here are a few more precisions on the implementation of this feature.
|
MikaelMayer
added a commit
that referenced
this issue
Oct 18, 2024
Related to #5795 ### Description This is the first intruction of @-attributes in Dafny. * In this PR @-attributes are parsed only for top-level declarations and member declarations * Old-style attributes are still supported * An early resolution rewriting phase takes care of converting these new attributes whose name is "@" and have one argument, to regular attributes * Boogie does not receives these user-supplied @- attributes for now * @-attributes are resolved as if they were datatypes * For attributes used during parsing (i.e. options), I changed the code to also detect @-attributes * Formatting of @-attributes ### Future features in development not included in this PR * User-defined @-attributes * @-attributes on clauses, statements, expressions * Language server auto-complete of @-attributes ### How has this been tested? - Existing CI tests ensure we did not loose existing attributes - One test to pass when @-attributes are correctly supplied - Two failure tests to verify that @-attributes have correct resolution errors and don't parse @-calls - The `Compile(false)` on the new version of some `DafnyCore/**/.dfy` files is tested by the fact that the generated code still does not contain the modules annotated that way. - Pretty-printing checked by all checked Dafny source files in DafnyCore and DafnyStandardLibraries Note that I could not reach in this PR a failing test to trigger the "Attribute not expected here" error, because Coco interprets an @-attribute as the start of a TopLevelDecl for now. In an upcoming PR, non-empty @-attributes will be returned by TopDecl so this error message will be tested. <small>By submitting this pull request, I confirm that my contribution is made under the terms of the [MIT license](https://github.com/dafny-lang/dafny/blob/master/LICENSE.txt).</small>
MikaelMayer
added a commit
to dafny-lang/ide-vscode
that referenced
this issue
Nov 12, 2024
Before:  After:  Related issue dafny-lang/dafny#5795 @-attributes are now ready on Dafny, and they also have auto-complete from the language server. Having syntax hightlight will make them shine. I added a "visual regression test". It's unfortunate that they are not programmatically tested, but I hope that we will have semantic highlighting that will get rid of that issue.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
kind: enhancement
Enhancements, feature requests, etc. These are NOT bugs, but ways we can improve Dafny
Summary
We plan on adding two new attributes in the language to have type-safe user-defined attributes.
{:attribute}
in front of a datatype to mark it as a potential attribute for the rest of the code.{:@ X}
which takes a constructor of a datatype marked as{:attribute}
To simplify the use of user-defined attributes and ensure they are typed appropriately, we introduce a special syntax
@X
or@X(...)
in places where attributes are accepted and more, as a shortcut for{:@ X(....)}
This is related to #3149
Background and Motivation
Attributes in Dafny tweak verification strategy (e.g. split_here, isolate_assertions, only, autocontracts, disable_nonlinear_arithmetic, fuel...), or compilation strategy (e.g. extern, nativeType, compile, synthesize, ...). Recently, with the Yucca project and now the Daffodyl project, We have seen Dafny users starting using custom attributes for their own projects as well.
Unfortunately, attributes do not have a strong resolution phase. Thus, if an attribute is misspelled, Dafny won’t generate any issue. This can create confusion in some code until the typo is spotted. Moreover, contrary to built-in attributes, user-defined attributes arguments are not checked against certain types, so this makes user codebase with attributes prone to errors or maintenance issues.
However, we can’t just type-check all attributes, as we expect that users will have to define their own attributes for more reasons, e.g. tweaking more the compiler (e.g. in Rust, see below), or the verification strategy (possibly an ITP in the future)
Here are a few other motivations about why attributes matter in the future.
Motivation 1. Lots of attributes to expect for Dafny-to-Rust
For the new Dafny-to-Rust code generator, I expect that there will need to be a lot more attributes to let our customers compile to more performant code. For example, to fine-tune how memory locations will encode types, that introduces 3 if not 4 new attributes. There are reason to believe more attributes will be necessary in the future, e.g. to model lifetimes, or to have different encoding of a type parameter as a generic or as a type member for interoperability.
Motivation 2. User-defined attributes
Besides these built-in attributes, there will also be necessary to provide support for user-defined attributes on which Rust heavily relies on. For example, there are many JSON serialization library, all of which define attributes to put on structures.
#[derive(Serialize)]
and#[derive(Deserialize)]
. Yet, we don’t have a reliable mechanism to identify what could serve as a custom attribute or not in Dafny.Motivation 3: More precise assertion scoping.
Assertions in expressions currently leak outside of their scope, similar to assertion in statement blocks. However, there are cases when we want to ensure they are local, accessible or not to inner blocks, or exported only for example. Fine-tuning this aspect of assertions could be done via attributes. However, this also means adding a lot of new attributes, and I've seen that attributes are a barrier to adopting such behaviors partly because of their syntax (see next paragraph)
Motivation 4: Escape the maze of curly braces.
Attributes trigger a maze of curly braces that I think are hard to read. However. It’s not only that. Braces are used for heterogeneous language constructs already. If you see a curly brace in Dafny, what can it contains? It depends both on the external context and on the internal value:
Of all of these, the last usage of curly brace is the only one that is not determined by the context but what it starts with. This inconsistency, although minor, is just an hint than maybe we could think of a different way to represent attributes to not overload the already crowded space of curly brace semantics.
Moreover, defining attributes with arguments separated by commas while not being enclosed with parentheses is pretty non-standard. In LISP, or bash, parentheses are also outside of the expression but arguments are space-separated, not comma-separated. In languages where arguments are separated by comma, they are regrouped as a tuple using parentheses after the function name. So that’s for my argument that our attribute syntax is a bit peculiar and not familiar for new users.
Proposed Feature
With everything in place, users would be able to define and use typed attributes like this:
Now, in remaining Dafny code, users can use the constructor of this datatype as an attribute, like this:
Note that such typed attributes can be placed between the datatype keyword and name, as for regular attributes, but we will probably deprecate that in the future for readability.
This would be the same for other declarations, methods, assertions, etc.
Existing attributes will have a typed attribute version available if one is using the standard library, so that one would be able to write
and even get suggestions if an attribute is mistyped.
Alternatives
Because the new type system is not yet compatible with the entire Dafny test suite, we will have only one kind of attribute. In the future, we might want to ensure attributes must extend some traits so that we can't use assertion attributes for methods.
The text was updated successfully, but these errors were encountered: