C
C#13mo 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
swagrid13mo 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
Thinker13mo ago
Hold on, where exactly are you getting the error? In the analyzer project or in the test project?
swagrid
swagrid13mo 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
Thinker13mo ago
Can you post the code for the tests? Sounds like you might not be adding the proper assembly references?
swagrid
swagrid13mo ago
That's what I'm suspecting as well, I had trouble with those in the past
swagrid
swagrid13mo ago
This file is used to generate the compilation
swagrid
swagrid13mo ago
Should be largely copied from a sample project
333fred
333fred13mo ago
Yeah, don't do that
swagrid
swagrid13mo ago
🙂
swagrid
swagrid13mo ago
since when does the testing framework exist?
333fred
333fred13mo ago
Quite a while
swagrid
swagrid13mo 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
333fred13mo ago
It's been around for 3 years at this point And in various incarnations before that
swagrid
swagrid13mo 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
Accord13mo 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.