Skip to content

Commit

Permalink
scanner: improve unknown comptime var error (#22371)
Browse files Browse the repository at this point in the history
  • Loading branch information
StunxFS authored Oct 2, 2024
1 parent 5e00270 commit 8d1ba46
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
47 changes: 42 additions & 5 deletions vlib/v/scanner/scanner.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
module scanner

import os
import strings
import strconv
import v.token
import v.pref
Expand Down Expand Up @@ -933,12 +934,14 @@ pub fn (mut s Scanner) text_scan() token.Token {
return s.new_token(.at, '@' + name, name.len + 1)
}
if !token.is_key(name) {
mut at_error_msg := '@ must be used before keywords or compile time variables (e.g. `@type string` or `@FN`)'
// If name is all uppercase, the user is probably looking for a compile time variable ("at-token")
if name.is_upper() {
at_error_msg += '\nAvailable compile time variables:\n${token.valid_at_tokens}'
comptime_vars := token.valid_at_tokens.join(', ')
s.add_error_detail(wrap('available compile time variables: ${comptime_vars}',
width: 90
))
}
s.error(at_error_msg)
s.error('@ must be used before keywords or compile time variables (e.g. `@type string` or `@FN`)')
} else {
// s.note('@keyword is being deprecated and then removed from V. Use `keyword_` or a different name (e.g. `typ` instead of `type`)')
}
Expand Down Expand Up @@ -1741,13 +1744,13 @@ pub fn (mut s Scanner) add_error_detail(msg string) {
}

pub fn (mut s Scanner) add_error_detail_with_pos(msg string, pos token.Pos) {
s.add_error_detail(util.formatted_error('details:', msg, s.file_path, pos))
s.add_error_detail('\n' + util.formatted_error('details:', msg, s.file_path, pos))
}

fn (mut s Scanner) eat_details() string {
mut details := ''
if s.error_details.len > 0 {
details = '\n' + s.error_details.join('\n')
details = s.error_details.join('\n')
s.error_details = []
}
return details
Expand Down Expand Up @@ -1860,3 +1863,37 @@ pub fn new_silent_scanner() &Scanner {
pref: p
}
}

@[params]
struct WrapConfig {
pub:
width int = 80
end string = '\n'
}

// wrap wraps the given string within `width` in characters.
fn wrap(s string, config WrapConfig) string {
if config.width <= 0 {
return ''
}
words := s.fields()
if words.len == 0 {
return ''
}
mut sb := strings.new_builder(50)
sb.write_string(words[0])
mut space_left := config.width - words[0].len
for i in 1 .. words.len {
word := words[i]
if word.len + 1 > space_left {
sb.write_string(config.end)
sb.write_string(word)
space_left = config.width - word.len
} else {
sb.write_string(' ')
sb.write_string(word)
space_left -= 1 + word.len
}
}
return sb.str()
}
8 changes: 8 additions & 0 deletions vlib/v/scanner/tests/unknown_comptime_var_err.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
vlib/v/scanner/tests/unknown_comptime_var_err.vv:2:9: error: @ must be used before keywords or compile time variables (e.g. `@type string` or `@FN`)
1 | fn main() {
2 | _ = @DEF
| ^
3 | }
Details: available compile time variables: @VROOT, @VMODROOT, @VEXEROOT, @FN, @METHOD, @MOD,
@STRUCT, @VEXE, @FILE, @LINE, @COLUMN, @VHASH, @VCURRENTHASH, @VMOD_FILE, @VMODHASH,
@FILE_LINE, @LOCATION, @BUILD_DATE, @BUILD_TIME, @BUILD_TIMESTAMP
3 changes: 3 additions & 0 deletions vlib/v/scanner/tests/unknown_comptime_var_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
_ = @DEF
}

0 comments on commit 8d1ba46

Please sign in to comment.