Skip to content

Sema: Fix non-determinism with ConstraintSystem::ConstraintRestrictions #80057

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 2 commits into from
Mar 18, 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
10 changes: 0 additions & 10 deletions include/swift/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -3627,16 +3627,6 @@ class ConstraintSystem {
});
}

bool
hasConversionRestriction(Type type1, Type type2,
ConversionRestrictionKind restrictionKind) const {
auto restriction =
ConstraintRestrictions.find({type1.getPointer(), type2.getPointer()});
return restriction == ConstraintRestrictions.end()
? false
: restriction->second == restrictionKind;
}

/// If an UnresolvedDotExpr, SubscriptMember, etc has been resolved by the
/// constraint system, return the decl that it references.
ValueDecl *findResolvedMemberRef(ConstraintLocator *locator);
Expand Down
9 changes: 9 additions & 0 deletions lib/Sema/CSSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ Solution ConstraintSystem::finalize() {

CanType first = simplifyType(types.first)->getCanonicalType();
CanType second = simplifyType(types.second)->getCanonicalType();

// Pick the restriction with the highest value to avoid depending on
// iteration order.
auto found = solution.ConstraintRestrictions.find({first, second});
if (found != solution.ConstraintRestrictions.end() &&
(unsigned) restriction <= (unsigned) found->second) {
continue;
}

solution.ConstraintRestrictions[{first, second}] = restriction;
}
}
Expand Down
37 changes: 37 additions & 0 deletions test/SILGen/ambiguous_pointer_conversion.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// RUN: %target-swift-frontend -emit-silgen %s | %FileCheck %s

// rdar://146780049

class Image {}

class MyV {
var image: Image = Image()
}

func takesPtr(_: UnsafePointer<Image>?, _: UnsafePointer<Image>?, _: UnsafePointer<Image>?) {
}

func test(buffers: [MyV]) {
withUnsafePointer(to: buffers[0].image) { ptrA in
withUnsafePointer(to: buffers[1].image) { ptrB in
withUnsafePointer(to: buffers[2].image) { ptrC in
_ = takesPtr(ptrA, ptrB, ptrC)
}
}
}
}

// The other valid conversion here that should not be considered because it has a worse
// score is pointer-to-pointer instead of value-to-optional. The generated SIL looks
// quite different in that case, involving calls to intrinsics. Make sure we pick the
// value-to-optional conversion, because it generates much simpler SIL:

// CHECK-LABEL: sil private [ossa] @$s28ambiguous_pointer_conversion4test7buffersySayAA3MyVCG_tFySPyAA5ImageCGXEfU_yAIXEfU_yAIXEfU_ : $@convention(thin) @substituted <τ_0_0, τ_0_1, τ_0_2> (UnsafePointer<τ_0_0>, UnsafePointer<Image>, UnsafePointer<Image>) -> (@out τ_0_2, @error_indirect τ_0_1) for <Image, Never, ()> {
// CHECK: bb0(%0 : $*(), %1 : $*Never, %2 : $UnsafePointer<Image>, %3 : @closureCapture $UnsafePointer<Image>, %4 : @closureCapture $UnsafePointer<Image>):
// CHECK: [[X:%.*]] = enum $Optional<UnsafePointer<Image>>, #Optional.some!enumelt, %3
// CHECK: [[Y:%.*]] = enum $Optional<UnsafePointer<Image>>, #Optional.some!enumelt, %4
// CHECK: [[Z:%.*]] = enum $Optional<UnsafePointer<Image>>, #Optional.some!enumelt, %2
// CHECK: [[FN:%.*]] = function_ref @$s28ambiguous_pointer_conversion8takesPtryySPyAA5ImageCGSg_A2FtF : $@convention(thin) (Optional<UnsafePointer<Image>>, Optional<UnsafePointer<Image>>, Optional<UnsafePointer<Image>>) -> ()
// CHECK: apply %11([[X]], [[Y]], [[Z]]) : $@convention(thin) (Optional<UnsafePointer<Image>>, Optional<UnsafePointer<Image>>, Optional<UnsafePointer<Image>>) -> ()
// CHECK: return