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] Warn on visibility of interface and StringMarshallingCustomType #87065

Merged
merged 23 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
6cbc44c
wip - build crashes on refs
jtschuster May 26, 2023
5cb008f
wip
jtschuster May 26, 2023
355fe17
wip
jtschuster May 26, 2023
cab2abf
wip
jtschuster May 26, 2023
7319660
Use Location and revert csprojs
jtschuster May 26, 2023
094a593
Re-add the old Diagnostic APIs to Microsoft.Interop.SourceGeneration
jtschuster May 30, 2023
68dac3b
Don't qualify Location
jtschuster May 31, 2023
c3041a2
wip
jtschuster May 31, 2023
97cdba2
Merge branch 'DiagnosticTemplate' into WarnOnStringMarshallingVisibility
jtschuster May 31, 2023
3d005bf
wip
jtschuster Jun 1, 2023
21698f5
Warn if StringMarshallingCustomType or Com interface is less than int…
jtschuster Jun 1, 2023
cbcbae8
Merge branch 'main' of https://github.com/dotnet/runtime into WarnOnS…
jtschuster Jun 2, 2023
0bd13f6
Add comment, fix typo
jtschuster Jun 8, 2023
9d57731
Merge branch 'main' of https://github.com/dotnet/runtime into WarnOnS…
jtschuster Jun 9, 2023
131f353
Merge branch 'main' of https://github.com/dotnet/runtime into WarnOnS…
jtschuster Jun 12, 2023
b16c34f
Merge branch 'main' of https://github.com/dotnet/runtime into WarnOnS…
jtschuster Jun 12, 2023
6fe867e
Use shared IsAccessibleFromGeneratedCode method
jtschuster Jun 12, 2023
7ccc1ec
Merge branch 'main' of https://github.com/dotnet/runtime into WarnOnS…
jtschuster Jun 13, 2023
3b7c3ad
Report warning on identifier and add expected diagnostic arguments
jtschuster Jun 13, 2023
5072766
Merge branch 'main' of https://github.com/dotnet/runtime into WarnOnS…
jtschuster Jun 13, 2023
bbfde84
Cleanup after merge
jtschuster Jun 13, 2023
913e8e9
Revert solution to main
jtschuster Jun 13, 2023
9724971
Revert solution to upstream/main
jtschuster Jun 13, 2023
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
Prev Previous commit
Next Next commit
wip
  • Loading branch information
jtschuster committed Jun 1, 2023
commit 3d005bf4e29084fa7d64f611942c9d8da77e4d97
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ DiagnosticsOr<ComInterfaceContext> AddContext(ComInterfaceInfo iface)
nameToContextCache[iface.ThisInterfaceKey] = diagnostic;
return diagnostic;
}
DiagnosticsOr<ComInterfaceContext> baseContext = baseCachedValue ?? baseReturnedValue;
DiagnosticOr<ComInterfaceContext> baseContext = baseCachedValue ?? baseReturnedValue;
Debug.Assert(baseContext.HasValue);
var ctx = DiagnosticsOr<ComInterfaceContext>.From(new ComInterfaceContext(iface, baseContext.Value));
var ctx = DiagnosticOr<ComInterfaceContext>.From(new ComInterfaceContext(iface, baseContext.Value));
nameToContextCache[iface.ThisInterfaceKey] = ctx;
return ctx;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ namespace Microsoft.Interop
/// </summary>
public abstract record DiagnosticOr<T>
{
public abstract bool IsValue { get; }
public abstract bool HasValue { get; }

public bool IsDiagnostic => !IsValue;
public abstract bool HasDiagnostic { get; }

/// <summary>
/// Throws <see cref="InvalidOperationException"/> if IsValue is false
Expand All @@ -32,33 +32,67 @@ public abstract record DiagnosticOr<T>
/// <summary>
/// Throws <see cref="InvalidOperationException"/> if IsDiagnostic is false
/// </summary>
public abstract DiagnosticInfo Diagnostic { get; }
public abstract ImmutableArray<DiagnosticInfo> Diagnostics { get; }

private sealed record Diag : DiagnosticOr<T>
{
private readonly DiagnosticInfo _diagnostic;
internal Diag(DiagnosticInfo diagnostic) => _diagnostic = diagnostic;
public override bool IsValue => false;
private readonly SequenceEqualImmutableArray<DiagnosticInfo> _diagnostics;
internal Diag(ImmutableArray<DiagnosticInfo> diagnostics) => _diagnostics = diagnostics.ToSequenceEqual();
public override bool HasValue => false;
public override bool HasDiagnostic => true;
public override T Value => throw new InvalidOperationException();
public override DiagnosticInfo Diagnostic => _diagnostic;
public override ImmutableArray<DiagnosticInfo> Diagnostics => _diagnostics.Array;
}

private sealed record Val : DiagnosticOr<T>
{
private readonly T _value;
internal Val(T value) => _value = value;
public override bool IsValue => true;
public override bool HasValue => true;
public override bool HasDiagnostic => false;
public override T Value => _value;
public override DiagnosticInfo Diagnostic => throw new InvalidOperationException();
public override ImmutableArray<DiagnosticInfo> Diagnostics => throw new InvalidOperationException();
}

private sealed record ValueAndDiagnostic : DiagnosticOr<T>
{
private readonly T _value;
private readonly SequenceEqualImmutableArray<DiagnosticInfo> _diagnostics;
internal ValueAndDiagnostic(T value, ImmutableArray<DiagnosticInfo> diagnostics) => (_value, _diagnostics) = (value, diagnostics.ToSequenceEqual());
public override bool HasValue => true;
public override bool HasDiagnostic => false;
public override T Value => _value;
public override ImmutableArray<DiagnosticInfo> Diagnostics => _diagnostics.Array;
}

/// <summary>
/// Adds a diagnostic to the <see cref="DiagnosticOr{T}.Diagnostics"/> property
/// </summary>
public DiagnosticOr<T> AddDiagnostic(DiagnosticInfo diagnostic) => this switch
{
Diag d => new Diag(d.Diagnostics.Add(diagnostic)),
Val v => new ValueAndDiagnostic(v.Value, ImmutableArray.Create(diagnostic)),
ValueAndDiagnostic vad => new ValueAndDiagnostic(vad.Value, vad.Diagnostics.Add(diagnostic)),
_ => throw new UnreachableException()
};

/// <summary>
/// Creates a new <see cref="DiagnosticOr{T}"/> with the <see cref="DiagnosticOr{T}.Value"/> set to <paramref name="value"/>
/// </summary>
public DiagnosticOr<T> WithValue(T value) => this switch
{
Diag d => new ValueAndDiagnostic(value, d.Diagnostics),
Val => new Val(value),
ValueAndDiagnostic vad => new ValueAndDiagnostic(value, vad.Diagnostics),
_ => throw new UnreachableException()
};

/// <summary>
/// Create a Diagnostic variant
/// </summary>
public static DiagnosticOr<T> From(DiagnosticInfo diagnostic)
public static DiagnosticOr<T> From(params DiagnosticInfo[] diagnostic)
jtschuster marked this conversation as resolved.
Show resolved Hide resolved
{
Debug.Assert(diagnostic is not null);
return new Diag(diagnostic);
return new Diag(ImmutableArray.Create(diagnostic));
}

/// <summary>
Expand All @@ -69,6 +103,16 @@ public static DiagnosticOr<T> From(T value)
Debug.Assert(value is not null);
return new Val(value);
}

/// <summary>
/// Create a ValueAndDiagnostic variant
/// </summary>
public static DiagnosticOr<T> From(T value, params DiagnosticInfo[] diagnostics)
{
Debug.Assert(value is not null);
Debug.Assert(diagnostics is not null);
return new ValueAndDiagnostic(value, ImmutableArray.Create(diagnostics));
}
}

public static class DiagnosticOrTHelperExtensions
Expand All @@ -78,8 +122,8 @@ public static class DiagnosticOrTHelperExtensions
/// </summary>
jtschuster marked this conversation as resolved.
Show resolved Hide resolved
public static (IncrementalValuesProvider<T>, IncrementalValuesProvider<DiagnosticInfo>) Split<T>(this IncrementalValuesProvider<DiagnosticOr<T>> provider)
{
var values = provider.Where(x => x.IsValue).Select(static (x, ct) => x.Value);
var diagnostics = provider.Where(x => x.IsDiagnostic).Select(static (x, ct) => x.Diagnostic);
var values = provider.Where(x => x.HasValue).Select(static (x, ct) => x.Value);
var diagnostics = provider.Where(x => x.HasDiagnostic).SelectMany(static (x, ct) => x.Diagnostics);
return (values, diagnostics);
}

Expand All @@ -88,8 +132,8 @@ public static (IncrementalValuesProvider<T>, IncrementalValuesProvider<Diagnosti
/// </summary>
public static (IncrementalValuesProvider<SequenceEqualImmutableArray<T>>, IncrementalValuesProvider<DiagnosticInfo>) SplitArrays<T>(this IncrementalValuesProvider<SequenceEqualImmutableArray<DiagnosticOr<T>>> provider)
{
var values = provider.Select((arr, ct) => arr.Where(x => x.IsValue).Select((x, ct) => x.Value).ToSequenceEqualImmutableArray());
var diagnostics = provider.SelectMany((arr, ct) => arr.Where(x => x.IsDiagnostic).Select((x, ct) => x.Diagnostic));
var values = provider.Select((arr, ct) => arr.Where(x => x.HasValue).Select((x, ct) => x.Value).ToSequenceEqualImmutableArray());
var diagnostics = provider.SelectMany((arr, ct) => arr.Where(x => x.HasDiagnostic).SelectMany((x, ct) => x.Diagnostics));
return (values, diagnostics);
}

Expand All @@ -98,8 +142,8 @@ public static (IncrementalValuesProvider<SequenceEqualImmutableArray<T>>, Increm
/// </summary>
public static (IncrementalValuesProvider<(T, T2)>, IncrementalValuesProvider<DiagnosticInfo>) Split<T, T2>(this IncrementalValuesProvider<(DiagnosticOr<T>, T2)> provider)
{
var values = provider.Where(x => x.Item1.IsValue).Select(static (x, ct) => (x.Item1.Value, x.Item2));
var diagnostics = provider.Where(x => !x.Item1.IsValue).Select(static (x, ct) => x.Item1.Diagnostic);
var values = provider.Where(x => x.Item1.HasValue).Select(static (x, ct) => (x.Item1.Value, x.Item2));
var diagnostics = provider.Where(x => x.Item1.HasDiagnostic).SelectMany(static (x, ct) => x.Item1.Diagnostics);
return (values, diagnostics);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public SequenceEqualImmutableArray(ImmutableArray<T> array)
{
}

public SequenceEqualImmutableArray<T> Add(T value)
{
return new SequenceEqualImmutableArray<T>(Array.Add(value));
}
AaronRobinsonMSFT marked this conversation as resolved.
Show resolved Hide resolved

public T this[int i] { get => Array[i]; }

public int Length => Array.Length;
Expand Down