@@ -203,7 +203,7 @@ class AssocFunctionType extends MkAssocFunctionType {
203203}
204204
205205pragma [ nomagic]
206- Trait getALookupTrait ( Type t ) {
206+ private Trait getALookupTrait ( Type t ) {
207207 result = t .( TypeParamTypeParameter ) .getTypeParam ( ) .( TypeParamItemNode ) .resolveABound ( )
208208 or
209209 result = t .( SelfTypeParameter ) .getTrait ( )
@@ -213,23 +213,40 @@ Trait getALookupTrait(Type t) {
213213 result = t .( DynTraitType ) .getTrait ( )
214214}
215215
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- */
220216pragma [ nomagic]
221- Type substituteLookupTraits ( Type t ) {
217+ private Trait getAdditionalLookupTrait ( Type t , Function f ) {
218+ result = t .( TypeParamTypeParameter ) .getTypeParam ( ) .( TypeParamItemNode ) .resolveAdditionalBound ( f )
219+ }
220+
221+ bindingset [ n, t]
222+ Trait getALookupTrait ( AstNode n , Type t ) {
223+ result = getALookupTrait ( t )
224+ or
225+ result = getAdditionalLookupTrait ( t , n .getEnclosingFunction ( ) )
226+ }
227+
228+ bindingset [ f, t]
229+ private Type substituteLookupTraits0 ( Function f , Type t ) {
222230 not exists ( getALookupTrait ( t ) ) and
231+ not exists ( getAdditionalLookupTrait ( t , f ) ) and
223232 result = t
224233 or
225234 result = TTrait ( getALookupTrait ( t ) )
235+ or
236+ result = TTrait ( getAdditionalLookupTrait ( t , f ) )
226237}
227238
228239/**
229- * Gets the `n`th `substituteLookupTraits` type for `t`, per some arbitrary order.
240+ * Gets the type obtained by substituting in relevant traits in which to do function
241+ * lookup, or `t` itself when no such trait exist, in the context of AST node `n`.
230242 */
243+ bindingset [ n, t]
244+ Type substituteLookupTraits ( AstNode n , Type t ) {
245+ result = substituteLookupTraits0 ( n .getEnclosingFunction ( ) , t )
246+ }
247+
231248pragma [ nomagic]
232- Type getNthLookupType ( Type t , int n ) {
249+ private Type getNthLookupType ( Type t , int n ) {
233250 not exists ( getALookupTrait ( t ) ) and
234251 result = t and
235252 n = 0
@@ -244,24 +261,64 @@ Type getNthLookupType(Type t, int n) {
244261}
245262
246263/**
247- * Gets the index of the last `substituteLookupTraits` type for `t`.
264+ * Gets the `n`th `substituteLookupTraits` type for `t`, per some arbitrary order,
265+ * in the context of AST node `node`.
248266 */
267+ bindingset [ node, t]
268+ Type getNthLookupType ( AstNode node , Type t , int n ) {
269+ exists ( Function f | f = node .getEnclosingFunction ( ) |
270+ if exists ( getAdditionalLookupTrait ( t , f ) )
271+ then
272+ result =
273+ TTrait ( rank [ n + 1 ] ( Trait trait , int i |
274+ trait = [ getALookupTrait ( t ) , getAdditionalLookupTrait ( t , f ) ] and
275+ i = idOfTypeParameterAstNode ( trait )
276+ |
277+ trait order by i
278+ ) )
279+ else result = getNthLookupType ( t , n )
280+ )
281+ }
282+
249283pragma [ nomagic]
250- int getLastLookupTypeIndex ( Type t ) { result = max ( int n | exists ( getNthLookupType ( t , n ) ) ) }
284+ private int getLastLookupTypeIndex ( Type t ) { result = max ( int n | exists ( getNthLookupType ( t , n ) ) ) }
285+
286+ /**
287+ * Gets the index of the last `substituteLookupTraits` type for `t`,
288+ * in the context of AST node `node`.
289+ */
290+ bindingset [ node, t]
291+ int getLastLookupTypeIndex ( AstNode node , Type t ) {
292+ if exists ( getAdditionalLookupTrait ( t , node ) )
293+ then result = max ( int n | exists ( getNthLookupType ( node , t , n ) ) )
294+ else result = getLastLookupTypeIndex ( t )
295+ }
296+
297+ signature class ArgSig {
298+ /** Gets the type of this argument at `path`. */
299+ Type getTypeAt ( TypePath path ) ;
300+
301+ /** Gets the enclosing function of this argument. */
302+ Function getEnclosingFunction ( ) ;
303+
304+ /** Gets a textual representation of this argument. */
305+ string toString ( ) ;
306+
307+ /** Gets the location of this argument. */
308+ Location getLocation ( ) ;
309+ }
251310
252311/**
253312 * A wrapper around `IsInstantiationOf` which ensures to substitute in lookup
254313 * traits when checking whether argument types are instantiations of function
255314 * types.
256315 */
257- module ArgIsInstantiationOf<
258- HasTypeTreeSig Arg, IsInstantiationOfInputSig< Arg , AssocFunctionType > Input>
259- {
316+ module ArgIsInstantiationOf< ArgSig Arg, IsInstantiationOfInputSig< Arg , AssocFunctionType > Input> {
260317 final private class ArgFinal = Arg ;
261318
262319 private class ArgSubst extends ArgFinal {
263320 Type getTypeAt ( TypePath path ) {
264- result = substituteLookupTraits ( super .getTypeAt ( path ) ) and
321+ result = substituteLookupTraits0 ( this . getEnclosingFunction ( ) , super .getTypeAt ( path ) ) and
265322 not result = TNeverType ( ) and
266323 not result = TUnknownType ( )
267324 }
@@ -318,6 +375,8 @@ signature module ArgsAreInstantiationsOfInputSig {
318375
319376 Location getLocation ( ) ;
320377
378+ Function getEnclosingFunction ( ) ;
379+
321380 Type getArgType ( FunctionPosition pos , TypePath path ) ;
322381
323382 predicate hasTargetCand ( ImplOrTraitItemNode i , Function f ) ;
@@ -366,6 +425,8 @@ module ArgsAreInstantiationsOf<ArgsAreInstantiationsOfInputSig Input> {
366425
367426 FunctionPosition getPos ( ) { result = pos }
368427
428+ Function getEnclosingFunction ( ) { result = call .getEnclosingFunction ( ) }
429+
369430 Location getLocation ( ) { result = call .getLocation ( ) }
370431
371432 Type getTypeAt ( TypePath path ) { result = call .getArgType ( pos , path ) }
0 commit comments