Skip to content

Commit 6835d65

Browse files
tautschnigkiro-agentaqjune-aws
authored
Fix #1146: require command_datatypes to be non-empty (#1155)
The Core grammar declared command_datatypes as op command_datatypes (datatypes : NewlineSepBy DatatypeDecl) : Command => datatypes ";\\n"; Without @[nonempty] on the field, NewlineSepBy compiles to a zero-or-more parser, so the trailing ";\\n" production alone matched any stray ";" in the input. This is particularly easy to trip accidentally: command_fndef's surface syntax has no trailing semicolon, so a user writing "};" at the end of a function body (by analogy with procedures, whose grammar does end in ";") silently produced a phantom command_datatypes op with zero datatypes. The phantom later tripped an explicit assertion in translateDatatypes: "Datatype block must contain at least one datatype" which calls panic!, producing a large backtrace at gen_smt_vcs time with no useful source location. Mark the datatypes field @[nonempty]. The stray ";" now surfaces as a parse error at the point it actually appears, and the original repro elaborates cleanly. Add a regression test (StrataTest/Languages/Core/Tests/Issue1146Test.lean) that pins the canonical form: a program mixing a datatype and a function can run through gen_smt_vcs without panicking. Fixes: #1146 By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice. --------- Co-authored-by: Kiro <kiro-agent@users.noreply.github.com> Co-authored-by: Juneyoung Lee <136006969+aqjune-aws@users.noreply.github.com>
1 parent 192b780 commit 6835d65

2 files changed

Lines changed: 60 additions & 1 deletion

File tree

Strata/Languages/Core/DDMTransform/Grammar.lean

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,8 +461,11 @@ op datatype_decl (name : Ident,
461461

462462
// Unified datatype command: one or more datatype declarations separated by
463463
// newlines, ending with a semicolon.
464+
//
465+
// `@[nonempty]` is load-bearing: see
466+
// https://github.com/strata-org/Strata/issues/1146.
464467
@[scope(datatypes), preRegisterTypes(datatypes)]
465-
op command_datatypes (datatypes : NewlineSepBy DatatypeDecl) : Command =>
468+
op command_datatypes (@[nonempty] datatypes : NewlineSepBy DatatypeDecl) : Command =>
466469
datatypes ";\n";
467470

468471
#end
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/-
2+
Copyright Strata Contributors
3+
4+
SPDX-License-Identifier: Apache-2.0 OR MIT
5+
-/
6+
7+
import Strata.Languages.Core.DDMTransform.Translate
8+
9+
/-!
10+
# Regression test for https://github.com/strata-org/Strata/issues/1146
11+
12+
A trailing `;` after a `function` body must not be silently accepted as an
13+
empty `command_datatypes` block (which would later panic in
14+
`translateDatatypes`), and a program mixing a datatype and a function must
15+
translate cleanly.
16+
-/
17+
18+
namespace Strata.Issue1146Test
19+
20+
/-! ## Canonical form: datatype + function translates without error -/
21+
22+
private def datatypeAndFunction : Strata.Program :=
23+
#strata
24+
program Core;
25+
26+
datatype List () { Nil() };
27+
28+
function Len (xs : List) : int
29+
{
30+
0
31+
}
32+
#end
33+
34+
/-- info: true -/
35+
#guard_msgs in
36+
#eval TransM.run Inhabited.default (translateProgram datatypeAndFunction) |>.snd |>.isEmpty
37+
38+
/-! ## Stray trailing `;` after a function body is a parse error -/
39+
40+
/--
41+
error: unexpected token ';'; expected 'function', Core.Block or expected at least one element
42+
-/
43+
#guard_msgs in
44+
def strayTrailingSemi : Strata.Program :=
45+
#strata
46+
program Core;
47+
48+
datatype List () { Nil() };
49+
50+
function Len (xs : List) : int
51+
{
52+
0
53+
};
54+
#end
55+
56+
end Strata.Issue1146Test

0 commit comments

Comments
 (0)