Skip to content

Commit 471a2c6

Browse files
Improved diagnostics of error and warning locations
1 parent 5680667 commit 471a2c6

6 files changed

Lines changed: 27 additions & 14 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Pure.DI.Core;
2+
3+
enum AttributeKind
4+
{
5+
Ordinal,
6+
Type,
7+
Tag
8+
}

src/Pure.DI.Core/Core/Attributes.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public T GetAttribute<TMdAttribute, T>(
1313
SemanticModel semanticModel,
1414
in ImmutableArray<TMdAttribute> metadata,
1515
ISymbol member,
16-
bool isTag,
16+
AttributeKind kind,
1717
T defaultValue)
1818
where TMdAttribute : IMdAttribute
1919
{
@@ -44,7 +44,7 @@ public T GetAttribute<TMdAttribute, T>(
4444
if (attr.ApplicationSyntaxReference?.GetSyntax() is AttributeSyntax { ArgumentList: {} argumentList }
4545
&& attributeMetadata.ArgumentPosition < argumentList.Arguments.Count)
4646
{
47-
return semantic.GetConstantValue<T>(semanticModel, argumentList.Arguments[attributeMetadata.ArgumentPosition].Expression, isTag ? SmartTagKind.Tag : SmartTagKind.Unknown)
47+
return semantic.GetConstantValue<T>(semanticModel, argumentList.Arguments[attributeMetadata.ArgumentPosition].Expression, GetSmartTagKind(kind))
4848
?? defaultValue;
4949
}
5050

@@ -77,6 +77,7 @@ public T GetAttribute<TMdAttribute, T>(
7777
SemanticModel semanticModel,
7878
in ImmutableArray<TMdAttribute> metadata,
7979
in ImmutableArray<AttributeSyntax> attributes,
80+
AttributeKind kind,
8081
T defaultValue)
8182
where TMdAttribute : IMdAttribute
8283
{
@@ -100,7 +101,7 @@ public T GetAttribute<TMdAttribute, T>(
100101
}
101102

102103
var argSyntax = args[attributeMetadata.ArgumentPosition];
103-
var val = semantic.GetConstantValue<object>(semanticModel, argSyntax.Expression);
104+
var val = semantic.GetConstantValue<object>(semanticModel, argSyntax.Expression, GetSmartTagKind(kind));
104105
if (val is T value)
105106
{
106107
return value;
@@ -111,6 +112,9 @@ public T GetAttribute<TMdAttribute, T>(
111112
return defaultValue;
112113
}
113114

115+
private static SmartTagKind GetSmartTagKind(AttributeKind kind) =>
116+
kind == AttributeKind.Tag ? SmartTagKind.Tag : SmartTagKind.Unknown;
117+
114118
private IReadOnlyList<AttributeData> GetAttributes(ISymbol member, INamedTypeSymbol attributeType) =>
115119
member
116120
.GetAttributes()

src/Pure.DI.Core/Core/FactoryDependencyNodeBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public IEnumerable<DependencyNode> Build(DependencyNodeBuildContext ctx)
2222
var resolvers = new List<DpResolver>(factory.Resolvers.Length);
2323
foreach (var resolver in factory.Resolvers)
2424
{
25-
var tag = attributes.GetAttribute(resolver.SemanticModel, setup.TagAttributes, resolver.Attributes, default(object?)) ?? resolver.Tag?.Value;
25+
var tag = attributes.GetAttribute(resolver.SemanticModel, setup.TagAttributes, resolver.Attributes, AttributeKind.Tag, default(object?)) ?? resolver.Tag?.Value;
2626
var injection = new Injection(InjectionKind.FactoryInjection, resolver.ContractType.WithNullableAnnotation(NullableAnnotation.NotAnnotated), tag);
2727
resolvers.Add(new DpResolver(resolver, injection, CreateOverrides(resolver.Overrides)));
2828
}

src/Pure.DI.Core/Core/IAttributes.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ T GetAttribute<TMdAttribute, T>(
66
SemanticModel semanticModel,
77
in ImmutableArray<TMdAttribute> metadata,
88
ISymbol member,
9-
bool isTag,
9+
AttributeKind kind,
1010
T defaultValue)
1111
where TMdAttribute : IMdAttribute;
1212

1313
T GetAttribute<TMdAttribute, T>(
1414
SemanticModel semanticModel,
1515
in ImmutableArray<TMdAttribute> metadata,
1616
in ImmutableArray<AttributeSyntax> attributes,
17+
AttributeKind kind,
1718
T defaultValue)
1819
where TMdAttribute : IMdAttribute;
1920
}

src/Pure.DI.Core/Core/ImplementationDependencyNodeBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public IEnumerable<DependencyNode> Build(DependencyNodeBuildContext ctx)
5454
constructors.Add(
5555
new DpMethod(
5656
constructor,
57-
attributes.GetAttribute(setup.SemanticModel, setup.OrdinalAttributes, constructor, false, default(int?)),
57+
attributes.GetAttribute(setup.SemanticModel, setup.OrdinalAttributes, constructor, AttributeKind.Ordinal, default(int?)),
5858
instanceDpProvider.GetParameters(setup, constructor.Parameters, ctx.TypeConstructor),
5959
locationProvider));
6060
}

src/Pure.DI.Core/Core/InstanceDpProvider.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ public InstanceDp Get(
3737
case IMethodSymbol method:
3838
if (method.MethodKind == MethodKind.Ordinary)
3939
{
40-
var ordinal = attributes.GetAttribute(setup.SemanticModel, setupAttributes, member, false, default(int?))
40+
var ordinal = attributes.GetAttribute(setup.SemanticModel, setupAttributes, member, AttributeKind.Ordinal, default(int?))
4141
?? method.Parameters
42-
.Select(param => attributes.GetAttribute(setup.SemanticModel, setupAttributes, param, false, default(int?)))
42+
.Select(param => attributes.GetAttribute(setup.SemanticModel, setupAttributes, param, AttributeKind.Ordinal, default(int?)))
4343
.FirstOrDefault(i => i.HasValue);
4444

4545
if (ordinal.HasValue)
@@ -53,7 +53,7 @@ public InstanceDp Get(
5353
case IFieldSymbol field:
5454
if (field is { IsReadOnly: false, IsStatic: false, IsConst: false })
5555
{
56-
var ordinal = attributes.GetAttribute(setup.SemanticModel, setupAttributes, member, false, default(int?));
56+
var ordinal = attributes.GetAttribute(setup.SemanticModel, setupAttributes, member, AttributeKind.Ordinal, default(int?));
5757
if (field.IsRequired || ordinal.HasValue)
5858
{
5959
var type = field.Type.WithNullableAnnotation(NullableAnnotation.NotAnnotated);
@@ -63,7 +63,7 @@ public InstanceDp Get(
6363
ordinal,
6464
new Injection(
6565
InjectionKind.Field,
66-
attributes.GetAttribute(setup.SemanticModel, setup.TypeAttributes, field, false, typeConstructor.Construct(setup, type)),
66+
attributes.GetAttribute(setup.SemanticModel, setup.TypeAttributes, field, AttributeKind.Type, typeConstructor.Construct(setup, type)),
6767
GetTagAttribute(setup, field))));
6868
}
6969
}
@@ -73,7 +73,7 @@ public InstanceDp Get(
7373
case IPropertySymbol property:
7474
if (property is { IsReadOnly: false, IsStatic: false, IsIndexer: false })
7575
{
76-
var ordinal = attributes.GetAttribute(setup.SemanticModel, setupAttributes, member, false, default(int?));
76+
var ordinal = attributes.GetAttribute(setup.SemanticModel, setupAttributes, member, AttributeKind.Ordinal, default(int?));
7777
if (ordinal.HasValue || property.IsRequired)
7878
{
7979
var type = property.Type.WithNullableAnnotation(NullableAnnotation.NotAnnotated);
@@ -83,7 +83,7 @@ public InstanceDp Get(
8383
ordinal,
8484
new Injection(
8585
InjectionKind.Property,
86-
attributes.GetAttribute(setup.SemanticModel, setup.TypeAttributes, property, false, typeConstructor.Construct(setup, type)),
86+
attributes.GetAttribute(setup.SemanticModel, setup.TypeAttributes, property, AttributeKind.Type, typeConstructor.Construct(setup, type)),
8787
GetTagAttribute(setup, property))));
8888
}
8989
}
@@ -117,7 +117,7 @@ public ImmutableArray<DpParameter> GetParameters(
117117
parameter,
118118
new Injection(
119119
InjectionKind.Parameter,
120-
attributes.GetAttribute(setup.SemanticModel, setup.TypeAttributes, parameter, false, typeConstructor.Construct(setup, type)),
120+
attributes.GetAttribute(setup.SemanticModel, setup.TypeAttributes, parameter, AttributeKind.Type, typeConstructor.Construct(setup, type)),
121121
GetTagAttribute(setup, parameter))));
122122
}
123123

@@ -145,7 +145,7 @@ private static IEnumerable<ISymbol> GetMembers(ITypeSymbol type)
145145
private object? GetTagAttribute(
146146
MdSetup setup,
147147
ISymbol member) =>
148-
attributes.GetAttribute(setup.SemanticModel, setup.TagAttributes, member, true, default(object?))
148+
attributes.GetAttribute(setup.SemanticModel, setup.TagAttributes, member, AttributeKind.Tag, default(object?))
149149
?? TryCreateTagOnSite(setup, member);
150150

151151
private object? TryCreateTagOnSite(

0 commit comments

Comments
 (0)