Skip to content

[TypeChecker] SE-0326: Check whether function declaration is a result… #42159

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 1 commit into from
Apr 4, 2022
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
6 changes: 6 additions & 0 deletions lib/Sema/CSClosure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,12 @@ bool ConstraintSystem::isInResultBuilderContext(ClosureExpr *closure) const {
if (!closure->hasSingleExpressionBody()) {
auto *DC = closure->getParent();
do {
// Result builder is applied to a function/getter body.
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(DC)) {
if (resultBuilderTransformed.count(AFD))
return true;
}

if (auto *parentClosure = dyn_cast<ClosureExpr>(DC)) {
if (resultBuilderTransformed.count(parentClosure))
return true;
Expand Down
69 changes: 69 additions & 0 deletions validation-test/Sema/SwiftUI/rdar91150414.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// RUN: %target-typecheck-verify-swift -target %target-cpu-apple-macosx10.15 -swift-version 5
// REQUIRES: objc_interop
// REQUIRES: OS=macosx

import SwiftUI
import Foundation

protocol P {
var info: Info? { get }
}

protocol Q : P {
func makeView() -> AnyView
}

struct Info {
var kind: Kind

var size: CGSize { fatalError() }
}

enum Kind {
case supported([Int])
case unsupported
}

struct Settings {
var setting = Setting.defaultValue
}

struct Setting {
static let defaultValue = Setting()

func scale(_: CGFloat) -> Setting {
fatalError()
}

func scale(_: CGSize) -> CGSize {
fatalError()
}
}

struct Test: View {
var res: P
var enable: Bool
var settings: Settings

var body: some View {
if let result = res as? Q {
let view = result.makeView()

if let info = result.info {
let show: Bool = {
guard enable else { return false }

switch info.kind {
case .supported: return true
case .unsupported: return false
}
}()

if show {
let size = settings.setting.scale(info.size)
view.frame(width: size.width, height: size.height)
}
}
}
}
}