fsprojects / FSharp.TypeProviders.SDK

The SDK for creating F# type providers
https://fsprojects.github.io/FSharp.TypeProviders.SDK/
MIT License
298 stars 94 forks source link

GetUnionCases fails on an option of provided type #336

Open kerams opened 4 years ago

kerams commented 4 years ago

Description

The following snippet causes an error message box to appear in the VS instance that consumes the type provider.

let typ = ProvidedTypeDefinition ("Blah", Some typeof<obj>)
let optionTyp = ProvidedTypeBuilder.MakeGenericType (typedefof<_ option>, [ typ ])
let cases = FSharp.Reflection.FSharpType.GetUnionCases optionTyp

Expected behavior

Expecting the function call so succeed and return 2 cases.

Actual behavior

A design time error message with The operation 'GetNestedType' on item 'FSharpOption``1' should not be called on provided type, member or parameter of type 'ProviderImplementation.ProvidedTypes.TypeSymbol' and a stack trace.

Is there any workaround for this?

7sharp9 commented 2 years ago

I believe this is because GetUnionCases is expecting the providedtype typ to have a member called Tags present which is retrieved via reflection.

It's possible to get around it via some fakery if you really need to: https://github.com/jet/falanx/blob/fe3bfbce4a9ec40aecfd7db6704aaab91283c3a5/src/Falanx.Machinery/ProvidedTypesExtensions.fs#L16-L70