Skip to content
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

[ComInterfaceGenerator] Trailing trivia after GeneratedComInterfaceAttribute included in generated files #88798

Closed
lordmilko opened this issue Jul 12, 2023 · 1 comment · Fixed by #88856

Comments

@lordmilko
Copy link

Consider the following declaration of an interface designed to be multi-targeted against .NET 8 and earlier versions of .NET

    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    [Guid("7DAC8207-D3AE-4C75-9B67-92801A497D44")]
#if !GENERATED_MARSHALLING
    [ComImport]
#else
    [GeneratedComInterface]
#endif
    public partial interface IFoo
    {
        void Foo();
    }

Attempting to compile this results in the following error:

error CS1028: Unexpected preprocessor directive

In the generated source code, we can see the #ifdef directive has erroneously been carried over to two locations

// MyProject.IFoo.cs
namespace MyProject
{
    [System.Runtime.InteropServices.Marshalling.IUnknownDerivedAttribute<InterfaceInformation, InterfaceImplementation>]
#endif
    public partial interface IFoo
    {
    }
}

namespace MyProject
{
#endif
    public partial interface IFoo
    {
    }
}

If we flip our #if directive around to the following

#if GENERATED_MARSHALLING
    [GeneratedComInterface]
#else
    [ComImport]
#endif
    public partial interface IFoo

then the #else, [ComImport] and #endif lines are all copied over to the generated source file.

You can "work around" this by moving the interface header into the #if statement, as follows

#if !GENERATED_MARSHALLING
    [ComImport]
    public partial interface IFoo
#else
    [GeneratedComInterface]
    public partial interface IFoo
#endif

However I would present that users attempting to multi-target by applying an #if around which attribute to use is a reasonable scenario; I would imagine upon reading the GeneratedComInterfaceAttribute AttributeSyntax the source generator should just trim all trailing trivia

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Jul 12, 2023
@ghost
Copy link

ghost commented Jul 12, 2023

Tagging subscribers to this area: @dotnet/interop-contrib
See info in area-owners.md if you want to be subscribed.

Issue Details

Consider the following declaration of an interface designed to be multi-targeted against .NET 8 and earlier versions of .NET

    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    [Guid("7DAC8207-D3AE-4C75-9B67-92801A497D44")]
#if !GENERATED_MARSHALLING
    [ComImport]
#else
    [GeneratedComInterface]
#endif
    public partial interface IFoo
    {
        void Foo();
    }

Attempting to compile this results in the following error:

error CS1028: Unexpected preprocessor directive

In the generated source code, we can see the #ifdef directive has erroneously been carried over to two locations

// MyProject.IFoo.cs
namespace MyProject
{
    [System.Runtime.InteropServices.Marshalling.IUnknownDerivedAttribute<InterfaceInformation, InterfaceImplementation>]
#endif
    public partial interface IFoo
    {
    }
}

namespace MyProject
{
#endif
    public partial interface IFoo
    {
    }
}

If we flip our #if directive around to the following

#if GENERATED_MARSHALLING
    [GeneratedComInterface]
#else
    [ComImport]
#endif
    public partial interface IFoo

then the #else, [ComImport] and #endif lines are all copied over to the generated source file.

You can "work around" this by moving the interface header into the #if statement, as follows

#if !GENERATED_MARSHALLING
    [ComImport]
    public partial interface IFoo
#else
    [GeneratedComInterface]
    public partial interface IFoo
#endif

However I would present that users attempting to multi-target by applying an #if around which attribute to use is a reasonable scenario; I would imagine upon reading the GeneratedComInterfaceAttribute AttributeSyntax the source generator should just trim all trailing trivia

Author: lordmilko
Assignees: -
Labels:

area-System.Runtime.InteropServices, untriaged

Milestone: -

@AaronRobinsonMSFT AaronRobinsonMSFT removed the untriaged New issue has not been triaged by the area owner label Jul 13, 2023
@AaronRobinsonMSFT AaronRobinsonMSFT added this to the 8.0.0 milestone Jul 13, 2023
jkoritzinsky added a commit to jkoritzinsky/runtime that referenced this issue Jul 13, 2023
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Jul 13, 2023
jkoritzinsky added a commit that referenced this issue Jul 14, 2023
* Strip trivia from tokens.

Fixes #88798

* Move the trivia stripping into ContainingSyntax record constructor

* Make ContainingSyntax a regular struct with a primary constructor instead of a record struct.

* Fix #88867

* Suppress compiler diagnostics based on the linked issue.
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Jul 14, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Aug 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants