Rust: Make Element.toString non-recursive

This commit is contained in:
Tom Hvitved
2025-03-31 14:37:02 +02:00
parent 279e9e2d70
commit 4fb138a1a3
7 changed files with 31 additions and 8 deletions

View File

@@ -30,7 +30,11 @@ module Generated {
* Gets the string representation of this element. * Gets the string representation of this element.
*/ */
cached cached
final string toString() { result = this.toStringImpl() } final string toString() {
result = this.toStringImpl() and
// recursion guard to prevent `toString` from being defined recursively
(exists(any(Element e).toStringImpl()) implies any())
}
/** /**
* INTERNAL: Do not use. * INTERNAL: Do not use.

View File

@@ -506,7 +506,7 @@ lib/codeql/rust/elements/internal/generated/ConstParam.qll 310342603959a4d521418
lib/codeql/rust/elements/internal/generated/ContinueExpr.qll e2010feb14fb6edeb83a991d9357e50edb770172ddfde2e8670b0d3e68169f28 48d09d661e1443002f6d22b8710e22c9c36d9daa9cde09c6366a61e960d717cb lib/codeql/rust/elements/internal/generated/ContinueExpr.qll e2010feb14fb6edeb83a991d9357e50edb770172ddfde2e8670b0d3e68169f28 48d09d661e1443002f6d22b8710e22c9c36d9daa9cde09c6366a61e960d717cb
lib/codeql/rust/elements/internal/generated/Crate.qll d245f24e9da4f180c526a6d092f554a9577bae7225c81c36a391947c0865eeb3 c95dbb32b2ce4d9664be56c95b19fcd01c2d3244385e55151f9b06b07f04ce9b lib/codeql/rust/elements/internal/generated/Crate.qll d245f24e9da4f180c526a6d092f554a9577bae7225c81c36a391947c0865eeb3 c95dbb32b2ce4d9664be56c95b19fcd01c2d3244385e55151f9b06b07f04ce9b
lib/codeql/rust/elements/internal/generated/DynTraitTypeRepr.qll a9d540717af1f00dbea1c683fd6b846cddfb2968c7f3e021863276f123337787 1972efb9bca7aae9a9708ca6dcf398e5e8c6d2416a07d525dba1649b80fbe4d1 lib/codeql/rust/elements/internal/generated/DynTraitTypeRepr.qll a9d540717af1f00dbea1c683fd6b846cddfb2968c7f3e021863276f123337787 1972efb9bca7aae9a9708ca6dcf398e5e8c6d2416a07d525dba1649b80fbe4d1
lib/codeql/rust/elements/internal/generated/Element.qll 69ce882811f2bef7e0a93c0a24494dd16120a108ba4180d455344e29144a98c4 7781bc5c69b5b08775902fcb97cb23f85359ef2303545afe9d44301b19024b3a lib/codeql/rust/elements/internal/generated/Element.qll d56d22c060fa929464f837b1e16475a4a2a2e42d68235a014f7369bcb48431db 0e48426ca72179f675ac29aa49bbaadb8b1d27b08ad5cbc72ec5a005c291848e
lib/codeql/rust/elements/internal/generated/Enum.qll 4f4cbc9cd758c20d476bc767b916c62ba434d1750067d0ffb63e0821bb95ec86 3da735d54022add50cec0217bbf8ec4cf29b47f4851ee327628bcdd6454989d0 lib/codeql/rust/elements/internal/generated/Enum.qll 4f4cbc9cd758c20d476bc767b916c62ba434d1750067d0ffb63e0821bb95ec86 3da735d54022add50cec0217bbf8ec4cf29b47f4851ee327628bcdd6454989d0
lib/codeql/rust/elements/internal/generated/Expr.qll 5fa34f2ed21829a1509417440dae42d416234ff43433002974328e7aabb8f30f 46f3972c7413b7db28a3ea8acb5a50a74b6dd9b658e8725f6953a8829ac912f8 lib/codeql/rust/elements/internal/generated/Expr.qll 5fa34f2ed21829a1509417440dae42d416234ff43433002974328e7aabb8f30f 46f3972c7413b7db28a3ea8acb5a50a74b6dd9b658e8725f6953a8829ac912f8
lib/codeql/rust/elements/internal/generated/ExprStmt.qll d1112230015fbeb216b43407a268dc2ccd0f9e0836ab2dca4800c51b38fa1d7d 4a80562dcc55efa5e72c6c3b1d6747ab44fe494e76faff2b8f6e9f10a4b08b5b lib/codeql/rust/elements/internal/generated/ExprStmt.qll d1112230015fbeb216b43407a268dc2ccd0f9e0836ab2dca4800c51b38fa1d7d 4a80562dcc55efa5e72c6c3b1d6747ab44fe494e76faff2b8f6e9f10a4b08b5b

View File

@@ -20,6 +20,7 @@ module Impl {
* Returns a string suitable to be inserted into the name of the parent. Typically `"..."`, * Returns a string suitable to be inserted into the name of the parent. Typically `"..."`,
* but may be overridden by subclasses. * but may be overridden by subclasses.
*/ */
pragma[nomagic]
string toAbbreviatedString() { result = "..." } string toAbbreviatedString() { result = "..." }
predicate isUnknown() { none() } // compatibility with test generation, to be fixed predicate isUnknown() { none() } // compatibility with test generation, to be fixed

View File

@@ -24,6 +24,6 @@ module Impl {
* ``` * ```
*/ */
class Module extends Generated::Module { class Module extends Generated::Module {
override string toStringImpl() { result = "mod " + this.getName() } override string toStringImpl() { result = "mod " + this.getName().getText() }
} }
} }

View File

@@ -19,6 +19,6 @@ module Impl {
* ``` * ```
*/ */
class Use extends Generated::Use { class Use extends Generated::Use {
override string toStringImpl() { result = "use " + this.getUseTree() } override string toStringImpl() { result = "use " + this.getUseTree().toAbbreviatedString() }
} }
} }

View File

@@ -26,14 +26,28 @@ module Impl {
result = strictconcat(int i | | this.toStringPart(i) order by i) result = strictconcat(int i | | this.toStringPart(i) order by i)
} }
private string toStringPart(int index) { private string toStringPartCommon(int index) {
result = this.getPath().toStringImpl() and index = 0
or
result = "::{...}" and this.hasUseTreeList() and index = 1 result = "::{...}" and this.hasUseTreeList() and index = 1
or or
result = "::*" and this.isGlob() and index = 2 result = "::*" and this.isGlob() and index = 2
or or
result = " as " + this.getRename().getName().getText() and index = 3 result = " as " + this.getRename().getName().getText() and index = 3
} }
private string toStringPart(int index) {
result = this.getPath().toStringImpl() and index = 0
or
result = this.toStringPartCommon(index)
}
override string toAbbreviatedString() {
result = strictconcat(int i | | this.toAbbreviatedStringPart(i) order by i)
}
private string toAbbreviatedStringPart(int index) {
result = this.getPath().toAbbreviatedString() and index = 0
or
result = this.toStringPartCommon(index)
}
} }
} }

View File

@@ -24,7 +24,11 @@ module Generated {
* Gets the string representation of this element. * Gets the string representation of this element.
*/ */
cached cached
final string toString() { result = this.toStringImpl() } final string toString() {
result = this.toStringImpl() and
// recursion guard to prevent `toString` from being defined recursively
(exists(any(Element e).toStringImpl()) implies any())
}
/** /**
* INTERNAL: Do not use. * INTERNAL: Do not use.