Skip to content

Match.subscript fails to properly cast non-matched Capture #624

Open
@jasonbobier

Description

@jasonbobier

This code should produce an Optional<Int>. Instead it produces an Optional<Substring> and crashes.

import RegexBuilder

let ref = Reference(Int.self)

let regex = Regex {
	"test"
	Optionally {
		TryCapture(as: ref) {
			OneOrMore(.digit)
		} transform: {
			Int($0)
		}
	}
	"a"
}

let match = "testa".wholeMatch(of: regex)

print("Int \(match![ref])")

This aborts with:

Could not cast value of type 'Swift.Optional<Swift.Substring>' (0x1f2db92c0) to 'Swift.Int' (0x1f3da0bf0).

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions