Skip to content

Commit b2233ee

Browse files
authored
Merge pull request #60883 from xedin/rdar-99352676
[ConstraintSystem] Teach `init` ref validation about implicit conversions
2 parents 94cbc76 + 4571c26 commit b2233ee

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8861,17 +8861,14 @@ static ConstraintFix *validateInitializerRef(ConstraintSystem &cs,
88618861
if (!anchor)
88628862
return nullptr;
88638863

8864+
// Avoid checking implicit conversions injected by the compiler.
8865+
if (locator->findFirst<LocatorPathElt::ImplicitConversion>())
8866+
return nullptr;
8867+
88648868
auto getType = [&cs](Expr *expr) -> Type {
88658869
return cs.simplifyType(cs.getType(expr))->getRValueType();
88668870
};
88678871

8868-
auto locatorEndsWith =
8869-
[](ConstraintLocator *locator,
8870-
ConstraintLocator::PathElementKind eltKind) -> bool {
8871-
auto path = locator->getPath();
8872-
return !path.empty() && path.back().getKind() == eltKind;
8873-
};
8874-
88758872
Expr *baseExpr = nullptr;
88768873
Type baseType;
88778874

@@ -8928,7 +8925,7 @@ static ConstraintFix *validateInitializerRef(ConstraintSystem &cs,
89288925
// member.
89298926
// We need to find type variable which represents contextual base.
89308927
auto *baseLocator = cs.getConstraintLocator(
8931-
UME, locatorEndsWith(locator, ConstraintLocator::ConstructorMember)
8928+
UME, locator->isLastElement<LocatorPathElt::ConstructorMember>()
89328929
? ConstraintLocator::UnresolvedMember
89338930
: ConstraintLocator::MemberRefBase);
89348931

@@ -8943,7 +8940,7 @@ static ConstraintFix *validateInitializerRef(ConstraintSystem &cs,
89438940
baseType = cs.simplifyType(*result)->getRValueType();
89448941
// Constraint for member base is formed as '$T.Type[.<member] = ...`
89458942
// which means MetatypeType has to be added after finding a type variable.
8946-
if (locatorEndsWith(baseLocator, ConstraintLocator::MemberRefBase))
8943+
if (baseLocator->isLastElement<LocatorPathElt::MemberRefBase>())
89478944
baseType = MetatypeType::get(baseType);
89488945
} else if (auto *keyPathExpr = getAsExpr<KeyPathExpr>(anchor)) {
89498946
// Key path can't refer to initializers e.g. `\Type.init`

test/Constraints/implicit_double_cgfloat_conversion.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,3 +330,15 @@ func test_implicit_conversion_clash_with_partial_application_check() {
330330
}
331331
}
332332
}
333+
334+
// rdar://99352676
335+
func test_init_validation() {
336+
class Foo {
337+
static let bar = 100.0
338+
339+
func getBar() -> CGFloat? {
340+
return Self.bar
341+
// CHECK: function_ref @$s12CoreGraphics7CGFloatVyACSdcfC : $@convention(method) (Double, @thin CGFloat.Type) -> CGFloat
342+
}
343+
}
344+
}

0 commit comments

Comments
 (0)