Skip to content

Commit b34fa7a

Browse files
authored
Merge pull request #20393 from paldepind/rust/impl-to-string
Rust: Improve `toString` for `impl`
2 parents 417e79c + f7c7466 commit b34fa7a

File tree

13 files changed

+79
-60
lines changed

13 files changed

+79
-60
lines changed

rust/ql/.generated.list

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/ql/.gitattributes

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/ql/lib/codeql/rust/elements/internal/ImplTraitTypeReprImpl.qll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,9 @@ module Impl {
3636
predicate isInReturnPos() {
3737
exists(Function f | f.getRetType().getTypeRepr() = this.getFunctionTypeRepr(f))
3838
}
39+
40+
override string toStringImpl() {
41+
result = "impl " + this.getTypeBoundList().toAbbreviatedString()
42+
}
3943
}
4044
}

rust/ql/lib/codeql/rust/elements/internal/LifetimeImpl.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ module Impl {
2222
* ```
2323
*/
2424
class Lifetime extends Generated::Lifetime {
25-
override string toStringImpl() {
25+
override string toStringImpl() { result = this.toAbbreviatedString() }
26+
27+
override string toAbbreviatedString() {
2628
result = this.getText()
2729
or
2830
not this.hasText() and result = "'_"

rust/ql/lib/codeql/rust/elements/internal/TypeBoundImpl.qll

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// generated by codegen, remove this comment if you wish to edit this file
21
/**
32
* This module provides a hand-modifiable wrapper around the generated class `TypeBound`.
43
*
@@ -12,6 +11,7 @@ private import codeql.rust.elements.internal.generated.TypeBound
1211
* be referenced directly.
1312
*/
1413
module Impl {
14+
// the following QLdoc is generated: if you need to edit it, do it in the schema file
1515
/**
1616
* A type bound in a trait or generic parameter.
1717
*
@@ -23,5 +23,12 @@ module Impl {
2323
* // ^^^^^^^^^^^^^^^^^^^^^
2424
* ```
2525
*/
26-
class TypeBound extends Generated::TypeBound { }
26+
class TypeBound extends Generated::TypeBound {
27+
override string toAbbreviatedString() {
28+
result = this.getLifetime().toAbbreviatedString()
29+
or
30+
not this.hasLifetime() and
31+
result = "..."
32+
}
33+
}
2734
}

rust/ql/lib/codeql/rust/elements/internal/TypeBoundListImpl.qll

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// generated by codegen, remove this comment if you wish to edit this file
21
/**
32
* This module provides a hand-modifiable wrapper around the generated class `TypeBoundList`.
43
*
@@ -12,6 +11,7 @@ private import codeql.rust.elements.internal.generated.TypeBoundList
1211
* be referenced directly.
1312
*/
1413
module Impl {
14+
// the following QLdoc is generated: if you need to edit it, do it in the schema file
1515
/**
1616
* A list of type bounds.
1717
*
@@ -21,5 +21,15 @@ module Impl {
2121
* // ^^^^^^^^^^^^^
2222
* ```
2323
*/
24-
class TypeBoundList extends Generated::TypeBoundList { }
24+
class TypeBoundList extends Generated::TypeBoundList {
25+
override string toStringImpl() { result = this.toAbbreviatedString() }
26+
27+
private string toAbbreviatedStringPart(int index) {
28+
result = this.getBound(index).toAbbreviatedString()
29+
}
30+
31+
override string toAbbreviatedString() {
32+
result = strictconcat(int i | | this.toAbbreviatedStringPart(i), " + " order by i)
33+
}
34+
}
2535
}

rust/ql/test/extractor-tests/generated/AssocTypeArg/AssocTypeArg.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ getRetType
99
getReturnTypeSyntax
1010
getTypeRepr
1111
getTypeBoundList
12-
| gen_assoc_type_arg.rs:9:21:9:31 | AssocTypeArg | gen_assoc_type_arg.rs:9:27:9:31 | TypeBoundList |
12+
| gen_assoc_type_arg.rs:9:21:9:31 | AssocTypeArg | gen_assoc_type_arg.rs:9:27:9:31 | ... |
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
instances
22
| gen_dyn_trait_type_repr.rs:7:13:7:21 | DynTraitTypeRepr |
33
getTypeBoundList
4-
| gen_dyn_trait_type_repr.rs:7:13:7:21 | DynTraitTypeRepr | gen_dyn_trait_type_repr.rs:7:17:7:21 | TypeBoundList |
4+
| gen_dyn_trait_type_repr.rs:7:13:7:21 | DynTraitTypeRepr | gen_dyn_trait_type_repr.rs:7:17:7:21 | ... |
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
instances
2-
| gen_impl_trait_type_repr.rs:7:17:7:41 | ImplTraitTypeRepr |
2+
| gen_impl_trait_type_repr.rs:7:17:7:41 | impl ... |
33
getTypeBoundList
4-
| gen_impl_trait_type_repr.rs:7:17:7:41 | ImplTraitTypeRepr | gen_impl_trait_type_repr.rs:7:22:7:41 | TypeBoundList |
4+
| gen_impl_trait_type_repr.rs:7:17:7:41 | impl ... | gen_impl_trait_type_repr.rs:7:22:7:41 | ... |

rust/ql/test/extractor-tests/generated/TraitAlias/TraitAlias.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ getGenericParamList
66
getName
77
| gen_trait_alias.rs:7:5:7:26 | TraitAlias | gen_trait_alias.rs:7:11:7:13 | Foo |
88
getTypeBoundList
9-
| gen_trait_alias.rs:7:5:7:26 | TraitAlias | gen_trait_alias.rs:7:17:7:25 | TypeBoundList |
9+
| gen_trait_alias.rs:7:5:7:26 | TraitAlias | gen_trait_alias.rs:7:17:7:25 | ... + ... |
1010
getVisibility
1111
getWhereClause

0 commit comments

Comments
 (0)