Skip to content

Report the current line of compiled frames - #2207

Open
BCSharp wants to merge 3 commits into
IronLanguages:mainfrom
BCSharp:f_lineno-func
Open

BCSharp wants to merge 3 commits into
IronLanguages:mainfrom
BCSharp:f_lineno-func

Conversation

@BCSharp

@BCSharp BCSharp commented Dec 4, 2026

Copy link
Copy Markdown
Member

This PR implements mechanism A from #2130, so it is only in effect for compiled regular code.
Interpreted code, async, and generators still do not update their line number. That's for another PR.

How it works: compiled code that pushes and pops its own frame on one thread (functions, class bodies, and module, exec and eval code) now publishes a pointer to its $lineNo local in its frame stack entry. f_lineno reads it only through an entry on the current thread's stack; when the entry is popped, or a traceback creates a frame for it, the frame saves the line number.

FramedCodeExpression is an IInstructionProvider, so the interpreter gets the push without the pointer: it passes the by-ref argument as a copy. Release builds now pop through PythonOps.PopFrame, which saves the line before removing the entry.

This can be further optimized, which I save for a separate commit in this PR.

@BCSharp

BCSharp commented Dec 5, 2026

Copy link
Copy Markdown
Member Author

I am marking this PR as ready for review, as the mechanism A is now complete. It doesn't mean that the line number will be correct in all circumstances for compiled functions (though it will be for warnings). The main gap is that it relies on $lineNo set in the executed code, and that happens only for statements that CanThrow. It misses return statements, which usually do not throw, so a line number after a function returns does not have the return line number, but the last CanThrow line. Because this is a separate issue, it will go into a separate PR.

@BCSharp
BCSharp requested a balanced review from Copilot December 5, 2026 04:02
@BCSharp
BCSharp marked this pull request as ready for review December 5, 2026 04:03

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

With adaptive compilation disabled, compiled eval still reports incorrect line numbers because the simple-body branch does not publish a line-number local.

1 open issue
What changed in this PR

Improves f_lineno reporting for compiled, non-suspending IronPython code, addressing incorrect line numbers used by warnings and stack inspection.

Changes:

  • Publishes live line numbers through thread-local frame entries.
  • Saves line numbers when frames are popped or created for tracebacks.
  • Adds regression tests in compiled and debug modes.
File Description
tests/​suite/​test_frame_lineno.py Tests current and retained frame line numbers.
tests/​suite/​test_frame_lineno_debug.py Runs frame tests in debug mode.
tests/​IronPython.Tests/​Cases/​IronPythonCasesManifest.ini Configures compilation modes for tests.
src/​core/​IronPython/​Runtime/​Operations/​PythonOps.cs Publishes and saves frame line numbers.
src/​core/​IronPython/​Runtime/​Exceptions/​TraceBack.cs Reads live or saved frame lines.
src/​core/​IronPython/​Modules/​sys.cs Preserves line pointers when creating frames.
src/​core/​IronPython/​Compiler/​Ast/​PythonAst.cs Configures module-frame line publication.
src/​core/​IronPython/​Compiler/​Ast/​Node.cs Separates compiled and interpreted frame handling.
src/​core/​IronPython/​Compiler/​Ast/​AstMethods.cs Registers updated frame helper methods.

Review effort: Balanced

@slozier slozier left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Looks good to me.

I spent some time chasing a performance regression (~30%) this causes on function calls on .NET Framework (because unfortunately I still care about that). It turns out using pointers in a SecurityTransparent assembly adds a lot of overhead. Anyway, my conclusion is that I'll be dropping [assembly: SecurityTransparent] from the IronPython assemblies (most likely a Silverlight leftover).

Small notes from Claude (take it or leave it):

  • I'd make publishLineNumber a required parameter of AddFrame. LineNumberExpression is shared, so a new call site in a nested lambda would silently capture the outer variable into a closure. Release builds would then publish a pointer into the heap.
  • For the interpreted follow-up: the line could come from the interpreted frame's current instruction and its debug info. Module-level code is always interpreted by default, so that's where module-level warnings still show line 1. Generators and async will need a heap-based line anyway.
  • Possible small follow-up: tb_frame.f_lineno is still 1 for frames built from DynamicStackFrames (functions already unwound), though the line is available there.
  • Copilot's eval thread seems low priority to me: eval is a single expression, so line 1 is almost always right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants