From a0c909914d1f87b142057f010d7dd9cc6c542982 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Thu, 28 Feb 2019 10:38:53 -0500 Subject: [PATCH] lang: funcs: Don't allow interpolation in printf format string We'd like to pre-compute the interpolation if we can, so that we can run this code properly... For now, we can't so it's a compile time error... Hopefully we can remove this restriction in the future. The problem is the string must be a constant, or it would be possible to switch it from "%d %s" to "%s %d %d" or anything that changes the type signature. --- lang/funcs/core/fmt/printf_func.go | 7 +++++++ .../TestAstFunc2/printfinterpolate0.output | 1 + .../TestAstFunc2/printfinterpolate0/main.mcl | 10 ++++++++++ 3 files changed, 18 insertions(+) create mode 100644 lang/interpret_test/TestAstFunc2/printfinterpolate0.output create mode 100644 lang/interpret_test/TestAstFunc2/printfinterpolate0/main.mcl diff --git a/lang/funcs/core/fmt/printf_func.go b/lang/funcs/core/fmt/printf_func.go index 23e468cf..71155778 100644 --- a/lang/funcs/core/fmt/printf_func.go +++ b/lang/funcs/core/fmt/printf_func.go @@ -85,6 +85,13 @@ func (obj *PrintfFunc) Polymorphisms(partialType *types.Type, partialValues []ty } } + // FIXME: we'd like to pre-compute the interpolation if we can, so that + // we can run this code properly... for now, we can't, so it's a compile + // time error... + if partialValues[0] == nil { + return nil, fmt.Errorf("could not determine type from format string") + } + format := partialValues[0].Str() // must not panic typList, err := parseFormatToTypeList(format) if err != nil { diff --git a/lang/interpret_test/TestAstFunc2/printfinterpolate0.output b/lang/interpret_test/TestAstFunc2/printfinterpolate0.output new file mode 100644 index 00000000..9e048e47 --- /dev/null +++ b/lang/interpret_test/TestAstFunc2/printfinterpolate0.output @@ -0,0 +1 @@ +# err: err3: polymorphic signatures for func `fmt.printf` could not be found: could not determine type from format string diff --git a/lang/interpret_test/TestAstFunc2/printfinterpolate0/main.mcl b/lang/interpret_test/TestAstFunc2/printfinterpolate0/main.mcl new file mode 100644 index 00000000..4a227571 --- /dev/null +++ b/lang/interpret_test/TestAstFunc2/printfinterpolate0/main.mcl @@ -0,0 +1,10 @@ +import "fmt" + +$str1 = "big" +$str2 = "world" + +# FIXME: we'd like to pre-compute the interpolation if we can, so that we can +# run this code properly... for now, we can't, so it's a compile time error... +print "print1" { + msg => fmt.printf("hello ${str1} %s", $str2), +}