Skip to content

Commit

Permalink
More diags
Browse files Browse the repository at this point in the history
  • Loading branch information
LPeter1997 committed Oct 5, 2023
1 parent 4f74f17 commit f174f7c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Draco.Compiler/Api/Diagnostics/Diagnostic_Builder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public Builder WithFormatArgs(params object?[] args)
return this;
}

public Builder WithLocation(Location location)
public Builder WithLocation(Location? location)
{
this.Location = location;
return this;
Expand Down
17 changes: 16 additions & 1 deletion src/Draco.Compiler/Internal/FlowAnalysis/DecisionTree.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -269,6 +270,15 @@ private SpecializationComparer()
_ => null,
};

// TODO: Doc
private static BoundPattern? ConstructUnhandledPattern(INode node)
{
if (node.NotCovered is null) return null;

// TODO: Wrap in parent
return node.NotCovered;
}

/// <summary>
/// The root node of this tree.
/// </summary>
Expand All @@ -291,7 +301,12 @@ private SpecializationComparer()
/// <summary>
/// An example of an unhandled pattern, if any.
/// </summary>
public BoundPattern? UnhandledExample => throw new NotImplementedException();
public BoundPattern? UnhandledExample => GraphTraversal
.DepthFirst(this.Root, n => n.Children.Select(c => c.Value))
.Where(n => n.IsFail)
.Select(ConstructUnhandledPattern)
.OfType<BoundPattern>()
.FirstOrDefault();

private readonly IntrinsicSymbols intrinsicSymbols;
private readonly HashSet<TAction> usedActions = new();
Expand Down
16 changes: 12 additions & 4 deletions src/Draco.Compiler/Internal/FlowAnalysis/MatchExhaustiveness.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,19 @@ public override void VisitMatchExpression(BoundMatchExpression node)

if (!decisionTree.IsExhaustive)
{
// TODO: Add example
// Report
this.diagnostics.Add(Diagnostic.Create(
template: FlowAnalysisErrors.NonExhaustiveMatchExpression,
location: node.Syntax?.Location));
var diagBuilder = Diagnostic.CreateBuilder()
.WithTemplate(FlowAnalysisErrors.NonExhaustiveMatchExpression)
.WithLocation(node.Syntax?.Location);
var example = decisionTree.UnhandledExample;
if (example is not null)
{
diagBuilder.WithRelatedInformation(DiagnosticRelatedInformation.Create(
location: node.Syntax?.Location,
format: "for example, the pattern {0} is not handled",
formatArgs: DecisionTree.ToDisplayString(example)));
}
this.diagnostics.Add(diagBuilder.Build());
}

foreach (var (covers, redundant) in decisionTree.Redundancies)
Expand Down

0 comments on commit f174f7c

Please sign in to comment.