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

Request: Make generic types with explicit layout a compiler error #8456

Open
leppie opened this issue Feb 8, 2016 · 4 comments
Open

Request: Make generic types with explicit layout a compiler error #8456

leppie opened this issue Feb 8, 2016 · 4 comments

Comments

@leppie
Copy link
Contributor

leppie commented Feb 8, 2016

The following compiles, but results in a TypeLoadException:

[StructLayout(LayoutKind.Explicit)]
struct Union<T1, T2>
{
    [FieldOffset(0)]
    readonly T1 t1;
    [FieldOffset(0)]
    readonly T2 t2;
}
Could not load type 'Union`2' ... because generic types cannot have explicit layout.

Given it will always fail at runtime, should this not be considered to be a compiler error instead?

@leppie
Copy link
Contributor Author

leppie commented Feb 18, 2016

While this is not illegal (C#), I think there should be at least a warning, if detectable. I am not sure if this is forbidden in the CLR or just a limitation that could be done in some other implementation.

@jskeet
Copy link

jskeet commented Feb 18, 2016

Presumably this wouldn't be the case if T1 and T2 were both constrained to be reference types?

@leppie
Copy link
Contributor Author

leppie commented Mar 3, 2016

@jskeet This happens when constrained too.

This also happens with a class with explicit layout.

@leppie
Copy link
Contributor Author

leppie commented Mar 3, 2016

This also fails in the following scenario, even though the size can be reliably determined:

class Pointer<T> // or struct
{
    public T Value;
}

[StructLayout(LayoutKind.Explicit)]
struct Union<T1, T2>
{
    [FieldOffset(0)]
    readonly Pointer<T1> t1;
    [FieldOffset(0)]
    readonly Pointer<T2> t2;
}

Maybe a runtime issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants