Skip to content

Defining a type that contains a method with a parameter typed as itself throws when the method is an override candidate #10623

Closed

Description

Steps to reproduce

class Test {
    [bool] Equals([Test] $other) {
        return $true
    }
}

Expected behavior

No exception, type is defined successfully.

Actual behavior

PS> class Test { [bool] Equals([Test] $other) { return $true } }
An error occurred while creating the pipeline.
+ CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : RuntimeException

PS> $Error[0].GetBaseException() | Format-List * -Force
Message        : Type must be a type provided by the runtime. (Parameter 'types')
ParamName      : types
TargetSite     : System.Reflection.MethodBase SelectMethod(System.Reflection.BindingFlags, System.Reflection.MethodBase[], System.Type[],
                 System.Reflection.ParameterModifier[])
StackTrace     :    at System.DefaultBinder.SelectMethod(BindingFlags bindingAttr, MethodBase[] match, Type[] types, ParameterModifier[]
                 modifiers)
                    at System.RuntimeType.GetMethodImplCommon(String name, Int32 genericParameterCount, BindingFlags bindingAttr, Binder
                 binder, CallingConventions callConv, Type[] types, ParameterModifier[] modifiers)
                    at System.RuntimeType.GetMethodImpl(String name, BindingFlags bindingAttr, Binder binder, CallingConventions callConv,
                 Type[] types, ParameterModifier[] modifiers)
                    at System.Type.GetMethod(String name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention,
                 Type[] types, ParameterModifier[] modifiers)
                    at System.Type.GetMethod(String name, Type[] types)
                    at System.Management.Automation.Language.TypeDefiner.DefineTypeHelper.MethodExistsOnBaseClassAndFinal(String
                 methodName, Type[] parameterTypes)
                    at System.Management.Automation.Language.TypeDefiner.DefineTypeHelper.DefineMethod(FunctionMemberAst functionMemberAst)
                    at System.Management.Automation.Language.TypeDefiner.DefineTypeHelper.DefineMembers()
                    at System.Management.Automation.Language.TypeDefiner.DefineTypes(Parser parser, Ast rootAst, TypeDefinitionAst[]
                 typeDefinitions)
                    at System.Management.Automation.Language.Compiler.DefinePowerShellTypes(Ast rootForDefiningTypes, TypeDefinitionAst[]
                 typeAsts)
                    at System.Management.Automation.Language.Compiler.GenerateTypesAndUsings(ScriptBlockAst rootForDefiningTypesAndUsings,
                 List`1 exprs)
                    at System.Management.Automation.Language.Compiler.CompileSingleLambda(ReadOnlyCollection`1 statements,
                 ReadOnlyCollection`1 traps, String funcName, IScriptExtent entryExtent, IScriptExtent exitExtent, ScriptBlockAst
                 rootForDefiningTypesAndUsings)
                    at System.Management.Automation.Language.Compiler.CompileNamedBlock(NamedBlockAst namedBlockAst, String funcName,
                 ScriptBlockAst rootForDefiningTypes)
                    at System.Management.Automation.Language.Compiler.VisitScriptBlock(ScriptBlockAst scriptBlockAst)
                    at System.Management.Automation.Language.Compiler.Compile(CompiledScriptBlockData scriptBlock, Boolean optimize)
                    at System.Management.Automation.CompiledScriptBlockData.ReallyCompile(Boolean optimize)
                    at System.Management.Automation.CompiledScriptBlockData.CompileUnoptimized()
                    at System.Management.Automation.CompiledScriptBlockData.Compile(Boolean optimized)
                    at System.Management.Automation.DlrScriptCommandProcessor.Init()
                    at System.Management.Automation.DlrScriptCommandProcessor..ctor(ScriptBlock scriptBlock, ExecutionContext context,
                 Boolean useNewScope, CommandOrigin origin, SessionStateInternal sessionState, Object dollarUnderbar)
                    at System.Management.Automation.Runspaces.Command.CreateCommandProcessor(ExecutionContext executionContext, Boolean
                 addToHistory, CommandOrigin origin)
                    at System.Management.Automation.Runspaces.LocalPipeline.CreatePipelineProcessor()
Data           : {}
InnerException :
HelpLink       :
Source         : System.Private.CoreLib
HResult        : -2147024809

Environment data

Name                           Value
----                           -----
PSVersion                      7.0.0-preview.4
PSEdition                      Core
GitCommitId                    7.0.0-preview.4
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

This also occurs in Core 6.2.3 but does not occur in Windows PowerShell.

Activity

  1. added
    Issue-Question
    on Sep 25, 2019
  2. added
    WG-Engine
    on Sep 25, 2019
  3. iSazonov commented on Sep 25, 2019

    @iSazonov
    Collaborator

    It seems we already discussed an issue like this. PowerShell should compile a type before it can use it. So any recursion in type definition (direct or indirect) is impossible.

  4. SeeminglyScience commented on Sep 25, 2019

    @SeeminglyScience
    ContributorAuthor

    Ilya (@iSazonov) It works when the method isn't an override candidate. For example, the same snippet works if you change the method name Equals to Equals2:

    class Test {
        [bool] Equals2([Test] $other) {
            return $true
        }
    }

    Basically DefineTypeHelper.MethodExistsOnBaseClassAndFinal should return early if the parameter types contain the type currently being defined.

  5. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  6. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  7. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  8. added
    Resolution-No Activity
    on Nov 16, 2023
  9. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-QuestionResolution-No ActivityWG-Engine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions