D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 15851 - Access violation when foreaching variadic template argument tuple
Summary: Access violation when foreaching variadic template argument tuple
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 All
: P3 normal
Assignee: No Owner
URL: http://dlang.org/
Keywords:
Depends on:
Blocks:
 
Reported: 2016-03-30 12:35 UTC by JR
Modified: 2020-02-11 19:47 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description JR 2016-03-30 12:35:49 UTC
The long description: Access violation when dereferencing a field in a foreach of a variadic template argument tuple, when inside a switch case.

dmd is v2.070.2 from the official installer, and the machine is running Windows 8. ldc2 0.17.1 exhibits the same behaviour. Could not test gdc because of difficulty of installation.


Reduced example (http://dpaste.dzfl.pl/ee9a4503f6f5):

-----------------------

import std.stdio;

version = broken;

struct Foo
{
    string abc;
    int def;
}

void walkArgumentTuple(T...)(ref T tup)
{
    top:
    final switch (0)
    {
        foreach (i, ref thing; tup)
        {
            // thing is invalid for some reason
            // but the below doesn't fail
            assert(thing is tup[i]);

            case 0:
                version(broken)
                { 
                    // object.Error@(0): Access Violation
                    writeln(thing);
                }
                else
                {
                    // works
                    writeln(tup[i]);
                }

                break top;
        }
    }
}

void main()
{
	Foo f;
	walkArgumentTuple(f);
}

-----------------------

All tested compilers accept and compile this. It behaves differently depending on the number (and maybe type) of members of the Foo struct. Likewise if compiled with or without -m64, and whether parameters are marked as ref or not. In many combinations it won't error out, but instead spam the screen with garbage in hex. See the dpaste linked earlier, with version = dpastefreaksout uncommented.

    Foo(x"10 1C 00 00 00 00 00 00 7C FD 45 [... ad nauseam]

I have also had it spout out semi-intelligible things removing ref from everything, but dpaste can't seem to reproduce that.

    Foo("\x04\x10\0\0\0\0\0\0gc.config\0src\\gc\\config.d\0\0\0\0\0", 0)
Comment 1 moonlightsentinel 2020-02-11 19:47:28 UTC
This no longer compiles:

onlineapp.d(14): Error: switch skips declaration of variable onlineapp.walkArgumentTuple!(Foo).walkArgumentTuple.thing at onlineapp.d(16)