Repository navigation
PowerShell fails to call .NET method that that has an optional IntPtr parameter whose default value is Int32 0 #19429
Copy link
Copy link
Open
Labels
Issue-BugIssue has been identified as a bug in the productIssue has been identified as a bug in the productKeepOpenThe bot will ignore these and not auto-closeThe bot will ignore these and not auto-closeUp-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Engine-ParameterBinder
Milestone
Description
Prerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
Steps to reproduce
# !! BROKEN:
# * `default(IntPtr)` (or `default`) are converted to System.Int32(!) 0 on compilation (the same happens in regular .NET SDK probject)
# * C# code can call such a method just fine, but PowerShell complains.
# * Workaround: use `new IntPtr()` as the default value.
(Add-Type -PassThru -Name Foo1 -MemberDefinition @'
public static void Bar(IntPtr p = default(IntPtr)) { }
'@)::Bar()Expected behavior
The method calls should succeed (it produces no output).
Actual behavior
The method call fails with the error below.
Error details
Exception :
Type : System.ArgumentException
Message : Expression of type 'System.Int32' cannot be used for parameter of type 'System.IntPtr' of method 'Void Bar(IntPtr)' (Parameter 'arg0')
ParamName : arg0
TargetSite :
Name : ValidateOneArgument
DeclaringType : System.Dynamic.Utils.ExpressionUtils, System.Linq.Expressions, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
MemberType : Method
Module : System.Linq.Expressions.dll
Data : System.Collections.ListDictionaryInternal
Source : System.Linq.Expressions
HResult : -2147024809
StackTrace :
at System.Dynamic.Utils.ExpressionUtils.ValidateOneArgument(MethodBase method, ExpressionType nodeKind, Expression arguments, ParameterInfo pi, String methodParamName, String argumentParamName, Int32 index)
at System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method, Expression arg0)
at System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method, IEnumerable`1 arguments)
at System.Management.Automation.Language.PSInvokeMemberBinder.InvokeMethod(MethodBase mi, DynamicMetaObject target, DynamicMetaObject[] args, Boolean expandParameters, MethodInvocationType invocationType)
at System.Management.Automation.Language.PSInvokeMemberBinder.InvokeDotNetMethod(CallInfo callInfo, String name, PSMethodInvocationConstraints psMethodInvocationConstraints, MethodInvocationType methodInvocationType, DynamicMetaObject target, DynamicMetaObject[] args, BindingRestrictions restrictions, MethodInformation[] mi, Type errorExceptionType)
at System.Management.Automation.Language.PSInvokeMemberBinder.FallbackInvokeMember(DynamicMetaObject target, DynamicMetaObject[] args, DynamicMetaObject errorSuggestion)
at System.Management.Automation.PSObject.PSDynamicMetaObject.BindInvokeMember(InvokeMemberBinder binder, DynamicMetaObject[] args)
at System.Dynamic.DynamicMetaObjectBinder.Bind(Object[] args, ReadOnlyCollection`1 parameters, LabelTarget returnLabel)
at System.Runtime.CompilerServices.CallSiteBinder.BindCore[T](CallSite`1 site, Object[] args)
at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
at System.Management.Automation.Interpreter.DynamicInstruction`2.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
CategoryInfo : OperationStopped: (:) [], ArgumentException
FullyQualifiedErrorId : System.ArgumentException
InvocationInfo :
ScriptLineNumber : 1
OffsetInLine : 1
HistoryId : 2
Line : (Add-Type -PassThru -Name Foo1 -MemberDefinition @'
Statement : (Add-Type -PassThru -Name Foo1 -MemberDefinition @'
public static void Bar(IntPtr p = default(IntPtr)) { }
'@)::Bar()
PositionMessage : At line:1 char:1
+ (Add-Type -PassThru -Name Foo1 -MemberDefinition @'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CommandOrigin : Internal
ScriptStackTrace : at <ScriptBlock>, <No file>: line 1Environment data
PowerShell Core 7.4.0-preview.2Visuals
No response
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Issue-BugIssue has been identified as a bug in the productIssue has been identified as a bug in the productKeepOpenThe bot will ignore these and not auto-closeThe bot will ignore these and not auto-closeUp-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Engine-ParameterBinder
Activity
iSazonov commentedon Mar 30, 2023
Is the issue in 7.3?
[-]Add-Type unexpectedly converts a `default(IntPtr)` default parameter value into `System.Int32`[/-][+]PowerShell fails to call .NET method that that has an optional IntPtr parameter whose default value is Int32 0 [/+]mklement0 commentedon Mar 30, 2023
Ilya (@iSazonov), the issue was framed incorrectly - please see the updated title and initial post.
It is a problem with method invocation, irrespective of whether the code was compiled with
Add-Typeor via a .NET SDK project.The problem also exists in 7.3.3, but not in 7.2.10, and also not in Windows PowerShell.
SeeminglyScience commentedon Oct 16, 2023
The Engine WG discussed this and agree it's a bug that should be fixed.
microsoft-github-policy-service commentedon Apr 14, 2024
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.