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

Dynamic variables fail to interpolate when using a custom InterpolatedStringHandler #72606

Closed
budgetdevv opened this issue Mar 19, 2024 · 3 comments · Fixed by #72674
Closed
Assignees
Milestone

Comments

@budgetdevv
Copy link

Version Used:
.NET 8

Steps to Reproduce:

  1. Run Sharplab Repro

Diagnostic Id:

If this is a report about a bug in an analyzer, please include the diagnostic if possible (e.g. "IDE0030").

Expected Behavior:

It should compile.

Actual Behavior:

System.TypeLoadException: GenericArguments[1], 'CustomInterpolationHandler', on '<>A{00000008}`3[T1,T2,T3]' violates the constraint of type parameter 'T2'.
   at Program.<Main>$(String[] args)
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Mar 19, 2024
@jaredpar jaredpar added Bug Feature - Interpolated String Improvements Interpolated string improvements and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Mar 21, 2024
@jaredpar
Copy link
Member

@333fred exception happening because we're passing CustomInterpolationHandler which is a ref struct as a generic argument when generating the dynamic call sites.

Can this even work? Can't really see a way of mixing these two items here given the types need to be plumbed through generics.

@333fred
Copy link
Member

333fred commented Mar 21, 2024

Can this even work? Can't really see a way of mixing these two items here given the types need to be plumbed through generics.

No, I don't think it could. We could consider a compile-time error here.

@333fred
Copy link
Member

333fred commented Mar 21, 2024

One thing to note here is that this is not specific to interpolated strings. If I adjust the code sample to manually call handler.AppendFormatted((dynamic)"Hello world!");, that will also fail at runtime, rather than at compile time, with the same error. It seems like we should possibly introduce a general rule that says that, when we attempt to bind a dynamic call, if the receiver is a ref struct, fail.
https://sharplab.io/#v2:EYLgtghglgdgNAFxAJwK4wD4AEBMBGAWACgsAGAAizwDoAldBKMAU2oGEB7MABygBtmyAMqCAblADGzAM4BuYmUo0AKswAeCeUWIATAJ4wIYSeTXkAvOQBEAC2Z8+HcgHcOyPjoCEVrcVERkchsIGB0BQMsYZmdyNlRpBC4ASRgEQW4OPghGDhgACRCwwQAKUjhyPABKLWDQ8OoAQW5uZlCAMTdIBDSdYrVq4mIAbRS05AysnqEEZFgAcwK6wQBdBQBmcmRmADNyBLQJBFj4xLBR9MzsqFzFouRiAG9ichfybln/NPJp2Zg5gCFUPwdIJvjN5oDgYItK9YXDnq8sBs4glkqkLpNrvlCuFirAjnwoGMIHwADKtOYIGzlfHkbadbI9TgMSoIl5PIhw14/CFAjygyLRMG/AF8kHIYoDTmvAC+bK5r3lSMoABZyE0WqFSUTBCTilQKGkNKzpez5bCeX9IfzkI1mq1ekaEFLYXLTQqXkqNlg1RqHR1kF0egAeZQAPmKynIOrAJthHI9ltFUNtftCeLSsZhssG7uVHFEglmIKUFGUHCTkvlCYVWAA7MLeSnqOXKy6c0QZUA===

@jaredpar jaredpar added this to the 17.10 milestone Mar 22, 2024
@jaredpar jaredpar modified the milestones: 17.10, 17.11 Apr 1, 2024
333fred added a commit that referenced this issue Apr 5, 2024
Fixes #72606. Note that the behavior in the bug has slightly changed from the sharplab build used in the repro due to #71421.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants