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

Use CallerArgumentExpression for internal assert-like constructs #10812

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

rainersigwald
Copy link
Member

CallerArgumentExpression is a new (C# 10) language feature that allows us to simplify a bunch of our assert-like code to drop the redundant specifications of, like, "which argument is null?".

I strongly recommend reviewing commit-by-commit; the bulk of the changes are a regex replace on top of a framework that is fairly small.

But don't use it anywhere yet.
Includes some nullability adjustments to work with the new paradigm.
@rainersigwald rainersigwald self-assigned this Oct 16, 2024
Copy link
Member

@JanKrivanek JanKrivanek left a comment

Choose a reason for hiding this comment

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

Great refactoring! Great separation into the reviawable chunks! I love this!

I only have small nonblocking nitpicks

@@ -467,7 +468,7 @@ internal static void VerifyThrowArgument([DoesNotReturnIf(false)] bool condition
/// Throws an argument out of range exception.
/// </summary>
[DoesNotReturn]
internal static void ThrowArgumentOutOfRange(string parameterName)
internal static void ThrowArgumentOutOfRange(string? parameterName)
Copy link
Member

Choose a reason for hiding this comment

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

Why is this needed? - I'd expect this change to not cause weaker nullability guarantee

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah this is a bit unfortunate. To use [CallerMemberName] you have to provide a default value, and the one for string parameterName that makes the most sense IMO is null. But then there's a possibility that null flows through the other overload of ThrowArgumentOutOfRange to here.

I think I prefer this to swapping it for "" but if you feel strongly the other way I can swap it. Definitely not a strong preference on my side.

src/Framework/AssemblyUtilities.cs Show resolved Hide resolved
@rainersigwald
Copy link
Member Author

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

From

```
ErrorUtilities\.VerifyThrowArgumentNull\(([a-zA-Z0-9]+), nameof\(\1\)\);
```

to

```
ErrorUtilities.VerifyThrowArgumentNull($1);
```

(And then revert `src/Deprecated`.)
From

```
ErrorUtilities\.VerifyThrowArgumentLength\(([a-zA-Z0-9]+), nameof\(\1\)\);
```

to

```
ErrorUtilities.VerifyThrowArgumentLength($1);
```

And revert `src/Deprecated`.
From

```
ErrorUtilities\.VerifyThrowInternalNull\(([a-zA-Z0-9]+), nameof\(\1\)\);
```

to

```
ErrorUtilities.VerifyThrowInternalNull($1);
```

and revert `src/Deprecated`.
@rainersigwald
Copy link
Member Author

[mlibc++abi: terminating due to uncaught exception of type PAL_SEHException
Executing [/Users/runner/work/1/s/.dotnet/dotnet "/Users/runner/work/1/s/artifacts/bin/Microsoft.Build.CommandLine.UnitTests/Debug/net9.0/MSBuild.dll" /var/folders/z0/m_s6x1rn5799ttl3khp8n86r0000gn/T/aaev4ycc.fr4/MSBuildTemprunner/Temporary1e6a54b3e9f040aeb48b6b1aa0094d93/testProject.proj]

on macOS???

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

Successfully merging this pull request may close these issues.

3 participants