diff --git a/vlib/v/scanner/scanner.v b/vlib/v/scanner/scanner.v index 12d7f9df4d..48e4066555 100644 --- a/vlib/v/scanner/scanner.v +++ b/vlib/v/scanner/scanner.v @@ -725,7 +725,11 @@ fn (mut s Scanner) text_scan() token.Token { } // end of `$expr` // allow `'$a.b'` and `'$a.c()'` - if s.is_inter_start && next_char != `.` && next_char != `(` { + if s.is_inter_start && next_char == `(` { + if s.look_ahead(2) != `)` { + s.warn('use e.g. `\${f(expr)}` or `\$name\\(` instead of `\$f(expr)`') + } + } else if s.is_inter_start && next_char != `.` { s.is_inter_end = true s.is_inter_start = false } @@ -1220,14 +1224,14 @@ fn (mut s Scanner) ident_string() string { } } // ${var} (ignore in vfmt mode) - if c == `{` && prevc == `$` && !is_raw && s.count_symbol_before(s.pos - 2, slash) % 2 == 0 { + if prevc == `$` && c == `{` && !is_raw && s.count_symbol_before(s.pos - 2, slash) % 2 == 0 { s.is_inside_string = true // so that s.pos points to $ at the next step s.pos -= 2 break } // $var - if util.is_name_char(c) && prevc == `$` && !is_raw && + if prevc == `$` && util.is_name_char(c) && !is_raw && s.count_symbol_before(s.pos - 2, slash) % 2 == 0 { s.is_inside_string = true s.is_inter_start = true @@ -1372,6 +1376,14 @@ fn (mut s Scanner) inc_line_number() { } } +pub fn (s &Scanner) warn(msg string) { + pos := token.Position{ + line_nr: s.line_nr + pos: s.pos + } + eprintln(util.formatted_error('warning:', msg, s.file_path, pos)) +} + pub fn (s &Scanner) error(msg string) { pos := token.Position{ line_nr: s.line_nr diff --git a/vlib/v/tests/string_interpolation_test.v b/vlib/v/tests/string_interpolation_test.v index 8b75dbeef3..e15e73f9fc 100644 --- a/vlib/v/tests/string_interpolation_test.v +++ b/vlib/v/tests/string_interpolation_test.v @@ -182,3 +182,12 @@ fn test_method_interpolation() { assert '>${y.f().a}<' == '>2<' assert '>$y.f().a<' == '>2<' } + +fn f(i int) int { + return i +} + +fn test_call() { + s := '${f(4)}' + assert s == '4' +}