Improve stack trace consistency for generic types across runtimes #63945
+62
−3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR addresses 4 out of 6 stack trace formatting issues identified in #63927 by enhancing
StackTraceHelper
to provide consistent generic type parameter display across different .NET runtimes.Problem
As documented in #63927, StackTraceHelper tests fail on Mono due to differences in how generic type parameters are represented in stack traces compared to CoreCLR:
CoreCLR (expected):
Mono (current):
Solution
This PR implements three targeted improvements using standard .NET reflection APIs:
1. Generic Type Definition Resolution
Uses
Type.GetGenericTypeDefinition()
to ensure declaring types show parameter names instead of concrete types.2. Generic Method Definition Resolution
Uses
MethodInfo.GetGenericMethodDefinition()
to show generic parameter names in method signatures.3. Method Parameter Type Resolution
Uses methods from generic type definitions to ensure parameter types show generic names.
Tests Fixed
This PR resolves 4 out of 6 failing tests from #63927:
StackTraceHelper_PrettyPrintsStackTraceForGenericMethods
StackTraceHelper_PrettyPrintsStackTraceForMethodsWithGenericOutParameters
StackTraceHelper_PrettyPrintsStackTraceForMethodsOnGenericTypes
StackTraceHelper_PrettyPrintsStackTraceForMethodsWithGenericRefParameters
Remaining Issues
Two tests from #63927 are not addressed in this PR:
5.
GetFrames_DoesNotFailForDynamicallyGeneratedAssemblies
Issue: Lambda method naming inconsistency
"lambda_method34(Closure )"
"object.lambda_method34(Closure )"
(includes declaring type prefix)Potential approach: Detect dynamically generated methods and strip the declaring type prefix from method names when present.
6.
StackTraceHelper_ProducesReadableOutput
Issue: Async state machine resolution for generic methods
"MethodAsync<TValue>(TValue value)"
"<MethodAsync>d__14<TValue>.MoveNext()"
(state machine not resolved to original method)Potential approach: Enhance state machine resolution with name pattern fallback that parses state machine type names to find the original async method, with special handling for generic method overloads.
Question
Should the remaining two issues be addressed in this PR by adding the workaround approaches described above, or does this need to be fixed in the runtime?
cc: @giritrivedi