codeql icon indicating copy to clipboard operation
codeql copied to clipboard

Rust: Add type inference for overloaded index expressions

Open paldepind opened this issue 5 months ago • 0 comments
trafficstars

This PR implements support for overloaded index expressions. The implementation is very similar to the existing overloaded operators.

Like the desugaring of * the desugaring of ..[..] includes a * after the overloaded call. This PR fixes a bug around those that I thought I fixed in https://github.com/github/codeql/pull/19820 but I made a mistake: In inferCallExprBaseType the first and last disjunct could be true at the same time since apos.isReturn() and a instanceof DerefExpr and not apos.isBorrowed(_) are not exclusive. So both disjunct where true in the return position of *. Fixing that removes a whole bunch of spurious reference types 😅

I had hoped we could remove the current special casing for arrays and slices, but that's not possible. The Index instances for those are more complex than what we can understand. See for instance the one for slices:

impl<T, I> ops::Index<I> for [T]
where
    I: SliceIndex<[T]>,
{
    type Output = I::Output;

    #[inline(always)]
    fn index(&self, index: I) -> &I::Output {
        index.index(self)
    }
}

It seems that the type of I is somehow deduced from the I: SliceIndex<[T]> bound and that is not something we can currently handle. So while this works great for Vec and other types, we still need the special casing.

Also, I left the data flow library as-is. Following this PR we could implement index expressions as calls there as well, and that should allow us to add models for index expressions on custom types.

paldepind avatar Jun 20 '25 14:06 paldepind