Skip to content

regexp compile failures on darwin/wasm #1306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ribasushi opened this issue Aug 22, 2020 · 5 comments
Closed

regexp compile failures on darwin/wasm #1306

ribasushi opened this issue Aug 22, 2020 · 5 comments
Labels
enhancement New feature or request wasm WebAssembly

Comments

@ribasushi
Copy link

TLDR
regexp is listed as fully implemented, yet code using it fails compilation.

Version
tinygo version 0.14.0 darwin/amd64 (using go version go1.14.7 and LLVM version 10.0.0)

Failure

~$ tinygo run tiny.go 
# command-line-arguments
/usr/local/go/src/regexp/syntax/parse.go:1535:5: interp: branch on a non-constant

traceback:
/usr/local/go/src/regexp/syntax/parse.go:774:28:
  %108 = call { %runtime._string, %runtime._interface } @"(*regexp/syntax.parser).parseClass"(%"regexp/syntax.parser"* %19, i8* %106, i64 %107, i8* undef, i8* undef), !dbg !1831
/usr/local/go/src/regexp/regexp.go:170:25:
  %4 = call { %"regexp/syntax.Regexp"*, %runtime._interface } @"regexp/syntax.Parse"(i8* %2, i64 %3, i16 %mode, i8* undef, i8* undef), !dbg !1739
/usr/local/go/src/regexp/regexp.go:133:16:
  %4 = call { %regexp.Regexp*, %runtime._interface } @regexp.compile(i8* %2, i64 %3, i16 212, i1 false, i8* undef, i8* undef), !dbg !1696
/usr/local/go/src/regexp/regexp.go:309:24:
  %4 = call { %regexp.Regexp*, %runtime._interface } @regexp.Compile(i8* %2, i64 %3, i8* undef, i8* undef), !dbg !1698
command-line-arguments/<init>:8:34:
  %0 = call %regexp.Regexp* @regexp.MustCompile(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @"command-line-arguments.init$string", i32 0, i32 0), i64 12, i8* undef, i8* undef), !dbg !1692

Expectation

~$ go run tiny.go 
(?m)^([^\n])
(?m)^\s{0,12}([^\s\n\-])
(?m)^(\s*)\-\-
\bMaxPayload\b

Testcase

package main

import (
	"fmt"
	"regexp"
)

var indenter = regexp.MustCompile(`(?m)^([^\n])`)
var nonOptIndenter = regexp.MustCompile(`(?m)^\s{0,12}([^\s\n\-])`)
var dashStripper = regexp.MustCompile(`(?m)^(\s*)\-\-`)
var maxPlaceholder = regexp.MustCompile(`\bMaxPayload\b`)

func main() {
	for _, re := range []*regexp.Regexp{
		indenter,
		nonOptIndenter,
		dashStripper,
		maxPlaceholder,
	} {
		fmt.Println(re.String())
	}
}
@deadprogram
Copy link
Member

Note that the fact they can be imported, does not mean that all functions and types in the program can be used. For example, sometimes using some functions or types of the package will still trigger compiler errors.

via https://tinygo.org/lang-support/stdlib/

@deadprogram deadprogram added the enhancement New feature or request label Aug 22, 2020
@ribasushi ribasushi changed the title regexp compile failures on darwin regexp compile failures on darwin/wasm Aug 22, 2020
@ribasushi
Copy link
Author

Removed the arch-label: same error with the wasm target:

~$ tinygo build -o wasm.wasm -target wasm tiny.go 
# command-line-arguments
/usr/local/go/src/regexp/syntax/parse.go:1535:5: interp: branch on a non-constant

traceback:
/usr/local/go/src/regexp/syntax/parse.go:774:28:
  %108 = call { %runtime._string, %runtime._interface } @"(*regexp/syntax.parser).parseClass"(%"regexp/syntax.parser"* %19, i8* %106, i32 %107, i8* undef, i8* undef), !dbg !1875
/usr/local/go/src/regexp/regexp.go:170:25:
  %4 = call { %"regexp/syntax.Regexp"*, %runtime._interface } @"regexp/syntax.Parse"(i8* %2, i32 %3, i16 %mode, i8* undef, i8* undef), !dbg !1783
/usr/local/go/src/regexp/regexp.go:133:16:
  %4 = call { %regexp.Regexp*, %runtime._interface } @regexp.compile(i8* %2, i32 %3, i16 212, i1 false, i8* undef, i8* undef), !dbg !1740
/usr/local/go/src/regexp/regexp.go:309:24:
  %4 = call { %regexp.Regexp*, %runtime._interface } @regexp.Compile(i8* %2, i32 %3, i8* undef, i8* undef), !dbg !1742
command-line-arguments/<init>:8:34:
  %0 = call %regexp.Regexp* @regexp.MustCompile(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @"command-line-arguments.init$string", i32 0, i32 0), i32 12, i8* undef, i8* undef), !dbg !1736

@ribasushi
Copy link
Author

@deadprogram I really want to try to fix this: could you give me 30 seconds of pointers? Like even a previous issue/PR that is similar to this in spirit will help!

@aykevl
Copy link
Member

aykevl commented Aug 25, 2020

You can work around this by not doing the rexexp.MustCompile in a global. This should work:

package main

import (
	"fmt"
	"regexp"
)

func main() {
	var indenter = regexp.MustCompile(`(?m)^([^\n])`)
	var nonOptIndenter = regexp.MustCompile(`(?m)^\s{0,12}([^\s\n\-])`)
	var dashStripper = regexp.MustCompile(`(?m)^(\s*)\-\-`)
	var maxPlaceholder = regexp.MustCompile(`\bMaxPayload\b`)

	for _, re := range []*regexp.Regexp{
		indenter,
		nonOptIndenter,
		dashStripper,
		maxPlaceholder,
	} {
		fmt.Println(re.String())
	}
}

@niaow
Copy link
Member

niaow commented Nov 4, 2022

This issue seems to have been fixed by some interp work at some point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request wasm WebAssembly
Projects
None yet
Development

No branches or pull requests

4 participants