123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <?xml version="1.0" encoding="utf-8" ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <!-- This file is generated by Nim. -->
- <html xmlns="https://www.w3.org/1999/xhtml" xml:lang="en" lang="en" data-theme="auto">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>doc/manual_experimental_strictnotnil</title>
- <!-- Google fonts -->
- <link href='https://fonts.googleapis.com/css?family=Lato:400,600,900' rel='stylesheet' type='text/css'/>
- <link href='https://fonts.googleapis.com/css?family=Source+Code+Pro:400,500,600' rel='stylesheet' type='text/css'/>
- <!-- Favicon -->
- <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=="/>
- <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=">
- <!-- CSS -->
- <link rel="stylesheet" type="text/css" href="nimdoc.out.css?v=2.3.1">
- <!-- JS -->
- <script type="text/javascript" src="dochack.js?v=2.3.1"></script>
- </head>
- <body>
- <div class="document" id="documentId">
- <div class="container">
- <h1 class="title">doc/manual_experimental_strictnotnil</h1>
-
- <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>
- <p><pre class="listing"><span class="Punctuation">{</span><span class="Operator">.</span><span class="Identifier">experimental</span><span class="Punctuation">:</span> <span class="StringLit">"strictNotNil"</span><span class="Operator">.</span><span class="Punctuation">}</span></pre></p>
- <p>or</p>
- <p><pre class="listing"><span class="program">nim</span> <span class="option">c</span> <span class="option">--experimental:strictNotNil</span> <span class="option"><program></span></pre></p>
- <p>In the second case it would check builtin and imported modules as well.</p>
- <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>
- <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>
- <p>We check several kinds of types for nilability:</p>
- <ul class="simple"><li>ref types</li>
- <li>pointer types</li>
- <li>proc types</li>
- <li>cstrings</li>
- </ul>
- <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>
- <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>
- <p><pre class="listing"> <span class="Keyword">type</span>
- <span class="Identifier">NilableObject</span> <span class="Operator">=</span> <span class="Keyword">ref</span> <span class="Keyword">object</span>
- <span class="Identifier">a</span><span class="Punctuation">:</span> <span class="Identifier">int</span>
- <span class="Keyword">Object</span> <span class="Operator">=</span> <span class="Identifier">NilableObject</span> <span class="Keyword">not</span> <span class="Keyword">nil</span>
-
- <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>
-
- <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>
- <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>
- <span class="Comment"># compiler catches this:</span>
- <span class="Identifier">p</span><span class="Punctuation">(</span><span class="Keyword">nil</span><span class="Punctuation">)</span>
- <span class="Comment"># and also this:</span>
- <span class="Keyword">var</span> <span class="Identifier">x</span><span class="Punctuation">:</span> <span class="Identifier">NilableObject</span>
- <span class="Keyword">if</span> <span class="Identifier">x</span><span class="Operator">.</span><span class="Identifier">isNil</span><span class="Punctuation">:</span>
- <span class="Identifier">p</span><span class="Punctuation">(</span><span class="Identifier">x</span><span class="Punctuation">)</span>
- <span class="Keyword">else</span><span class="Punctuation">:</span>
- <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>
- <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>
- <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>
- <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>
- <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>
- <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>
- <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>
- <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>
- <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>
- </ul>
- <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>
- <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>
- <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>
- <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>
- <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>
- <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>
- <p><pre class="listing"><span class="Identifier">callByVar</span><span class="Punctuation">(</span><span class="Identifier">a</span><span class="Punctuation">)</span></pre></p>
- <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>
- <p><pre class="listing"><span class="Identifier">call</span><span class="Punctuation">(</span><span class="Identifier">a</span><span class="Punctuation">)</span></pre></p>
- <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>
- <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>
- <p>We create a new layer/"scope" for each branch where we map expressions to nilability. This happens when we "fork": usually on the beginning of a construct. When branches "join" we usually unify their expression maps or/and nilabilities.</p>
- <p>Merging usually merges maps and alias sets: nilabilities are merged like this:</p>
- <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>
- <span class="Comment">## unify two states</span>
- <span class="Keyword">if</span> <span class="Identifier">l</span> <span class="Operator">==</span> <span class="Identifier">r</span><span class="Punctuation">:</span>
- <span class="Identifier">l</span>
- <span class="Keyword">else</span><span class="Punctuation">:</span>
- <span class="Identifier">MaybeNil</span></pre></p>
- <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>
- <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 "forking" : 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 "merging": the right and left expression should be both checked in the original layer.</p>
- <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>
- <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>
- <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>
- <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>
- <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>
- <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>
- <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>
- <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"><</span><span class="Identifier">any</span><span class="Operator">></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 "aliasing" 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>
- <h2 id="element-tracking">element tracking</h2><p>When we assign an object construction, we should track the fields as well:</p>
- <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>
- <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>
- <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>
- <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>
- <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>
- <span class="Identifier">b</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
- <span class="Keyword">break</span>
- <span class="Identifier">code</span> <span class="Comment"># here a: Nil , because if not, we would have breaked</span></pre></p>
- <h2 id="aliasing">aliasing</h2><p>We support alias detection for local expressions.</p>
- <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>
- <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>
- <p><pre class="listing"><span class="Keyword">var</span> <span class="Identifier">left</span> <span class="Operator">=</span> <span class="Identifier">b</span>
- <span class="Identifier">left</span> <span class="Operator">=</span> <span class="Identifier">right</span> <span class="Comment"># moving left to right</span></pre></p>
- <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>
- <p><pre class="listing"><span class="Keyword">var</span> <span class="Identifier">left</span> <span class="Operator">=</span> <span class="Identifier">b</span>
- <span class="Identifier">left</span> <span class="Operator">=</span> <span class="Keyword">nil</span> <span class="Comment"># moving out</span></pre></p>
- <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>
- <p>We might also show a history of the transitions and the reasons for them that might change the nilability of the expression.</p>
- <div class="twelve-columns footer">
- <span class="nim-sprite"></span>
- <br>
- <small style="color: var(--hint);">Made with Nim. Generated: 2025-03-15 14:12:49 UTC</small>
- </div>
- </div>
- </div>
- <script defer data-domain="nim-lang.org" src="https://plausible.io/js/plausible.js"></script>
-
- </body>
- </html>
|