Skip to content

Commit 0b12ec8

Browse files
committed
---
yaml --- r: 326575 b: refs/heads/tensorflow c: 253abad h: refs/heads/master i: 326573: 4799d03 326571: abb8130 326567: 7c083d6 326559: 08d38cb
1 parent b3171ef commit 0b12ec8

File tree

8 files changed

+57
-101
lines changed

8 files changed

+57
-101
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ refs/tags/swift-DEVELOPMENT-SNAPSHOT-2018-04-25-a: 22f738a831d43aff2b9c9773bcb65
816816
refs/tags/swift-DEVELOPMENT-SNAPSHOT-2018-05-08-a: 7d98cc16689baba5c8a3b90a9329bdcc1a12b4e9
817817
refs/heads/cherr42: a566ad54b073c2c56ac0a705d0a5bed9743135a5
818818
"refs/heads/codable_test_comment_fix": fc8f6824f7f347e1e8db55bff62db385c5728b5a
819-
refs/heads/tensorflow: 1ecd209e3b8c36766ec3f04805d42f298a7bb55b
819+
refs/heads/tensorflow: 253abad78995af39c42263894ed8f3617a2e41e9
820820
refs/tags/swift-4.1-DEVELOPMENT-SNAPSHOT-2018-05-11-a: 8126fd7a652e2f70ad6d76505239e34fb2ef3e1a
821821
refs/tags/swift-4.1-DEVELOPMENT-SNAPSHOT-2018-05-12-a: b3fd3dd84df6717f2e2e9df58c6d7e99fed57086
822822
refs/tags/swift-4.1-DEVELOPMENT-SNAPSHOT-2018-05-13-a: 71135119579039dc321c5f65d870050fe36efda2

branches/tensorflow/lib/Sema/CSDiag.cpp

Lines changed: 4 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,15 +1636,6 @@ bool FailureDiagnosis::diagnoseCalleeResultContextualConversionError() {
16361636
}
16371637
}
16381638

