# Understanding weird @code\_warntype warning

**URL:** <https://discourse.julialang.org/t/understanding-weird-code-warntype-warning/71488>\
**Category:** Performance\
**Tags:** question, code\_warntype, type-stability\
**Created:** [November 15, 2021, 11:27am UTC](https://discourse.julialang.org/t/understanding-weird-code-warntype-warning/71488 "2021-11-15T11:27:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)\
**Post date:** [November 15, 2021, 11:27am UTC](https://discourse.julialang.org/t/understanding-weird-code-warntype-warning/71488/1 "2021-11-15T11:27:25Z")

</div>

While testing one of my codes for type instability, I stumbled upon what feels like a strange behavior, emphasized by the example below.

```julia
julia> test(filepath::String) = @assert ispath(filepath) "File path $filepath is not valid, please verify you input"
test (generic function with 1 method)

julia> test("toto.dat")
ERROR: AssertionError: File path toto.dat is not valid, please verify you input
Stacktrace:
 [1] test(filepath::String)
   @ Main .\REPL[1]:1
 [2] top-level scope
   @ REPL[2]:1

julia> @code_warntype test("toto.dat")
MethodInstance for test(::String)
  from test(filepath::String) in Main at REPL[1]:1
Arguments
  #self#::Core.Const(test)
  filepath::String
Body::Nothing
1 ─ %1 = Main.ispath(filepath)::Bool
└── goto #3 if not %1
2 ─ return nothing
3 ─ %4 = Main.Base::Core.Const(Base)
│ %5 = Base.getproperty(%4, :string)::Any
│ %6 = Base.string("File path ", filepath, " is not valid, please verify you input")::Any
│ %7 = (%5)(%6)::Any
│ %8 = Base.AssertionError(%7)::Any
│ Base.throw(%8)
└── Core.Const(:(return %9))

```

Basically, I did not expect `@assert` to result in unstable type since among other things `AssertionError` is a subtype of `Exception` while `@code_warntype` returns an `Any`.

Can somebody clarify this behavior ?

My configuration:

> Julia Version 1.7.0-rc2  
> Commit f23fc0d27a (2021-10-20 12:45 UTC)  
> Platform Info:  
> OS: Windows (x86\_64-w64-mingw32)  
> CPU: 11th Gen Intel(R) Core™ i7-1185G7 @ 3.00GHz  
> WORD\_SIZE: 64  
> LIBM: libopenlibm  
> LLVM: libLLVM-12.0.1 (ORCJIT, tigerlake)  
> Environment:  
> JULIA\_EDITOR = code  
> JULIA\_NUM\_THREADS = 6

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [November 15, 2021, 3:38pm UTC](https://discourse.julialang.org/t/understanding-weird-code-warntype-warning/71488/2 "2021-11-15T15:38:29Z")

</div>

My understanding is that exceptions only impact performance when they are triggered. Thus the `Any` you see is not bad. But you should test this with a suitable setup.

---

<div class="post-metadata">

**Author:** ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)\
**Post date:** [November 15, 2021, 4:12pm UTC](https://discourse.julialang.org/t/understanding-weird-code-warntype-warning/71488/3 "2021-11-15T16:12:04Z")

</div>

Excuse my lack of understanding, but what do you mean by _test this with a suitable setup_ ?

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [November 15, 2021, 4:21pm UTC](https://discourse.julialang.org/t/understanding-weird-code-warntype-warning/71488/4 "2021-11-15T16:21:39Z")

</div>

Test whether you see bad performance on the code-path which does not hit the exception.

```julia
test_assert(filepath::String) = @assert ispath(filepath) "File path $filepath is not valid, please verify you input"
function test_noassert(filepath::String)
if ispath(filepath)
return 1
else
return 2
end
end

```

then test with `@btime` from benchmarktools.jl whether the two functions are equally fast when the file exists. This is indeed the case. Thus the `Any` does not impact performance of the no-throw code-path.

---

<div class="post-metadata">

**Author:** ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)\
**Post date:** [November 15, 2021, 4:27pm UTC](https://discourse.julialang.org/t/understanding-weird-code-warntype-warning/71488/5 "2021-11-15T16:27:54Z")

</div>

I understand now, thanks for the explanation. However, do you think the `Any` results from a design choice ? I always thought that this was the last resort in `code_warntype`, hence my surprise seeing this for a basic type as the `AssertionError`.

---

<div class="post-metadata">

**Author:** ![mmiller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mmiller/32/20805_2.png) [@mmiller](https://discourse.julialang.org/u/mmiller)\
**Post date:** [November 15, 2021, 6:21pm UTC](https://discourse.julialang.org/t/understanding-weird-code-warntype-warning/71488/6 "2021-11-15T18:21:49Z")

</div>

The code in your example is actually returning `Nothing`, as shown by this line:

> [@BambOoxX](#):
>
> `Body::Nothing`

We can see why this happens by using the macro `@macro_expand`:

```Julia
julia> @macroexpand @assert ispath(filepath)
:(if ispath(filepath)
      nothing
  else
      Base.throw(Base.AssertionError("ispath(filepath)"))
  end)

```

which shows us that if the condition passed to `@assert` returns `true`, then the macro returns `nothing`.

We can see this in the `@code_warntype` output:

```julia
1 ─ %1 = Main.ispath(filepath)::Bool
└── goto #3 if not %1
2 ─ return nothing
3 ─ %4 = Main.Base::Core.Const(Base)
...

```

If the line indicated by `%1` is false, then the code goes to the section of code indicated by `3`, which is the exception being raised. Otherwise, `nothing` is returned.

Therefore the function is type stable - it either returns `nothing` or errors.

I’m not sure why `Base.AssertionError` has the type of `Any`, but from a performance point of view this doesn’t matter, as this instability is only ever encountered when raising the exception.

---

<div class="post-metadata">

**Author:** ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)\
**Post date:** [November 15, 2021, 6:24pm UTC](https://discourse.julialang.org/t/understanding-weird-code-warntype-warning/71488/7 "2021-11-15T18:24:27Z")

</div>

Thanks for the additional information !
