roc/test/snapshots/match_expr/mixed_pattern_scoping.md
2025-11-23 01:22:01 -05:00

2.7 KiB

META

description=Match expression with mixed tag and list patterns testing variable scoping
type=expr

SOURCE

match data {
    Ok([x, y]) => x + y
    Err(x) => x - 1
    Ok([x]) => x * 2
    Err(y) => y / 2
}

EXPECTED

UNDEFINED VARIABLE - mixed_pattern_scoping.md:1:7:1:11

PROBLEMS

UNDEFINED VARIABLE Nothing is named data in this scope. Is there an import or exposing missing up-top?

mixed_pattern_scoping.md:1:7:1:11:

match data {
  ^^^^

TOKENS

KwMatch,LowerIdent,OpenCurly,
UpperIdent,NoSpaceOpenRound,OpenSquare,LowerIdent,Comma,LowerIdent,CloseSquare,CloseRound,OpFatArrow,LowerIdent,OpPlus,LowerIdent,
UpperIdent,NoSpaceOpenRound,LowerIdent,CloseRound,OpFatArrow,LowerIdent,OpBinaryMinus,Int,
UpperIdent,NoSpaceOpenRound,OpenSquare,LowerIdent,CloseSquare,CloseRound,OpFatArrow,LowerIdent,OpStar,Int,
UpperIdent,NoSpaceOpenRound,LowerIdent,CloseRound,OpFatArrow,LowerIdent,OpSlash,Int,
CloseCurly,
EndOfFile,

PARSE

(e-match
	(e-ident (raw "data"))
	(branches
		(branch
			(p-tag (raw "Ok")
				(p-list
					(p-ident (raw "x"))
					(p-ident (raw "y"))))
			(e-binop (op "+")
				(e-ident (raw "x"))
				(e-ident (raw "y"))))
		(branch
			(p-tag (raw "Err")
				(p-ident (raw "x")))
			(e-binop (op "-")
				(e-ident (raw "x"))
				(e-int (raw "1"))))
		(branch
			(p-tag (raw "Ok")
				(p-list
					(p-ident (raw "x"))))
			(e-binop (op "*")
				(e-ident (raw "x"))
				(e-int (raw "2"))))
		(branch
			(p-tag (raw "Err")
				(p-ident (raw "y")))
			(e-binop (op "/")
				(e-ident (raw "y"))
				(e-int (raw "2"))))))

FORMATTED

match data {
	Ok([x, y]) => x + y
	Err(x) => x - 1
	Ok([x]) => x * 2
	Err(y) => y / 2
}

CANONICALIZE

(e-match
	(match
		(cond
			(e-runtime-error (tag "ident_not_in_scope")))
		(branches
			(branch
				(patterns
					(pattern (degenerate false)
						(p-applied-tag)))
				(value
					(e-binop (op "add")
						(e-lookup-local
							(p-assign (ident "x")))
						(e-lookup-local
							(p-assign (ident "y"))))))
			(branch
				(patterns
					(pattern (degenerate false)
						(p-applied-tag)))
				(value
					(e-binop (op "sub")
						(e-lookup-local
							(p-assign (ident "x")))
						(e-num (value "1")))))
			(branch
				(patterns
					(pattern (degenerate false)
						(p-applied-tag)))
				(value
					(e-binop (op "mul")
						(e-lookup-local
							(p-assign (ident "x")))
						(e-num (value "2")))))
			(branch
				(patterns
					(pattern (degenerate false)
						(p-applied-tag)))
				(value
					(e-binop (op "div")
						(e-lookup-local
							(p-assign (ident "y")))
						(e-num (value "2"))))))))

TYPES

(expr (type "a where [a.from_numeral : Numeral -> Try(a, [InvalidNumeral(Str)])]"))