From b4f7263bcef14bd2de17fe04bc0f2b0f027ec0b1 Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Wed, 17 Jan 2024 03:21:10 +0100 Subject: [PATCH] make `Base.remove_linenums!` public (#52911) --- base/expr.jl | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/base/expr.jl b/base/expr.jl index 553f5a360dff1..ab4314fbd6cc8 100644 --- a/base/expr.jl +++ b/base/expr.jl @@ -998,27 +998,36 @@ function findmeta_block(exargs, argsmatch=args->true) return 0, [] end -remove_linenums!(ex) = ex -function remove_linenums!(ex::Expr) - if ex.head === :block || ex.head === :quote - # remove line number expressions from metadata (not argument literal or inert) position - filter!(ex.args) do x - isa(x, Expr) && x.head === :line && return false - isa(x, LineNumberNode) && return false - return true +""" + Base.remove_linenums!(ex) + +Remove all line-number metadata from expression-like object `ex`. +""" +function remove_linenums!(@nospecialize ex) + if ex isa Expr + if ex.head === :block || ex.head === :quote + # remove line number expressions from metadata (not argument literal or inert) position + filter!(ex.args) do x + isa(x, Expr) && x.head === :line && return false + isa(x, LineNumberNode) && return false + return true + end end + for subex in ex.args + subex isa Expr && remove_linenums!(subex) + end + return ex + elseif ex isa CodeInfo + ex.codelocs .= 0 + length(ex.linetable) > 1 && resize!(ex.linetable, 1) + return ex + else + return ex end - for subex in ex.args - subex isa Expr && remove_linenums!(subex) - end - return ex -end -function remove_linenums!(src::CodeInfo) - src.codelocs .= 0 - length(src.linetable) > 1 && resize!(src.linetable, 1) - return src end +public remove_linenums! + replace_linenums!(ex, ln::LineNumberNode) = ex function replace_linenums!(ex::Expr, ln::LineNumberNode) if ex.head === :block || ex.head === :quote