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.
This commit is contained in:
James Shubin
2019-02-28 10:38:53 -05:00
parent 170e56b34a
commit a0c909914d
3 changed files with 18 additions and 0 deletions

View File

@@ -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 format := partialValues[0].Str() // must not panic
typList, err := parseFormatToTypeList(format) typList, err := parseFormatToTypeList(format)
if err != nil { if err != nil {

View File

@@ -0,0 +1 @@
# err: err3: polymorphic signatures for func `fmt.printf` could not be found: could not determine type from format string

View File

@@ -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),
}