Skip to content

Commit 8e1e167

Browse files
authored
Merge pull request #39096 from xedin/rdar-80271666-take-2
[ConstraintSystem] Allow IUO types to be unrelated while forming a di…
2 parents 068291c + c126988 commit 8e1e167

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4524,8 +4524,25 @@ class ConstraintSystem {
45244524
Type underlyingType;
45254525
if (auto *fnTy = type->getAs<AnyFunctionType>())
45264526
underlyingType = replaceFinalResultTypeWithUnderlying(fnTy);
4527-
else
4527+
else if (auto *typeVar =
4528+
type->getWithoutSpecifierType()->getAs<TypeVariableType>()) {
4529+
auto *locator = typeVar->getImpl().getLocator();
4530+
4531+
// If `type` hasn't been resolved yet, we need to allocate a type
4532+
// variable to represent an object type of a future optional, and
4533+
// add a constraint beetween `type` and `underlyingType` to model it.
4534+
underlyingType = createTypeVariable(
4535+
getConstraintLocator(locator, LocatorPathElt::GenericArgument(0)),
4536+
TVO_PrefersSubtypeBinding | TVO_CanBindToLValue |
4537+
TVO_CanBindToNoEscape);
4538+
4539+
// Using a `typeVar` here because l-value is going to be applied
4540+
// to the underlying type below.
4541+
addConstraint(ConstraintKind::OptionalObject, typeVar, underlyingType,
4542+
locator);
4543+
} else {
45284544
underlyingType = type->getWithoutSpecifierType()->getOptionalObjectType();
4545+
}
45294546

45304547
assert(underlyingType);
45314548

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// RUN: %target-typecheck-verify-swift -target %target-cpu-apple-macosx10.15 -swift-version 5
2+
3+
// REQUIRES: OS=macosx
4+
5+
enum Category {
6+
case first
7+
}
8+
9+
protocol View {}
10+
11+
extension View {
12+
func test(_ tag: Category) -> some View {
13+
Image()
14+
}
15+
}
16+
17+
@resultBuilder struct ViewBuilder {
18+
static func buildBlock<Content>(_ content: Content) -> Content where Content : View { fatalError() }
19+
}
20+
21+
struct Image : View {
22+
}
23+
24+
struct MyView {
25+
@ViewBuilder var body: some View {
26+
let icon: Category! = Category.first // expected-error {{using '!' is not allowed here; perhaps '?' was intended?}} {{23-24=?}}
27+
Image().test(icon)
28+
}
29+
}

0 commit comments

Comments
 (0)