Skip to content

Commit ab6d300

Browse files
committed
Skip generating properties when there is a name collision
1 parent 31f5c95 commit ab6d300

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.Execute.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ internal static class Execute
2929
/// </summary>
3030
/// <param name="fieldSymbol">The input <see cref="IFieldSymbol"/> instance to process.</param>
3131
/// <param name="diagnostics">The resulting diagnostics from the processing operation.</param>
32-
/// <returns>The resulting <see cref="PropertyInfo"/> instance for <paramref name="fieldSymbol"/>.</returns>
33-
public static PropertyInfo GetInfo(IFieldSymbol fieldSymbol, out ImmutableArray<Diagnostic> diagnostics)
32+
/// <returns>The resulting <see cref="PropertyInfo"/> instance for <paramref name="fieldSymbol"/>, if successful.</returns>
33+
public static PropertyInfo? TryGetInfo(IFieldSymbol fieldSymbol, out ImmutableArray<Diagnostic> diagnostics)
3434
{
3535
ImmutableArray<Diagnostic>.Builder builder = ImmutableArray.CreateBuilder<Diagnostic>();
3636

@@ -54,6 +54,13 @@ public static PropertyInfo GetInfo(IFieldSymbol fieldSymbol, out ImmutableArray<
5454
fieldSymbol,
5555
fieldSymbol.ContainingType,
5656
fieldSymbol.Name);
57+
58+
diagnostics = builder.ToImmutable();
59+
60+
// If the generated property would collide, skip generating it entirely. This makes sure that
61+
// users only get the helpful diagnostic about the collsiion, and not the normal compiler error
62+
// about a definition for "Property" already existing on the target type, which might be confusing.
63+
return null;
5764
}
5865

5966
ImmutableArray<string>.Builder propertyChangedNames = ImmutableArray.CreateBuilder<string>();

CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
4444
context.FilterWithLanguageVersion(ref fieldSymbolsWithAttribute, LanguageVersion.CSharp8, UnsupportedCSharpLanguageVersionError);
4545

4646
// Gather info for all annotated fields
47-
IncrementalValuesProvider<(HierarchyInfo Hierarchy, Result<PropertyInfo> Info)> propertyInfoWithErrors =
47+
IncrementalValuesProvider<(HierarchyInfo Hierarchy, Result<PropertyInfo?> Info)> propertyInfoWithErrors =
4848
fieldSymbolsWithAttribute
4949
.Select(static (item, _) =>
5050
{
5151
HierarchyInfo hierarchy = HierarchyInfo.From(item.ContainingType);
52-
PropertyInfo propertyInfo = Execute.GetInfo(item, out ImmutableArray<Diagnostic> diagnostics);
52+
PropertyInfo? propertyInfo = Execute.TryGetInfo(item, out ImmutableArray<Diagnostic> diagnostics);
5353

54-
return (hierarchy, new Result<PropertyInfo>(propertyInfo, diagnostics));
54+
return (hierarchy, new Result<PropertyInfo?>(propertyInfo, diagnostics));
5555
});
5656

5757
// Output the diagnostics
@@ -60,7 +60,8 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
6060
// Get the filtered sequence to enable caching
6161
IncrementalValuesProvider<(HierarchyInfo Hierarchy, PropertyInfo Info)> propertyInfo =
6262
propertyInfoWithErrors
63-
.Select(static (item, _) => (item.Hierarchy, item.Info.Value))
63+
.Select(static (item, _) => (item.Hierarchy, Info: item.Info.Value))
64+
.Where(static item => item.Info is not null)!
6465
.WithComparers(HierarchyInfo.Comparer.Default, PropertyInfo.Comparer.Default);
6566

6667
// Split and group by containing type

0 commit comments

Comments
 (0)