Skip to content

Commit

Permalink
[glsl-in] Switch implicit type conversion (gfx-rs#2273)
Browse files Browse the repository at this point in the history
  • Loading branch information
evahop authored and kvark committed Mar 18, 2023
1 parent 046f644 commit ac382b8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
14 changes: 10 additions & 4 deletions src/front/glsl/parser/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,14 @@ impl<'source> ParsingContext<'source> {

self.expect(frontend, TokenValue::LeftParen)?;

let selector = {
let (selector, uint) = {
let mut stmt = ctx.stmt_ctx();
let expr = self.parse_expression(frontend, ctx, &mut stmt, body)?;
ctx.lower_expect(stmt, frontend, expr, ExprPos::Rhs, body)?
.0
let (root, meta) =
ctx.lower_expect(stmt, frontend, expr, ExprPos::Rhs, body)?;
let uint = frontend.resolve_type(ctx, root, meta)?.scalar_kind()
== Some(crate::ScalarKind::Uint);
(root, uint)
};

self.expect(frontend, TokenValue::RightParen)?;
Expand All @@ -197,7 +200,10 @@ impl<'source> ParsingContext<'source> {
ConstantInner::Scalar {
value: ScalarValue::Sint(int),
..
} => crate::SwitchValue::I32(int as i32),
} => match uint {
true => crate::SwitchValue::U32(int as u32),
false => crate::SwitchValue::I32(int as i32),
},
ConstantInner::Scalar {
value: ScalarValue::Uint(int),
..
Expand Down
9 changes: 9 additions & 0 deletions tests/in/glsl/statements.frag
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ void switchNoDefault(int a) {
return;
}

void switchCaseImplConv(uint a) {
switch (a) {
case 0:
break;
}

return;
}

void switchNoLastBreak(int a) {
switch (a) {
default:
Expand Down
22 changes: 18 additions & 4 deletions tests/out/wgsl/statements-frag.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,29 @@ fn switchNoDefault(a_2: i32) {
return;
}

fn switchNoLastBreak(a_4: i32) {
var a_5: i32;
var b: i32;
fn switchCaseImplConv(a_4: u32) {
var a_5: u32;

a_5 = a_4;
let _e2 = a_5;
switch _e2 {
case 0u: {
}
default: {
}
}
return;
}

fn switchNoLastBreak(a_6: i32) {
var a_7: i32;
var b: i32;

a_7 = a_6;
let _e2 = a_7;
switch _e2 {
default: {
let _e3 = a_5;
let _e3 = a_7;
b = _e3;
}
}
Expand Down

0 comments on commit ac382b8

Please sign in to comment.