Skip to content

New custom attribute required to support C++ inline namespaces #783

Closed

Description

@tgani-msft

Namespaces in .NET are not a first class concept in metadata but are implicit in type names. C++ inline namespaces are namespaces whose contents are implicitly available in the enclosing namespace. E.g.

namespace N {
  inline namespace I {
     ref class R {};
  }
}

The type R is simply name N.I.R in the metadata and upon import the C++ compiler does not know whether I is an inline namespace or not. To help the compiler import this correctly, an assembly-level custom attribute is emitted to the assembly for every inline namespace in the assembly. E.g., for the above we would emit

[CppInlineNamespace("N.I")];

To implement this, we need the attribute System.Runtime.CompilerServices.CppInlineNamespaceAttribute in System.Runtime.CompilerServices.VisualC.dll:

namespace System.Runtime.CompilerServices
{
    [AttributeUsage(AttributeTargets.Assembly, AllowMultiple=true)]
    public sealed class CppInlineNamespaceAttribute : Attribute
    {
        public CppInlineNamespaceAttribute(string dottedName);
    }
}

An explanation of how inline namespaces work is at https://foonathan.net/2018/11/inline-namespaces:

Activity

  1. terrajobst commented on Dec 12, 2019

    @terrajobst
    Contributor

    Couple of questions.

    Is this only emitted for assemblies? Can this also be emitted for modules?

    C++ inline namespaces are namespaces whose contents are implicitly available in the enclosing namespace.

    So when I have a using N; directive (or whatever the C++ equivalent is) I can see R even though it's a nested namespace I? What's the point of having a nested namespace then?

    Do you expect other compilers (such as C#) to honor this attribute?

  2. tgani-msft commented on Dec 12, 2019

    @tgani-msft
    ContributorAuthor

    In theory, yes, we could build a .netmodule though we don't officially support it.

    An explanation of how inline namespaces work is at https://foonathan.net/2018/11/inline-namespaces/

    We don't expect other compilers to do anything with this custom attribute, hence its location in VisualC.dll.

  3. terrajobst commented on Dec 12, 2019

    @terrajobst
    Contributor

    An explanation of how inline namespaces work is at https://foonathan.net/2018/11/inline-namespaces/

    Excellent, that helps!

    We're reviewing API requests once a week on Tuesdays. So expect an answer by EOD the upcoming Tuesday.

  4. added this to the 5.0 milestone on Dec 12, 2019
  5. added
    api-approvedAPI was approved in API review, it can be implemented
    and removed
    api-ready-for-reviewAPI is ready for review, it is NOT ready for implementation
    on Jul 16, 2020
  6. terrajobst commented on Jul 16, 2020

    @terrajobst
    Contributor

    Video

    • Looks good as proposed.
    • @jkotas pointed out that you may no longer need it. If you don't, please close this issue. If you do, consider it approved.
    namespace System.Runtime.CompilerServices
    {
        [AttributeUsage(AttributeTargets.Assembly, AllowMultiple=true)]
        public sealed class CppInlineNamespaceAttribute : Attribute
        {
            public CppInlineNamespaceAttribute(string dottedName);
        }
    }
  7. tgani commented on Jul 16, 2020

    @tgani

    We went with a different design based on @jkotas feedback. I'll close this issue.

  8. ghost locked as resolved and limited conversation to collaborators on Dec 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions