Skip to content

Commit

Permalink
Make sure we emit diagnostics for "unsupported" marshalling for downl…
Browse files Browse the repository at this point in the history
…evel platforms (#71342)
  • Loading branch information
jkoritzinsky authored Jun 27, 2022
1 parent 601ef26 commit d99caf1
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 32 deletions.
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 },
});
}
}

0 comments on commit d99caf1

Please sign in to comment.