From 00e95a075733f9ed97d2ffcccbfee5c63d593d83 Mon Sep 17 00:00:00 2001 From: Tom Hvitved Date: Mon, 1 Jun 2026 09:34:49 +0200 Subject: [PATCH] Rust: Add `Impl::getSelf()` and `Impl::getTrait()` --- .../rust/elements/internal/ImplImpl.qll | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/rust/ql/lib/codeql/rust/elements/internal/ImplImpl.qll b/rust/ql/lib/codeql/rust/elements/internal/ImplImpl.qll index 54bf6932420..87c42ed7dad 100644 --- a/rust/ql/lib/codeql/rust/elements/internal/ImplImpl.qll +++ b/rust/ql/lib/codeql/rust/elements/internal/ImplImpl.qll @@ -4,7 +4,11 @@ * INTERNAL: Do not use. */ +private import rust private import codeql.rust.elements.internal.generated.Impl +private import codeql.rust.internal.PathResolution as PathResolution +private import codeql.rust.internal.typeinference.Type +private import codeql.rust.internal.typeinference.TypeMention /** * INTERNAL: This module contains the customizable definition of `Impl` and should not @@ -39,5 +43,39 @@ module Impl { */ pragma[nomagic] predicate isInherent() { not this.hasTraitTy() } + + /** + * Gets the type being implemented. + * + * For example, in + * + * ```rust + * impl MyType { ... } + * + * impl MyTrait for MyType { ... } + * ``` + * + * the type being implemented is in both cases`MyType`. + */ + TypeItem getSelf() { + result = this.getSelfTy().(TypeMention).getType().(DataType).getTypeItem() + } + + /** + * Gets the trait being implemented, if any. + * + * For example, in + * + * ```rust + * impl MyType { ... } + * + * impl MyTrait for MyType { ... } + * ``` + * + * the trait being implemented is in the second case `MyTrait`. + */ + Trait getTrait() { + result = PathResolution::resolvePath(this.getTraitTy().(PathTypeRepr).getPath()) + } } }