Skip to content

C#: Handle non-unique type arguments when computing generics strings #19782

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 1 commit into from
Jun 17, 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
5 changes: 4 additions & 1 deletion csharp/ql/lib/semmle/code/csharp/Generics.qll
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,12 @@ private string getTypeArgumentsToString(ConstructedGeneric cg) {
strictconcat(Type t, int i | t = cg.getTypeArgument(i) | t.toStringWithTypes(), ", " order by i)
}

language[monotonicAggregates]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add comments stating that this is merely a precaution and that we expect he concat to only concat a single name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

pragma[nomagic]
private string getTypeArgumentName(ConstructedGeneric cg, int i) {
result = cg.getTypeArgument(i).getName()
// Normally, `cg.getTypeArgument(i)` will be unique, and in those cases the line below
// is simply the same as `result = cg.getTypeArgument(i).getName()`.
result = strictconcat(Type t | t = cg.getTypeArgument(i) | t.getName(), "/")
}

/** Gets the concatenation of the `getName()` of type arguments. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@ module QualifiedName<QualifiedNameInputSig Input> {
)
}

language[monotonicAggregates]
pragma[nomagic]
private string getTypeArgumentsQualifiedName(ConstructedGeneric cg, int i) {
result = getFullName(cg.getTypeArgument(i))
// Normally, `cg.getTypeArgument(i)` will be unique, and in those cases the line below
// is simply the same as `result = getFullName(cg.getTypeArgument(i))`.
result = strictconcat(Type t | t = cg.getTypeArgument(i) | getFullName(t), "/")
}

/** Gets the concatenation of the `getFullName` of type arguments. */
Expand Down