manual_experimental_strictnotnil.html 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <!-- This file is generated by Nim. -->
  4. <html xmlns="https://www.w3.org/1999/xhtml" xml:lang="en" lang="en" data-theme="auto">
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. <title>doc/manual_experimental_strictnotnil</title>
  9. <!-- Google fonts -->
  10. <link href='https://fonts.googleapis.com/css?family=Lato:400,600,900' rel='stylesheet' type='text/css'/>
  11. <link href='https://fonts.googleapis.com/css?family=Source+Code+Pro:400,500,600' rel='stylesheet' type='text/css'/>
  12. <!-- Favicon -->
  13. <link rel="shortcut icon" href="data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAUAAAAF////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAIAAABbAAAAlQAAAKIAAACbAAAAmwAAAKIAAACVAAAAWwAAAAL///8A////AP///wD///8A////AAAAABQAAADAAAAAYwAAAA3///8A////AP///wD///8AAAAADQAAAGMAAADAAAAAFP///wD///8A////AP///wAAAACdAAAAOv///wD///8A////AP///wD///8A////AP///wD///8AAAAAOgAAAJ3///8A////AP///wAAAAAnAAAAcP///wAAAAAoAAAASv///wD///8A////AP///wAAAABKAAAAKP///wAAAABwAAAAJ////wD///8AAAAAgQAAABwAAACIAAAAkAAAAJMAAACtAAAAFQAAABUAAACtAAAAkwAAAJAAAACIAAAAHAAAAIH///8A////AAAAAKQAAACrAAAAaP///wD///8AAAAARQAAANIAAADSAAAARf///wD///8AAAAAaAAAAKsAAACk////AAAAADMAAACcAAAAnQAAABj///8A////AP///wAAAAAYAAAAGP///wD///8A////AAAAABgAAACdAAAAnAAAADMAAAB1AAAAwwAAAP8AAADpAAAAsQAAAE4AAAAb////AP///wAAAAAbAAAATgAAALEAAADpAAAA/wAAAMMAAAB1AAAAtwAAAOkAAAD/AAAA/wAAAP8AAADvAAAA3gAAAN4AAADeAAAA3gAAAO8AAAD/AAAA/wAAAP8AAADpAAAAtwAAAGUAAAA/AAAA3wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAADfAAAAPwAAAGX///8A////AAAAAEgAAADtAAAAvwAAAL0AAADGAAAA7wAAAO8AAADGAAAAvQAAAL8AAADtAAAASP///wD///8A////AP///wD///8AAAAAO////wD///8A////AAAAAIcAAACH////AP///wD///8AAAAAO////wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A//8AAP//AAD4HwAA7/cAAN/7AAD//wAAoYUAAJ55AACf+QAAh+EAAAAAAADAAwAA4AcAAP5/AAD//wAA//8AAA=="/>
  14. <link rel="icon" type="image/png" sizes="32x32" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAA3XAAAN1wFCKJt4AAAAB3RJTUUH4QQQEwksSS9ZWwAAAk1JREFUWMPtll2ITVEUx39nn/O7Y5qR8f05wtCUUr6ZIS++8pEnkZInPImneaCQ5METNdOkeFBKUhMPRIkHKfEuUZSUlGlKPN2TrgfncpvmnntnmlEyq1Z7t89/rf9a6+y99oZxGZf/XeIq61EdtgKXgdXA0xrYAvBjOIF1AI9zvjcC74BSpndrJPkBWDScTF8Aa4E3wDlgHbASaANmVqlcCnwHvgDvgVfAJ+AikAAvgfVZwLnSVZHZaOuKoQi3ZOMi4NkYkpe1p4J7A8BpYAD49hfIy/oqG0+hLomiKP2L5L+1ubn5115S+3OAn4EnwBlgMzCjyt6ZAnQCJ4A7wOs88iRJHvw50HoujuPBoCKwHWiosy8MdfZnAdcHk8dxXFJ3VQbQlCTJvRBCGdRbD4M6uc5glpY3eAihpN5S5w12diSEcCCEcKUO4ljdr15T76ur1FDDLIQQ3qv71EdDOe3Kxj3leRXyk+pxdWnFWod6Wt2bY3de3aSuUHcPBVimHs7mK9WrmeOF6lR1o9qnzskh2ar2qm1qizpfXaPeVGdlmGN5pb09qMxz1Xb1kLqgzn1RyH7JUXW52lr5e/Kqi9qpto7V1atuUzfnARrV7jEib1T76gG2qxdGmXyiekkt1GswPTtek0aBfJp6YySGBfWg2tPQ0FAYgf1stUfdmdcjarbYJEniKIq6gY/Aw+zWHAC+p2labGpqiorFYgGYCEzN7oQdQClN07O1/EfDyGgC0ALMBdYAi4FyK+4H3gLPsxfR1zRNi+NP7nH5J+QntnXe5B5mpfQAAAAASUVORK5CYII=">
  15. <!-- CSS -->
  16. <link rel="stylesheet" type="text/css" href="nimdoc.out.css?v=2.3.1">
  17. <!-- JS -->
  18. <script type="text/javascript" src="dochack.js?v=2.3.1"></script>
  19. </head>
  20. <body>
  21. <div class="document" id="documentId">
  22. <div class="container">
  23. <h1 class="title">doc/manual_experimental_strictnotnil</h1>
  24. <h1 id="strict-not-nil-checking">Strict not nil checking</h1><p><strong>Note:</strong> This feature is experimental, you need to enable it with</p>
  25. <p><pre class="listing"><span class="Punctuation">{</span><span class="Operator">.</span><span class="Identifier">experimental</span><span class="Punctuation">:</span> <span class="StringLit">&quot;strictNotNil&quot;</span><span class="Operator">.</span><span class="Punctuation">}</span></pre></p>
  26. <p>or</p>
  27. <p><pre class="listing"><span class="program">nim</span> <span class="option">c</span> <span class="option">--experimental:strictNotNil</span> <span class="option">&lt;program&gt;</span></pre></p>
  28. <p>In the second case it would check builtin and imported modules as well.</p>
  29. <p>It checks the nilability of ref-like types and makes dereferencing safer based on flow typing and <tt class="docutils literal"><span class="pre"><span class="Keyword">not</span> <span class="Keyword">nil</span></span></tt> annotations.</p>
  30. <p>Its implementation is different than the <tt class="docutils literal"><span class="pre"><span class="Identifier">notnil</span></span></tt> one: defined under <tt class="docutils literal"><span class="pre"><span class="Identifier">strictNotNil</span></span></tt>. Keep in mind the difference in option names, be careful with distinguishing them.</p>
  31. <p>We check several kinds of types for nilability:</p>
  32. <ul class="simple"><li>ref types</li>
  33. <li>pointer types</li>
  34. <li>proc types</li>
  35. <li>cstrings</li>
  36. </ul>
  37. <h2 id="nil">nil</h2><p>The default kind of nilability types is the nilable kind: they can have the value <tt class="docutils literal"><span class="pre"><span class="Keyword">nil</span></span></tt>. If you have a non-nilable type <tt class="docutils literal"><span class="pre"><span class="Identifier">T</span></span></tt>, you can use <tt class="docutils literal"><span class="pre"><span class="Identifier">T</span> <span class="Keyword">nil</span></span></tt> to get a nilable type for it.</p>
  38. <h2 id="not-nil">not nil</h2><p>You can annotate a type where nil isn't a valid value with <tt class="docutils literal"><span class="pre"><span class="Keyword">not</span> <span class="Keyword">nil</span></span></tt>.</p>
  39. <p><pre class="listing"> <span class="Keyword">type</span>
  40. <span class="Identifier">NilableObject</span> <span class="Operator">=</span> <span class="Keyword">ref</span> <span class="Keyword">object</span>
  41. <span class="Identifier">a</span><span class="Punctuation">:</span> <span class="Identifier">int</span>
  42. <span class="Keyword">Object</span> <span class="Operator">=</span> <span class="Identifier">NilableObject</span> <span class="Keyword">not</span> <span class="Keyword">nil</span>
  43. <span class="Keyword">Proc</span> <span class="Operator">=</span> <span class="Punctuation">(</span><span class="Keyword">proc</span> <span class="Punctuation">(</span><span class="Identifier">x</span><span class="Punctuation">,</span> <span class="Identifier">y</span><span class="Punctuation">:</span> <span class="Identifier">int</span><span class="Punctuation">)</span><span class="Punctuation">)</span>
  44. <span class="Keyword">proc</span> <span class="Identifier">p</span><span class="Punctuation">(</span><span class="Identifier">x</span><span class="Punctuation">:</span> <span class="Keyword">Object</span><span class="Punctuation">)</span> <span class="Operator">=</span>
  45. <span class="Identifier">echo</span> <span class="Identifier">x</span><span class="Operator">.</span><span class="Identifier">a</span> <span class="Comment"># ensured to dereference without an error</span>
  46. <span class="Comment"># compiler catches this:</span>
  47. <span class="Identifier">p</span><span class="Punctuation">(</span><span class="Keyword">nil</span><span class="Punctuation">)</span>
  48. <span class="Comment"># and also this:</span>
  49. <span class="Keyword">var</span> <span class="Identifier">x</span><span class="Punctuation">:</span> <span class="Identifier">NilableObject</span>
  50. <span class="Keyword">if</span> <span class="Identifier">x</span><span class="Operator">.</span><span class="Identifier">isNil</span><span class="Punctuation">:</span>
  51. <span class="Identifier">p</span><span class="Punctuation">(</span><span class="Identifier">x</span><span class="Punctuation">)</span>
  52. <span class="Keyword">else</span><span class="Punctuation">:</span>
  53. <span class="Identifier">p</span><span class="Punctuation">(</span><span class="Identifier">x</span><span class="Punctuation">)</span> <span class="Comment"># ok</span></pre></p>
  54. <p>If a type can include <tt class="docutils literal"><span class="pre"><span class="Keyword">nil</span></span></tt> as a valid value, dereferencing values of the type is checked by the compiler: if a value which might be nil is dereferenced, this produces a warning by default, you can turn this into an error using the compiler options <tt class="docutils literal"><span class="pre option">--warningAsError:strictNotNil</span></tt>.</p>
  55. <p>If a type is nilable, you should dereference its values only after a <tt class="docutils literal"><span class="pre"><span class="Identifier">isNil</span></span></tt> or equivalent check.</p>
  56. <h2 id="local-turn-onslashoff">local turn on/off</h2><p>You can still turn off nil checking on function/module level by using a <tt class="docutils literal"><span class="pre"><span class="Punctuation">{</span><span class="Operator">.</span><span class="Identifier">strictNotNil</span><span class="Punctuation">:</span> <span class="Identifier">off</span><span class="Operator">.</span><span class="Punctuation">}</span></span></tt> pragma.</p>
  57. <h2 id="nilability-state">nilability state</h2><p>Currently, a nilable value can be <tt class="docutils literal"><span class="pre"><span class="Identifier">Safe</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Identifier">MaybeNil</span></span></tt> or <tt class="docutils literal"><span class="pre"><span class="Keyword">Nil</span></span></tt> : we use internally <tt class="docutils literal"><span class="pre"><span class="Identifier">Parent</span></span></tt> and <tt class="docutils literal"><span class="pre"><span class="Identifier">Unreachable</span></span></tt> but this is an implementation detail(a parent layer has the actual nilability).</p>
  58. <ul class="simple"><li><tt class="docutils literal"><span class="pre"><span class="Identifier">Safe</span></span></tt> means it shouldn't be nil at that point: e.g. after assignment to a non-nil value or <tt class="docutils literal"><span class="pre"><span class="Keyword">not</span> <span class="Identifier">a</span><span class="Operator">.</span><span class="Identifier">isNil</span></span></tt> check</li>
  59. <li><tt class="docutils literal"><span class="pre"><span class="Identifier">MaybeNil</span></span></tt> means it might be nil, but it might not be nil: e.g. an argument, a call argument or a value after an <tt class="docutils literal"><span class="pre"><span class="Keyword">if</span></span></tt> and <tt class="docutils literal"><span class="pre"><span class="Keyword">else</span></span></tt>.</li>
  60. <li><tt class="docutils literal"><span class="pre"><span class="Keyword">Nil</span></span></tt> means it should be nil at that point; e.g. after an assignment to <tt class="docutils literal"><span class="pre"><span class="Keyword">nil</span></span></tt> or a <tt class="docutils literal"><span class="pre"><span class="Operator">.</span><span class="Identifier">isNil</span></span></tt> check.</li>
  61. <li><tt class="docutils literal"><span class="pre"><span class="Identifier">Unreachable</span></span></tt> means it shouldn't be possible to access this in this branch: so we do generate a warning as well.</li>
  62. </ul>
  63. <p>We show an error for each dereference (<tt class="docutils literal"><span class="pre"><span class="Punctuation">[</span><span class="Punctuation">]</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Operator">.</span><span class="Identifier">field</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Punctuation">[</span><span class="Identifier">index</span><span class="Punctuation">]</span></span></tt> <tt class="docutils literal"><span class="pre"><span class="Punctuation">(</span><span class="Punctuation">)</span></span></tt> etc.) which is of a tracked expression which is in <tt class="docutils literal"><span class="pre"><span class="Identifier">MaybeNil</span></span></tt> or <tt class="docutils literal"><span class="pre"><span class="Keyword">Nil</span></span></tt> state.</p>
  64. <h2 id="type-nilability">type nilability</h2><p>Types are either nilable or non-nilable. When you pass a param or a default value, we use the type : for nilable types we return <tt class="docutils literal"><span class="pre"><span class="Identifier">MaybeNil</span></span></tt> and for non-nilable <tt class="docutils literal"><span class="pre"><span class="Identifier">Safe</span></span></tt>.</p>
  65. <h2 id="params-rules">params rules</h2><p>Param's nilability is detected based on type nilability. We use the type of the argument to detect the nilability.</p>
  66. <h2 id="assignment-rules">assignment rules</h2><p>Let's say we have <tt class="docutils literal"><span class="pre"><span class="Identifier">left</span> <span class="Operator">=</span> <span class="Identifier">right</span></span></tt>.</p>
  67. <p>When we assign, we pass the right's nilability to the left's expression. There should be special handling of aliasing and compound expressions which we specify in their sections. (Assignment is a possible alias <tt class="docutils literal"><span class="pre"><span class="Identifier">move</span></span></tt> or <tt class="docutils literal"><span class="pre"><span class="Identifier">move</span> <span class="Keyword">out</span></span></tt>).</p>
  68. <h2 id="call-args-rules">call args rules</h2><p>When we call with arguments, we have two cases when we might change the nilability.</p>
  69. <p><pre class="listing"><span class="Identifier">callByVar</span><span class="Punctuation">(</span><span class="Identifier">a</span><span class="Punctuation">)</span></pre></p>
  70. <p>Here <tt class="docutils literal"><span class="pre"><span class="Identifier">callByVar</span></span></tt> can re-assign <tt class="docutils literal"><span class="pre"><span class="Identifier">a</span></span></tt>, so this might change <tt class="docutils literal"><span class="pre"><span class="Identifier">a</span></span></tt>'s nilability, so we change it to <tt class="docutils literal"><span class="pre"><span class="Identifier">MaybeNil</span></span></tt>. This is also a possible aliasing <tt class="docutils literal"><span class="pre"><span class="Identifier">move</span> <span class="Keyword">out</span></span></tt> (moving out of a current alias set).</p>
  71. <p><pre class="listing"><span class="Identifier">call</span><span class="Punctuation">(</span><span class="Identifier">a</span><span class="Punctuation">)</span></pre></p>
  72. <p>Here <tt class="docutils literal"><span class="pre"><span class="Identifier">call</span></span></tt> can change a field or element of <tt class="docutils literal"><span class="pre"><span class="Identifier">a</span></span></tt>, so if we have a dependant expression of <tt class="docutils literal"><span class="pre"><span class="Identifier">a</span></span></tt> : e.g. <tt class="docutils literal"><span class="pre"><span class="Identifier">a</span><span class="Operator">.</span><span class="Identifier">field</span></span></tt>. Dependants become <tt class="docutils literal"><span class="pre"><span class="Identifier">MaybeNil</span></span></tt>.</p>
  73. <h2 id="branches-rules">branches rules</h2><p>Branches are the reason we do nil checking like this: with flow checking. Sources of branching are <tt class="docutils literal"><span class="pre"><span class="Keyword">if</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Keyword">while</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Keyword">for</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Keyword">and</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Keyword">or</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Keyword">case</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Keyword">try</span></span></tt> and combinations with <tt class="docutils literal"><span class="pre"><span class="Keyword">return</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Keyword">break</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Keyword">continue</span></span></tt> and <tt class="docutils literal"><span class="pre"><span class="Keyword">raise</span></span></tt></p>
  74. <p>We create a new layer/&quot;scope&quot; for each branch where we map expressions to nilability. This happens when we &quot;fork&quot;: usually on the beginning of a construct. When branches &quot;join&quot; we usually unify their expression maps or/and nilabilities.</p>
  75. <p>Merging usually merges maps and alias sets: nilabilities are merged like this:</p>
  76. <p><pre class="listing"><span class="Keyword">template</span> <span class="Identifier">union</span><span class="Punctuation">(</span><span class="Identifier">l</span><span class="Punctuation">:</span> <span class="Identifier">Nilability</span><span class="Punctuation">,</span> <span class="Identifier">r</span><span class="Punctuation">:</span> <span class="Identifier">Nilability</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">Nilability</span> <span class="Operator">=</span>
  77. <span class="Comment">## unify two states</span>
  78. <span class="Keyword">if</span> <span class="Identifier">l</span> <span class="Operator">==</span> <span class="Identifier">r</span><span class="Punctuation">:</span>
  79. <span class="Identifier">l</span>
  80. <span class="Keyword">else</span><span class="Punctuation">:</span>
  81. <span class="Identifier">MaybeNil</span></pre></p>
  82. <p>Special handling is for <tt class="docutils literal"><span class="pre"><span class="Operator">.</span><span class="Identifier">isNil</span></span></tt> and <tt class="docutils literal"><span class="pre"><span class="Operator">==</span> <span class="Keyword">nil</span></span></tt>, also for <tt class="docutils literal"><span class="pre"><span class="Keyword">not</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Keyword">and</span></span></tt> and <tt class="docutils literal"><span class="pre"><span class="Keyword">or</span></span></tt>.</p>
  83. <p><tt class="docutils literal"><span class="pre"><span class="Keyword">not</span></span></tt> reverses the nilability, <tt class="docutils literal"><span class="pre"><span class="Keyword">and</span></span></tt> is similar to &quot;forking&quot; : the right expression is checked in the layer resulting from the left one and <tt class="docutils literal"><span class="pre"><span class="Keyword">or</span></span></tt> is similar to &quot;merging&quot;: the right and left expression should be both checked in the original layer.</p>
  84. <p><tt class="docutils literal"><span class="pre"><span class="Identifier">isNil</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Operator">==</span> <span class="Keyword">nil</span></span></tt> make expressions <tt class="docutils literal"><span class="pre"><span class="Keyword">Nil</span></span></tt>. If there is a <tt class="docutils literal"><span class="pre"><span class="Keyword">not</span></span></tt> or <tt class="docutils literal"><span class="pre"><span class="Operator">!=</span> <span class="Keyword">nil</span></span></tt>, they make them <tt class="docutils literal"><span class="pre"><span class="Identifier">Safe</span></span></tt>. We also reverse the nilability in the opposite branch: e.g. <tt class="docutils literal"><span class="pre"><span class="Keyword">else</span></span></tt>.</p>
  85. <h2 id="compound-expressionscolon-field-index-expressions">compound expressions: field, index expressions</h2><p>We want to track also field(dot) and index(bracket) expressions.</p>
  86. <p>We track some of those compound expressions which might be nilable as dependants of their bases: <tt class="docutils literal"><span class="pre"><span class="Identifier">a</span><span class="Operator">.</span><span class="Identifier">field</span></span></tt> is changed if <tt class="docutils literal"><span class="pre"><span class="Identifier">a</span></span></tt> is moved (re-assigned), similarly <tt class="docutils literal"><span class="pre"><span class="Identifier">a</span><span class="Punctuation">[</span><span class="Identifier">index</span><span class="Punctuation">]</span></span></tt> is dependent on <tt class="docutils literal"><span class="pre"><span class="Identifier">a</span></span></tt> and <tt class="docutils literal"><span class="pre"><span class="Identifier">a</span><span class="Operator">.</span><span class="Identifier">field</span><span class="Operator">.</span><span class="Identifier">field</span></span></tt> on <tt class="docutils literal"><span class="pre"><span class="Identifier">a</span><span class="Operator">.</span><span class="Identifier">field</span></span></tt>.</p>
  87. <p>When we move the base, we update dependants to <tt class="docutils literal"><span class="pre"><span class="Identifier">MaybeNil</span></span></tt>. Otherwise, we usually start with type nilability.</p>
  88. <p>When we call args, we update the nilability of their dependants to <tt class="docutils literal"><span class="pre"><span class="Identifier">MaybeNil</span></span></tt> as the calls usually can change them. We might need to check for <tt class="docutils literal"><span class="pre"><span class="Identifier">strictFuncs</span></span></tt> pure funcs and not do that then.</p>
  89. <p>For field expressions <tt class="docutils literal"><span class="pre"><span class="Identifier">a</span><span class="Operator">.</span><span class="Identifier">field</span></span></tt>, we calculate an integer value based on a hash of the tree and just accept equivalent trees as equivalent expressions.</p>
  90. <p>For item expression <tt class="docutils literal"><span class="pre"><span class="Identifier">a</span><span class="Punctuation">[</span><span class="Identifier">index</span><span class="Punctuation">]</span></span></tt>, we also calculate an integer value based on a hash of the tree and accept equivalent trees as equivalent expressions: for static values only. For now, we support only constant indices: we don't track expression with no-const indices. For those we just report a warning even if they are safe for now: one can use a local variable to workaround. For loops this might be annoying: so one should be able to turn off locally the warning using the <tt class="docutils literal"><span class="pre"><span class="Punctuation">{</span><span class="Operator">.</span><span class="Identifier">warning</span><span class="Punctuation">[</span><span class="Identifier">StrictNotNil</span><span class="Punctuation">]</span><span class="Punctuation">:</span><span class="Identifier">off</span><span class="Operator">.</span><span class="Punctuation">}</span></span></tt>.</p>
  91. <p>For bracket expressions, in the future we might count <tt class="docutils literal"><span class="pre"><span class="Identifier">a</span><span class="Punctuation">[</span><span class="Operator">&lt;</span><span class="Identifier">any</span><span class="Operator">&gt;</span><span class="Punctuation">]</span></span></tt> as the same general expression. This means we should the index but otherwise handle it the same for assign (maybe &quot;aliasing&quot; all the non-static elements) and differentiate only for static: e.g. <tt class="docutils literal"><span class="pre"><span class="Identifier">a</span><span class="Punctuation">[</span><span class="DecNumber">0</span><span class="Punctuation">]</span></span></tt> and <tt class="docutils literal"><span class="pre"><span class="Identifier">a</span><span class="Punctuation">[</span><span class="DecNumber">1</span><span class="Punctuation">]</span></span></tt>.</p>
  92. <h2 id="element-tracking">element tracking</h2><p>When we assign an object construction, we should track the fields as well:</p>
  93. <p><pre class="listing"><span class="Keyword">var</span> <span class="Identifier">a</span> <span class="Operator">=</span> <span class="Identifier">Nilable</span><span class="Punctuation">(</span><span class="Identifier">field</span><span class="Punctuation">:</span> <span class="Identifier">Nilable</span><span class="Punctuation">(</span><span class="Punctuation">)</span><span class="Punctuation">)</span> <span class="Comment"># a : Safe, a.field: Safe</span></pre></p>
  94. <p>Usually we just track the result of an expression: probably this should apply for elements in other cases as well. Also related to tracking initialization of expressions/fields.</p>
  95. <h2 id="unstructured-control-flow-rules">unstructured control flow rules</h2><p>Unstructured control flow keywords as <tt class="docutils literal"><span class="pre"><span class="Keyword">return</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Keyword">break</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Keyword">continue</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Keyword">raise</span></span></tt> mean that we jump from a branch out. This means that if there is code after the finishing of the branch, it would be run if one hasn't hit the direct parent branch of those: so it is similar to an <tt class="docutils literal"><span class="pre"><span class="Keyword">else</span></span></tt>. In those cases we should use the reverse nilabilities for the local to the condition expressions. E.g.</p>
  96. <p><pre class="listing"><span class="Keyword">for</span> <span class="Identifier">a</span> <span class="Keyword">in</span> <span class="Identifier">c</span><span class="Punctuation">:</span>
  97. <span class="Keyword">if</span> <span class="Keyword">not</span> <span class="Identifier">a</span><span class="Operator">.</span><span class="Identifier">isNil</span><span class="Punctuation">:</span>
  98. <span class="Identifier">b</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
  99. <span class="Keyword">break</span>
  100. <span class="Identifier">code</span> <span class="Comment"># here a: Nil , because if not, we would have breaked</span></pre></p>
  101. <h2 id="aliasing">aliasing</h2><p>We support alias detection for local expressions.</p>
  102. <p>We track sets of aliased expressions. We start with all nilable local expressions in separate sets. Assignments and other changes to nilability can move / move out expressions of sets.</p>
  103. <p><tt class="docutils literal"><span class="pre"><span class="Identifier">move</span></span></tt>: Moving <tt class="docutils literal"><span class="pre"><span class="Identifier">left</span></span></tt> to <tt class="docutils literal"><span class="pre"><span class="Identifier">right</span></span></tt> means we remove <tt class="docutils literal"><span class="pre"><span class="Identifier">left</span></span></tt> from its current set and unify it with the <tt class="docutils literal"><span class="pre"><span class="Identifier">right</span></span></tt>'s set. This means it stops being aliased with its previous aliases.</p>
  104. <p><pre class="listing"><span class="Keyword">var</span> <span class="Identifier">left</span> <span class="Operator">=</span> <span class="Identifier">b</span>
  105. <span class="Identifier">left</span> <span class="Operator">=</span> <span class="Identifier">right</span> <span class="Comment"># moving left to right</span></pre></p>
  106. <p><tt class="docutils literal"><span class="pre"><span class="Identifier">move</span> <span class="Keyword">out</span></span></tt>: Moving out <tt class="docutils literal"><span class="pre"><span class="Identifier">left</span></span></tt> might remove it from the current set and ensure that it's in its own set as a single element. e.g.</p>
  107. <p><pre class="listing"><span class="Keyword">var</span> <span class="Identifier">left</span> <span class="Operator">=</span> <span class="Identifier">b</span>
  108. <span class="Identifier">left</span> <span class="Operator">=</span> <span class="Keyword">nil</span> <span class="Comment"># moving out</span></pre></p>
  109. <h2 id="warnings-and-errors">warnings and errors</h2><p>We show an error for each dereference (<tt class="docutils literal"><span class="pre"><span class="Punctuation">[</span><span class="Punctuation">]</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Operator">.</span><span class="Identifier">field</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Punctuation">[</span><span class="Identifier">index</span><span class="Punctuation">]</span></span></tt> <tt class="docutils literal"><span class="pre"><span class="Punctuation">(</span><span class="Punctuation">)</span></span></tt> etc.) which is of a tracked expression which is in <tt class="docutils literal"><span class="pre"><span class="Identifier">MaybeNil</span></span></tt> or <tt class="docutils literal"><span class="pre"><span class="Keyword">Nil</span></span></tt> state.</p>
  110. <p>We might also show a history of the transitions and the reasons for them that might change the nilability of the expression.</p>
  111. <div class="twelve-columns footer">
  112. <span class="nim-sprite"></span>
  113. <br>
  114. <small style="color: var(--hint);">Made with Nim. Generated: 2025-03-15 14:12:49 UTC</small>
  115. </div>
  116. </div>
  117. </div>
  118. <script defer data-domain="nim-lang.org" src="https://plausible.io/js/plausible.js"></script>
  119. </body>
  120. </html>