-
Notifications
You must be signed in to change notification settings - Fork 195
Add support for #[cfg(test)] attribute #2168
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
base: main
Are you sure you want to change the base?
Conversation
6bee119
to
fe6d77c
Compare
fe6d77c
to
6b3fc81
Compare
@rw1nkler can you add DSLX examples w/ tests functions? |
This does seems slightly inconsistent w/ our current annotations |
This is consistent with how rust does things (sort of, #[cfg(whatever)] acts more like a #ifdef and style is to place it on the 'tests' module but its pretty close): rust docs |
Ah you're right that's close to what Rust does:
I guess one difference is that because they have Maybe worth logging as a issue if we want something similar as a follow-up. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, is there a test for what happens when we refer to an entity that's test-only though in a non-test scenario? Seems like it'd be nicer to make a DSLX level error message instead of having a linker-style error at IR conversion/opt time.
6b3fc81
to
e5bb172
Compare
There's a test that checks if a test utility function was called from a regular function or proc: Right now, there are no checks for spawning utility procs inside regular procs. |
xls/dslx/fmt/ast_fmt.cc
Outdated
@@ -2208,6 +2208,11 @@ DocRef Formatter::Format(const Function& n) { | |||
arena_.MakeText("\")]"), | |||
arena_.hard_line(), | |||
})); | |||
} else if (n.test_only() && !is_test) { | |||
fn_pieces.push_back(ConcatN(arena_, { | |||
arena_.MakeText("#[cfg(test)]"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be good to expose an inline constexpr std::string_view from ast_utils.h that is the "cfg(test)" string, so that in real code we don't have to repeat it in different places (it's fine to repeat it in test DSLX).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added the constant
FYI @rw1nkler looks like you'll need to rebase to get this in. |
Signed-off-by: Robert Winkler <[email protected]>
e5bb172
to
71546c3
Compare
} else if (n.test_only() && !is_test) { | ||
fn_pieces.push_back( | ||
ConcatN(arena_, { | ||
arena_.MakeText("#"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The proc version inserts the whole attribute at the same time. Is there a benefit either way?
@@ -2376,6 +2376,8 @@ class Function : public AstNode { | |||
disable_format_ = disable_format; | |||
} | |||
bool disable_format() const { return disable_format_; } | |||
void set_test_only(bool test_only) { test_only_ = test_only; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If at all possible this should be done in the constructor instead of in a mutator
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to be consistent with other attributes like dslx_format_disable
or extern_verilog
. If using a constructor is preferred I can change that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, having the bit in the constructor is preferred. The other mutator methods are only present out of necessity due to quirks in the parser.
Description
This commit adds a
#[cfg(test)]
attribute that you can use to mark functions or procs that are only meant for testing. When marked this way, they won't be lowered to IR. The original request in #1524 was just for functions, but I applied the same idea to procs as well.Implementation details
This feature uses a
test_only
flag in theFunction
class to mark functions and procs that are only used for tests. When converting code to IR, functions and procs marked as test-only are skipped, unless theconvert_tests
option is enabled. This allows test helpers to be included in the IR if needed.IR conversion now provides clearer error messages. If a test-only function or proc is selected for conversion without the
convert_tests
option, a dedicated error is shown. Same happens when a test-only function is called from a regular function or proc and IR is being dumped.In DSLX, the interpreter will fail if a test-only function is called from a regular one. This check is not yet in place for procs, as communication between regular and test-only procs involves more complexity, but I can add it too if needed.
Resolves #1524