@@ -87,6 +87,7 @@ private newtype TAssocFunctionType =
8787 }
8888
8989bindingset [ abs, constraint, tp]
90+ pragma [ inline_late]
9091private Type getTraitConstraintTypeAt (
9192 TypeAbstraction abs , TypeMention constraint , TypeParameter tp , TypePath path
9293) {
@@ -203,7 +204,7 @@ class AssocFunctionType extends MkAssocFunctionType {
203204}
204205
205206pragma [ nomagic]
206- Trait getALookupTrait ( Type t ) {
207+ private Trait getALookupTrait ( Type t ) {
207208 result = t .( TypeParamTypeParameter ) .getTypeParam ( ) .( TypeParamItemNode ) .resolveABound ( )
208209 or
209210 result = t .( SelfTypeParameter ) .getTrait ( )
@@ -213,23 +214,47 @@ Trait getALookupTrait(Type t) {
213214 result = t .( DynTraitType ) .getTrait ( )
214215}
215216
216- /**
217- * Gets the type obtained by substituting in relevant traits in which to do function
218- * lookup, or `t` itself when no such trait exist.
219- */
220217pragma [ nomagic]
221- Type substituteLookupTraits ( Type t ) {
218+ private Trait getAdditionalLookupTrait ( ItemNode i , Type t ) {
219+ result =
220+ t .( TypeParamTypeParameter )
221+ .getTypeParam ( )
222+ .( TypeParamItemNode )
223+ .resolveAdditionalBound ( i .getImmediateParent * ( ) )
224+ }
225+
226+ bindingset [ n, t]
227+ pragma [ inline_late]
228+ Trait getALookupTrait ( AstNode n , Type t ) {
229+ result = getALookupTrait ( t )
230+ or
231+ result = getAdditionalLookupTrait ( any ( ItemNode i | n = i .getADescendant ( ) ) , t )
232+ }
233+
234+ bindingset [ i, t]
235+ pragma [ inline_late]
236+ private Type substituteLookupTraits0 ( ItemNode i , Type t ) {
222237 not exists ( getALookupTrait ( t ) ) and
238+ not exists ( getAdditionalLookupTrait ( i , t ) ) and
223239 result = t
224240 or
225241 result = TTrait ( getALookupTrait ( t ) )
242+ or
243+ result = TTrait ( getAdditionalLookupTrait ( i , t ) )
226244}
227245
228246/**
229- * Gets the `n`th `substituteLookupTraits` type for `t`, per some arbitrary order.
247+ * Gets the type obtained by substituting in relevant traits in which to do function
248+ * lookup, or `t` itself when no such trait exists, in the context of AST node `n`.
230249 */
250+ bindingset [ n, t]
251+ pragma [ inline_late]
252+ Type substituteLookupTraits ( AstNode n , Type t ) {
253+ result = substituteLookupTraits0 ( any ( ItemNode i | n = i .getADescendant ( ) ) , t )
254+ }
255+
231256pragma [ nomagic]
232- Type getNthLookupType ( Type t , int n ) {
257+ private Type getNthLookupType ( Type t , int n ) {
233258 not exists ( getALookupTrait ( t ) ) and
234259 result = t and
235260 n = 0
@@ -244,24 +269,66 @@ Type getNthLookupType(Type t, int n) {
244269}
245270
246271/**
247- * Gets the index of the last `substituteLookupTraits` type for `t`.
272+ * Gets the `n`th `substituteLookupTraits` type for `t`, per some arbitrary order,
273+ * in the context of AST node `node`.
248274 */
275+ bindingset [ node, t]
276+ pragma [ inline_late]
277+ Type getNthLookupType ( AstNode node , Type t , int n ) {
278+ exists ( ItemNode i | node = i .getADescendant ( ) |
279+ if exists ( getAdditionalLookupTrait ( i , t ) )
280+ then
281+ result =
282+ TTrait ( rank [ n + 1 ] ( Trait trait , int j |
283+ trait = [ getALookupTrait ( t ) , getAdditionalLookupTrait ( i , t ) ] and
284+ j = idOfTypeParameterAstNode ( trait )
285+ |
286+ trait order by j
287+ ) )
288+ else result = getNthLookupType ( t , n )
289+ )
290+ }
291+
249292pragma [ nomagic]
250- int getLastLookupTypeIndex ( Type t ) { result = max ( int n | exists ( getNthLookupType ( t , n ) ) ) }
293+ private int getLastLookupTypeIndex ( Type t ) { result = max ( int n | exists ( getNthLookupType ( t , n ) ) ) }
294+
295+ /**
296+ * Gets the index of the last `substituteLookupTraits` type for `t`,
297+ * in the context of AST node `node`.
298+ */
299+ bindingset [ node, t]
300+ pragma [ inline_late]
301+ int getLastLookupTypeIndex ( AstNode node , Type t ) {
302+ if exists ( getAdditionalLookupTrait ( node , t ) )
303+ then result = max ( int n | exists ( getNthLookupType ( node , t , n ) ) )
304+ else result = getLastLookupTypeIndex ( t )
305+ }
306+
307+ signature class ArgSig {
308+ /** Gets the type of this argument at `path`. */
309+ Type getTypeAt ( TypePath path ) ;
310+
311+ /** Gets the enclosing item node of this argument. */
312+ ItemNode getEnclosingItemNode ( ) ;
313+
314+ /** Gets a textual representation of this argument. */
315+ string toString ( ) ;
316+
317+ /** Gets the location of this argument. */
318+ Location getLocation ( ) ;
319+ }
251320
252321/**
253322 * A wrapper around `IsInstantiationOf` which ensures to substitute in lookup
254323 * traits when checking whether argument types are instantiations of function
255324 * types.
256325 */
257- module ArgIsInstantiationOf<
258- HasTypeTreeSig Arg, IsInstantiationOfInputSig< Arg , AssocFunctionType > Input>
259- {
326+ module ArgIsInstantiationOf< ArgSig Arg, IsInstantiationOfInputSig< Arg , AssocFunctionType > Input> {
260327 final private class ArgFinal = Arg ;
261328
262329 private class ArgSubst extends ArgFinal {
263330 Type getTypeAt ( TypePath path ) {
264- result = substituteLookupTraits ( super .getTypeAt ( path ) ) and
331+ result = substituteLookupTraits0 ( this . getEnclosingItemNode ( ) , super .getTypeAt ( path ) ) and
265332 not result = TNeverType ( ) and
266333 not result = TUnknownType ( )
267334 }
@@ -318,6 +385,8 @@ signature module ArgsAreInstantiationsOfInputSig {
318385
319386 Location getLocation ( ) ;
320387
388+ ItemNode getEnclosingItemNode ( ) ;
389+
321390 Type getArgType ( FunctionPosition pos , TypePath path ) ;
322391
323392 predicate hasTargetCand ( ImplOrTraitItemNode i , Function f ) ;
@@ -366,6 +435,8 @@ module ArgsAreInstantiationsOf<ArgsAreInstantiationsOfInputSig Input> {
366435
367436 FunctionPosition getPos ( ) { result = pos }
368437
438+ ItemNode getEnclosingItemNode ( ) { result = call .getEnclosingItemNode ( ) }
439+
369440 Location getLocation ( ) { result = call .getLocation ( ) }
370441
371442 Type getTypeAt ( TypePath path ) { result = call .getArgType ( pos , path ) }
0 commit comments