C
C#2y ago
swagrid

❔ Roslyn analyzer test project can't compile `where T : unmanaged` type constraint

I've made a roslyn analyzer for some marshaling stuff. In my test project I try to analyze this source code:
struct FooTest<T> where T : unmanaged
{
T good;
}
struct FooTest<T> where T : unmanaged
{
T good;
}
but when I try to compile this code I get:
error CS0518: Predefined type 'System.Runtime.CompilerServices.IsUnmanagedAttribute' is not defined or imported
Why the error? I know there are similar issues with IsExternalInit, but my test project runs on .net 6.0 and the test code above is compiled with the latest C# version. AFAIK, the unmanaged constrain came with C# 7.3 / .net core 2.0.
16 Replies
swagrid
swagridOP2y ago
I've worked around it by injecting my own flag type into the compilation:
using System;
using Microsoft.CodeAnalysis;
using System.Runtime.CompilerServices;

namespace Microsoft.CodeAnalysis
{
[CompilerGenerated]
[Embedded]
internal sealed class EmbeddedAttribute : Attribute
{
}
}

namespace System.Runtime.CompilerServices
{
[CompilerGenerated]
[Embedded]
internal sealed class IsUnmanagedAttribute : Attribute
{
}
}
using System;
using Microsoft.CodeAnalysis;
using System.Runtime.CompilerServices;

namespace Microsoft.CodeAnalysis
{
[CompilerGenerated]
[Embedded]
internal sealed class EmbeddedAttribute : Attribute
{
}
}

namespace System.Runtime.CompilerServices
{
[CompilerGenerated]
[Embedded]
internal sealed class IsUnmanagedAttribute : Attribute
{
}
}
Thinker
Thinker2y ago
Hold on, where exactly are you getting the error? In the analyzer project or in the test project?
swagrid
swagridOP2y ago
the test project or rather, during execution of the tests each test is building some source code that is then analyzed by the analyzer and when building that source code during test execution, that build fails
Thinker
Thinker2y ago
Can you post the code for the tests? Sounds like you might not be adding the proper assembly references?
swagrid
swagridOP2y ago
That's what I'm suspecting as well, I had trouble with those in the past
swagrid
swagridOP2y ago
This file is used to generate the compilation
swagrid
swagridOP2y ago
Should be largely copied from a sample project
333fred
333fred2y ago
Yeah, don't do that
swagrid
swagridOP2y ago
🙂
swagrid
swagridOP2y ago
since when does the testing framework exist?
333fred
333fred2y ago
Quite a while
swagrid
swagridOP2y ago
Hmm, I've started using the analyzers basically when they were first introduced, and copy & pasted the testing for new analyzers from then probably shouldn't have done that
333fred
333fred2y ago
It's been around for 3 years at this point And in various incarnations before that
swagrid
swagridOP2y ago
🤷 maybe I've already missed it back then. Anyways thanks for the help This is sooo much better. Now I can delete this cursed helper.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server