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

Make sure we emit diagnostics for "unsupported" marshalling for downlevel platforms #71342

Merged
merged 2 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,8 @@ private static IncrementalStubGenerationContext CalculateStubInformation(
generatorFactory = new UnsupportedMarshallingFactory();
}

generatorFactory = new NoMarshallingInfoErrorMarshallingFactory(generatorFactory);

// The presence of System.Runtime.CompilerServices.DisableRuntimeMarshallingAttribute is tied to TFM,
// so we use TFM in the generator factory key instead of the Compilation as the compilation changes on every keystroke.
IAssemblySymbol coreLibraryAssembly = env.Compilation.GetSpecialType(SpecialType.System_Object).ContainingAssembly;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Runtime.InteropServices;
using Microsoft.CodeAnalysis;

namespace Microsoft.Interop
{
public sealed class NoMarshallingInfoErrorMarshallingFactory : IMarshallingGeneratorFactory
{
private readonly IMarshallingGeneratorFactory _inner;

public IMarshallingGenerator Create(
TypePositionInfo info,
StubCodeContext context)
{
if (info.MarshallingAttributeInfo is NoMarshallingInfo && CustomTypeToErrorMessageMap.TryGetValue(info.ManagedType, out string errorMessage))
{
throw new MarshallingNotSupportedException(info, context)
{
NotSupportedDetails = errorMessage
};
}
return _inner.Create(info, context);
}

public NoMarshallingInfoErrorMarshallingFactory(IMarshallingGeneratorFactory inner)
: this(inner, DefaultTypeToErrorMessageMap)
{
}

private NoMarshallingInfoErrorMarshallingFactory(IMarshallingGeneratorFactory inner, ImmutableDictionary<ManagedTypeInfo, string> customTypeToErrorMessageMap)
{
_inner = inner;
CustomTypeToErrorMessageMap = customTypeToErrorMessageMap;
}

public ImmutableDictionary<ManagedTypeInfo, string> CustomTypeToErrorMessageMap { get; }

private static ImmutableDictionary<ManagedTypeInfo, string> DefaultTypeToErrorMessageMap { get; } =
ImmutableDictionary.CreateRange(new Dictionary<ManagedTypeInfo, string>
{
{ SpecialTypeInfo.String, SR.MarshallingStringOrCharAsUndefinedNotSupported },
{ SpecialTypeInfo.Boolean, SR.MarshallingBoolAsUndefinedNotSupported },
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,7 @@ namespace Microsoft.Interop
{
public sealed class UnsupportedMarshallingFactory : IMarshallingGeneratorFactory
{
public IMarshallingGenerator Create(
TypePositionInfo info,
StubCodeContext context)
{
if (info.MarshallingAttributeInfo is NoMarshallingInfo && CustomTypeToErrorMessageMap.TryGetValue(info.ManagedType, out string errorMessage))
{
throw new MarshallingNotSupportedException(info, context)
{
NotSupportedDetails = errorMessage
};
}
public IMarshallingGenerator Create(TypePositionInfo info, StubCodeContext context) =>
throw new MarshallingNotSupportedException(info, context);
}

public UnsupportedMarshallingFactory()
: this(DefaultTypeToErrorMessageMap)
{

}

private UnsupportedMarshallingFactory(ImmutableDictionary<ManagedTypeInfo, string> customTypeToErrorMessageMap)
{
CustomTypeToErrorMessageMap = customTypeToErrorMessageMap;
}

public ImmutableDictionary<ManagedTypeInfo, string> CustomTypeToErrorMessageMap { get; }

private static ImmutableDictionary<ManagedTypeInfo, string> DefaultTypeToErrorMessageMap { get; } =
ImmutableDictionary.CreateRange(new Dictionary<ManagedTypeInfo, string>
{
{ SpecialTypeInfo.String, SR.MarshallingStringOrCharAsUndefinedNotSupported },
{ SpecialTypeInfo.Boolean, SR.MarshallingBoolAsUndefinedNotSupported },
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ partial class Test
}

[Fact]
[OuterLoop("Uses the network for downlevel ref packs")]
//[OuterLoop("Uses the network for downlevel ref packs")]
jkoritzinsky marked this conversation as resolved.
Show resolved Hide resolved
public async Task StringMarshallingForwardingNotSupported_ReportsDiagnostic()
{
string source = @"
Expand Down