1639-
/// Return true if the conversion from fromType to toType is an invalid string
1640-
/// index operation.
1641-
static bool isIntegerToStringIndexConversion(Type fromType, Type toType,
1642-
ConstraintSystem &CS) {
1643-
auto kind = KnownProtocolKind::ExpressibleByIntegerLiteral;
1644-
return (conformsToKnownProtocol(CS, fromType, kind) &&
1645-
toType->getCanonicalType().getString() == "String.CharacterView.Index");
1646-
}
1647-
16481639
bool FailureDiagnosis::diagnoseContextualConversionError(
16491640
Expr *expr, Type contextualType, ContextualTypePurpose CTP,
16501641
Type suggestedType) {
@@ -1689,7 +1680,6 @@ bool FailureDiagnosis::diagnoseContextualConversionError(
16891680
// Try to find the contextual type in a variety of ways. If the constraint
16901681
// system had a contextual type specified, we use it - it will have a purpose
16911682
// indicator which allows us to give a very "to the point" diagnostic.
1692-
Diag<Type, Type> diagID;
16931683
Diag<Type> nilDiag;
16941684
std::function<void(void)> nilFollowup;
16951685

@@ -1705,7 +1695,6 @@ bool FailureDiagnosis::diagnoseContextualConversionError(
17051695
llvm_unreachable("CTP_CalleeResult does not actually install a "
17061696
"contextual type");
17071697
case CTP_Initialization:
1708-
diagID = diag::cannot_convert_initializer_value;
17091698
nilDiag = diag::cannot_convert_initializer_value_nil;
17101699
nilFollowup = [this] {
17111700
TypeRepr *patternTR = CS.getContextualTypeLoc().getTypeRepr();
@@ -1730,7 +1719,6 @@ bool FailureDiagnosis::diagnoseContextualConversionError(
17301719
return true;
17311720
}
17321721

1733-
diagID = diag::cannot_convert_to_return_type;
17341722
nilDiag = diag::cannot_convert_to_return_type_nil;
17351723
break;
17361724
case CTP_ThrowStmt: {
@@ -1777,11 +1765,9 @@ bool FailureDiagnosis::diagnoseContextualConversionError(
17771765
}
17781766

17791767
case CTP_EnumCaseRawValue:
1780-
diagID = diag::cannot_convert_raw_initializer_value;
17811768
nilDiag = diag::cannot_convert_raw_initializer_value_nil;
17821769
break;
17831770
case CTP_DefaultParameter:
1784-
diagID = diag::cannot_convert_default_arg_value;
17851771
nilDiag = diag::cannot_convert_default_arg_value_nil;
17861772
break;
17871773

@@ -1800,39 +1786,30 @@ bool FailureDiagnosis::diagnoseContextualConversionError(
18001786
}
18011787
return true;
18021788
case CTP_YieldByValue:
1803-
diagID = diag::cannot_convert_yield_value;
18041789
nilDiag = diag::cannot_convert_yield_value_nil;
18051790
break;
18061791
case CTP_CallArgument:
1807-
diagID = diag::cannot_convert_argument_value;
18081792
nilDiag = diag::cannot_convert_argument_value_nil;
18091793
break;
18101794
case CTP_ClosureResult:
1811-
diagID = diag::cannot_convert_closure_result;
18121795
nilDiag = diag::cannot_convert_closure_result_nil;
18131796
break;
18141797
case CTP_ArrayElement:
1815-
diagID = diag::cannot_convert_array_element;
18161798
nilDiag = diag::cannot_convert_array_element_nil;
18171799
break;
18181800
case CTP_DictionaryKey:
1819-
diagID = diag::cannot_convert_dict_key;
18201801
nilDiag = diag::cannot_convert_dict_key_nil;
18211802
break;
18221803
case CTP_DictionaryValue:
1823-
diagID = diag::cannot_convert_dict_value;
18241804
nilDiag = diag::cannot_convert_dict_value_nil;
18251805
break;
18261806
case CTP_CoerceOperand:
1827-
diagID = diag::cannot_convert_coerce;
18281807
nilDiag = diag::cannot_convert_coerce_nil;
18291808
break;
18301809
case CTP_AssignSource:
1831-
diagID = diag::cannot_convert_assign;
18321810
nilDiag = diag::cannot_convert_assign_nil;
18331811
break;
18341812
case CTP_SubscriptAssignSource:
1835-
diagID = diag::cannot_convert_subscript_assign;
18361813
nilDiag = diag::cannot_convert_subscript_assign_nil;
18371814
break;
18381815
}
@@ -1858,69 +1835,18 @@ bool FailureDiagnosis::diagnoseContextualConversionError(
18581835
return false;
18591836
}
18601837

1861-
// If we're trying to convert something to Bool, check to see if it is for
1862-
// a known reason.
1863-
ContextualFailure failure(expr, CS, exprType, contextualType,
1864-
CS.getConstraintLocator(expr));
1865-
1866-
if (failure.diagnoseMissingFunctionCall())
1867-
return true;
1868-
1869-
if (failure.diagnoseConversionToBool())
1870-
return true;
1871-
18721838
exprType = exprType->getRValueType();
18731839

1874-
// Special case of some common conversions involving Swift.String
1875-
// indexes, catching cases where people attempt to index them with an integer.
1876-
if (isIntegerToStringIndexConversion(exprType, contextualType, CS)) {
1877-
diagnose(expr->getLoc(), diag::string_index_not_integer,
1878-
exprType->getRValueType())
1879-
.highlight(expr->getSourceRange());
1880-
diagnose(expr->getLoc(), diag::string_index_not_integer_note);
1881-
return true;
1882-
}
1883-
1884-
// When converting from T to [T] or UnsafePointer<T>, we can offer fixit to wrap
1885-
// the expr with brackets.
1886-
auto *genericType = contextualType->getAs<BoundGenericType>();
1887-
if (genericType) {
1888-
auto *contextDecl = genericType->getDecl();
1889-
if (contextDecl == CS.TC.Context.getArrayDecl()) {
1890-
for (Type arg : genericType->getGenericArgs()) {
1891-
if (arg->isEqual(exprType)) {
1892-
diagnose(expr->getLoc(), diagID, exprType, contextualType)
1893-
.fixItInsert(expr->getStartLoc(), "[")
1894-
.fixItInsert(Lexer::getLocForEndOfToken(CS.TC.Context.SourceMgr,
1895-
expr->getEndLoc()),
1896-
"]");
1897-
return true;
1898-
}
1899-
}
1900-
}
1901-
}
1902-
19031840
// Don't attempt fixits if we have an unsolved type variable, since
19041841
// the recovery path's recursion into the type checker via typeCheckCast()
19051842
// will confuse matters.
19061843
if (exprType->hasTypeVariable())
19071844
return false;
19081845

1909-
// When complaining about conversion to a protocol type, complain about
1910-
// conformance instead of "conversion".
1911-
if (contextualType->isExistentialType()) {
1912-
MissingContextualConformanceFailure failure(
1913-
expr, CS, CTP, exprType, contextualType,
1914-
CS.getConstraintLocator(expr, LocatorPathElt::getContextualType()));
1915-
return failure.diagnoseAsError();
1916-
}
1917-
1918-
InFlightDiagnostic diag = diagnose(expr->getLoc(), diagID,
1919-
exprType, contextualType);
1920-
diag.highlight(expr->getSourceRange());
1921-
1922-
failure.tryFixIts(diag);
1923-
return true;
1846+
ContextualFailure failure(
1847+
expr, CS, CTP, exprType, contextualType,
1848+
CS.getConstraintLocator(expr, LocatorPathElt::getContextualType()));
1849+
return failure.diagnoseAsError();
19241850
}
19251851

19261852
//===----------------------------------------------------------------------===//

branches/tensorflow/lib/Sema/CSDiagnostics.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,6 +1847,16 @@ bool ContextualFailure::diagnoseAsError() {
18471847
if (diagnoseConversionToDictionary())
18481848
return true;
18491849

1850+
// Special case of some common conversions involving Swift.String
1851+
// indexes, catching cases where people attempt to index them with an integer.
1852+
if (isIntegerToStringIndexConversion()) {
1853+
emitDiagnostic(anchor->getLoc(), diag::string_index_not_integer,
1854+
getFromType())
1855+
.highlight(anchor->getSourceRange());
1856+
emitDiagnostic(anchor->getLoc(), diag::string_index_not_integer_note);
1857+
return true;
1858+
}
1859+
18501860
Diag<Type, Type> diagnostic;
18511861
switch (path.back().getKind()) {
18521862
case ConstraintLocator::ClosureResult: {
@@ -1858,8 +1868,8 @@ bool ContextualFailure::diagnoseAsError() {
18581868
if (diagnoseConversionToBool())
18591869
return true;
18601870

1861-
if (auto msg = getDiagnosticFor(getContextualTypePurpose(),
1862-
/*forProtocol=*/false)) {
1871+
auto contextualType = getToType();
1872+
if (auto msg = getDiagnosticFor(CTP, contextualType->isExistentialType())) {
18631873
diagnostic = *msg;
18641874
break;
18651875
}
@@ -2300,6 +2310,16 @@ void ContextualFailure::tryComputedPropertyFixIts(Expr *expr) const {
23002310
}
23012311
}
23022312

2313+
bool ContextualFailure::isIntegerToStringIndexConversion() const {
2314+
auto &cs = getConstraintSystem();
2315+
auto kind = KnownProtocolKind::ExpressibleByIntegerLiteral;
2316+
2317+
auto fromType = getFromType();
2318+
auto toType = getToType()->getCanonicalType();
2319+
return (conformsToKnownProtocol(cs, fromType, kind) &&
2320+
toType.getString() == "String.CharacterView.Index");
2321+
}
2322+
23032323
Optional<Diag<Type, Type>>
23042324
ContextualFailure::getDiagnosticFor(ContextualTypePurpose context,
23052325
bool forProtocol) {

branches/tensorflow/lib/Sema/CSDiagnostics.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -661,13 +661,20 @@ class AssignmentFailure final : public FailureDiagnostic {
661661
/// Intended to diagnose any possible contextual failure
662662
/// e.g. argument/parameter, closure result, conversions etc.
663663
class ContextualFailure : public FailureDiagnostic {
664+
ContextualTypePurpose CTP;
664665
Type FromType, ToType;
665666

666667
public:
667668
ContextualFailure(Expr *root, ConstraintSystem &cs, Type lhs, Type rhs,
668669
ConstraintLocator *locator)
669-
: FailureDiagnostic(root, cs, locator), FromType(resolve(lhs)),
670-
ToType(resolve(rhs)) {}
670+
: ContextualFailure(root, cs, cs.getContextualTypePurpose(), lhs, rhs,
671+
locator) {}
672+
673+
ContextualFailure(Expr *root, ConstraintSystem &cs,
674+
ContextualTypePurpose purpose, Type lhs, Type rhs,
675+
ConstraintLocator *locator)
676+
: FailureDiagnostic(root, cs, locator), CTP(purpose),
677+
FromType(resolve(lhs)), ToType(resolve(rhs)) {}
671678

672679
Type getFromType() const { return FromType; }
673680

@@ -735,10 +742,7 @@ class ContextualFailure : public FailureDiagnostic {
735742
Type contextualType);
736743

737744
private:
738-
ContextualTypePurpose getContextualTypePurpose() const {
739-
auto &cs = getConstraintSystem();
740-
return cs.getContextualTypePurpose();
741-
}
745+
ContextualTypePurpose getContextualTypePurpose() const { return CTP; }
742746

743747
Type resolve(Type rawType) {
744748
auto type = resolveType(rawType)->getWithoutSpecifierType();
@@ -759,6 +763,10 @@ class ContextualFailure : public FailureDiagnostic {
759763
KnownProtocolKind::ExpressibleByIntegerLiteral);
760764
}
761765

766+
/// Return true if the conversion from fromType to toType is
767+
/// an invalid string index operation.
768+
bool isIntegerToStringIndexConversion() const;
769+
762770
protected:
763771
static Optional<Diag<Type, Type>>
764772
getDiagnosticFor(ContextualTypePurpose context, bool forProtocol);

branches/tensorflow/test/Constraints/diagnostics.swift

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ func testImplConversion(a : Float?) -> Bool {}
773773
func testImplConversion(a : Int?) -> Bool {
774774
let someInt = 42
775775
let a : Int = testImplConversion(someInt) // expected-error {{missing argument label 'a:' in call}} {{36-36=a: }}
776-
// expected-error@-2 {{cannot convert value of type 'Bool' to specified type 'Int'}}
776+
// expected-error@-1 {{cannot convert value of type 'Bool' to specified type 'Int'}}
777777
}
778778

779779
// <rdar://problem/23752537> QoI: Bogus error message: Binary operator '&&' cannot be applied to two 'Bool' operands
@@ -1225,7 +1225,7 @@ func rdar43525641(_ a: Int, _ b: Int = 0, c: Int = 0, _ d: Int) {}
12251225
rdar43525641(1, c: 2, 3) // Ok
12261226

12271227
struct Array {}
1228-
let foo: Swift.Array = Array() // expected-error {{cannot convert value of type 'diagnostics.Array' to specified type 'Swift.Array'}}
1228+
let foo: Swift.Array = Array() // expected-error {{cannot convert value of type 'Array' to specified type 'Array<Any>'}}
12291229

12301230
struct Error {}
12311231
let bar: Swift.Error = Error() //expected-error {{value of type 'diagnostics.Error' does not conform to specified type 'Swift.Error'}}
@@ -1235,21 +1235,23 @@ let baz3: (Swift.Error) = (Error()) //expected-error {{value of type 'diagnostic
12351235
let baz4: ((Swift.Error)) = (Error()) //expected-error {{value of type 'diagnostics.Error' does not conform to specified type 'Swift.Error'}}
12361236

12371237
// SyntaxSugarTypes with unresolved types
1238+
// TODO(diagnostics): It should be easy to improve messages here e.g. infer type of `Element` with a fix which would
1239+
// add as many brackets as required by parameter type. It's a bit harder for dictionaries but should be doable.
12381240
func takesGenericArray<T>(_ x: [T]) {}
12391241
takesGenericArray(1) // expected-error {{cannot convert value of type 'Int' to expected argument type 'Array<Element>'}}
12401242
func takesNestedGenericArray<T>(_ x: [[T]]) {}
1241-
takesNestedGenericArray(1) // expected-error {{cannot convert value of type 'Int' to expected argument type 'Array<Array<_>>'}}
1243+
takesNestedGenericArray(1) // expected-error {{cannot convert value of type 'Int' to expected argument type 'Array<Element>'}}
12421244
func takesSetOfGenericArrays<T>(_ x: Set<[T]>) {}
1243-
takesSetOfGenericArrays(1) // expected-error {{cannot convert value of type 'Int' to expected argument type 'Set<Array<_>>'}}
1245+
takesSetOfGenericArrays(1) // expected-error {{cannot convert value of type 'Int' to expected argument type 'Set<Element>'}}
12441246
func takesArrayOfSetOfGenericArrays<T>(_ x: [Set<[T]>]) {}
1245-
takesArrayOfSetOfGenericArrays(1) // expected-error {{cannot convert value of type 'Int' to expected argument type 'Array<Set<Array<_>>>'}}
1247+
takesArrayOfSetOfGenericArrays(1) // expected-error {{cannot convert value of type 'Int' to expected argument type 'Array<Element>'}}
12461248
func takesArrayOfGenericOptionals<T>(_ x: [T?]) {}
1247-
takesArrayOfGenericOptionals(1) // expected-error {{cannot convert value of type 'Int' to expected argument type 'Array<Optional<_>>'}}
1249+
takesArrayOfGenericOptionals(1) // expected-error {{cannot convert value of type 'Int' to expected argument type 'Array<Element>'}}
12481250
func takesGenericDictionary<T, U>(_ x: [T : U]) {}
1249-
takesGenericDictionary(true) // expected-error {{cannot convert value of type 'Bool' to expected argument type 'Dictionary<_, _>'}}
1251+
takesGenericDictionary(true) // expected-error {{cannot convert value of type 'Bool' to expected argument type 'Dictionary<Key, Value>'}}
12501252
typealias Z = Int
12511253
func takesGenericDictionaryWithTypealias<T>(_ x: [T : Z]) {}
1252-
takesGenericDictionaryWithTypealias(true) // expected-error {{cannot convert value of type 'Bool' to expected argument type 'Dictionary<_, Z>'}}
1254+
takesGenericDictionaryWithTypealias(true) // expected-error {{cannot convert value of type 'Bool' to expected argument type 'Dictionary<Key, Value>'}}
12531255
func takesGenericFunction<T>(_ x: ([T]) -> Void) {}
12541256
takesGenericFunction(true) // expected-error {{cannot convert value of type 'Bool' to expected argument type '(Array<_>) -> Void'}}
12551257
func takesTuple<T>(_ x: ([T], [T])) {}

branches/tensorflow/test/TypeCoercion/overload_noncall.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ struct X { }
44
struct Y { }
55
struct Z { }
66

7-
func f0(_ x1: X, x2: X) -> X {} // expected-note{{found this candidate}}
8-
func f0(_ y1: Y, y2: Y) -> Y {} // expected-note{{found this candidate}}
9-
var f0 : X // expected-note {{found this candidate}} expected-note {{'f0' previously declared here}}
7+
func f0(_ x1: X, x2: X) -> X {}
8+
func f0(_ y1: Y, y2: Y) -> Y {}
9+
var f0 : X // expected-note {{'f0' previously declared here}}
1010
func f0_init(_ x: X, y: Y) -> X {}
1111
var f0 : (_ x : X, _ y : Y) -> X = f0_init // expected-error{{invalid redeclaration}}
1212
func f1(_ x: X) -> X {}
@@ -16,7 +16,7 @@ func f2(_ g: (_ x: X) -> X) -> ((_ y: Y) -> Y) { }
1616
func test_conv() {
1717
var _ : (_ x1 : X, _ x2 : X) -> X = f0
1818
var _ : (X, X) -> X = f0
19-
var _ : (Y, X) -> X = f0 // expected-error{{ambiguous reference to member 'f0(_:x2:)'}}
19+
var _ : (Y, X) -> X = f0 // expected-error{{cannot convert value of type 'X' to specified type '(Y, X) -> X'}}
2020
var _ : (X) -> X = f1
2121
var a7 : (X) -> (X) = f1
2222
var a8 : (_ x2 : X) -> (X) = f1

branches/tensorflow/test/decl/protocol/req/recursion.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public struct S<A: P> where A.T == S<A> {
4848
// expected-error@-2 {{generic struct 'S' references itself}}
4949
func f(a: A.T) {
5050
g(a: id(t: a))
51-
// expected-error@-1 {{cannot convert value of type 'A.T' to expected argument type 'S<_>'}}
51+
// expected-error@-1 {{cannot convert value of type 'A.T' to expected argument type 'S<A>'}}
5252
_ = A.T.self
5353
}
5454

branches/tensorflow/test/expr/closure/anonymous.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func variadic() {
3535
// expected-error@-1 {{cannot convert value of type '([Int]) -> ()' to specified type '(Int...) -> ()'}}
3636

3737
takesVariadicGeneric({takesIntArray($0)})
38-
// expected-error@-1 {{cannot convert value of type 'Array<_>' to expected argument type '[Int]'}}
38+
// expected-error@-1 {{cannot convert value of type 'Array<Element>' to expected argument type '[Int]'}}
3939

4040
takesVariadicGeneric({let _: [Int] = $0})
4141
// expected-error@-1 {{cannot convert value of type '(_) -> ()' to expected argument type '(_...) -> ()'}}

0 commit comments

Comments
 (0)