From 42b657145dbc939eda98e6b5a0d66717eb7d4039 Mon Sep 17 00:00:00 2001
From: Jameson Nash <vtjnash@gmail.com>
Date: Fri, 8 Feb 2019 12:02:09 -0500
Subject: [PATCH] code_warntype: restore printing of Variables

also expose `optimize` flag, and set default to false

closes #29287
---
 base/show.jl                            |  2 +-
 doc/Makefile                            |  2 +-
 doc/src/manual/performance-tips.md      | 30 +++++++++----------------
 src/julia.h                             |  4 ++--
 stdlib/InteractiveUtils/src/codeview.jl | 17 +++++++++++---
 stdlib/Test/src/Test.jl                 |  7 +++++-
 test/reflection.jl                      | 14 ++++++------
 7 files changed, 42 insertions(+), 34 deletions(-)

diff --git a/base/show.jl b/base/show.jl
index 0335f34784597..e1afc333092e7 100644
--- a/base/show.jl
+++ b/base/show.jl
@@ -635,7 +635,7 @@ function sourceinfo_slotnames(src::CodeInfo)
     for i in eachindex(slotnames)
         name = string(slotnames[i])
         idx = get!(names, name, i)
-        if idx != i
+        if idx != i || isempty(name)
             printname = "$name@_$i"
             idx > 0 && (printnames[idx] = "$name@_$idx")
             names[name] = 0
diff --git a/doc/Makefile b/doc/Makefile
index 99e60ee665c1f..b10f59e74790f 100644
--- a/doc/Makefile
+++ b/doc/Makefile
@@ -6,7 +6,7 @@ default: html
 SRCDIR           := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
 JULIAHOME        := $(abspath $(SRCDIR)/..)
 include $(JULIAHOME)/Make.inc
-JULIA_EXECUTABLE := $(call spawn,$(build_bindir)/julia)
+JULIA_EXECUTABLE := $(call spawn,$(build_bindir)/julia) --startup-file=no
 
 .PHONY: help clean cleanall html pdf deps deploy
 
diff --git a/doc/src/manual/performance-tips.md b/doc/src/manual/performance-tips.md
index b5d0eec88c59b..71a5234115ca8 100644
--- a/doc/src/manual/performance-tips.md
+++ b/doc/src/manual/performance-tips.md
@@ -1362,29 +1362,21 @@ julia> @noinline pos(x) = x < 0 ? 0 : x;
 
 julia> function f(x)
            y = pos(x)
-           sin(y*x + 1)
+           return sin(y*x + 1)
        end;
 
 julia> @code_warntype f(3.2)
+Variables
+  #self#::Core.Compiler.Const(f, false)
+  x::Float64
+  y::Union{Float64, Int64}
+
 Body::Float64
