-
Notifications
You must be signed in to change notification settings - Fork 10
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
Static analysis attributes #21
Comments
I've made 1.0.36-pre prerelease that should work as expected. |
Mabye we can generate only System.Diagnostics.CodeAnalysis namespace attributes? |
I've made changes. Only specific attributes are bing forwarded now. Here is the list:
|
@beakona https://learn.microsoft.com/ru-ru/dotnet/csharp/language-reference/language-specification/attributes |
Yes, you are right. It is easier to skip interface ITest
{
int Count
{
[return: SomeForwardedAttribute]
get;
}
} then we will not generate short version: class Test : ITest
{
[return: SomeForwardedAttribute]
int Count => 1
} but rather skip to full version: class Test : ITest
{
int Count
{
[return: SomeForwardedAttribute]
get => 1
}
} |
Check net 6 - all it`s work. if we want translate internal attributes, we have compilation error CS0122 Today i use https://www.nuget.org/packages/PolySharp/ for netstandard2.0 code. I think, we can generate System.Diagnostics.CodeAnalysis attributes for .net 2.0 targeting, or use external nuget packet at default references. |
As I understood, you have following problems:
Nullability can be avoided by switching to newer language version. Add LangVersion in .csproj and reload that project <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>8.0</LangVersion>
</PropertyGroup> Luckily, code analysis will work based on class names as long as you have new environment (IDE, compiler..). You can reference existing library that have such classes (but then you depends on foreign code) and also you can write it yourself. Here is roslyn implementation, you can copy paste desired classes in your codebase. I've created new dummy solution with two projects. One is console app targeting .net 8.0 and other is class library targeting netstandard 2.0.
Inside class library I've added following public interface ITest
{
public bool TryParse(string s, [NotNullWhen(true)] out int? value);
}
public partial class Test
{
[BeaKona.AutoInterface(IncludeBaseInterfaces = true)]
public ITest TestElement { get; set; } //warning: not populated...
}
...
namespace System.Diagnostics.CodeAnalysis
{
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
internal sealed class NotNullWhenAttribute : Attribute
{
/// <summary>Initializes the attribute with the specified return value condition.</summary>
/// <param name="returnValue">
/// The return value condition. If the method returns this value, the associated parameter will not be null.
/// </param>
public NotNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;
/// <summary>Gets the return value condition.</summary>
public bool ReturnValue { get; }
}
} Inside console app I've added following static void Main(string[] args)
{
ITest test = new Test();
if (test.TryParse("x", out var value))
{
int val = value.Value;//case1
}
else
{
int val = value.Value;//case2
}
} I've also edited .csproj for class library and add lang version that supports nullable syntax It compiles and branch labeled with case2 will be marked as invalid because |
if you declare NotNullWhenAttribute in your library, and use AutoInterface in the same library, you don`t have problem. please, in your example, move Test from library to console app AutoInterface not generated internal attribute NotNullWhenAttribute from library, |
I've made following changes:
extern alias kok;
using kok::MyTestClassLibrary; after that, emited code looks like this: bool kok::MyTestClassLibrary.ITest.Parse(string s, [System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] out int? value) It is erroneous, because at the moment, we are emitting attributes contextless (ie. without passing through type conversion mechanism)... thats a bug on my side. To compiler it looks like we abandoned kok::System.Diagnostics.CodeAnalysis.NotNullWhenAttribute After fixing that bug and after emitting right type everything will be right, code analysis would continue to work as expected but only method 'signature' would be slightly different: pointing to kok:: target, rather than global:: I like this erroneous output more than pure one (that should be valid). |
thanks, https://github.com/beakona/AutoInterface/releases/tag/v1.0.39-pre work for me. |
@beakona please make release version. |
I've made some final changes prior to release.. |
I want reopen issue. Please, we want translate attributes for parameters partial record TecProgDbConnection([property: BeaKona.AutoInterface(typeof(IDbConnection), IncludeBaseInterfaces = true)] NpgsqlConnection inner): ITecProgDbConnection
{
public ValueTask DisposeAsync()
{
return inner.DisposeAsync();
}
} partial record TecProgDbConnection
{
...
string System.Data.IDbConnection.ConnectionString
{
get => (this.inner as System.Data.IDbConnection)!.ConnectionString;
set => (this.inner as System.Data.IDbConnection)!.ConnectionString = value;
}
...
} |
I've made changes... Now, attributes attached to I've discovered that string this[int a, [AllowNull] string b]
{
get;
[AllowNull]
set;
} then setter's signature would look like void (*this)(int a, [AllowNull] string b, [AllowNull] string value) |
Please, make new version for nuget package. |
We have a problem with AllowNullAttribute
For example, it`s work in npgsql See plz counterexample https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/attributes/nullable-analysis#preconditions-allownull-and-disallownull |
I've made changes and I don't have time to test it... Prerelease |
@beakona thanks. it`s work for me |
Nice generator.
i think, source generator must add static analysis attributes on generated code.
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/attributes/nullable-analysis
For example
generated file
The text was updated successfully, but these errors were encountered: