Skip to content

Commit 054202c

Browse files
Remove syntax annotations from the Razor compiler
Here's the thing: the Razor compiler doesn't need syntax annotations. Originally, annotations were added to Roslyn to support the IDE when making tree transformations. However, Razor tooling doesn't actually perform any tree transformations. So, tooling doesn't use annotations. The problem is that the compiler *does* use annotations to stuff data onto syntax nodes -- data that should have been part of the syntax model in the first place. Now that all compiler data has been moved into the syntax model, syntax annotations are pure overhead and can be removed.
1 parent 29deee8 commit 054202c

File tree

15 files changed

+217
-792
lines changed

15 files changed

+217
-792
lines changed

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Legacy/TagHelperBlockRewriter.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -638,8 +638,6 @@ public override SyntaxNode VisitCSharpImplicitExpression(CSharpImplicitExpressio
638638
var firstToken = firstChild.GetFirstToken();
639639
var newFirstToken = SyntaxFactory.Token(firstToken.Kind, node.Transition.Transition.Content + firstToken.Content);
640640

641-
firstToken.CopyAnnotationsTo(newFirstToken);
642-
643641
var newFirstChild = firstChild.ReplaceToken(firstToken, newFirstToken);
644642
builder.AddRange(rewrittenBody.Children.Replace(firstChild, newFirstChild));
645643

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Syntax/Generated/Syntax.xml.Internal.Generated.cs

Lines changed: 133 additions & 373 deletions
Large diffs are not rendered by default.

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Syntax/Generated/Syntax.xml.Syntax.Generated.cs

Lines changed: 40 additions & 80 deletions
Large diffs are not rendered by default.

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Syntax/GreenNode.cs

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Syntax;
1111
[DebuggerDisplay("{GetDebuggerDisplay(), nq}")]
1212
internal abstract partial class GreenNode
1313
{
14-
private static readonly RazorDiagnostic[] EmptyDiagnostics = [];
15-
private static readonly SyntaxAnnotation[] EmptyAnnotations = [];
16-
private static readonly ConditionalWeakTable<GreenNode, RazorDiagnostic[]> DiagnosticsTable = new();
17-
private static readonly ConditionalWeakTable<GreenNode, SyntaxAnnotation[]> AnnotationsTable = new();
14+
private static readonly ConditionalWeakTable<GreenNode, RazorDiagnostic[]> s_diagnosticsTable = new();
15+
private static readonly RazorDiagnostic[] s_emptyDiagnostics = [];
1816

1917
private int _width;
2018
private byte _slotCount;
@@ -30,32 +28,18 @@ protected GreenNode(SyntaxKind kind, int width)
3028
_width = width;
3129
}
3230

33-
protected GreenNode(SyntaxKind kind, RazorDiagnostic[]? diagnostics, SyntaxAnnotation[]? annotations)
34-
: this(kind, 0, diagnostics, annotations)
31+
protected GreenNode(SyntaxKind kind, RazorDiagnostic[]? diagnostics)
32+
: this(kind, 0, diagnostics)
3533
{
3634
}
3735

38-
protected GreenNode(SyntaxKind kind, int width, RazorDiagnostic[]? diagnostics, SyntaxAnnotation[]? annotations)
36+
protected GreenNode(SyntaxKind kind, int width, RazorDiagnostic[]? diagnostics)
3937
: this(kind, width)
4038
{
4139
if (diagnostics?.Length > 0)
4240
{
4341
Flags |= NodeFlags.ContainsDiagnostics;
44-
DiagnosticsTable.Add(this, diagnostics);
45-
}
46-
47-
if (annotations?.Length > 0)
48-
{
49-
foreach (var annotation in annotations)
50-
{
51-
if (annotation == null)
52-
{
53-
throw new ArgumentException("Annotation cannot be null", nameof(annotations));
54-
}
55-
}
56-
57-
Flags |= NodeFlags.ContainsAnnotations;
58-
AnnotationsTable.Add(this, annotations);
42+
s_diagnosticsTable.Add(this, diagnostics);
5943
}
6044
}
6145

@@ -171,8 +155,6 @@ internal void ClearFlags(NodeFlags flags)
171155
internal virtual bool IsMissing => (Flags & NodeFlags.IsMissing) != 0;
172156

173157
public bool ContainsDiagnostics => (Flags & NodeFlags.ContainsDiagnostics) != 0;
174-
175-
public bool ContainsAnnotations => (Flags & NodeFlags.ContainsAnnotations) != 0;
176158
#endregion
177159

178160
#region Diagnostics
@@ -182,31 +164,13 @@ internal RazorDiagnostic[] GetDiagnostics()
182164
{
183165
if (ContainsDiagnostics)
184166
{
185-
if (DiagnosticsTable.TryGetValue(this, out var diagnostics))
167+
if (s_diagnosticsTable.TryGetValue(this, out var diagnostics))
186168
{
187169
return diagnostics;
188170
}
189171
}
190172

191-
return EmptyDiagnostics;
192-
}
193-
#endregion
194-
195-
#region Annotations
196-
internal abstract GreenNode SetAnnotations(SyntaxAnnotation[]? annotations);
197-
198-
internal SyntaxAnnotation[] GetAnnotations()
199-
{
200-
if (ContainsAnnotations)
201-
{
202-
if (AnnotationsTable.TryGetValue(this, out var annotations))
203-
{
204-
Debug.Assert(annotations.Length != 0, "There cannot be an empty annotation entry.");
205-
return annotations;
206-
}
207-
}
208-
209-
return EmptyAnnotations;
173+
return s_emptyDiagnostics;
210174
}
211175
#endregion
212176

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Syntax/GreenNodeExtensions.cs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
using System.Collections.Immutable;
77
using System.Runtime.InteropServices;
8-
using Microsoft.AspNetCore.Razor.PooledObjects;
98

109
namespace Microsoft.AspNetCore.Razor.Language.Syntax;
1110

@@ -23,31 +22,6 @@ internal static InternalSyntax.SyntaxList<T> ToGreenList<T>(this GreenNode node)
2322
return new InternalSyntax.SyntaxList<T>(node);
2423
}
2524

26-
public static TNode WithAnnotationsGreen<TNode>(this TNode node, params SyntaxAnnotation[] annotations) where TNode : GreenNode
27-
{
28-
if (annotations.Length == 0)
29-
{
30-
var existingAnnotations = node.GetAnnotations();
31-
if (existingAnnotations != null && existingAnnotations.Length > 0)
32-
{
33-
node = (TNode)node.SetAnnotations(null);
34-
}
35-
36-
return node;
37-
}
38-
39-
using var newAnnotations = new PooledArrayBuilder<SyntaxAnnotation>(annotations.Length);
40-
foreach (var candidate in annotations)
41-
{
42-
if (!newAnnotations.Contains(candidate))
43-
{
44-
newAnnotations.Add(candidate);
45-
}
46-
}
47-
48-
return (TNode)node.SetAnnotations(newAnnotations.ToArray());
49-
}
50-
5125
public static TNode WithDiagnosticsGreen<TNode>(this TNode node, RazorDiagnostic[] diagnostics)
5226
where TNode : GreenNode
5327
{

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Syntax/InternalSyntax/RazorSyntaxNode.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ protected RazorSyntaxNode(SyntaxKind kind, int width)
1616
{
1717
}
1818

19-
protected RazorSyntaxNode(SyntaxKind kind, RazorDiagnostic[] diagnostics, SyntaxAnnotation[] annotations)
20-
: base(kind, diagnostics, annotations)
19+
protected RazorSyntaxNode(SyntaxKind kind, RazorDiagnostic[] diagnostics)
20+
: base(kind, diagnostics)
2121
{
2222
}
2323

24-
protected RazorSyntaxNode(SyntaxKind kind, int width, RazorDiagnostic[] diagnostics, SyntaxAnnotation[] annotations)
25-
: base(kind, width, diagnostics, annotations)
24+
protected RazorSyntaxNode(SyntaxKind kind, int width, RazorDiagnostic[] diagnostics)
25+
: base(kind, width, diagnostics)
2626
{
2727
}
2828
}

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Syntax/InternalSyntax/SyntaxList.cs

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ internal SyntaxList()
1515
{
1616
}
1717

18-
internal SyntaxList(RazorDiagnostic[] diagnostics, SyntaxAnnotation[] annotations)
19-
: base(SyntaxKind.List, diagnostics, annotations)
18+
internal SyntaxList(RazorDiagnostic[] diagnostics)
19+
: base(SyntaxKind.List, diagnostics)
2020
{
2121
}
2222

@@ -72,7 +72,7 @@ internal static WithThreeChildren List(GreenNode child0, GreenNode child1, Green
7272
}
7373

7474
internal static GreenNode List<TNode>(ReadOnlySpan<TNode> nodes)
75-
where TNode : GreenNode
75+
where TNode : GreenNode
7676
{
7777
var count = nodes.Length;
7878
var array = new ArrayElement<GreenNode>[count];
@@ -184,7 +184,8 @@ internal WithTwoChildren(GreenNode child0, GreenNode child1)
184184
_child1 = child1;
185185
}
186186

187-
internal WithTwoChildren(RazorDiagnostic[] diagnostics, SyntaxAnnotation[] annotations, GreenNode child0, GreenNode child1)
187+
internal WithTwoChildren(GreenNode child0, GreenNode child1, RazorDiagnostic[] diagnostics)
188+
: base(diagnostics)
188189
{
189190
SlotCount = 2;
190191
AdjustFlagsAndWidth(child0);
@@ -219,12 +220,7 @@ internal override SyntaxNode CreateRed(SyntaxNode parent, int position)
219220

220221
internal override GreenNode SetDiagnostics(RazorDiagnostic[] errors)
221222
{
222-
return new WithTwoChildren(errors, this.GetAnnotations(), _child0, _child1);
223-
}
224-
225-
internal override GreenNode SetAnnotations(SyntaxAnnotation[] annotations)
226-
{
227-
return new WithTwoChildren(GetDiagnostics(), annotations, _child0, _child1);
223+
return new WithTwoChildren(_child0, _child1, errors);
228224
}
229225
}
230226

@@ -245,8 +241,8 @@ internal WithThreeChildren(GreenNode child0, GreenNode child1, GreenNode child2)
245241
_child2 = child2;
246242
}
247243

248-
internal WithThreeChildren(RazorDiagnostic[] diagnostics, SyntaxAnnotation[] annotations, GreenNode child0, GreenNode child1, GreenNode child2)
249-
: base(diagnostics, annotations)
244+
internal WithThreeChildren(GreenNode child0, GreenNode child1, GreenNode child2, RazorDiagnostic[] diagnostics)
245+
: base(diagnostics)
250246
{
251247
SlotCount = 3;
252248
AdjustFlagsAndWidth(child0);
@@ -286,12 +282,7 @@ internal override SyntaxNode CreateRed(SyntaxNode parent, int position)
286282

287283
internal override GreenNode SetDiagnostics(RazorDiagnostic[] errors)
288284
{
289-
return new WithThreeChildren(errors, GetAnnotations(), _child0, _child1, _child2);
290-
}
291-
292-
internal override GreenNode SetAnnotations(SyntaxAnnotation[] annotations)
293-
{
294-
return new WithThreeChildren(GetDiagnostics(), annotations, _child0, _child1, _child2);
285+
return new WithThreeChildren(_child0, _child1, _child2, errors);
295286
}
296287
}
297288

@@ -305,8 +296,8 @@ internal WithManyChildrenBase(ArrayElement<GreenNode>[] children)
305296
this.InitializeChildren();
306297
}
307298

308-
internal WithManyChildrenBase(RazorDiagnostic[] diagnostics, SyntaxAnnotation[] annotations, ArrayElement<GreenNode>[] children)
309-
: base(diagnostics, annotations)
299+
internal WithManyChildrenBase(ArrayElement<GreenNode>[] children, RazorDiagnostic[] diagnostics)
300+
: base(diagnostics)
310301
{
311302
this.children = children;
312303
this.InitializeChildren();
@@ -358,19 +349,14 @@ internal WithManyChildren(ArrayElement<GreenNode>[] children)
358349
{
359350
}
360351

361-
internal WithManyChildren(RazorDiagnostic[] diagnostics, SyntaxAnnotation[] annotations, ArrayElement<GreenNode>[] children)
362-
: base(diagnostics, annotations, children)
352+
internal WithManyChildren(ArrayElement<GreenNode>[] children, RazorDiagnostic[] diagnostics)
353+
: base(children, diagnostics)
363354
{
364355
}
365356

366357
internal override GreenNode SetDiagnostics(RazorDiagnostic[] errors)
367358
{
368-
return new WithManyChildren(errors, GetAnnotations(), children);
369-
}
370-
371-
internal override GreenNode SetAnnotations(SyntaxAnnotation[] annotations)
372-
{
373-
return new WithManyChildren(GetDiagnostics(), annotations, children);
359+
return new WithManyChildren(children, errors);
374360
}
375361
}
376362

@@ -384,8 +370,8 @@ internal WithLotsOfChildren(ArrayElement<GreenNode>[] children)
384370
_childOffsets = CalculateOffsets(children);
385371
}
386372

387-
internal WithLotsOfChildren(RazorDiagnostic[] diagnostics, SyntaxAnnotation[] annotations, ArrayElement<GreenNode>[] children, int[] childOffsets)
388-
: base(diagnostics, annotations, children)
373+
internal WithLotsOfChildren(ArrayElement<GreenNode>[] children, int[] childOffsets, RazorDiagnostic[] diagnostics)
374+
: base(children, diagnostics)
389375
{
390376
_childOffsets = childOffsets;
391377
}
@@ -425,12 +411,7 @@ private static int[] CalculateOffsets(ArrayElement<GreenNode>[] children)
425411

426412
internal override GreenNode SetDiagnostics(RazorDiagnostic[] errors)
427413
{
428-
return new WithLotsOfChildren(errors, this.GetAnnotations(), children, _childOffsets);
429-
}
430-
431-
internal override GreenNode SetAnnotations(SyntaxAnnotation[] annotations)
432-
{
433-
return new WithLotsOfChildren(GetDiagnostics(), annotations, children, _childOffsets);
414+
return new WithLotsOfChildren(children, _childOffsets, errors);
434415
}
435416

436417
/// <summary>

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Syntax/InternalSyntax/SyntaxToken.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ internal class SyntaxToken : RazorSyntaxNode
1010
internal SyntaxToken(
1111
SyntaxKind kind,
1212
string content,
13-
RazorDiagnostic[]? diagnostics,
14-
SyntaxAnnotation[]? annotations = null)
15-
: base(kind, content.Length, diagnostics, annotations)
13+
RazorDiagnostic[]? diagnostics)
14+
: base(kind, content.Length, diagnostics)
1615
{
1716
Content = content;
1817
}
@@ -28,12 +27,7 @@ internal override SyntaxNode CreateRed(SyntaxNode? parent, int position)
2827

2928
internal override GreenNode SetDiagnostics(RazorDiagnostic[]? diagnostics)
3029
{
31-
return new SyntaxToken(Kind, Content, diagnostics, GetAnnotations());
32-
}
33-
34-
internal override GreenNode SetAnnotations(SyntaxAnnotation[]? annotations)
35-
{
36-
return new SyntaxToken(Kind, Content, GetDiagnostics(), annotations);
30+
return new SyntaxToken(Kind, Content, diagnostics);
3731
}
3832

3933
protected sealed override int GetSlotCount()

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Syntax/NodeFlags.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ internal enum NodeFlags : byte
1010
{
1111
None = 0,
1212
ContainsDiagnostics = 1 << 0,
13-
ContainsAnnotations = 1 << 1,
1413
IsMissing = 1 << 2,
1514

16-
InheritMask = ContainsDiagnostics | ContainsAnnotations | IsMissing
15+
InheritMask = ContainsDiagnostics | IsMissing
1716
}

0 commit comments

Comments
 (0)