-2 1 ─ %1  = invoke Main.pos(%%x::Float64)::UNION{FLOAT64, INT64}
-3 │   %2  = isa(%1, Float64)::Bool
-  └──       goto 3 if not %2
-  2 ─ %4  = π (%1, Float64)
-  │   %5  = Base.mul_float(%4, %%x)::Float64
-  └──       goto 6
-  3 ─ %7  = isa(%1, Int64)::Bool
-  └──       goto 5 if not %7
-  4 ─ %9  = π (%1, Int64)
-  │   %10 = Base.sitofp(Float64, %9)::Float64
-  │   %11 = Base.mul_float(%10, %%x)::Float64
-  └──       goto 6
-  5 ─       Base.error("fatal error in type inference (type bound)")
-  └──       unreachable
-  6 ┄ %15 = φ (2 => %5, 4 => %11)::Float64
-  │   %16 = Base.add_float(%15, 1.0)::Float64
-  │   %17 = invoke Main.sin(%16::Float64)::Float64
-  └──       return %17
+1 ─      (y = Main.pos(x))
+│   %2 = (y * x)::Float64
+│   %3 = (%2 + 1)::Float64
+│   %4 = Main.sin(%3)::Float64
+└──      return %4
 ```
 
 Interpreting the output of [`@code_warntype`](@ref), like that of its cousins [`@code_lowered`](@ref),
diff --git a/src/julia.h b/src/julia.h
index efa69675535db..39aa2530fec64 100644
--- a/src/julia.h
+++ b/src/julia.h
@@ -309,7 +309,7 @@ typedef struct _jl_method_t {
 } jl_method_t;
 
 // This type caches the data for a specType signature specialization of a Method
-typedef struct _jl_method_instance_t {
+struct _jl_method_instance_t {
     JL_DATA_TYPE
     union {
         jl_value_t *value; // generic accessor
@@ -332,7 +332,7 @@ typedef struct _jl_method_instance_t {
     // names of declarations in the JIT,
     // suitable for referencing in LLVM IR
     jl_llvm_functions_t functionObjectsDecls;
-} jl_method_instance_t;
+};
 
 // all values are callable as Functions
 typedef jl_value_t jl_function_t;
diff --git a/stdlib/InteractiveUtils/src/codeview.jl b/stdlib/InteractiveUtils/src/codeview.jl
index 3089e61e0f4f4..822533582acde 100644
--- a/stdlib/InteractiveUtils/src/codeview.jl
+++ b/stdlib/InteractiveUtils/src/codeview.jl
@@ -31,13 +31,24 @@ Keyword argument `debuginfo` may be one of `:source` or `:none` (default), to sp
 
 See [`@code_warntype`](@ref man-code-warntype) for more information.
 """
-function code_warntype(io::IO, @nospecialize(f), @nospecialize(t); debuginfo::Symbol=:default)
+function code_warntype(io::IO, @nospecialize(f), @nospecialize(t); debuginfo::Symbol=:default, optimize::Bool=false)
     debuginfo = Base.IRShow.debuginfo(debuginfo)
     lineprinter = Base.IRShow.__debuginfo[debuginfo]
-    for (src, rettype) in code_typed(f, t)
+    for (src, rettype) in code_typed(f, t, optimize=optimize)
         lambda_io::IOContext = io
         if src.slotnames !== nothing
-            lambda_io = IOContext(lambda_io, :SOURCE_SLOTNAMES => Base.sourceinfo_slotnames(src))
+            slotnames = Base.sourceinfo_slotnames(src)
+            lambda_io = IOContext(lambda_io, :SOURCE_SLOTNAMES => slotnames)
+            println(io, "Variables")
+            slottypes = src.slottypes
+            for i = 1:length(slotnames)
+                print(io, "  ", slotnames[i])
+                if isa(slottypes, Vector{Any})
+                    warntype_type_printer(io, slottypes[i], true)
+                end
+                println(io)
+            end
+            println(io)
         end
         print(io, "Body")
         warntype_type_printer(io, rettype, true)
diff --git a/stdlib/Test/src/Test.jl b/stdlib/Test/src/Test.jl
index 667c627718a08..9c3c2e0ba6403 100644
--- a/stdlib/Test/src/Test.jl
+++ b/stdlib/Test/src/Test.jl
@@ -1287,6 +1287,7 @@ end
 
 _args_and_call(args...; kwargs...) = (args[1:end-1], kwargs, args[end](args[1:end-1]...; kwargs...))
 _materialize_broadcasted(f, args...) = Broadcast.materialize(Broadcast.broadcasted(f, args...))
+
 """
     @inferred [AllowedType] f(x)
 
@@ -1309,8 +1310,12 @@ julia> typeof(f(2))
 Int64
 
 julia> @code_warntype f(2)
+Variables
+  #self#::Core.Compiler.Const(f, false)
+  a::Int64
+
 Body::UNION{FLOAT64, INT64}
-1 ─ %1 = Base.slt_int(1, a)::Bool
+1 ─ %1 = (a > 1)::Bool
 └──      goto #3 if not %1
 2 ─      return 1
 3 ─      return 1.0
diff --git a/test/reflection.jl b/test/reflection.jl
index 6739a07d61878..1494b2528fc7a 100644
--- a/test/reflection.jl
+++ b/test/reflection.jl
@@ -332,13 +332,13 @@ function g15714(array_var15714)
         array_var15714[index_var15714] += 0
     end
     let index_var15714
-        for index_var15714 in eachindex(array_var15714)
+        for outer index_var15714 in eachindex(array_var15714)
             array_var15714[index_var15714] += 0
         end
         index_var15714
     end
     let index_var15714
-        for index_var15714 in eachindex(array_var15714)
+        for outer index_var15714 in eachindex(array_var15714)
             array_var15714[index_var15714] += 0
         end
         index_var15714
@@ -350,11 +350,11 @@ import InteractiveUtils.code_warntype
 used_dup_var_tested15714 = false
 used_unique_var_tested15714 = false
 function test_typed_ast_printing(Base.@nospecialize(f), Base.@nospecialize(types), must_used_vars)
-    src, rettype = code_typed(f, types)[1]
+    src, rettype = code_typed(f, types, optimize=false)[1]
     dupnames = Set()
     slotnames = Set()
     for name in src.slotnames
-        if name in slotnames
+        if name in slotnames || name === Symbol("")
             push!(dupnames, name)
         else
             push!(slotnames, name)
@@ -368,7 +368,7 @@ function test_typed_ast_printing(Base.@nospecialize(f), Base.@nospecialize(types
     for sym in must_used_vars
         must_used_checked[sym] = false
     end
-    for str in (sprint(code_warntype, f, types),
+    for str in (sprint(io -> code_warntype(io, f, types, optimize=false)),
                 repr("text/plain", src))
         for var in must_used_vars
             @test occursin(string(var), str)
@@ -410,9 +410,9 @@ function test_typed_ast_printing(Base.@nospecialize(f), Base.@nospecialize(types
     end
 end
 test_typed_ast_printing(f15714, Tuple{Vector{Float32}},
-                        [:array_var15714])
+                        [:array_var15714,  :index_var15714])
 test_typed_ast_printing(g15714, Tuple{Vector{Float32}},
-                        [:array_var15714])
+                        [:array_var15714,  :index_var15714])
 #This test doesn't work with the new optimizer because we drop slotnames
 #We may want to test it against debug info eventually
 #@test used_dup_var_tested15715