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

[release/6.0] Fixes two logging source gen bugs - when using "in" or "ref" modifier / when dealing with constraints #64705

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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 @@ -78,7 +78,7 @@ namespace {lc.Namespace}
// loop until you find top level nested class
while (parent != null)
{
parentClasses.Add($"partial {parent.Keyword} {parent.Name} {parent.Constraints}");
parentClasses.Add($"partial {parent.Keyword} {parent.Name} ");
parent = parent.ParentClass;
}

Expand All @@ -92,7 +92,7 @@ namespace {lc.Namespace}
}

_builder.Append($@"
{nestedIndentation}partial {lc.Keyword} {lc.Name} {lc.Constraints}
{nestedIndentation}partial {lc.Keyword} {lc.Name}
{nestedIndentation}{{");

foreach (LoggerMethod lm in lc.Methods)
Expand Down Expand Up @@ -327,6 +327,10 @@ private void GenArguments(LoggerMethod lm)
_builder.Append(", ");
}

if (p.Qualifier != null)
{
_builder.Append($"{p.Qualifier} ");
}
_builder.Append($"{p.Type} {p.Name}");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,15 @@ public IReadOnlyList<LoggerClass> GetLogClasses(IEnumerable<ClassDeclarationSynt
break;
}

string? qualifier = null;
if (paramSymbol.RefKind == RefKind.In)
{
qualifier = "in";
}
else if (paramSymbol.RefKind == RefKind.Ref)
{
qualifier = "ref";
}
string typeName = paramTypeSymbol.ToDisplayString(
SymbolDisplayFormat.FullyQualifiedFormat.WithMiscellaneousOptions(
SymbolDisplayMiscellaneousOptions.IncludeNullableReferenceTypeModifier));
Expand All @@ -331,6 +340,7 @@ public IReadOnlyList<LoggerClass> GetLogClasses(IEnumerable<ClassDeclarationSynt
{
Name = paramName,
Type = typeName,
Qualifier = qualifier,
IsLogger = !foundLogger && IsBaseOrIdentity(paramTypeSymbol!, loggerSymbol),
IsException = !foundException && IsBaseOrIdentity(paramTypeSymbol!, exceptionSymbol),
IsLogLevel = !foundLogLevel && IsBaseOrIdentity(paramTypeSymbol!, logLevelSymbol),
Expand Down Expand Up @@ -476,7 +486,6 @@ potentialNamespaceParent is not NamespaceDeclarationSyntax
Keyword = classDec.Keyword.ValueText,
Namespace = nspace,
Name = classDec.Identifier.ToString() + classDec.TypeParameterList,
Constraints = classDec.ConstraintClauses.ToString(),
ParentClass = null,
};

Expand All @@ -495,7 +504,6 @@ bool IsAllowedKind(SyntaxKind kind) =>
Keyword = parentLoggerClass.Keyword.ValueText,
Namespace = nspace,
Name = parentLoggerClass.Identifier.ToString() + parentLoggerClass.TypeParameterList,
Constraints = parentLoggerClass.ConstraintClauses.ToString(),
ParentClass = null,
};

Expand Down Expand Up @@ -681,7 +689,6 @@ internal class LoggerClass
public string Keyword = string.Empty;
public string Namespace = string.Empty;
public string Name = string.Empty;
public string Constraints = string.Empty;
public LoggerClass? ParentClass;
}

Expand Down Expand Up @@ -712,6 +719,7 @@ internal class LoggerParameter
{
public string Name = string.Empty;
public string Type = string.Empty;
public string? Qualifier;
public bool IsLogger;
public bool IsException;
public bool IsLogLevel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ namespace Microsoft.Extensions.Logging.Generators.Tests.TestClasses.NestedNamesp
{
partial record NestedRecord
{
partial class NestedClassTestsExtensions<T1> where T1 : Class1
partial class NestedClassTestsExtensions<T1>
{
partial class NestedMiddleParentClass
{
partial class Nested<T2> where T2 : Class2
partial class Nested<T2>
{
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "%VERSION%")]
private static readonly global::System.Action<global::Microsoft.Extensions.Logging.ILogger, global::System.Exception?> __M9Callback =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@

using System;
using System.Collections.Generic;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Logging.Generators.Tests.TestClasses;
using Microsoft.Extensions.Logging.Generators.Tests.TestClasses.UsesConstraintInAnotherNamespace;
using Xunit;
using NamespaceForABC;
using ConstraintInAnotherNamespace;

namespace Microsoft.Extensions.Logging.Generators.Tests
{
Expand Down Expand Up @@ -410,6 +414,71 @@ public void SkipEnabledCheckTests()
Assert.Equal(1, logger.CallCount);
Assert.Equal("LoggerMethodWithTrueSkipEnabledCheck", logger.LastEventId.Name);
}
private struct MyStruct { }

[Fact]
public void ConstraintsTests()
{
var logger = new MockLogger();

var printer = new MessagePrinter<Message>();
logger.Reset();
printer.Print(logger, new Message() { Text = "Hello" });
Assert.Equal(LogLevel.Information, logger.LastLogLevel);
Assert.Null(logger.LastException);
Assert.Equal("The message is Hello.", logger.LastFormattedString);
Assert.Equal(1, logger.CallCount);

var printer2 = new MessagePrinterHasConstraintOnLogClassAndLogMethod<Message>();
logger.Reset();
printer2.Print(logger, new Message() { Text = "Hello" });
Assert.Equal(LogLevel.Information, logger.LastLogLevel);
Assert.Null(logger.LastException);
Assert.Equal("The message is `Hello`.", logger.LastFormattedString);
Assert.Equal(1, logger.CallCount);

logger.Reset();
ConstraintsTestExtensions<Object>.M0(logger, 12);
Assert.Equal(LogLevel.Debug, logger.LastLogLevel);
Assert.Null(logger.LastException);
Assert.Equal("M012", logger.LastFormattedString);
Assert.Equal(1, logger.CallCount);

logger.Reset();
ConstraintsTestExtensions1<MyStruct>.M0(logger, 12);
Assert.Equal(LogLevel.Debug, logger.LastLogLevel);
Assert.Null(logger.LastException);
Assert.Equal("M012", logger.LastFormattedString);
Assert.Equal(1, logger.CallCount);

logger.Reset();
ConstraintsTestExtensions2<MyStruct>.M0(logger, 12);
Assert.Equal(LogLevel.Debug, logger.LastLogLevel);
Assert.Null(logger.LastException);
Assert.Equal("M012", logger.LastFormattedString);
Assert.Equal(1, logger.CallCount);

logger.Reset();
ConstraintsTestExtensions3<MyStruct>.M0(logger, 12);
Assert.Equal(LogLevel.Debug, logger.LastLogLevel);
Assert.Null(logger.LastException);
Assert.Equal("M012", logger.LastFormattedString);
Assert.Equal(1, logger.CallCount);

logger.Reset();
ConstraintsTestExtensions4<Attribute>.M0(logger, 12);
Assert.Equal(LogLevel.Debug, logger.LastLogLevel);
Assert.Null(logger.LastException);
Assert.Equal("M012", logger.LastFormattedString);
Assert.Equal(1, logger.CallCount);

logger.Reset();
ConstraintsTestExtensions5<MyStruct>.M0(logger, 12);
Assert.Equal(LogLevel.Debug, logger.LastLogLevel);
Assert.Null(logger.LastException);
Assert.Equal("M012", logger.LastFormattedString);
Assert.Equal(1, logger.CallCount);
}

[Fact]
public void NestedClassTests()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,21 @@ partial class C
Assert.Equal(DiagnosticDescriptors.LoggingMethodIsGeneric.Id, diagnostics[0].Id);
}

[Theory]
[InlineData("ref")]
[InlineData("in")]
public async Task SupportsRefKindsInAndRef(string modifier)
{
IReadOnlyList<Diagnostic> diagnostics = await RunGenerator(@$"
partial class C
{{
[LoggerMessage(EventId = 0, Level = LogLevel.Debug, Message = ""Parameter {{P1}}"")]
static partial void M(ILogger logger, {modifier} int p1);
}}");

Assert.Empty(diagnostics);
}

[Fact]
public async Task Templates()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.Extensions.Logging.Generators.Tests.TestClasses
{
using ConstraintInAnotherNamespace;
namespace UsesConstraintInAnotherNamespace
{
public partial class MessagePrinter<T>
where T : Message
{
public void Print(ILogger logger, T message)
{
Log.Message(logger, message.Text);
}

internal static partial class Log
{
[LoggerMessage(EventId = 1, Level = LogLevel.Information, Message = "The message is {Text}.")]
internal static partial void Message(ILogger logger, string? text);
}
}

public partial class MessagePrinterHasConstraintOnLogClassAndLogMethod<T>
where T : Message
{
public void Print(ILogger logger, T message)
{
Log<Message>.Message(logger, message);
}

internal static partial class Log<U> where U : Message
{
[LoggerMessage(EventId = 1, Level = LogLevel.Information, Message = "The message is {Text}.")]
internal static partial void Message(ILogger logger, U text);
}
}
}

internal static partial class ConstraintsTestExtensions<T>
where T : class
{
[LoggerMessage(EventId = 0, Level = LogLevel.Debug, Message = "M0{p0}")]
public static partial void M0(ILogger logger, int p0);

public static void Foo(T dummy)
{
}
}

internal static partial class ConstraintsTestExtensions1<T>
where T : struct
{
[LoggerMessage(EventId = 0, Level = LogLevel.Debug, Message = "M0{p0}")]
public static partial void M0(ILogger logger, int p0);

public static void Foo(T dummy)
{
}
}

internal static partial class ConstraintsTestExtensions2<T>
where T : unmanaged
{
[LoggerMessage(EventId = 0, Level = LogLevel.Debug, Message = "M0{p0}")]
public static partial void M0(ILogger logger, int p0);

public static void Foo(T dummy)
{
}
}

internal static partial class ConstraintsTestExtensions3<T>
where T : new()
{
[LoggerMessage(EventId = 0, Level = LogLevel.Debug, Message = "M0{p0}")]
public static partial void M0(ILogger logger, int p0);

public static void Foo(T dummy)
{
}
}

internal static partial class ConstraintsTestExtensions4<T>
where T : System.Attribute
{
[LoggerMessage(EventId = 0, Level = LogLevel.Debug, Message = "M0{p0}")]
public static partial void M0(ILogger logger, int p0);

public static void Foo(T dummy)
{
}
}

internal static partial class ConstraintsTestExtensions5<T>
where T : notnull
{
[LoggerMessage(EventId = 0, Level = LogLevel.Debug, Message = "M0{p0}")]
public static partial void M0(ILogger logger, int p0);

public static void Foo(T dummy)
{
}
}
}

namespace ConstraintInAnotherNamespace
{
public class Message
{
public string? Text { get; set; }

public override string ToString()
{
return $"`{Text}`";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace Microsoft.Extensions.Logging.Generators.Tests.TestClasses
{
using NamespaceForABC;

internal static partial class NestedClassTestsExtensions<T> where T : ABC
{
internal static partial class NestedMiddleParentClass
Expand All @@ -26,7 +28,6 @@ internal static partial class NestedClass
}
}
}
public class ABC {}

public partial struct NestedStruct
{
Expand Down Expand Up @@ -61,3 +62,8 @@ internal static partial class Logger
}
}
}

namespace NamespaceForABC
{
public class ABC {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.Extensions.Logging.Generators.Tests.TestClasses
{
internal static partial class ParameterTestExtensions
{
internal struct S
{
public override string ToString() => "Hello from S";
}

[LoggerMessage(EventId = 0, Level = LogLevel.Information, Message = "UseInParameter {s}")]
internal static partial void UseInParameter(ILogger logger, in S s);

[LoggerMessage(EventId = 1, Level = LogLevel.Information, Message = "UseRefParameter {s}")]
internal static partial void UseRefParameter(ILogger logger, ref S s);
}
}