Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions src/System.Management.Automation/engine/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1733,11 +1733,6 @@ internal ReadOnlyBag(HashSet<T> hashset)
/// </summary>
internal static class Requires
{
internal static void NotNull(object value, string paramName)
{
ArgumentNullException.ThrowIfNull(value, paramName);
}

internal static void NotNullOrEmpty(string value, string paramName)
{
if (string.IsNullOrEmpty(value))
Expand All @@ -1748,9 +1743,7 @@ internal static void NotNullOrEmpty(string value, string paramName)

internal static void NotNullOrEmpty(ICollection value, string paramName)
{
ArgumentNullException.ThrowIfNull(value, paramName);

if (value.Count == 0)
if (value is null || value.Count == 0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I pointed already in another PR, this generates worst code.
Also in .Net 8 Preview 1 we will get ArgumentOutOfRangeException.ThrowIfZero (and more) so that the code will be:

ArgumentNullException.ThrowIfNull(value, paramName);
ArgumentNullException.ThrowIfZero(value.Count);

Copy link
Member Author

@daxian-dbw daxian-dbw Feb 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The difference in generated code doesn't always matter, and you cannot use it as the single criteria. In this case, the original code is more readable.

As far as I can found in the dotnet/runtime repo, it's ArgumentOutOfRangeException.ThrowIfZero, not ArgumentNullException.ThrowIfZero. It throws a different exception.
Also, even in case of ArgumentOutOfRangeException.ThrowIfZero(value.count), I don't know what will be used as the parameter name, "value.count" I guess?

Copy link
Contributor

@CarloToso CarloToso Feb 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right it will throw a different exception, I don't know if it will be a problem (maybe we just have to change the tests).

From dotnet/runtime#69590
public static void ThrowIfZero<T>(T value, [CallerArgumentExpression("value")] string? paramName = null) it seems can set any string as parameter name

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original code is not correct. There are not too few places in our code where an exception is called that is not what it should be. This is not even a breaking change, as it is not a functional exception, and it is worth correcting it for the correct one. If someone even uses this behavior in their code, it is catastrophically bad code.

I don't know what will be used as the parameter name, "value.count" I guess?

Yes, as the attribute name (CallerArgumentExpression) says expression is used as "name". It is more informative and more correct. (Otherwise this API would never have been approved.)

So I think we should fix such protective (non-functional) exceptions to the correct ones in whole code base.

Copy link
Member Author

@daxian-dbw daxian-dbw Feb 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I think we should fix such protective (non-functional) exceptions to the correct ones in whole code base.

That will need to be discussed and approved by all maintainers. If you insist on fixing those exceptions, then please open an issue on it and mark it with Review - Maintainer.

ArgumentOutOfRangeException won't be the right exception either. If you insist to be accurate about the exception, then an ArgumentException with a customized error message should be the way to go.

In my personal view, changing those exceptions is not worth the time (discussion, making the change, and code review). But if all other maintainers agree to make the change, then I'm fine to replace the current code with a customized ArgumentException.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I already explained in #19151 (comment) why it's arguably incorrect. I'm not trying to persuade you, just to clarify my opinion.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to figure out what to demand from other contributors so we don't waste time later. So you'd better convince me. 😃
Right now I don't see that a ArgumentException message would be better than an ArgumentOutOfRangeException. (If I understand correctly you have no objection to the exception class type used but only to content of message.)
While I'm on vacation and have no opportunity to experiment. I'll try it a week later.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A custom message will be absolutely better than 'paramName' must be a non-zero value. (Parameter 'paramName'), which is what ArgumentOutOfRangeException.ThrowIfZero gives you.

In this particular case, the message should be something like 'paramName' must be a non-empty collection. (Parameter 'paramName')

you have no objection to the exception class type

I'm fine changing the type of exception to be thrown. That's a bucket 3 breaking change.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took a look at the history of this API. It was created by Stephan Toub and updated by him. It is hard to suspect him of negligence. I think this is intentional since the exact API was rejected as I mentioned above. So after a while this pattern may become a frequently used one.

To be clear, ClientWebSocketOptions.SetBuffer(Int32, Int32, ArraySegment<Byte>) is a public API introduced in .NET Framework 4.5. In any case, please could we should start discussion in a new issue.

Copy link
Contributor

@xtqqczze xtqqczze Feb 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took a look at the history of this API. It was created by Stephan Toub and updated by him. It is hard to suspect him of negligence. I think this is intentional since the exact API was rejected as I mentioned above. So after a while this pattern may become a frequently used one.

To be clear, ClientWebSocketOptions.SetBuffer(Int32, Int32, ArraySegment<Byte>) is a public API introduced in .NET Framework 4.5. In any case, please could we continue discussion in a new issue.

{
throw new ArgumentNullException(paramName);
}
Expand Down
7 changes: 4 additions & 3 deletions src/System.Management.Automation/engine/lang/scriptblock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1138,9 +1138,10 @@ public void Begin(bool expectInput, EngineIntrinsics contextToRedirectTo)
/// <param name="command">The command you're calling this from (i.e. instance of PSCmdlet or value of $PSCmdlet variable).</param>
public void Begin(InternalCommand command)
{
ArgumentNullException.ThrowIfNull(command);

ArgumentNullException.ThrowIfNull(command.MyInvocation, nameof(command));
if (command is null || command.MyInvocation is null)
{
throw new ArgumentNullException(nameof(command));
}

Begin(command.MyInvocation.ExpectingInput, command.commandRuntime);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,10 +530,11 @@ public object VisitIndexExpression(IndexExpressionAst indexExpressionAst)
// Get the value of the index and value and call the compiler
var index = indexExpressionAst.Index.Accept(this);
var target = indexExpressionAst.Target.Accept(this);

ArgumentNullException.ThrowIfNull(index, nameof(indexExpressionAst));

ArgumentNullException.ThrowIfNull(target, nameof(indexExpressionAst));
if (index is null || target is null)
{
throw new ArgumentNullException(nameof(indexExpressionAst));
}

return GetIndexedValueFromTarget(target, index);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7969,6 +7969,7 @@ internal static bool CreateJunction(string path, string target)
{
throw new ArgumentNullException(nameof(target));
}

using (SafeHandle handle = WinOpenReparsePoint(path, FileAccess.Write))
{
byte[] mountPointBytes = Encoding.Unicode.GetBytes(NonInterpretedPathPrefix + Path.GetFullPath(target));
Expand Down
4 changes: 1 addition & 3 deletions src/System.Management.Automation/utils/ParserException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,7 @@ public ParseException(string message,
/// <param name="errors">The collection of error messages.</param>
public ParseException(ParseError[] errors)
{
ArgumentNullException.ThrowIfNull(errors);

if (errors.Length == 0)
if (errors is null || errors.Length == 0)
{
throw new ArgumentNullException(nameof(errors));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ protected CounterSetRegistrarBase(
CounterSetInstType = counterSetInstType;
CounterSetName = counterSetName;

ArgumentNullException.ThrowIfNull(counterInfoArray);

if (counterInfoArray.Length == 0)
if (counterInfoArray is null || counterInfoArray.Length == 0)
{
throw new ArgumentNullException(nameof(counterInfoArray));
}
Expand Down