From c0247219514f4d92374ae8a87de516b2de469252 Mon Sep 17 00:00:00 2001 From: Turiiya <34311583+ttytm@users.noreply.github.com> Date: Fri, 15 Mar 2024 11:31:06 +0100 Subject: [PATCH] scanner: disallow a shebang line, that is not at the top of a file (#21029) --- vlib/v/scanner/scanner.v | 10 ++++++++++ vlib/v/scanner/tests/shebang_outside_vsh_err.out | 7 +++++++ vlib/v/scanner/tests/shebang_outside_vsh_err.vv | 5 +++++ 3 files changed, 22 insertions(+) create mode 100644 vlib/v/scanner/tests/shebang_outside_vsh_err.out create mode 100644 vlib/v/scanner/tests/shebang_outside_vsh_err.vv diff --git a/vlib/v/scanner/scanner.v b/vlib/v/scanner/scanner.v index d98f0aca42df79..99264fa233bc9f 100644 --- a/vlib/v/scanner/scanner.v +++ b/vlib/v/scanner/scanner.v @@ -959,6 +959,16 @@ pub fn (mut s Scanner) text_scan() token.Token { if nextc == `!` { // treat shebang line (#!) as a comment comment := s.text[start - 1..s.pos].trim_space() + if s.line_nr != 1 { + comment_pos := token.Pos{ + line_nr: s.line_nr - 1 + len: comment.len + pos: start + col: s.current_column() - comment.len + } + s.error_with_pos('a shebang is only valid at the top of the file', + comment_pos) + } // s.fgenln('// shebang line "$s.line_comment"') return s.new_token(.comment, comment, comment.len + 2) } diff --git a/vlib/v/scanner/tests/shebang_outside_vsh_err.out b/vlib/v/scanner/tests/shebang_outside_vsh_err.out new file mode 100644 index 00000000000000..5f35b996fb71aa --- /dev/null +++ b/vlib/v/scanner/tests/shebang_outside_vsh_err.out @@ -0,0 +1,7 @@ +vlib/v/scanner/tests/shebang_outside_vsh_err.vv:3:1: error: a shebang is only valid at the top of the file + 1 | module foo + 2 | + 3 | #!/usr/bin/env -S v + | ^ + 4 | + 5 | import os diff --git a/vlib/v/scanner/tests/shebang_outside_vsh_err.vv b/vlib/v/scanner/tests/shebang_outside_vsh_err.vv new file mode 100644 index 00000000000000..56a44b6c236ee1 --- /dev/null +++ b/vlib/v/scanner/tests/shebang_outside_vsh_err.vv @@ -0,0 +1,5 @@ +module foo + +#!/usr/bin/env -S v + +import os