Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Correctly show the '}', ']' within the exception message in Utf8JsonWriter #36229

Merged
merged 2 commits into from
Mar 23, 2019

Conversation

ahsonkhan
Copy link
Member

Before:
'0' is invalid without a matching open.

Now:
']' is invalid without a matching open.

Was previously passing the token value as currentDepth so the default byte value (0) was being displayed. Using named parameters fixes that.

@stephentoub
Copy link
Member

Using named parameters fixes that.

This seems like a pretty easy mistake to make, given all the optional parameters. Have you considered just not making them optional?

@ahsonkhan
Copy link
Member Author

This seems like a pretty easy mistake to make, given all the optional parameters. Have you considered just not making them optional?

At all the call sites, I ThrowInvalidOperationException, I am passing one of the optional arguments, all of which get passed to GetResourceString.

I could have three overloads instead.

public static void ThrowInvalidOperationException(ExceptionResource resource, int currentDepth)
{
    throw GetInvalidOperationException(resource, currentDepth, default, default);
}

public static void ThrowInvalidOperationException(ExceptionResource resource, byte token)
{
    throw GetInvalidOperationException(resource, default, token, default);
}

public static void ThrowInvalidOperationException(ExceptionResource resource, JsonTokenType tokenType)
{
    throw GetInvalidOperationException(resource, default, default, tokenType);
}

@stephentoub
Copy link
Member

stephentoub commented Mar 22, 2019

I am passing one of the optional arguments

Why not just pass all three? It can't be a perf argument (compared to the optional arguments), as the IL will be identical. (There would be an IL difference for the three methods, obviously... if that's the motivation and it actually matters, then sure, go for the three different methods.)

@ahsonkhan
Copy link
Member Author

ahsonkhan commented Mar 22, 2019

Why not just pass all three?

And pass in just default values for the ones I don't have in the context of the method (for instance, I don't always have token)? The other two are fields so I could always pass those around (_currentDepth, _tokenType).

It can't be a perf argument, as the IL will be identical.

I don't get what you mean here. Passing in more (unnecessary) arguments doesn't affect the IL?

@stephentoub
Copy link
Member

stephentoub commented Mar 22, 2019

I don't get what you mean here. Passing in more (unnecessary) arguments doesn't affect the IL?

If I have a method:

Foo(int a = 1, int b = 2) {}

and I have two call sites:

Foo();

and

Foo(1, 2);

the IL for both will be identical, both of them loading 1 and 2 and passing them into Foo. The optional arguments aren't actually optional at the IL level: the compiler burns those values in at the call site.

@ahsonkhan
Copy link
Member Author

The optional arguments aren't actually optional at the IL level: the compiler burns those values in at the call site.

Ah I see what you mean. That makes sense.

However, if you didn't have optional arguments.

Foo() {}
Foo(int a, int b) {}

Now the two call sites would be different, correct?

Foo()

and

Foo(1, 2)

@stephentoub
Copy link
Member

However, if you didn't have optional arguments. ... Now the two call sites would be different, correct?

Correct.

@ahsonkhan
Copy link
Member Author

ahsonkhan commented Mar 22, 2019

Looks like the disassembly and IL is affected though (creating stack space - push/pop). At least for netfx:
sharplab.io

// Code size 29 (0x1d)
vs 
// Code size 24 (0x18)

@stephentoub
Copy link
Member

Looks like the disassembly and IL is affected though

You're not doing the right comparison: the right comparison is passing in the default values.

@ahsonkhan
Copy link
Member Author

You're not doing the right comparison: the right comparison is passing in the default values.

Got it. That makes sense now :)

ThrowInvalidOperationException(_currentDepth, default, default);

sharplab.io

@stephentoub stephentoub merged commit d3b6640 into dotnet:master Mar 23, 2019
@ahsonkhan ahsonkhan deleted the ShowRightToken branch March 23, 2019 04:39
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
…riter (dotnet/corefx#36229)

* Correctly show the '{', '[' within the exception message in
Utf8JsonWriter

* Address PR feedback - don't make throwhelper parameters optional.


Commit migrated from dotnet/corefx@d3b6640
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants