11/**
22 * Provides a library for resolving local names based on syntactic scopes, including
3- * handling of shadowing declarations.
3+ * handling of shadowing sibling declarations.
44 */
55overlay [ local?]
66module ;
@@ -25,7 +25,12 @@ signature module LocalNameBindingInputSig<LocationSig Location> {
2525 Location getLocation ( ) ;
2626 }
2727
28- /** Gets the child of AST node `n` at the specified index. */
28+ /**
29+ * Gets the child of AST node `n` at the specified index.
30+ *
31+ * The order of the children is only relevant for determining nearest preceding
32+ * shadowing sibling declarations.
33+ */
2934 AstNode getChild ( AstNode n , int index ) ;
3035
3136 /**
@@ -54,18 +59,25 @@ signature module LocalNameBindingInputSig<LocationSig Location> {
5459 }
5560
5661 /**
57- * A shadowing declaration where any local declarations in the left-hand side
58- * are in scope _after_ the declaration and shadow any declarations with the
59- * same name preceding it.
62+ * A declaration where all local declarations in the left-hand side are in
63+ * scope _after_ the declaration, and where any sibling declarations with
64+ * the same name and syntactic scope preceding it are shadowed .
6065 *
6166 * Example:
67+ *
6268 * ```rust
63- * let x = 1;
64- * let x = x + 1; // this declaration of `x` shadows the previous one, but the `x` in the right-hand side still refers to the first declaration
65- * println!("{}", x); // this access of `x` refers to the second declaration
69+ * fn f() {
70+ * let x = 1;
71+ * // this declaration of `x` shadows the previous one (in the syntactic scope
72+ * // being the body of `f`), but the `x` in the right-hand side still refers
73+ * // to the first declaration
74+ * let x = x + 1;
75+ * // this access of `x` refers to the second declaration
76+ * println!("{}", x);
77+ * }
6678 * ```
6779 */
68- class ShadowingDecl extends AstNode {
80+ class SiblingShadowingDecl extends AstNode {
6981 /** Gets the left-hand side of this declaration. */
7082 AstNode getLhs ( ) ;
7183
@@ -91,7 +103,7 @@ signature module LocalNameBindingInputSig<LocationSig Location> {
91103 * the syntactic scope `scope`.
92104 *
93105 * Note that declarations with a `definingNode` in the left-hand side of a
94- * shadowing declaration `decl` should use `scope = decl`.
106+ * shadowing sibling declaration `decl` should use `scope = decl`.
95107 */
96108 predicate declInScope ( AstNode definingNode , string name , AstNode scope ) ;
97109
@@ -127,7 +139,7 @@ signature module LocalNameBindingInputSig<LocationSig Location> {
127139
128140/**
129141 * Provides logic for resolving local names based on syntactic scopes, including
130- * handling of shadowing declarations.
142+ * handling of shadowing sibling declarations.
131143 */
132144module LocalNameBinding< LocationSig Location, LocalNameBindingInputSig< Location > Input> {
133145 private import Input
@@ -160,8 +172,8 @@ module LocalNameBinding<LocationSig Location, LocalNameBindingInputSig<Location>
160172 /**
161173 * An adjusted version of `getChild` from the `Input` module where in conditionals like
162174 * `if cond body`, instead of letting `body` be a child of `if`, we make it the last
163- * child of `cond`. This ensures that shadowing declarations inside `cond` are properly
164- * handled inside `body`.
175+ * child of `cond`. This ensures that shadowing sibling declarations inside `cond` are
176+ * properly handled inside `body`.
165177 *
166178 * Example:
167179 *
@@ -171,8 +183,8 @@ module LocalNameBinding<LocationSig Location, LocalNameBindingInputSig<Location>
171183 * }
172184 * ```
173185 *
174- * We also move any `else` branch _before_ the condition to ensure that shadowing declarations
175- * inside the condition are not in scope.
186+ * We also move any `else` branch _before_ the condition to ensure that shadowing sibling
187+ * declarations inside the condition are not in scope.
176188 */
177189 private AstNode getChildAdj ( AstNode parent , int index ) {
178190 result = getChild ( parent , index ) and
@@ -198,29 +210,31 @@ module LocalNameBinding<LocationSig Location, LocalNameBindingInputSig<Location>
198210
199211 int getRank ( C parent , Ranked child ) {
200212 child = getChildAdj ( parent , result ) and
201- getChildAdj ( parent , _) instanceof ShadowingDecl
213+ getChildAdj ( parent , _) instanceof SiblingShadowingDecl
202214 }
203215 }
204216
205217 private predicate getRankedChild = DenseRank1< DenseRankInput > :: denseRank / 2 ;
206218
207219 /**
208220 * Holds if `n` is the `i`th child of `parent`, but should instead be considered
209- * a child of a shadowing declaration `decl` when resolving accesses.
221+ * a child of a shadowing sibling declaration `decl` when resolving accesses.
210222 *
211- * This is the case when `decl` is the nearest shadowing declaration preceding `n`
212- * amongst all the children of `parent`.
223+ * This is the case when `decl` is the nearest shadowing sibling declaration
224+ * preceding `n` amongst all the children of `parent`.
213225 *
214226 * Note that `decl` may itself also have to be nested under another shadowing
215- * declaration.
227+ * sibling declaration.
216228 */
217- private predicate shouldBeShadowingDeclChild ( AstNode parent , ShadowingDecl decl , int i , AstNode n ) {
229+ private predicate shouldBeShadowingDeclChild (
230+ AstNode parent , SiblingShadowingDecl decl , int i , AstNode n
231+ ) {
218232 n = getRankedChild ( parent , i ) and
219233 (
220234 decl = getRankedChild ( parent , i - 1 )
221235 or
222236 shouldBeShadowingDeclChild ( parent , decl , i - 1 ,
223- any ( AstNode prev | not prev instanceof ShadowingDecl ) )
237+ any ( AstNode prev | not prev instanceof SiblingShadowingDecl ) )
224238 )
225239 }
226240
@@ -269,12 +283,12 @@ module LocalNameBinding<LocationSig Location, LocalNameBindingInputSig<Location>
269283 */
270284 private AstNode getParentForScoping ( AstNode n ) {
271285 not shouldBeShadowingDeclChild ( _, _, _, n ) and
272- not exists ( ShadowingDecl decl | n = [ decl .getRhs ( ) , decl .getElse ( ) ] ) and
286+ not exists ( SiblingShadowingDecl decl | n = [ decl .getRhs ( ) , decl .getElse ( ) ] ) and
273287 n = getChildAdj ( result , _)
274288 or
275289 shouldBeShadowingDeclChild ( _, result , _, n )
276290 or
277- exists ( ShadowingDecl decl |
291+ exists ( SiblingShadowingDecl decl |
278292 result = getParentForScoping ( decl ) and
279293 n = [ decl .getRhs ( ) , decl .getElse ( ) ]
280294 )
0 commit comments