Skip to content

[ClangImporter] Import C++ subscript overloads in stable order#89478

Open
airspeedswift wants to merge 1 commit into
swiftlang:mainfrom
airspeedswift:cxx-interop-deterministic-subscript-import
Open

[ClangImporter] Import C++ subscript overloads in stable order#89478
airspeedswift wants to merge 1 commit into
swiftlang:mainfrom
airspeedswift:cxx-interop-deterministic-subscript-import

Conversation

@airspeedswift
Copy link
Copy Markdown
Member

lookupAndImportSubscripts grouped operator[] overloads in a SmallDenseMap keyed by CXXOverloadArgTypes, whose hash is built from clang::QualType opaque pointers. ASLR makes those pointer values vary across runs, so iteration order over the map varies too — and that order determined the order of subscripts added to the imported Swift struct. Downstream, that fed the constraint solver's overload set in varying order; for cases with multiple equally-ranked viable candidates, which one wins could change run-to-run.

Switch to SmallMapVector so iteration follows insertion order. The hash-keyed lookup that groups same-arg-type getters/setters is preserved; only iteration becomes deterministic.

Concrete repro: typechecking
test/Interop/Cxx/operators/subscript-overloads-typechecker.swift 20 times produced 3 distinct outputs before this change and 1 afterwards.

`lookupAndImportSubscripts` grouped operator[] overloads in a
SmallDenseMap keyed by CXXOverloadArgTypes, whose hash is built from
clang::QualType opaque pointers. ASLR makes those pointer values vary
across runs, so iteration order over the map varies too — and that
order determined the order of subscripts added to the imported Swift
struct. Downstream, that fed the constraint solver's overload set in
varying order; for cases with multiple equally-ranked viable
candidates, *which one wins* could change run-to-run.

Switch to SmallMapVector so iteration follows insertion order. The
hash-keyed lookup that groups same-arg-type getters/setters is
preserved; only iteration becomes deterministic.

Concrete repro: typechecking
test/Interop/Cxx/operators/subscript-overloads-typechecker.swift
20 times produced 3 distinct outputs before this change and 1
afterwards.
@airspeedswift
Copy link
Copy Markdown
Member Author

@swift-ci please smoke test

Copy link
Copy Markdown
Contributor

@j-hui j-hui left a comment

Choose a reason for hiding this comment

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

Oof, thanks for catching this!

Out of curiosity, how did this manifest elsewhere/outside of that test?

@airspeedswift
Copy link
Copy Markdown
Member Author

airspeedswift commented May 27, 2026

I was trying to fix diagnostics like this and the tests for the fix were failing because the results varied run to run. That would mean if there were multiple diagnostics, the order would change (including the top one Xcode shows) unless we sorted them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants