Skip to content

Add test for using case statement inside if statement.#919

Merged
kant2002 merged 6 commits intomainfrom
kant/cursed-case
Jan 23, 2026
Merged

Add test for using case statement inside if statement.#919
kant2002 merged 6 commits intomainfrom
kant/cursed-case

Conversation

@kant2002
Copy link
Copy Markdown
Collaborator

@kant2002 kant2002 commented Jan 2, 2026

No description provided.

@kant2002
Copy link
Copy Markdown
Collaborator Author

kant2002 commented Jan 2, 2026

This test generates following output

public static int main()
{
	int result = 41;
	int num = 3;
	int num2 = 0;
	int num3 = num;
	if (num3 != 3)
	{
		if (num3 == 2)
		{
			goto IL_0033;
		}
	}
	else if (num2 != 0)
	{
		goto IL_0033;
	}
	goto IL_0042;
	IL_0074:
	int num4 = num4 + 1;
	goto IL_0083;
	IL_0083:
	return result;
	IL_0033:
	num4++;
	goto IL_0042;
	IL_0042:
	num = 2;
	int num5 = num;
	if (num5 != 3)
	{
		if (num5 == 2)
		{
			goto IL_0074;
		}
	}
	else if (num2 != 0)
	{
		goto IL_0074;
	}
	goto IL_0083;
}

Notice num4/num5 variables which are generated

@kant2002
Copy link
Copy Markdown
Collaborator Author

kant2002 commented Jan 2, 2026

Based on diagnostics, lowering and graph transformation seems to be preserve code semantics. I'ts emit phase probably which is broken.

This is sample of code which we are sending to Emitter

{
        <var #0> = 41;
        <var #1> = 3;
        <var #2> = 0;
        <var #3> = <var #1>;
        if (<var #3>==3)
            goto d7153162-ef15-4703-9b2c-a7204926f591;
        if (<var #3>==2)
            goto 6b1382d5-216b-4d48-9efc-22c3a313745b;
        goto 711ab9d6-338e-4892-994e-79e63981d7a1;
    d7153162-ef15-4703-9b2c-a7204926f591:
            if (<var #2>)
            {
                6b1382d5-216b-4d48-9efc-22c3a313745b:
                        <var #0> = <var #0>+1;
        goto 711ab9d6-338e-4892-994e-79e63981d7a1;
    711ab9d6-338e-4892-994e-79e63981d7a1:
            ;
        <var #1> = 2;
        <var #4> = <var #1>;
        if (<var #4>==3)
            goto ee94a688-c8c0-4215-9325-4b8f59372909;
        if (<var #4>==2)
            goto 9bbb36fe-298c-46f0-8f76-d2e21f158406;
        goto abd502db-230b-4cd3-904a-f3033f978278;
    ee94a688-c8c0-4215-9325-4b8f59372909:
            if (<var #2>)
            {
                9bbb36fe-298c-46f0-8f76-d2e21f158406:
                        <var #0> = <var #0>+1;
        goto abd502db-230b-4cd3-904a-f3033f978278;
    abd502db-230b-4cd3-904a-f3033f978278:
            ;
    return <var #0>;

@kant2002
Copy link
Copy Markdown
Collaborator Author

Probably we have issues somewhere in the Linearization phase. After it, all EmitScope should be empty. When I set assert, it fails only in new test. If PR sits for too long probably good to merge and create issue asking to add

Debug.Assert(s.EmitScope == null || s.EmitScope == scope, "EmitScope should be function scope after Lineralize");

in the BlockItemEmitting.EmitCode

@kant2002 kant2002 merged commit cc3472a into main Jan 23, 2026
8 checks passed
@kant2002 kant2002 deleted the kant/cursed-case branch January 23, 2026 21:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant