Skip to content
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

Improve static_assert #6136

Open
skallweitNV opened this issue Jan 20, 2025 · 2 comments
Open

Improve static_assert #6136

skallweitNV opened this issue Jan 20, 2025 · 2 comments
Assignees
Labels
goal:quality & productivity Quality issues and issues that impact our productivity coding day to day inside slang

Comments

@skallweitNV
Copy link
Collaborator

static_assert usage is very limited right now as it only works in function scope.

static_assert must be available in any scope.

Also, the error message argument should be optional.

@bmillsNV bmillsNV added this to the Q1 2025 (Winter) milestone Jan 24, 2025
@bmillsNV bmillsNV added the goal:quality & productivity Quality issues and issues that impact our productivity coding day to day inside slang label Jan 24, 2025
@csyonghe
Copy link
Collaborator

@skallweitNV can you share more details on how you want to use static_assert?

Since slang has much more complicated semantics around what is compile-time (link time constants, specialization constants), it is more important for us to understand different kinds of needs.

@skallweitNV
Copy link
Collaborator Author

To elaborate a bit. This works, the static_assert is reported correctly:

struct S
{
    uint value;
};

void func()
{
    static_assert(sizeof(S) == 2, "some message"); // <-- REPORTS FAILED ASSERT
}

[shader("compute")]
void main() {
    func();
}

However, if func is not called, the static_assert is not checked:

struct S
{
    uint value;
};

void func()
{
    static_assert(sizeof(S) == 2, "some message"); // <-- NOT CHECKED
}

[shader("compute")]
void main() {
    // func();
}

If we take the static_assert out of function scope to global scope, the program doesn't compile anymore (but it should IMO):

struct S
{
    uint value;
};

static_assert(sizeof(S) == 2, "some message"); // <-- COMPILATION ERROR

[shader("compute")]
void main() {
}

The error is:

USER error: /user.slang(6): error 20001: unexpected '(', expected ')'
static_assert(sizeof(S) == 2, "some message");

In addition to making static_assert work in global scope, it would be a nice addition to make the string message argument optional:

static_assert(sizeof(S) == 2, "some message");
static_assert(sizeof(S) == 2); // <-- THIS SHOULD WORK TOO

So, this issues isn't really about what can go into the expression (any constant/compile-time expression should work) but in what scope static_assert can be used (and the compiler actually checks the assert).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
goal:quality & productivity Quality issues and issues that impact our productivity coding day to day inside slang
Projects
None yet
Development

No branches or pull requests

3 participants