Skip to content

Rust: Implement support for inference of type aliases #19146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion rust/ql/lib/codeql/rust/internal/TypeMention.qll
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,31 @@ class TypeReprMention extends TypeMention, TypeRepr {
or
result = this.(PathTypeRepr).getPath().(PathMention).resolveType()
}

override Type resolveTypeAt(TypePath path) {
result = this.(PathTypeRepr).getPath().(PathMention).resolveTypeAt(path)
or
not exists(this.(PathTypeRepr).getPath()) and
result = super.resolveTypeAt(path)
}
}

/** Holds if `path` resolves to the type alias `alias` with the definition `rhs`. */
private predicate resolvePathAlias(Path path, TypeAlias alias, TypeReprMention rhs) {
alias = resolvePath(path) and rhs = alias.getTypeRepr()
}

class PathMention extends TypeMention, Path {
abstract class PathMention extends TypeMention, Path {
override TypeMention getTypeArgument(int i) {
result = this.getSegment().getGenericArgList().getTypeArg(i)
}
}

class NonAliasPathMention extends PathMention {
NonAliasPathMention() { not resolvePathAlias(this, _, _) }

override TypeMention getTypeArgument(int i) {
result = super.getTypeArgument(i)
or
// `Self` paths inside `impl` blocks have implicit type arguments that are
// the type parameters of the `impl` block. For example, in
Expand Down Expand Up @@ -98,6 +118,33 @@ class PathMention extends TypeMention, Path {
}
}

class AliasPathMention extends PathMention {
TypeAlias alias;
TypeReprMention rhs;

AliasPathMention() { resolvePathAlias(this, alias, rhs) }

/** Get the `i`th type parameter of the alias itself. */
private TypeParameter getTypeParameter(int i) {
result = TTypeParamTypeParameter(alias.getGenericParamList().getTypeParam(i))
}

override Type resolveType() { result = rhs.resolveType() }

override Type resolveTypeAt(TypePath path) {
result = rhs.resolveTypeAt(path) and
not result = this.getTypeParameter(_)
or
exists(TypeParameter tp, TypeMention arg, TypePath prefix, TypePath suffix, int i |
tp = rhs.resolveTypeAt(prefix) and
tp = this.getTypeParameter(i) and
arg = this.getTypeArgument(i) and
result = arg.resolveTypeAt(suffix) and
path = prefix.append(suffix)
)
}
}

// Used to represent implicit `Self` type arguments in traits and `impl` blocks,
// see `PathMention` for details.
class TypeParamMention extends TypeMention, TypeParam {
Expand Down
32 changes: 28 additions & 4 deletions rust/ql/test/library-tests/type-inference/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,17 @@ mod type_aliases {
PairBoth(Fst, Snd),
}

impl<Fst, Snd> PairOption<Fst, Snd> {
fn unwrapSnd(self) -> Snd {
match self {
PairOption::PairNone() => panic!("PairNone has no second element"),
PairOption::PairFst(_) => panic!("PairFst has no second element"),
PairOption::PairSnd(snd) => snd,
PairOption::PairBoth(_, snd) => snd,
}
}
}

#[derive(Debug)]
struct S1;

Expand All @@ -553,24 +564,37 @@ mod type_aliases {
type MyPair = PairOption<S1, S2>;

// Generic type alias that partially applies the generic type
type AnotherPair<Thr> = PairOption<S2, Thr>;
type AnotherPair<A3> = PairOption<S2, A3>;

// Alias to another alias
type AliasToAlias<A4> = AnotherPair<A4>;

// Alias that appears nested within another alias
type NestedAlias<A5> = AnotherPair<AliasToAlias<A5>>;

fn g(t: NestedAlias<S3>) {
let x = t.unwrapSnd().unwrapSnd(); // $ method=unwrapSnd type=x:S3
println!("{:?}", x);
}

pub fn f() {
// Type can be inferred from the constructor
let p1: MyPair = PairOption::PairBoth(S1, S2);
println!("{:?}", p1);

// Type can be only inferred from the type alias
let p2: MyPair = PairOption::PairNone(); // types for `Fst` and `Snd` missing
let p2: MyPair = PairOption::PairNone(); // $ type=p2:Fst.S1 type=p2:Snd.S2
println!("{:?}", p2);

// First type from alias, second from constructor
let p3: AnotherPair<_> = PairOption::PairSnd(S3); // type for `Fst` missing
let p3: AnotherPair<_> = PairOption::PairSnd(S3); // $ type=p3:Fst.S2
println!("{:?}", p3);

// First type from alias definition, second from argument to alias
let p3: AnotherPair<S3> = PairOption::PairNone(); // type for `Snd` missing, spurious `S3` for `Fst`
let p3: AnotherPair<S3> = PairOption::PairNone(); // $ type=p3:Fst.S2 type=p3:Snd.S3
println!("{:?}", p3);

g(PairOption::PairSnd(PairOption::PairSnd(S3)));
}
}

Expand Down
Loading