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

Unhandled edge cases for cast/assignment to vector type #1215

Open
cbuttner opened this issue Jul 1, 2024 · 9 comments
Open

Unhandled edge cases for cast/assignment to vector type #1215

cbuttner opened this issue Jul 1, 2024 · 9 comments
Assignees
Labels
bug Something isn't working fixed needs check Fixed, but needs verification that it works
Milestone

Comments

@cbuttner
Copy link

cbuttner commented Jul 1, 2024

Cast/assignment to vector has some unhandled edge cases:

float[<4>] a;
a = true; // Permitted, but shouldn't be
a = "1234"; // I get this casts char[4] to float[<4>], but can this specific case be prevented for the String type?

// Casting from pointer
a = (void*)1; // No proper error emitted
a = null; // No proper error emitted
a = &foo; // Casting from function pointer, no proper error emitted
@lerno
Copy link
Collaborator

lerno commented Jul 1, 2024

Yeah, looks bad.

@lerno lerno self-assigned this Jul 1, 2024
@lerno lerno added the bug Something isn't working label Jul 1, 2024
@lerno
Copy link
Collaborator

lerno commented Jul 1, 2024

Should be fixed now.

@lerno lerno added the fixed needs check Fixed, but needs verification that it works label Jul 1, 2024
@lerno lerno added this to the 0.6.1 milestone Jul 2, 2024
@cbuttner
Copy link
Author

cbuttner commented Jul 4, 2024

Thanks. I think the implicit scalar broadcast cast should be explicit instead. This should better signify intent to actually do a broadcast, and prevent accidentally assigning or passing a scalar where a vector is expected.

@lerno
Copy link
Collaborator

lerno commented Jul 4, 2024

That would break things like

int[<3>] x = foo();
int[<3>] y = x * 3 + 1; 
// int[<3>] y = x * (int[<3>])3 + (int[<3>])1;

@cbuttner
Copy link
Author

cbuttner commented Jul 4, 2024

It should break this, scaling is the only operator that makes sense mathematically ("ah yes, let me add 1 to a vector"). I'd rather it be x + (int[<3>])1.

@lerno
Copy link
Collaborator

lerno commented Jul 4, 2024

I think this is fine in GLSL though?

@cbuttner
Copy link
Author

cbuttner commented Jul 4, 2024

True, but it's important to note that for GLSL the implicit cast is only legal for operators, meaning you can't assign or pass a scalar to a vector (see https://registry.khronos.org/OpenGL/specs/gl/GLSLangSpec.4.60.pdf Section 5.9).
If that were also the case in C3 I'd have no problem with the implicit broadcasting.

@lerno
Copy link
Collaborator

lerno commented Jul 4, 2024

If we think about how casts work:

  1. First we have the normal casts, for example between binary sub expression.
  2. Then we have the assign expression, here there is some inference and more leeway.
  3. Finally we have the most generous casts, which is during initialization.

So this is problematic in reversing how "generous" the rules are in the particular case of scalar -> vector conversion. This special case will also likely affect how one would write macros.

For example we would have 1 not being assignable (so $assignable(1, int[<2>]) would fail) to int[<2>] but it would be possible to multiply the scalar with the expression? So that is my concern.

@lerno
Copy link
Collaborator

lerno commented Jul 4, 2024

For what it's worth, HLSL always does the scalar conversion: https://microsoft.github.io/hlsl-specs/specs/hlsl.pdf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed needs check Fixed, but needs verification that it works
Projects
None yet
Development

No branches or pull requests

2 participants