Runtime: InvalidProgramException with ReadOnlySpan<T> ToString()

Created on 27 Jun 2017  路  9Comments  路  Source: dotnet/runtime

Invoking the ToString() method of the Span type results in a System.InvalidProgramException: Common Language Runtime detected an invalid program.

I wouldn't expect the ToString method to work with Span, but I didn't expect this to be an invalid program.

string s = "Hello, World!";
ReadOnlySpan<char> span = s.AsSpan();
string s2 = span.ToString();  // exception: Unhandled Exception: System.InvalidProgramException: Common Language Runtime detected an invalid program.

See: https://github.com/christiannagel/SpanIssues for a test project

area-System.Memory up-for-grabs

Most helpful comment

The better error message should be thrown in the runtime. Doing that will handle all byref like types, not just Span.

I think changing this line https://github.com/dotnet/coreclr/blob/master/src/vm/jitinterface.cpp#L6466 to be COMPlusThrow(kInvalidOperationException,W("InvalidOperation_TypeCannotBeBoxed")); (ie fold the two cases to throw same error) should do the trick.

All 9 comments

Span does not overwrite ToString. Calling inherited ToString from object results into boxing.

Maybe this should be flagged as an error in the compiler. It affects all byref-like struct, not just Span.

@VSadov ?

Should we override it?

Overriding would take care of it for Span, but not for all other byreflike structs (e.g. structs that embed Span).

GetHashCode is already overridden with Span and throws a NotSupportedException.
Wouldn't it be useful to have the same approach with GetHashCode and ToString? The overridden GetHashCode also doesn't take care of all other byreflike structs.

I found good information about this issue in the C# proposals:
https://github.com/dotnet/csharplang/blob/master/proposals/span-safety.md

Hi. I am interested to work on this. Essentially is this the ask,

  1. Throw NotSupportedException for ReadOnlySpan.ToString on the same lines as GetHashCode?
  2. Like wise handle these two methods for other 'byref' like structs. (Which all structs ...?)

And I guess new test cases would have to be added or current ones fixed.

Appreciate inputs. Thanks!

Essentially is this the ask

Yes.

For this issue, in particular, I would consider step 2 as optional but nice to have :)

Thanks for taking this issue. Let me know if you need any help.

The better error message should be thrown in the runtime. Doing that will handle all byref like types, not just Span.

I think changing this line https://github.com/dotnet/coreclr/blob/master/src/vm/jitinterface.cpp#L6466 to be COMPlusThrow(kInvalidOperationException,W("InvalidOperation_TypeCannotBeBoxed")); (ie fold the two cases to throw same error) should do the trick.

@jkotas thanks for inputs. So I guess, instead of here, I should fix the issue dotnet/coreclr#15458...?

Yes, I agree. We can close this issue and just use the CoreCLR issue to track this problem.

Was this page helpful?
0 / 5 - 0 ratings