Skip to content

Subclassing ValueType with custom constructor crashes with internal CLR error #10231

Closed

Description

Steps to reproduce

# Parameterless constructor isn't the best example since you can't
# do that in C# either, but it also repros with parameters.
class Test : ValueType {
    Test() { }
}

[Test]::new()

Expected behavior

Either

  1. Parse error when ValueType is the base class
  2. Special handling for value types when generating constructors. I'm guessing this has to do with $this needing to be explicitly boxed, but I haven't looked into it

Actual behavior

Internal CLR error. (0x80131506)
   at DynamicClass.Test(System.Runtime.CompilerServices.Closure, System.Management.Automation.Language.FunctionContext)
   at System.Management.Automation.ScriptBlock.InvokeWithPipeImpl(System.Management.Automation.ScriptBlockClauseToInvoke, Boolean, System.Collections.Generic.Dictionary`2<System.String,System.Management.Automation.ScriptBlock>, System.Collections.Generic.List`1<System.Management.Automation.PSVariable>, ErrorHandlingBehavior, System.Object, System.Object, System.Object, System.Management.Automation.Internal.Pipe, System.Management.Automation.InvocationInfo, System.Object[])
   at System.Management.Automation.ScriptBlock.InvokeWithPipe(Boolean, ErrorHandlingBehavior, System.Object, System.Object, System.Object, System.Management.Automation.Internal.Pipe, System.Management.Automation.InvocationInfo, Boolean, System.Collections.Generic.List`1<System.Management.Automation.PSVariable>, System.Collections.Generic.Dictionary`2<System.String,System.Management.Automation.ScriptBlock>, System.Object[])
   at System.Management.Automation.ScriptBlock.InvokeAsMemberFunction(System.Object, System.Object[])
   at System.Management.Automation.Internal.ScriptBlockMemberMethodWrapper.InvokeHelper(System.Object, System.Object, System.Object[])
   at System.RuntimeTypeHandle.CreateInstance(System.RuntimeType, Boolean, Boolean, Boolean ByRef, System.RuntimeMethodHandleInternal ByRef, Boolean ByRef)
   at System.RuntimeType.CreateInstanceDefaultCtorSlow(Boolean, Boolean, Boolean)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean, Boolean, Boolean, Boolean)
   at System.Activator.CreateInstance(System.Type, Boolean, Boolean)
   at System.Activator.CreateInstance(System.Type)
   at System.Management.Automation.Interpreter.InitializeLocalInstruction+MutableValue.Run(System.Management.Automation.Interpreter.InterpretedFrame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(System.Management.Automation.Interpreter.InterpretedFrame)
   at System.Management.Automation.Interpreter.Interpreter.Run(System.Management.Automation.Interpreter.InterpretedFrame)
   at System.Management.Automation.Interpreter.LightLambda.RunVoid1[[System.__Canon, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.__Canon)

The above stack trace minus the first two lines repeats for about 5,000 lines before PowerShell crashes. Sometimes the stack trace isn't included, and it's just the first line.

Environment data

Name                           Value
----                           -----
PSVersion                      7.0.0-preview.2
PSEdition                      Core
GitCommitId                    7.0.0-preview.2
OS                             Microsoft Windows 10.0.18362
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Activity

  1. added
    Issue-Question
    on Jul 26, 2019
  2. added
    WG-Engine
    on Jul 29, 2019
  3. iSazonov commented on Jan 31, 2020

    @iSazonov
    Collaborator

    /cc Rob Holt (@rjmholt) for your class issue collection.

  4. added
    Needs-Triage
    on Jun 2, 2022
  5. vexx32 commented on Jun 2, 2022

    @vexx32
    Collaborator

    Marking for triage so the Engine WG can pick it up.

  6. IISResetMe commented on Jul 21, 2022

    @IISResetMe
    Collaborator

    Engine WG reviewed this issue, and agree the current behavior is less than ideal.

    Given no immediate plans exist to introduce support for emitting value types, we should actively prevent System.ValueType from being used as a base class and instead emit a more helpful error at parse-time ("Subclassing ValueType is not supported" or similar).

  7. removed
    Needs-Triage
    on Jul 21, 2022
  8. added
    Resolution-No Activity
    on Nov 16, 2023
  9. mklement0 commented on Dec 29, 2024

    @mklement0
    Contributor

    This issue should be labeled Keep-Open and possibly also Up-for-Grabs

  10. added
    WG-Reviewed
    Up-for-Grabs
    and removed
    Resolution-No Activity
    on Feb 15, 2025
  11. microsoft-github-policy-service commented on Aug 15, 2025

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if there is no further activity in 7 days, the issue will be closed automatically.

    Activity in this case refers only to comments on the issue. If the issue is closed and you are the author, you can re-open the issue using the button below. Please add more information to be considered during retriage. If you are not the author but the issue is impacting you after it has been closed, please submit a new issue with updated details and a link to this issue and the original.

  12. added
    Resolution-No Activity
    on Aug 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Issue-QuestionResolution-No ActivityUp-for-GrabsWG-EngineWG-Reviewed

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions