Skip to content

Commit 4446434

Browse files
authored
v.eval: lookup constants in builtin, when they are not found in the current module; add test (#23745)
1 parent 428b328 commit 4446434

4 files changed

Lines changed: 28 additions & 2 deletions

File tree

‎vlib/v/eval/eval.v‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ pub fn (mut e Eval) eval(mut files []&ast.File) {
8686

8787
// first arg is receiver (if method)
8888
pub fn (mut e Eval) run_func(func ast.FnDecl, _args ...Object) {
89-
e.back_trace << EvalTrace{func.idx, func.source_file.idx, func.pos.line_nr}
89+
e.back_trace << EvalTrace{func.idx, if unsafe { func.source_file != 0 } {
90+
func.source_file.idx
91+
} else {
92+
0
93+
}, func.pos.line_nr}
9094
old_mod := e.cur_mod
9195
old_file := e.cur_file
9296
e.cur_mod = func.mod

‎vlib/v/eval/expr.c.v‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,18 @@ pub fn (mut e Eval) expr(expr ast.Expr, expecting ast.Type) Object {
222222
return e.local_vars[expr.name].val
223223
}
224224
.constant {
225+
key := expr.name.all_after_last('.')
225226
return if expr.name.contains('.') {
226227
e.mods[expr.name.all_before_last('.')]
227228
} else {
228229
e.mods[e.cur_mod]
229-
}[expr.name.all_after_last('.')] or { ast.EmptyStmt{} } as Object
230+
}[key] or {
231+
if builtin_constant := e.mods['builtin'][key] {
232+
builtin_constant
233+
} else {
234+
e.error('unknown constant `${key}`')
235+
}
236+
} as Object
230237
}
231238
else {
232239
e.error('unknown ident kind for `${expr.name}`: ${expr.kind}')
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-2147483648
2+
2147483647
3+
2147483647
4+
-2147483648
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const local_max_int = int(2147483647)
2+
const local_min_int = int(-2147483648)
3+
const i64_min_int32 = i64(local_min_int + 1) - 1
4+
const i64_max_int32 = i64(local_max_int - 1) + 1
5+
const abc = i64(max_int)
6+
const def = i64(min_int)
7+
8+
println(i64_min_int32)
9+
println(i64_max_int32)
10+
println(abc)
11+
println(def)

0 commit comments

Comments
 (0)