From d88cfe98f83db9500ebb64b179a9f63e4aaf4399 Mon Sep 17 00:00:00 2001 From: Simon Friis Vindum Date: Thu, 27 Nov 2025 09:35:31 +0100 Subject: [PATCH] Rust: Implement `toString` for struct fields and visibility --- rust/ql/.generated.list | 1 - rust/ql/.gitattributes | 1 - .../rust/elements/internal/StructFieldImpl.qll | 12 ++++++++++++ .../codeql/rust/elements/internal/VisibilityImpl.qll | 12 ++++++++++-- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/rust/ql/.generated.list b/rust/ql/.generated.list index fc9e0d0f30c..98cd6b01af4 100644 --- a/rust/ql/.generated.list +++ b/rust/ql/.generated.list @@ -434,7 +434,6 @@ lib/codeql/rust/elements/internal/VariantConstructor.qll 0297d4a9a9b32448d6d6063 lib/codeql/rust/elements/internal/VariantListConstructor.qll c841fb345eb46ea3978a0ed7a689f8955efc9178044b140b74d98a6bcd0c926a c9e52d112abdba2b60013fa01a944c8770766bf7368f9878e6b13daaa4eed446 lib/codeql/rust/elements/internal/VariantListImpl.qll 4ceeda617696eb547c707589ba26103cf4c5c3d889955531be24cbf224e79dff 4258196c126fd2fad0e18068cb3d570a67034a8b26e2f13f8223d7f1a246d1a4 lib/codeql/rust/elements/internal/VisibilityConstructor.qll 1fd30663d87945f08d15cfaca54f586a658f26b7a98ea45ac73a35d36d4f65d0 6ddaf11742cc8fbbe03af2aa578394041ae077911e62d2fa6c885ae0543ba53a -lib/codeql/rust/elements/internal/VisibilityImpl.qll 85c1e75d6a7f9246cfef5c261e2aea40891c016724de49b3d6632623ccc30dcf 278be4648a8aefb0d926480c4d98e1605196ad64d1e4dbad42aa58499e6d485d lib/codeql/rust/elements/internal/WhereClauseConstructor.qll 6d6f0f0376cf45fac37ea0c7c4345d08718d2a3d6d913e591de1de9e640317c9 ff690f3d4391e5f1fae6e9014365810105e8befe9d6b52a82625994319af9ffd lib/codeql/rust/elements/internal/WhereClauseImpl.qll 006e330df395183d15896e5f81128e24b8274d849fe45afb5040444e4b764226 ed5e8317b5f33104e5c322588dc400755c8852bbb77ef835177b13af7480fd43 lib/codeql/rust/elements/internal/WherePredConstructor.qll f331c37085792a01159e8c218e9ef827e80e99b7c3d5978b6489808f05bd11f8 179cad3e4c5aaaf27755891694ef3569322fcf34c5290e6af49e5b5e3f8aa732 diff --git a/rust/ql/.gitattributes b/rust/ql/.gitattributes index 99728972ec7..6afbf118f11 100644 --- a/rust/ql/.gitattributes +++ b/rust/ql/.gitattributes @@ -436,7 +436,6 @@ /lib/codeql/rust/elements/internal/VariantListConstructor.qll linguist-generated /lib/codeql/rust/elements/internal/VariantListImpl.qll linguist-generated /lib/codeql/rust/elements/internal/VisibilityConstructor.qll linguist-generated -/lib/codeql/rust/elements/internal/VisibilityImpl.qll linguist-generated /lib/codeql/rust/elements/internal/WhereClauseConstructor.qll linguist-generated /lib/codeql/rust/elements/internal/WhereClauseImpl.qll linguist-generated /lib/codeql/rust/elements/internal/WherePredConstructor.qll linguist-generated diff --git a/rust/ql/lib/codeql/rust/elements/internal/StructFieldImpl.qll b/rust/ql/lib/codeql/rust/elements/internal/StructFieldImpl.qll index 4ed4466c2d9..a8e8a0fcf87 100644 --- a/rust/ql/lib/codeql/rust/elements/internal/StructFieldImpl.qll +++ b/rust/ql/lib/codeql/rust/elements/internal/StructFieldImpl.qll @@ -29,5 +29,17 @@ module Impl { /** Holds if this record field is named `name` and belongs to the struct `s`. */ predicate isStructField(Struct s, string name) { this = s.getStructField(name) } + + override string toStringImpl() { result = concat(int i | | this.toStringPart(i) order by i) } + + private string toStringPart(int index) { + index = 0 and result = this.getVisibility().toAbbreviatedString() + " " + or + index = 1 and result = this.getName().getText() + or + index = 2 and result = ": " + or + index = 3 and result = this.getTypeRepr().toAbbreviatedString() + } } } diff --git a/rust/ql/lib/codeql/rust/elements/internal/VisibilityImpl.qll b/rust/ql/lib/codeql/rust/elements/internal/VisibilityImpl.qll index 21de0c88ca8..e2bc7140b59 100644 --- a/rust/ql/lib/codeql/rust/elements/internal/VisibilityImpl.qll +++ b/rust/ql/lib/codeql/rust/elements/internal/VisibilityImpl.qll @@ -1,4 +1,3 @@ -// generated by codegen, remove this comment if you wish to edit this file /** * This module provides a hand-modifiable wrapper around the generated class `Visibility`. * @@ -12,6 +11,7 @@ private import codeql.rust.elements.internal.generated.Visibility * be referenced directly. */ module Impl { + // the following QLdoc is generated: if you need to edit it, do it in the schema file /** * A visibility modifier. * @@ -21,5 +21,13 @@ module Impl { * //^^^ * ``` */ - class Visibility extends Generated::Visibility { } + class Visibility extends Generated::Visibility { + override string toStringImpl() { result = this.toAbbreviatedString() } + + override string toAbbreviatedString() { + result = "pub(" + this.getPath().toAbbreviatedString() + ")" + or + not this.hasPath() and result = "pub" + } + } }