Skip to content

[CSGen] Fallback to a type variable if preferred type for placeholder… #36558

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
Mar 24, 2021
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: 6 additions & 4 deletions lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3034,10 +3034,12 @@ namespace {

Type visitEditorPlaceholderExpr(EditorPlaceholderExpr *E) {
if (auto *placeholderRepr = E->getPlaceholderTypeRepr()) {
// Just resolve the referenced type.
return resolveTypeReferenceInExpression(
placeholderRepr, TypeResolverContext::InExpression,
CS.getConstraintLocator(E));
// Let's try to use specified type, if that's impossible,
// fallback to a type variable.
if (auto preferredTy = resolveTypeReferenceInExpression(
placeholderRepr, TypeResolverContext::InExpression,
CS.getConstraintLocator(E)))
return preferredTy;
}

auto locator = CS.getConstraintLocator(E);
Expand Down
8 changes: 4 additions & 4 deletions test/Sema/editor_placeholders.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// RUN: %target-typecheck-verify-swift

func foo(_ x: Int) -> Int {}
func foo(_ x: Float) -> Float {}
func foo<T>(_ t: T) -> T {}
func foo(_ x: Int) -> Int {} // expected-note {{found this candidate}}
func foo(_ x: Float) -> Float {} // expected-note {{found this candidate}}
func foo<T>(_ t: T) -> T {} // expected-note {{found this candidate}}

var v = foo(<#T##x: Float##Float#>) // expected-error {{editor placeholder}}
v = "" // expected-error {{cannot assign value of type 'String' to type 'Float'}}
Expand All @@ -11,7 +11,7 @@ if (true) {
<#code#> // expected-error {{editor placeholder}}
}

foo(<#T##x: Undeclared##Undeclared#>) // expected-error {{editor placeholder}} expected-error {{cannot find type 'Undeclared' in scope}}
foo(<#T##x: Undeclared##Undeclared#>) // expected-error {{editor placeholder}} expected-error {{cannot find type 'Undeclared' in scope}} expected-error {{ambiguous use of 'foo'}}

func f(_ n: Int) {}
let a1 = <#T#> // expected-error{{editor placeholder in source file}}
Expand Down
16 changes: 16 additions & 0 deletions validation-test/Sema/SwiftUI/sr14213.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// RUN: %target-typecheck-verify-swift -target x86_64-apple-macosx10.15 -swift-version 5

// REQUIRES: objc_interop
// REQUIRES: OS=macosx

import SwiftUI

struct Experiment: View {
var body: some View {
HStack { // expected-error {{generic parameter 'Content' could not be inferred}} expected-note {{explicitly specify the generic arguments to fix this issue}}emacs
Slider(value: <#T##Binding<BinaryFloatingPoint>#>, in: <#T##ClosedRange<BinaryFloatingPoint>#>, label: <#T##() -> _#>) // expected-error 3 {{editor placeholder in source file}} expected-error {{expected type for function result}}
// expected-error@-1 {{protocol 'BinaryFloatingPoint' as a type cannot conform to 'Comparable'}}
// expected-note@-2 {{only concrete types such as structs, enums and classes can conform to protocols}}
}
}
}