# Base.getfield() gives warntype?

**URL:** https://discourse.julialang.org/t/base-getfield-gives-warntype/31845
**Category:** New to Julia
**Created:** [December 4, 2019, 10:55am UTC](https://discourse.julialang.org/t/base-getfield-gives-warntype/31845 "2019-12-04T10:55:51Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)
#### Post date: [December 4, 2019, 10:55am UTC](https://discourse.julialang.org/t/base-getfield-gives-warntype/31845/1 "2019-12-04T10:55:52Z")

</div>

I have a quite complicated parametric type that contains a simple `Int64` field like:

```julia
struct A{T, U, V, W, X, Y}
    n::Int64
    ... (a lot of other possibly parametric-typed fields)
end

a = A(3, .....)

```

when I try to do `@code_warntype a.n`, I got a warntype like:

```julia
Body::Any
1 ─ %1 = Base.getfield(x, f)::Any
└── return %1

```

I got this situation in `v1.1.1` many times. Going to `v1.3.0` seems better, but now it comes again. Is it a bug? Thanks.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [December 4, 2019, 11:22am UTC](https://discourse.julialang.org/t/base-getfield-gives-warntype/31845/2 "2019-12-04T11:22:35Z")

</div>

Please include a self-contained MWE.

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [December 4, 2019, 1:15pm UTC](https://discourse.julialang.org/t/base-getfield-gives-warntype/31845/3 "2019-12-04T13:15:46Z")

</div>

No, it is just because `@code_warntype` does not do constant propagation for your input.

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [December 4, 2019, 4:04pm UTC](https://discourse.julialang.org/t/base-getfield-gives-warntype/31845/4 "2019-12-04T16:04:44Z")

</div>

You’ll see that if you wrap the access in a function (which it would be as long as you’re not operating at global scope), then the `:x` is correctly constant-propagated and everything is fine:

```julia
julia> struct Foo
         x::Int
         y::String
       end

julia> function get_x(f)
         f.x
       end
get_x (generic function with 1 method)

julia> f = Foo(1, "hello")
Foo(1, "hello")

julia> @code_warntype get_x(f)
Variables
  #self#::Core.Compiler.Const(get_x, false)
  f::Foo

Body::Int64
1 ─ %1 = Base.getproperty(f, :x)::Int64
└── return %1

```

---

<div class="post-metadata">

### Author: ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)
#### Post date: [December 5, 2019, 3:13am UTC](https://discourse.julialang.org/t/base-getfield-gives-warntype/31845/5 "2019-12-05T03:13:58Z")

</div>

I want to include a MWE… but the case is just too complicated (needs a lot of custom types)… I could **not** reproduce the case in a simpler example…

In essence, the situation is like:

```julia
struct A
	npar::Int64
end

const a = A(3)

function f(x::A)
	npar = x.npar
	function g()
		y = 0
		for i in 1:npar
			y += i
		end
		return y
	end
	@code_warntype g()
end

julia> f(a)
Variables
  #self#::var"#g#3"{Int64}
  v::Array{Int64,1}
  @_3::Union{Nothing, Tuple{Int64,Int64}}
  t::Int64

Body::Array{Int64,1}
1 ─ %1 = Core.apply_type(Main.Vector, Main.Int64)::Core.Compiler.Const(Array{Int64,1}, false)
│ %2 = Core.getfield(#self#, :npar)::Int64
...

```

in **this** case, everything is fine (that’s why I said I failed to reproduce the problem in a simpler case).

However, in the **actual** situation, I got the following:

```julia
Variables
 ...
  npar::Union{}

Body::Array{Float64,1}
1 ── Core.NewvarNode(:(threadsfor_fun))
│ Core.NewvarNode(:(grads))
...
│ %18 = Core.getfield(#self#, :npar)::Core.Box
...
8 ┄─ %24 = Core.getfield(%18, :contents)::Any
...

```

now, `Core.getfield(#self#, :npar)` has type `Core.Box` rather than `Int64`.  
Another noticable difference is that `npar` is a variable (with type `Union`) in the actual case, but it’s **not** a variable in the simpler case.

any idea why it happens? thanks.

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [December 5, 2019, 3:26am UTC](https://discourse.julialang.org/t/base-getfield-gives-warntype/31845/6 "2019-12-05T03:26:48Z")

</div>

Probably [performance of captured variables in closures · Issue #15276 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/15276)
