@@ -465,24 +465,54 @@ private async Task<string> InterceptInputWhenRefStructReturnNeedsToBeHandled(str
465465 if ( ( await scriptRunner . HasValueReturningStatement ( input , cancellationToken ) . ConfigureAwait ( false ) ) . TryGet ( out var result ) &&
466466 result . Type . IsRefLikeType )
467467 {
468+ var root = result . Expression . SyntaxTree . GetRoot ( cancellationToken ) ;
469+ var expressionToBeWrapped = result . Expression ;
470+ ExpressionSyntax wrappedExpression ;
468471 if ( result . Type is { Name : "Span" or "ReadOnlySpan" , ContainingNamespace . Name : "System" } )
469472 {
470- var root = result . Expression . SyntaxTree . GetRoot ( cancellationToken ) ;
471- var wrappedExpression =
472- SyntaxFactory . InvocationExpression (
473+ wrappedExpression = Wrap ( nameof ( __CSharpRepl_RuntimeHelper . HandleSpanOutput ) , result . Expression ) ;
474+ }
475+ else
476+ {
477+ var toStringMethod = result . Type
478+ . GetMembers ( nameof ( ToString ) )
479+ . OfType < IMethodSymbol > ( )
480+ . Where ( m => m . ReturnType . SpecialType == SpecialType . System_String && m . Parameters . Length == 0 && m . IsOverride )
481+ . FirstOrDefault ( ) ;
482+
483+ if ( toStringMethod != null )
484+ {
485+ expressionToBeWrapped = SyntaxFactory . InvocationExpression (
473486 SyntaxFactory . MemberAccessExpression (
474- SyntaxKind . SimpleMemberAccessExpression ,
475- SyntaxFactory . IdentifierName ( nameof ( __CSharpRepl_RuntimeHelper ) ) ,
476- SyntaxFactory . IdentifierName ( nameof ( __CSharpRepl_RuntimeHelper . HandleSpanOutput ) ) ) )
477- . WithArgumentList (
478- SyntaxFactory . ArgumentList (
479- SyntaxFactory . SingletonSeparatedList (
480- SyntaxFactory . Argument (
481- result . Expression ) ) ) ) ;
482- root = root . ReplaceNode ( result . Expression , wrappedExpression ) ;
483- return root . GetText ( ) . ToString ( ) ;
487+ SyntaxKind . SimpleMemberAccessExpression ,
488+ expressionToBeWrapped ,
489+ SyntaxFactory . IdentifierName ( nameof ( ToString ) ) ) ) ;
490+ }
491+ else
492+ {
493+ expressionToBeWrapped = SyntaxFactory . LiteralExpression (
494+ SyntaxKind . StringLiteralExpression ,
495+ SyntaxFactory . Literal ( $ "Cannot output a value of '{ result . Type . Name } ' because it's a ref-struct. It has to override ToString() to see its value.") ) ;
496+ }
497+ wrappedExpression = Wrap (
498+ nameof ( __CSharpRepl_RuntimeHelper . HandleRefStructOutput ) ,
499+ expressionToBeWrapped ) ;
484500 }
501+ root = root . ReplaceNode ( result . Expression , wrappedExpression ) ;
502+ return root . GetText ( ) . ToString ( ) ;
485503 }
486504 return input ;
505+
506+ static ExpressionSyntax Wrap ( string runtimeHelperMethod , ExpressionSyntax expressionToBeWrapped ) =>
507+ SyntaxFactory . InvocationExpression (
508+ SyntaxFactory . MemberAccessExpression (
509+ SyntaxKind . SimpleMemberAccessExpression ,
510+ SyntaxFactory . IdentifierName ( nameof ( __CSharpRepl_RuntimeHelper ) ) ,
511+ SyntaxFactory . IdentifierName ( runtimeHelperMethod ) ) )
512+ . WithArgumentList (
513+ SyntaxFactory . ArgumentList (
514+ SyntaxFactory . SingletonSeparatedList (
515+ SyntaxFactory . Argument (
516+ expressionToBeWrapped ) ) ) ) ;
487517 }
488518}
0 commit comments