Skip to content

Conversation

patrykstefanski
Copy link

Before this change, we could only map ValueDecl to Expr to model dependent values. This change adds support for mapping ValueDecl with optional dereference to Expr. For example, this allows us to model dependent values with the following mapping:

  count -> 42
  *out_count -> 100

Supporting dereference is necessary in order to check assignments to inout pointer and count in the future.

@patrykstefanski patrykstefanski self-assigned this Sep 29, 2025
@patrykstefanski patrykstefanski added the clang:bounds-safety Issue relating to the experimental -fbounds-safety feature in Clang label Sep 29, 2025
@patrykstefanski
Copy link
Author

This is taken from the original PR: #11490

@hnrklssn
Copy link

What happens with something like void foo(int * __counted_by(x) &p, int &x) in this model?

@patrykstefanski
Copy link
Author

What happens with something like void foo(int * __counted_by(x) &p, int &x) in this model?

We don't have plans to support C++ refs. If any of this works, we should file a bug and fix it.

// This function will be repeatedly called on the "Other" Expr, because the
// kind of "Other" stays unknown during the traversal.
const Expr *
trySubstituteAndSimplify(const Expr *E, bool &hasBeenSubstituted,

Choose a reason for hiding this comment

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

The trySubstituteAndSimplify function only applies to Other in the comparator. Do you want to apply such substitution to both comparands?

Copy link
Author

Choose a reason for hiding this comment

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

I updated VisitUnaryOperator to try to substitute Self before comparing the expr with Other. So, if Self has deref, it will substitute it first, and then recursively compare to Other.

@ziqingluo-90
Copy link

What is a motivating example of this? I'm curious where you can extract the information that *out_count -> 100?

@patrykstefanski
Copy link
Author

What is a motivating example of this? I'm curious where you can extract the information that *out_count -> 100?

A test case from my original PR:

void good_inout_subspan_const(int *__counted_by(*count) *p, size_t *count, std::span<int> sp) {
  *p = sp.first(42).data();
  *count = 42;
}

Here, we would have *count -> 42.

Before this change, we could only map ValueDecl to Expr to model
dependent values. This change adds support for mapping ValueDecl with
optional dereference to Expr. For example, this allows us to model
dependent values with the following mapping:
  ```
    count -> 42
    *out_count -> 100
  ```
Supporting dereference is necessary in order to check assignments to
inout pointer and count in the future.
@patrykstefanski patrykstefanski force-pushed the eng/pstefanski/dependent-values-with-deref branch from 44efedf to 0699295 Compare September 30, 2025 21:18
@patrykstefanski
Copy link
Author

@swift-ci llvm test

@patrykstefanski
Copy link
Author

@swift-ci test llvm

@patrykstefanski patrykstefanski merged commit a1cc31b into swiftlang:next Oct 1, 2025
0 of 2 checks passed
@patrykstefanski patrykstefanski deleted the eng/pstefanski/dependent-values-with-deref branch October 1, 2025 17:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:bounds-safety Issue relating to the experimental -fbounds-safety feature in Clang
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants