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

Fix: Compiled disjunctive nested pattern matching no longer crashing #5574

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ public class IdPattern : ExtendedPattern, IHasReferences {
public DatatypeCtor Ctor;

public bool IsWildcardPattern =>
Arguments == null && Id.StartsWith("_");
Arguments == null && Id.StartsWith(WildcardString);

public bool IsWildcardExact =>
Arguments == null && Id == "_";
Arguments == null && Id == WildcardString;

public const string WildcardString = "_";

public void MakeAConstructor() {
this.Arguments = new List<ExtendedPattern>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,10 @@ private ConcreteSyntaxTree EmitNestedMatchCaseConditions(string sourceName,
writer = thenWriter;
} else if (pattern is IdPattern idPattern) {
if (idPattern.BoundVar != null) {
if (idPattern.BoundVar.Tok.val.StartsWith(IdPattern.WildcardString)) {
return writer;
}

var boundVar = idPattern.BoundVar;
var valueWriter = DeclareLocalVar(IdName(boundVar), boundVar.Type, idPattern.Tok, writer);
valueWriter.Write(sourceName);
Expand Down Expand Up @@ -651,9 +655,11 @@ private ConcreteSyntaxTree EmitNestedMatchStmtCaseConstructor(string sourceName,
var childPattern = idPattern.Arguments[index];
if (childPattern is IdPattern { BoundVar: not null } childIdPattern) {
var boundVar = childIdPattern.BoundVar;
newSourceName = IdName(boundVar);
var valueWriter = DeclareLocalVar(newSourceName, boundVar.Type, idPattern.Tok, result);
valueWriter.Append(destructor);
if (!childIdPattern.BoundVar.Name.StartsWith(IdPattern.WildcardString)) {
newSourceName = IdName(boundVar);
var valueWriter = DeclareLocalVar(newSourceName, boundVar.Type, idPattern.Tok, result);
valueWriter.Append(destructor);
}
} else {
newSourceName = ProtectedFreshId(arg.CompileName);
var valueWriter = DeclareLocalVar(newSourceName, type, idPattern.Tok, result);
Expand Down
10 changes: 5 additions & 5 deletions Source/DafnyCore/GeneratedFromDafny/DAST_Format.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ public static BigInteger SeqToHeight<__T>(Dafny.ISequence<__T> s, Func<__T, BigI
if ((new BigInteger((s).Count)).Sign == 0) {
return BigInteger.Zero;
} else {
BigInteger _763_i = Dafny.Helpers.Id<Func<__T, BigInteger>>(f)((s).Select(BigInteger.Zero));
BigInteger _764_j = DAST.Format.__default.SeqToHeight<__T>((s).Drop(BigInteger.One), f);
if ((_763_i) < (_764_j)) {
return _764_j;
BigInteger _759_i = Dafny.Helpers.Id<Func<__T, BigInteger>>(f)((s).Select(BigInteger.Zero));
BigInteger _760_j = DAST.Format.__default.SeqToHeight<__T>((s).Drop(BigInteger.One), f);
if ((_759_i) < (_760_j)) {
return _760_j;
} else {
return _763_i;
return _759_i;
}
}
}
Expand Down
4,544 changes: 2,249 additions & 2,295 deletions Source/DafnyCore/GeneratedFromDafny/DCOMP.cs

Large diffs are not rendered by default.

678 changes: 325 additions & 353 deletions Source/DafnyCore/GeneratedFromDafny/RAST.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: %testDafnyForEachCompiler "%s"

datatype D = A(int) | C(int) {
function Test(): D {
match this{
case A(_) | C(_) =>
this
}
}
}
method Main() {
var x := C(0).Test();
print "ok";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ok
1 change: 1 addition & 0 deletions docs/dev/news/5572.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Compiled disjunctive nested pattern matching no longer crashing
Loading