Skip to content

Commit aa23687

Browse files
committed
Add negative golden tests for invalid-identifier parse errors (#7742)
Freeze the current (unhelpful) error output for three forms of invalid UPLC identifier: - `foo-bar` — hyphen followed by non-digits - `foo-123-456` — double `-NNN` suffix - `pubKeyHash-305478r71` — hyphen + digits + more letters (the shape Scalus 0.16.0's `toUplcOptimized` emits, from issue #7742) All three cases produce misleading diagnostics today — notably the Scalus case reports the error 8+ characters past the offending name. Capturing the status quo as goldens so that a follow-up improvement to name-parser diagnostics shows up as an explicit golden-file diff.
1 parent 2a05024 commit aa23687

4 files changed

Lines changed: 64 additions & 0 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
test:1:28:
2+
|
3+
1 | (program 1.1.0 (lam foo-123-456 foo-123-456))
4+
| ^
5+
unexpected '-'
6+
expecting '`', digit, opening bracket '[', or opening parenthesis '('
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
test:1:42:
2+
|
3+
1 | (program 1.1.0 (lam pubKeyHash-305478r71 (lam x x)))
4+
| ^
5+
unexpected '('
6+
expecting closing parenthesis ')'
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
test:1:24:
2+
|
3+
1 | (program 1.1.0 (lam foo-bar foo-bar))
4+
| ^
5+
unexpected '-'
6+
expecting '`', identifier-unquoted, opening bracket '[', or opening parenthesis '('

plutus-core/untyped-plutus-core/testlib/Generators/Spec.hs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ test_parsing =
6060
, propMissingConOperands
6161
, propInvalidKeyword
6262
, propBracketMismatch
63+
, propInvalidIdentifierHyphenLetters
64+
, propInvalidIdentifierHyphenWord
65+
, propInvalidIdentifierDoubleUnique
6366
]
6467
]
6568

@@ -241,6 +244,49 @@ propBracketMismatch =
241244
"bracket-mismatch"
242245
"(program 1.1.0 [(var x))"
243246

247+
{- Note [Negative identifier-grammar tests]
248+
The parser's name grammar treats '-NNN' purely as the numeric unique-suffix:
249+
'foo-123' → Name "foo" (Unique 123). A '-' anywhere else in an identifier is
250+
not allowed by the unquoted grammar (see 'isIdentifierChar' in
251+
'PlutusCore.Name.Unique'). Several tools in the wild (e.g. Scalus 0.16.0's
252+
'toUplcOptimized') emit names like 'pubKeyHash-305478r71' that violate this,
253+
and today the parser mis-parses them in a way that surfaces as a confusing
254+
error hundreds of lines away from the offending name — see issue #7742.
255+
256+
The goldens below freeze the *current* (unhelpful) error output so that a
257+
future diagnostic improvement shows up as an explicit golden-file diff.
258+
When the parser is taught to point at the bad name itself, accept the new
259+
goldens with 'scripts/regen-goldens.sh' (or '--accept'). -}
260+
261+
{-| @pubKeyHash-305478r71@ — the exact shape Scalus 0.16.0 produces, inside a
262+
binder. Current behaviour: the parser eats @pubKeyHash-305478@ as name+unique,
263+
picks up @r71@ as the lam body, then fails far away on the next paren. -}
264+
propInvalidIdentifierHyphenLetters :: TestTree
265+
propInvalidIdentifierHyphenLetters =
266+
testParseErrorGolden
267+
"Invalid identifier: hyphen followed by digits then letters"
268+
"invalid-identifier-hyphen-letters"
269+
"(program 1.1.0 (lam pubKeyHash-305478r71 (lam x x)))"
270+
271+
{-| @foo-bar@ — hyphen followed by non-digits. Current behaviour: the parser
272+
stops at '-' (it is not in 'isIdentifierChar'), takes @foo@ as the name, and
273+
then explodes on @-bar@ which is not a valid continuation anywhere. -}
274+
propInvalidIdentifierHyphenWord :: TestTree
275+
propInvalidIdentifierHyphenWord =
276+
testParseErrorGolden
277+
"Invalid identifier: hyphen followed by non-digits"
278+
"invalid-identifier-hyphen-word"
279+
"(program 1.1.0 (lam foo-bar foo-bar))"
280+
281+
{-| @foo-123-456@ — ambiguous double '-NNN' run. Current behaviour: the first
282+
@-123@ wins as the unique, @-456@ is left over and fails the next check. -}
283+
propInvalidIdentifierDoubleUnique :: TestTree
284+
propInvalidIdentifierDoubleUnique =
285+
testParseErrorGolden
286+
"Invalid identifier: double unique-suffix"
287+
"invalid-identifier-double-unique"
288+
"(program 1.1.0 (lam foo-123-456 foo-123-456))"
289+
244290
--------------------------------------------------------------------------------
245291
-- Helper Functions ------------------------------------------------------------
246292

0 commit comments

Comments
 (0)