Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lowering: track location of macros better for stackwalk #44995

Merged
merged 1 commit into from
May 25, 2023

Conversation

vtjnash
Copy link
Member

@vtjnash vtjnash commented Apr 15, 2022

Noticed since scrub_backtrace is looking for a macro expansion when
printing an exception thrown in @test, but that was missing for simple
expressions, particularly those at the top-level or spliced in.

Fix #44757

@vtjnash vtjnash requested a review from JeffBezanson April 15, 2022 18:45
@KristofferC
Copy link
Member

Does this have any relation to #44551?

@vtjnash
Copy link
Member Author

vtjnash commented Apr 15, 2022

No, I would need to change this to (push_loc f "macro expansion"), like we do for generated functions, to handle that case

src/ast.c Outdated Show resolved Hide resolved
@JeffBezanson
Copy link
Member

I would rather try to handle this elsewhere. e.g. wrapping top-level expressions in blocks with line numbers in the parser, instead of putting it inside the macro expansion code.

@giordano giordano added the compiler:lowering Syntax lowering (compiler front end, 2nd stage) label Oct 27, 2022
@vtjnash vtjnash force-pushed the jn/macro-stack-scrub branch from b1d0e91 to 8f6c198 Compare November 16, 2022 05:37
@vtjnash vtjnash added the needs pkgeval Tests for all registered packages should be run with this change label Nov 16, 2022
@timholy
Copy link
Member

timholy commented Mar 16, 2023

Given that this came up in discussions of #40537, what's the status here? Was #44995 (comment) addressed and it's just waiting for more review?

@vtjnash
Copy link
Member Author

vtjnash commented Mar 16, 2023

I didn't think that would be possible to address, without notable breakage, as it generally is a proposal to change the parsing of any macro call @foo to Expr(:begin, Expr(:line, ...), Expr(:macrocall, :@foo)) from Expr(:macrocall, :@foo, Expr(:line))

The reason is that many expressions will get to the runtime with the form @eval @test foo. So since @test was not initially appearing inside a block with outer source location (the only source location is the macro, and the implied node that we forward as the __source__ argument), thus we currently expect that it will expand to an expression which does not have source location. We can attempt to enforce either on the macroexpansion that it attempt to preserve the __source__ location argument of many macrocalls, or on the parser, that it tries to tag everything with a source location. But right now, our parser (somewhat intentionally) is very bad at annotating locations of simple independent expressions, and we are not particularly reliable about handing that information around unaided for those cases, as can be seen from Meta.@lower which is missing the REPL[1]:1 line info.

julia> Meta.@lower @test x
:($(Expr(:thunk, CodeInfo(
     @ /data/vtjnash/julia/usr/share/julia/stdlib/v1.10/Test/src/Test.jl:474 within `top-level scope`

Usually, backtraces are okay because the function containing the statements has most of the nearby line numbers marked in the AST nearby, so it does pretty well at getting them right if they are embedded into any sort of block. So this PR tries to add a block in places where we didn't have one.

@vtjnash vtjnash force-pushed the jn/macro-stack-scrub branch 2 times, most recently from c9d6087 to ea9fde8 Compare May 20, 2023 00:50
@vtjnash
Copy link
Member Author

vtjnash commented May 20, 2023

This is now my 4th attempt at this, haha. The main difference is that it is now a little better integrated as part of parsing / lowering and I think that should be a bit more reliable. I think this should happen to also be compatible with #6910, which is a significant advantage too. I would include more bits of that since I have them, but plan to do that in a later PR instead, once I have time to update that content.

@nanosoldier runtests()

@nanosoldier
Copy link
Collaborator

Your package evaluation job has completed - possible new issues were detected.
A full report can be found here.

@vtjnash
Copy link
Member Author

vtjnash commented May 20, 2023

Some notable issues:

  • FastClosures.jl needs us to allow let esc(x)=esc(x) to work, which seems implemented wrong in macroexpansion.scm right now anyways, but we need to not break whatever it is doing now
  • FLoops.jl wants to call deepcopy on Expr (for NLLSsolver.jl), even though that is pretty thoroughly undefined behavior. Hopefully a straight copy there would work better (e.g. correctly)?
  • JET.jl does not handle hygiene correctly (https://s3.amazonaws.com/julialang-reports/nanosoldier/pkgeval/by_hash/ea9fde8_vs_6d70d2a/JET.primary.log), same for JSExpr.jl, MLStyle.jl, and likely quite a few others
  • WatchJuliaBurn.jl seems to be toast, which seems like a possible bug
  • TryCatch.jl seems to also not getting error handling correct after this, which seems like a possible bug

I think the best solution here will be to make a slow deprecation cycle here, so that people can fix their hygiene issues, then opt into having line info correct (there are a lot of tests that rely on macros not being able to track line numbers because they test explicitly that parsing and evaluating a macro from different lines gives egal results). So we can add a legacy=true keyword to the macroexpand function to preserve the old behavior for a bit. Before the next LTS, we can flip the default to legacy=false, then in a later LTS we can drop that flag.

@vtjnash
Copy link
Member Author

vtjnash commented May 22, 2023

Oh sigh. TryCatch.jl and WatchJuliaBurn.jl fail because I stopped having @test strip line number information, and that breaks lowering. Lowering assumes that the last expression is the return value of a block, but that assumption is invalid if the last expression happens to be a metadata node (such as a line number), as happens to be explicitly put into the wrong place in the AST by both of those packages.

This is to preserve the line number. But we make slightly more changes
than strictly necessary to prepare for future improvements in this area.
@vtjnash vtjnash force-pushed the jn/macro-stack-scrub branch from ea9fde8 to ad5d6d5 Compare May 24, 2023 18:10
@vtjnash
Copy link
Member Author

vtjnash commented May 24, 2023

This now gives up entirely on fixing macroexpand for now, but just keeps the patch for lowering, so that the uses with @test 0//0 works.

@vtjnash vtjnash removed the needs pkgeval Tests for all registered packages should be run with this change label May 24, 2023
@vtjnash vtjnash merged commit 01ddf80 into master May 25, 2023
@vtjnash vtjnash deleted the jn/macro-stack-scrub branch May 25, 2023 13:52
@timholy
Copy link
Member

timholy commented May 25, 2023

Thanks, @vtjnash! Looking forward to trying this out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler:lowering Syntax lowering (compiler front end, 2nd stage)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants