Skip to content

C# 8 nullable reference type support #54

Closed

Description

@jskeet

It would be great if PublicApiGenerator "understood" NullableAttribute, so could print out the API with suitable ? syntax.
Trickier: exposing whether an API is null-aware at all.

More info: https://codeblog.jonskeet.uk/2019/02/10/nullableattribute-and-c-8/
Sample code that's way hackier than PublicApiGenerator: https://github.com/nodatime/nodatime/tree/master/build/NullableApiReviewer

Activity

  1. danielmarbach commented on Feb 11, 2019

    @danielmarbach
    Member

    Technically PublicApiGenerator already understands it because it has the ability to print out attributes as well as the constructor values of the attributes. You are asking for is custom code that translates the existence of

    public void X([NullableAttribute(1)] string input) {}    
    public void Y([NullableAttribute(2)] string input) {}
    

    to

    public void X(string input) {}    
    public void Y(string? input) {}
    

    and obviously also all the other cases you mentioned in your post?

    I'm wondering what the value proposition of that mapping would be given that already today you get the API surface with all the necessary information. I think the value proposition would be that you might get better readability?

  2. jskeet commented on Feb 11, 2019

    @jskeet
    Author

    Yes, readability is the key here - particularly when it comes to generics. I would like users not to have to wonder what [NullableAttribute(new byte[] { 1, 2, 1 })] Dictionary<string, string> map means, for example.

    I have more feature requests to add when I'm on a laptop. I may be able to implement some myself, but I wanted to just get them out of my head first :)

  3. changed the title [-]Feature request: C# 8 nullable refernece type support[/-] [+]Feature request: C# 8 nullable reference type support[/+] on Feb 11, 2019
  4. danielmarbach commented on Jun 30, 2019

    @danielmarbach
    Member

    Not sure I have a feasible way of implementing this without considerably revamping how the code currently works. The problem is that the current code turns everything into a CodeDom and then writes that into a string writer. So in order to pretty print the types and things annotated with NullableAttribute I would need to do some intensive string replacing logic that covers all the special cases. If you have a good idea on how to achieve this without redesigning the whole ApiGenerator I'm all ears.

  5. jskeet commented on Jun 30, 2019

    @jskeet
    Author

    I'll see if I can find some time to clone the repo and experiment - but that may well not lead to anything concrete, unfortunately.

  6. sungam3r commented on Aug 12, 2019

    @sungam3r
    Member

    Generally good idea but it seems to me that it will be better to wait NET Core 3.0 final release to work with the final design of these attributes.

  7. jnm2 commented on Oct 14, 2019

    @jnm2
    Contributor

    I have a similar project (needing to be updated for NRTs) which does all the code generation from a domain model directly to TextWriter: https://github.com/jnm2/ApiContractGenerator/blob/v1.0.0/src/ApiContractGenerator/CSharpTextFormatter.cs

    Maybe some of that could be usable, since the approach will have to be similar?

  8. sungam3r commented on Oct 14, 2019

    @sungam3r
    Member

    I have conflicting feelings about this change. On the one hand, it seems that the proposal is really good. On the other hand, the current way of representing this information through attributes seems to be cleaner.

  9. jskeet commented on Oct 14, 2019

    @jskeet
    Author

    @sungam3r: I'm not sure in what way it's "cleaner" - it's certainly much harder to read in terms of the attribute form.

  10. jnm2 commented on Oct 14, 2019

    @jnm2
    Contributor

    The compression scheme for the byte array passed to NullableAttribute for things with nested nullability is not something I'm capable of mentally unwinding, either.

  11. sungam3r commented on Oct 14, 2019

    @sungam3r
    Member

    @jskeet This means that we will have to do all the dirty work of reversing the conversion of type information from attributes — decompression, move bits around, matching, etc. All to solve the inverse problem of the compiler.

  12. jskeet commented on Oct 14, 2019

    @jskeet
    Author

    @sungam3r: Yes - but code can do that much more reliably than humans can.

  13. jnm2 commented on Oct 14, 2019

    @jnm2
    Contributor

    Part of the purpose of the text file is to be human-reviewable. It's hard to do that effectively if you don't know what you're looking at.

  14. sungam3r commented on Oct 14, 2019

    @sungam3r
    Member

    It's all clear. We can say that these attributes are special, although they are only attributes. On the other hand, someone may want to ignore these attributes. Now it is possible to do since code does not interpret them in a special way.

  15. jnm2 commented on Oct 14, 2019

    @jnm2
    Contributor

    I don't think anyone will want to ignore them any more than they'd want to ignore [TupleElementNames(...)] etc (which should also be decoded and written using proper C# syntax). It's API stuff that you are shipping to the world; you should not be able to opt out of seeing what you are doing.

  16. 59 remaining items

  17. danielmarbach commented on Nov 1, 2019

    @danielmarbach
    Member
  18. jnm2 commented on Nov 1, 2019

    @jnm2
    Contributor

    I tried it with Shouldly again. No errors! System.Void? is shown where void used to be before:

    -public static void ShouldBeEquivalentTo(this object actual, object expected) { }
    +public static System.Void? ShouldBeEquivalentTo(this object? actual, object? expected) { }

    Sometimes void is still shown properly even when one of the parameters is nullable.

    One more problem. Invalid attribute syntax (? is not allowed):

    -[System.ObsoleteAttribute("Foo")]
    +[System.ObsoleteAttribute?("Foo")]

    Everything else looks perfect! Let me know if you need repro code.

  19. sungam3r commented on Nov 1, 2019

    @sungam3r
    Member

    I could not reproduce both situations. Tests are green.

  20. jnm2 commented on Nov 1, 2019

    @jnm2
    Contributor

    @sungam3r My guess is that both parameters will need to be object? rather than object. I'll work on repro code.

  21. jnm2 commented on Nov 1, 2019

    @jnm2
    Contributor

    I can repro against commit aacbc4b 2019-11-01 | fix recursion with this as the only source file:

    namespace PublicApiGeneratorTests.Examples
    {
        public static class VoidReturn
        {
            public static void ShouldBeEquivalentTo(this object? actual, object? expected) { }
        }
    }

    Output from ApiGenerator.GeneratePublicApi:

    namespace PublicApiGeneratorTests.Examples
    {
        public class static VoidReturn
        {
            public static System.Void? ShouldBeEquivalentTo(this object? actual, object? expected) { }
        }
    }

    Also against commit 677fd84 2019-11-02 | tests.

    More instances I found:

    public delegate System.Void? WaitCallback(object? state);
    public delegate System.Void? WaitOrTimerCallback(object? state, bool timedOut);
  22. jnm2 commented on Nov 1, 2019

    @jnm2
    Contributor

    Repro code for the attribute syntax against commit 677fd84 2019-11-02 | tests:

    using System;
    
    namespace PublicApiGeneratorTests.Examples
    {
        [Obsolete("Foo")]
        public class NullableCtor
        {
            [Obsolete("Bar")]
            public NullableCtor(string? nullableLabel) { }
    
            [Obsolete("Bar")]
            public NullableCtor(string? nullableLabel, string? nullableLabel2) { }
        }
    }

    Produces:

    namespace PublicApiGeneratorTests.Examples
    {
        [System.ObsoleteAttribute?("Foo")]
        public class NullableCtor
        {
            [System.ObsoleteAttribute?("Bar")]
            public NullableCtor(string? nullableLabel) { }
            [System.ObsoleteAttribute?("Bar")]
            public NullableCtor(string? nullableLabel, string? nullableLabel2) { }
        }
    }

    Here's another instance I ran across:

    [System.Runtime.CompilerServices.TypeForwardedFromAttribute?("System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
    public sealed class TreeSet<T> : System.Collections.Generic.SortedSet<T> { }
  23. jnm2 commented on Nov 2, 2019

    @jnm2
    Contributor

    I ran commit 677fd84 2019-11-02 | tests against the entirety of https://www.nuget.org/packages/Microsoft.NETCore.App.Ref/3.0.0 (151 assemblies, 22 annotated) and both searched for all nullability syntax and visually scanned the files. Nothing looks wrong to me besides what I've already reported. There was some good variety.

  24. sungam3r commented on Nov 2, 2019

    @sungam3r
    Member

    Fixed both.

  25. jnm2 commented on Nov 2, 2019

    @jnm2
    Contributor

    Tried c5db6cc on Shouldly and https://www.nuget.org/packages/Microsoft.NETCore.App.Ref/3.0.0, all the nullability changes look perfect that I can see!

    I noticed a new thing which is actually more of a problem than the encoded NullableAttribute appearing. The [NotNull], [MaybeNullWhen(...)], [DoesNotReturn] etc attributes are not showing up at all. This is a separate problem and I'll open a new issue once I know more.

  26. jnm2 commented on Nov 2, 2019

    @jnm2
    Contributor

    The issue is that internal attributes aren't shown. Anyone working with nullable reference types on target frameworks other than netcoreapp3.0+ or netstandard2.1+ will need #117.

  27. changed the title [-]Feature request: C# 8 nullable reference type support[/-] [+]C# 8 nullable reference type support[/+] on Nov 2, 2019
  28. added this to the 10.0.0 milestone on Nov 2, 2019
  29. sungam3r commented on Nov 2, 2019

    @sungam3r
    Member

    @jnm2 So this is not the end of the story :(

  30. jnm2 commented on Nov 2, 2019

    @jnm2
    Contributor

    No, but the implementation for #117 will not be related at all to the work in this thread and I think it will be dramatically easier. You already have logic to show or hide attributes, so we augment that by checking against a list of full attribute type names that must never be hidden.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions