From a55f62c86091d32d7bef918306fd71fa80d92d17 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Fri, 22 Aug 2025 17:08:18 +0800 Subject: [PATCH] set `def_line` for function --- scripts/98_profiler.py | 10 ++++++++++ src/compiler/compiler.c | 7 ++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/scripts/98_profiler.py b/scripts/98_profiler.py index af7c1d4a..d0554db0 100644 --- a/scripts/98_profiler.py +++ b/scripts/98_profiler.py @@ -9,3 +9,13 @@ y = 2 costly_func(2) # 3s time.sleep(1) # 1s + +def new_func(a, b, c): + x = a + b + x = x + c + return a + +def new_func2(a, b, c): + x = a + b + x = x + c + return a diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index a1e8eba2..d1c3b9d0 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -2416,6 +2416,7 @@ static Error* consume_pep695_py312(Compiler* self) { static Error* compile_function(Compiler* self, int decorators) { Error* err; + int def_line = prev()->line; consume(TK_ID); c11_sv decl_name_sv = Token__sv(prev()); int decl_index; @@ -2445,7 +2446,7 @@ static Error* compile_function(Compiler* self, int decorators) { } } - Ctx__emit_(ctx(), OP_LOAD_FUNCTION, decl_index, prev()->line); + Ctx__emit_(ctx(), OP_LOAD_FUNCTION, decl_index, def_line); Ctx__s_emit_decorators(ctx(), decorators); py_Name decl_name = py_namev(decl_name_sv); @@ -2458,9 +2459,9 @@ static Error* compile_function(Compiler* self, int decorators) { } } - Ctx__emit_(ctx(), OP_STORE_CLASS_ATTR, Ctx__add_name(ctx(), decl_name), prev()->line); + Ctx__emit_(ctx(), OP_STORE_CLASS_ATTR, Ctx__add_name(ctx(), decl_name), def_line); } else { - NameExpr* e = NameExpr__new(prev()->line, decl_name, name_scope(self)); + NameExpr* e = NameExpr__new(def_line, decl_name, name_scope(self)); vtemit_store((Expr*)e, ctx()); vtdelete((Expr*)e); }