Skip to content

Commit

Permalink
Fix match patterns with a dotted constructor
Browse files Browse the repository at this point in the history
This bug was a regression since the last release, so it doesn't get a NEWS entry.
  • Loading branch information
Kodiologist committed Feb 4, 2023
1 parent 489ed58 commit aeca832
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
11 changes: 8 additions & 3 deletions hy/core/result_macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -1084,7 +1084,8 @@ def compile_with_expression(compiler, expr, root, args, body):
| pexpr(keepsym("|"), many(_pattern))
| braces(many(LITERAL + _pattern), maybe(pvalue("unpack-mapping", SYM)))
| pexpr(
notsym(".", "|", "unpack-mapping", "unpack-iterable"),
pexpr(keepsym("."), oneplus(SYM))
| notsym(".", "|", "unpack-mapping", "unpack-iterable"),
many(parse_if(lambda x: not isinstance(x, Keyword), _pattern)),
many(KEYWORD + _pattern),
)
Expand Down Expand Up @@ -1219,11 +1220,15 @@ def compile_pattern(compiler, pattern):
)
)
elif isinstance(value, Expression):
root, args, kwargs = value
head, args, kwargs = value
keywords, values = zip(*kwargs) if kwargs else ([], [])
return asty.MatchClass(
value,
cls=compiler.scope.access(asty.Name(root, id=mangle(root), ctx=ast.Load())),
cls=compiler.compile(
# `head` could be a symbol or a dotted form.
(head[:1] + head[1]).replace(head)
if type(head) is Expression
else head).expr,
patterns=[compile_pattern(compiler, v) for v in args],
kwd_attrs=[kwd.name for kwd in keywords],
kwd_patterns=[compile_pattern(compiler, value) for value in values],
Expand Down
6 changes: 6 additions & 0 deletions tests/native_tests/match.hy
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@
(hy.eval '(match 1
1 :if True :as x x))))

(defn test-dotted-constructor []
; https://github.com/hylang/hy/issues/2404
(defclass C [Point]
(setv C Point))
(assert (= (match (Point 1 2) (C.C 1 2) "ok") "ok")))

(defn test-matching-side-effects []
(setv x 0)
(defn foo []
Expand Down

0 comments on commit aeca832

Please sign in to comment.