diff --git a/include/swift/Sema/CSFix.h b/include/swift/Sema/CSFix.h index 159e5a86a2fba..cc53faa9ab1f2 100644 --- a/include/swift/Sema/CSFix.h +++ b/include/swift/Sema/CSFix.h @@ -560,6 +560,11 @@ class ConstraintFix { return false; } + template + bool directlyAt() const { + return getLocator()->directlyAt(); + } + void print(llvm::raw_ostream &Out) const; SWIFT_DEBUG_DUMP; diff --git a/include/swift/Sema/ConstraintSystem.h b/include/swift/Sema/ConstraintSystem.h index c23b4836bf360..d43ceabc98e50 100644 --- a/include/swift/Sema/ConstraintSystem.h +++ b/include/swift/Sema/ConstraintSystem.h @@ -3484,6 +3484,15 @@ class ConstraintSystem { return nullptr; } + Expr *getSemanticsProvidingParentExpr(Expr *expr) { + while (auto *parent = getParentExpr(expr)) { + if (parent->getSemanticsProvidingExpr() == parent) + return parent; + expr = parent; + } + return nullptr; + } + /// Retrieve the depth of the given expression. std::optional getExprDepth(Expr *expr) { if (auto result = getExprDepthAndParent(expr)) diff --git a/lib/Sema/CSDiagnostics.cpp b/lib/Sema/CSDiagnostics.cpp index 5383ba2ebaeec..a8532d2be374a 100644 --- a/lib/Sema/CSDiagnostics.cpp +++ b/lib/Sema/CSDiagnostics.cpp @@ -1114,7 +1114,12 @@ bool GenericArgumentsMismatchFailure::diagnoseAsError() { if (!diagnostic) return false; - emitDiagnosticAt(::getLoc(anchor), *diagnostic, fromType, toType); + { + auto diag = + emitDiagnosticAt(::getLoc(anchor), *diagnostic, fromType, toType); + (void)tryFixIts(diag); + } + emitNotesForMismatches(); return true; } diff --git a/lib/Sema/CSSimplify.cpp b/lib/Sema/CSSimplify.cpp index ac4a9876d38f0..01de409681a68 100644 --- a/lib/Sema/CSSimplify.cpp +++ b/lib/Sema/CSSimplify.cpp @@ -4039,6 +4039,12 @@ ConstraintSystem::matchDeepEqualityTypes(Type type1, Type type2, getConstraintLocator(locator, {LocatorPathElt::GenericType(type1), LocatorPathElt::GenericType(type2)}); + // Optionals have a lot of special diagnostics and only one + // generic argument so if we're dealing with one, let's allow + // `repairFailures` to take care of it. + if (bound1->getDecl()->isOptionalDecl()) + return matchDeepTypeArguments(*this, subflags, args1, args2, baseLoc); + auto argMatchingFlags = subflags; // Allow the solver to produce separate fixes while matching // key path's root/value to a contextual type instead of the @@ -4049,13 +4055,6 @@ ConstraintSystem::matchDeepEqualityTypes(Type type1, Type type2, argMatchingFlags |= TMF_MatchingGenericArguments; } - // Optionals have a lot of special diagnostics and only one - // generic argument so if we' re dealing with one, don't produce generic - // arguments mismatch fixes. - if (bound1->getDecl()->isOptionalDecl()) - return matchDeepTypeArguments(*this, argMatchingFlags, args1, args2, - baseLoc); - SmallVector mismatches; auto result = matchDeepTypeArguments( *this, argMatchingFlags, args1, args2, baseLoc, @@ -4282,7 +4281,8 @@ ConstraintSystem::matchExistentialTypes(Type type1, Type type2, if (!path.empty()) { auto last = path.back(); - if (last.is()) { + if (last.is() || + last.is()) { auto proto = protoDecl->getDeclaredInterfaceType(); // Impact is 2 here because there are two failures // 1 - missing conformance and 2 - incorrect argument type. @@ -4310,6 +4310,15 @@ ConstraintSystem::matchExistentialTypes(Type type1, Type type2, break; } + if ((isExpr(anchor) || isExpr(anchor)) && + last.is()) { + auto *fix = CollectionElementContextualMismatch::create( + *this, type1, type2, getConstraintLocator(anchor, path)); + if (recordFix(fix, /*impact=*/2)) + return getTypeMatchFailure(locator); + break; + } + // TODO(diagnostics): If there are any requirement failures associated // with result types which are part of a function type conversion, // let's record general conversion mismatch in order for it to capture @@ -4979,8 +4988,18 @@ repairViaOptionalUnwrap(ConstraintSystem &cs, Type fromType, Type toType, // First, let's check whether it has been determined that // it was incorrect to use `?` in this position. - if (cs.hasFixFor(cs.getConstraintLocator(subExpr), FixKind::RemoveUnwrap)) + if (cs.hasFixFor(cs.getConstraintLocator(subExpr), FixKind::RemoveUnwrap)) { + if (auto *typeVar = + fromType->getOptionalObjectType()->getAs()) { + // If the optional chain is invalid let's unwrap optional and + // re-introduce the constraint to be solved later once both sides + // are sufficiently resolved, this would allow to diagnose not only + // the invalid unwrap but an invalid conversion (if any) as well. + cs.addConstraint(matchKind, typeVar, toType, + cs.getConstraintLocator(locator)); + } return true; + } auto type = cs.getType(subExpr); // If the type of sub-expression is optional, type of the @@ -5775,6 +5794,41 @@ bool ConstraintSystem::repairFailures( break; } + // There is no subtyping between object types of inout argument/parameter. + if (auto argConv = path.back().getAs()) { + // Attempt conversions first. + if (hasAnyRestriction()) + break; + + // Unwraps are allowed to preserve l-valueness so we can suggest + // them here. + if (repairViaOptionalUnwrap(*this, lhs, rhs, matchKind, + conversionsOrFixes, locator)) + return true; + + auto *loc = getConstraintLocator(locator); + + auto result = matchTypes(lhs, rhs, ConstraintKind::Conversion, + TMF_ApplyingFix, locator); + + ConstraintFix *fix = nullptr; + if (result.isFailure()) { + // If this is a "destination" argument to a mutating operator + // like `+=`, let's consider it contextual and only attempt + // to fix type mismatch on the "source" right-hand side of + // such operators. + if (isOperatorArgument(loc) && argConv->getArgIdx() == 0) + break; + + fix = AllowArgumentMismatch::create(*this, lhs, rhs, loc); + } else { + fix = AllowInOutConversion::create(*this, lhs, rhs, loc); + } + + conversionsOrFixes.push_back(fix); + break; + } + // If this is a problem with result type of a subscript setter, // let's re-attempt to repair without l-value conversion in the // locator to fix underlying type mismatch. @@ -5794,7 +5848,7 @@ bool ConstraintSystem::repairFailures( break; } - LLVM_FALLTHROUGH; + break; } case ConstraintLocator::ApplyArgToParam: { @@ -5874,52 +5928,6 @@ bool ConstraintSystem::repairFailures( if (repairByTreatingRValueAsLValue(lhs, rhs)) break; - // If the problem is related to missing unwrap, there is a special - // fix for that. - if (lhs->getOptionalObjectType() && !rhs->getOptionalObjectType()) { - // If this is an attempt to check whether optional conforms to a - // particular protocol, let's do that before attempting to force - // unwrap the optional. - if (hasConversionOrRestriction(ConversionRestrictionKind::Existential)) - break; - - auto result = matchTypes(lhs->getOptionalObjectType(), rhs, matchKind, - TMF_ApplyingFix, locator); - - if (result.isSuccess()) { - conversionsOrFixes.push_back( - ForceOptional::create(*this, lhs, rhs, loc)); - break; - } - } - - // There is no subtyping between object types of inout argument/parameter. - if (elt.getKind() == ConstraintLocator::LValueConversion) { - auto result = matchTypes(lhs, rhs, ConstraintKind::Conversion, - TMF_ApplyingFix, locator); - - ConstraintFix *fix = nullptr; - if (result.isFailure()) { - // If this is a "destination" argument to a mutating operator - // like `+=`, let's consider it contextual and only attempt - // to fix type mismatch on the "source" right-hand side of - // such operators. - if (isOperatorArgument(loc) && - loc->findLast()->getArgIdx() == 0) - break; - - fix = AllowArgumentMismatch::create(*this, lhs, rhs, loc); - } else { - fix = AllowInOutConversion::create(*this, lhs, rhs, loc); - } - - conversionsOrFixes.push_back(fix); - break; - } - - if (elt.getKind() != ConstraintLocator::ApplyArgToParam) - break; - // If argument in l-value type and parameter is `inout` or a pointer, // let's see if it's generic parameter matches and suggest adding explicit // `&`. @@ -6046,7 +6054,7 @@ bool ConstraintSystem::repairFailures( if (repairViaOptionalUnwrap(*this, lhs, rhs, matchKind, conversionsOrFixes, locator)) - break; + return true; { auto *calleeLocator = getCalleeLocator(loc); @@ -6856,9 +6864,28 @@ bool ConstraintSystem::repairFailures( if (!path.empty() && path.back().is()) path.pop_back(); - if (!path.empty() && path.back().is()) { - return repairFailures(lhs, rhs, matchKind, flags, conversionsOrFixes, - getConstraintLocator(anchor, path)); + if (!path.empty()) { + if (path.back().is()) { + return repairFailures(lhs, rhs, matchKind, flags, conversionsOrFixes, + getConstraintLocator(anchor, path)); + } + + if (auto argConv = path.back().getAs()) { + auto argIdx = argConv->getArgIdx(); + auto paramIdx = argConv->getParamIdx(); + + auto *argLoc = getConstraintLocator(anchor, path); + if (auto overload = findSelectedOverloadFor(getCalleeLocator(argLoc))) { + auto *overloadTy = + simplifyType(overload->boundType)->castTo(); + auto *argList = getArgumentList(argLoc); + ASSERT(argList); + conversionsOrFixes.push_back(AllowArgumentMismatch::create( + *this, getType(argList->getExpr(argIdx)), + overloadTy->getParams()[paramIdx].getPlainType(), argLoc)); + return true; + } + } } // When the solver sets `TMF_MatchingGenericArguments` it means @@ -6915,12 +6942,19 @@ bool ConstraintSystem::repairFailures( path.pop_back(); ConstraintFix *fix = nullptr; - if (!path.empty() && path.back().is()) { + auto *fixLoc = getConstraintLocator(anchor, path); + + if (fixLoc->isLastElement()) { fix = fixRequirementFailure(*this, fromType, toType, anchor, path); + } else if (fixLoc->isLastElement()) { + return repairFailures(lhs, rhs, matchKind, flags, conversionsOrFixes, + fixLoc); + } else if (!lhs->mayHaveSuperclass() && rhs->isAnyObject()) { + fix = AllowNonClassTypeToConvertToAnyObject::create(*this, fromType, + fixLoc); } else { fix = GenericArgumentsMismatch::create( - *this, fromType, toType, {genericArgElt.getIndex()}, - getConstraintLocator(anchor, path)); + *this, fromType, toType, {genericArgElt.getIndex()}, fixLoc); } if (!fix) @@ -11410,6 +11444,14 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyMemberConstraint( // `key path` constraint can't be retired until all components // are simplified. addTypeVariableConstraintsToWorkList(memberTypeVar); + } else if (locator->getAnchor().is() && + !getSemanticsProvidingParentExpr( + getAsExpr(locator->getAnchor()))) { + // If there are no contextual expressions that could provide + // a type for the member type variable, let's default it to + // a placeholder eagerly so it could be propagated to the + // pattern if necessary. + recordTypeVariablesAsHoles(memberTypeVar); } else if (locator->isLastElement()) { // Let's handle member patterns specifically because they use // equality instead of argument application constraint, so allowing @@ -15649,7 +15691,8 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint( case FixKind::AllowFunctionSpecialization: case FixKind::IgnoreGenericSpecializationArityMismatch: case FixKind::IgnoreKeyPathSubscriptIndexMismatch: - case FixKind::AllowMemberRefOnExistential: { + case FixKind::AllowMemberRefOnExistential: + case FixKind::AllowNonClassTypeToConvertToAnyObject: { return recordFix(fix) ? SolutionKind::Error : SolutionKind::Solved; } @@ -15657,6 +15700,22 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint( unsigned impact = 1; if (type1->isMarkerExistential() || type2->isMarkerExistential()) ++impact; + + // If generic arguments mismatch ends up being recorded on the result + // of the chain or a try expression it means that there is a contextual + // conversion mismatch. + // + // For optional conversions the solver currently generates a disjunction + // with two choices - bind and optional-to-optional conversion which is + // anchored on the contextual expression. If we can get a fix recorded + // there that would result in a better diagnostic. It's only possible + // for optional-to-optional choice because it doesn't bind the + // variable immediately, so we need to downgrade direct fixes to prevent + // `bind` choice from considered better. + if (fix->directlyAt() || + fix->directlyAt()) + impact += 2; + return recordFix(fix, impact) ? SolutionKind::Error : SolutionKind::Solved; } @@ -15884,7 +15943,6 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint( case FixKind::DefaultGenericArgument: case FixKind::AllowMutatingMemberOnRValueBase: case FixKind::AllowTupleSplatForSingleParameter: - case FixKind::AllowNonClassTypeToConvertToAnyObject: case FixKind::SpecifyClosureParameterType: case FixKind::SpecifyClosureReturnType: case FixKind::AddQualifierToAccessTopLevelName: diff --git a/test/ClangImporter/MixedSource/mixed-target-using-header.swift b/test/ClangImporter/MixedSource/mixed-target-using-header.swift index 48581ef5812aa..5c777c703bc9f 100644 --- a/test/ClangImporter/MixedSource/mixed-target-using-header.swift +++ b/test/ClangImporter/MixedSource/mixed-target-using-header.swift @@ -66,11 +66,13 @@ func testProtocolNamingConflict() { let a: ConflictingName1? var b: ConflictingName1Protocol? b = a // expected-error {{cannot assign value of type 'ConflictingName1?' to type '(any ConflictingName1Protocol)?'}} + // expected-note@-1 {{arguments to generic parameter 'Wrapped' ('ConflictingName1' and 'any ConflictingName1Protocol') are expected to be equal}} _ = b let c: ConflictingName2? var d: ConflictingName2Protocol? d = c // expected-error {{cannot assign value of type 'ConflictingName2?' to type '(any ConflictingName2Protocol)?'}} + // expected-note@-1 {{arguments to generic parameter 'Wrapped' ('ConflictingName2' and 'any ConflictingName2Protocol') are expected to be equal}} _ = d } diff --git a/test/ClangImporter/cf.swift b/test/ClangImporter/cf.swift index 4e8fb52787ee6..5cb2e9e7a3b0b 100644 --- a/test/ClangImporter/cf.swift +++ b/test/ClangImporter/cf.swift @@ -118,6 +118,7 @@ func testOutParametersGood() { func testOutParametersBad() { let fridge: CCRefrigerator? CCRefrigeratorCreateIndirect(fridge) // expected-error {{cannot convert value of type 'CCRefrigerator?' to expected argument type 'UnsafeMutablePointer?'}} + // expected-note@-1 {{arguments to generic parameter 'Wrapped' ('CCRefrigerator' and 'UnsafeMutablePointer') are expected to be equal}} let power: CCPowerSupply? CCRefrigeratorGetPowerSupplyIndirect(0, power) @@ -128,13 +129,16 @@ func testOutParametersBad() { CCRefrigeratorGetItemUnaudited(0, 0, item) // expected-error@-1:34 {{cannot convert value of type 'Int' to expected argument type 'CCRefrigerator'}} // expected-error@-2:40 {{cannot convert value of type 'CCItem?' to expected argument type 'UnsafeMutablePointer?>?'}} + // expected-note@-3 {{arguments to generic parameter 'Wrapped' ('CCItem' and 'UnsafeMutablePointer?>') are expected to be equal}} } func nameCollisions() { var objc: MyProblematicObject? var cf: MyProblematicObjectRef? cf = objc // expected-error {{cannot assign value of type 'MyProblematicObject?' to type 'MyProblematicObjectRef?'}} + // expected-note@-1 {{arguments to generic parameter 'Wrapped' ('MyProblematicObject' and 'MyProblematicObjectRef') are expected to be equal}} objc = cf // expected-error {{cannot assign value of type 'MyProblematicObjectRef?' to type 'MyProblematicObject?'}} + // expected-note@-1 {{arguments to generic parameter 'Wrapped' ('MyProblematicObjectRef' and 'MyProblematicObject') are expected to be equal}} var cfAlias: MyProblematicAliasRef? cfAlias = cf // okay @@ -142,7 +146,9 @@ func nameCollisions() { var otherAlias: MyProblematicAlias? otherAlias = cfAlias // expected-error {{cannot assign value of type 'MyProblematicAliasRef?' (aka 'Optional') to type 'MyProblematicAlias?' (aka 'Optional')}} + // expected-note@-1 {{arguments to generic parameter 'Wrapped' ('MyProblematicAliasRef' (aka 'MyProblematicObjectRef') and 'MyProblematicAlias' (aka 'Float')) are expected to be equal}} cfAlias = otherAlias // expected-error {{cannot assign value of type 'MyProblematicAlias?' (aka 'Optional') to type 'MyProblematicAliasRef?' (aka 'Optional')}} + // expected-note@-1 {{arguments to generic parameter 'Wrapped' ('MyProblematicAlias' (aka 'Float') and 'MyProblematicAliasRef' (aka 'MyProblematicObjectRef')) are expected to be equal}} func isOptionalFloat(_: inout Optional) {} isOptionalFloat(&otherAlias) // okay diff --git a/test/ClangImporter/ctypes_parse.swift b/test/ClangImporter/ctypes_parse.swift index f14f4ec025882..629af9d085aad 100644 --- a/test/ClangImporter/ctypes_parse.swift +++ b/test/ClangImporter/ctypes_parse.swift @@ -214,6 +214,7 @@ func testFunctionPointers() { useFunctionPointer2(anotherFP) sizedFP = fp // expected-error {{cannot assign value of type 'fptr?' (aka 'Optional<@convention(c) (Int32) -> Int32>') to type '(@convention(c) (CInt, CInt, UnsafeMutableRawPointer?) -> Void)?'}} + // expected-note@-1 {{arguments to generic parameter 'Wrapped' ('fptr' (aka '@convention(c) (Int32) -> Int32') and '@convention(c) (CInt, CInt, UnsafeMutableRawPointer?) -> Void'}} } func testStructDefaultInit() { diff --git a/test/Constraints/bridging-nsnumber-and-nsvalue.swift.gyb b/test/Constraints/bridging-nsnumber-and-nsvalue.swift.gyb index 044d9fdd6a730..a58ee8587afc1 100644 --- a/test/Constraints/bridging-nsnumber-and-nsvalue.swift.gyb +++ b/test/Constraints/bridging-nsnumber-and-nsvalue.swift.gyb @@ -62,7 +62,7 @@ func bridgeNSNumberBackToSpecificType(object: ${ObjectType}, _ = object as? ${Type} _ = object as! ${Type} - _ = optional as ${Type}? // expected-error{{is not convertible to}} expected-note {{use 'as!'}} + _ = optional as ${Type}? // expected-error{{cannot convert value of type '${ObjectType}?' to type '${Type}?' in coercion}} expected-note {{arguments to generic parameter 'Wrapped' ('${ObjectType}' and '${Type}') are expected to be equal}} _ = optional is ${Type}? _ = optional as? ${Type}? _ = optional as! ${Type}? diff --git a/test/Constraints/bridging.swift b/test/Constraints/bridging.swift index 2ee61c0eed737..ee36e9e4fee85 100644 --- a/test/Constraints/bridging.swift +++ b/test/Constraints/bridging.swift @@ -283,24 +283,29 @@ func rdar19831698() { // Incorrect fixit for NSString? to String? conversions func rdar19836341(_ ns: NSString?, vns: NSString?) { var vns = vns - let _: String? = ns // expected-error{{cannot convert value of type 'NSString?' to specified type 'String?'}}{{22-22= as String?}} - var _: String? = ns // expected-error{{cannot convert value of type 'NSString?' to specified type 'String?'}}{{22-22= as String?}} + let _: String? = ns // expected-error{{cannot assign value of type 'NSString?' to type 'String?'}}{{22-22= as String?}} + // expected-note@-1 {{arguments to generic parameter 'Wrapped' ('NSString' and 'String') are expected to be equal}} + var _: String? = ns // expected-error{{cannot assign value of type 'NSString?' to type 'String?'}}{{22-22= as String?}} + // expected-note@-1 {{arguments to generic parameter 'Wrapped' ('NSString' and 'String') are expected to be equal}} // Important part about below diagnostic is that from-type is described as // 'NSString?' and not '@lvalue NSString?': - let _: String? = vns // expected-error{{cannot convert value of type 'NSString?' to specified type 'String?'}}{{23-23= as String?}} - var _: String? = vns // expected-error{{cannot convert value of type 'NSString?' to specified type 'String?'}}{{23-23= as String?}} + let _: String? = vns // expected-error{{cannot assign value of type 'NSString?' to type 'String?'}}{{23-23= as String?}} + // expected-note@-1 {{arguments to generic parameter 'Wrapped' ('NSString' and 'String') are expected to be equal}} + var _: String? = vns // expected-error{{cannot assign value of type 'NSString?' to type 'String?'}}{{23-23= as String?}} + // expected-note@-1 {{arguments to generic parameter 'Wrapped' ('NSString' and 'String') are expected to be equal}} vns = ns } // Swift compiler sometimes suggests changing "as!" to "as?!" func rdar20029786(_ ns: NSString?) { - var s: String = ns ?? "str" as String as String // expected-error{{'NSString' is not implicitly convertible to 'String'; did you mean to use 'as' to explicitly convert?}} {{19-19=(}} {{50-50=) as String}} - // expected-error@-1 {{cannot convert value of type 'String' to expected argument type 'NSString'}} {{50-50= as NSString}} - var s2 = ns ?? "str" as String as String // expected-error {{cannot convert value of type 'String' to expected argument type 'NSString'}}{{43-43= as NSString}} + var s: String = ns ?? "str" as String as String + // expected-error@-1 {{cannot convert value of type 'NSString?' to expected argument type 'String?'}} {{21-21= as String?}} + var s2 = ns ?? "str" as String as String // expected-error {{binary operator '??' cannot be applied to operands of type 'NSString?' and 'String'}} - let s3: NSString? = "str" as String? // expected-error {{cannot convert value of type 'String?' to specified type 'NSString?'}}{{39-39= as NSString?}} + let s3: NSString? = "str" as String? // expected-error {{cannot assign value of type 'String?' to type 'NSString?'}}{{39-39= as NSString?}} + // expected-note@-1 {{arguments to generic parameter 'Wrapped' ('String' and 'NSString') are expected to be equal}} var s4: String = ns ?? "str" // expected-error{{'NSString' is not implicitly convertible to 'String'; did you mean to use 'as' to explicitly convert?}} {{20-20=(}} {{31-31=) as String}} var s5: String = (ns ?? "str") as String // fixed version @@ -308,7 +313,8 @@ func rdar20029786(_ ns: NSString?) { // Make sure more complicated cast has correct parenthesization func castMoreComplicated(anInt: Int?) { - let _: (NSObject & NSCopying)? = anInt // expected-error{{cannot convert value of type 'Int?' to specified type '(any NSObject & NSCopying)?'}}{{41-41= as (any NSObject & NSCopying)?}} + let _: (NSObject & NSCopying)? = anInt // expected-error{{cannot assign value of type 'Int?' to type '(any NSObject & NSCopying)?'}}{{41-41= as (any NSObject & NSCopying)?}} + // expected-note@-1 {{arguments to generic parameter 'Wrapped' ('Int' and 'any NSObject & NSCopying') are expected to be equal}} } diff --git a/test/Constraints/casts_objc.swift b/test/Constraints/casts_objc.swift index e9630761472e2..e7ffc5820a8a3 100644 --- a/test/Constraints/casts_objc.swift +++ b/test/Constraints/casts_objc.swift @@ -82,8 +82,8 @@ func optionalityMismatchingCasts(f: CGFloat, n: NSNumber, fooo: CGFloat???, nooo: NSNumber???) { _ = f as NSNumber? _ = f as NSNumber?? - let _ = fooo as NSNumber?? // expected-error{{'CGFloat???' is not convertible to 'NSNumber??'}} - //expected-note@-1 {{did you mean to use 'as!' to force downcast?}} {{16-18=as!}} + let _ = fooo as NSNumber?? // expected-error{{cannot convert value of type 'CGFloat???' to type 'NSNumber??' in coercion}} + //expected-note@-1 {{arguments to generic parameter 'Wrapped' ('CGFloat?' and 'NSNumber') are expected to be equal}} let _ = fooo as NSNumber???? // okay: injects extra optionals } diff --git a/test/Constraints/closures.swift b/test/Constraints/closures.swift index 6c2d91201130c..26547edae4053 100644 --- a/test/Constraints/closures.swift +++ b/test/Constraints/closures.swift @@ -803,7 +803,6 @@ overloaded { print("hi"); print("bye") } // multiple expression closure without // expected-error@-1 {{ambiguous use of 'overloaded'}} func not_overloaded(_ handler: () -> Int) {} -// expected-note@-1 {{'not_overloaded' declared here}} not_overloaded { } // empty body // expected-error@-1 {{cannot convert value of type '()' to closure result type 'Int'}} @@ -1156,7 +1155,7 @@ struct R_76250381 { func rdar77022842(argA: Bool? = nil, argB: Bool? = nil) { if let a = argA ?? false, if let b = argB ?? { // expected-error@-1 {{initializer for conditional binding must have Optional type, not 'Bool'}} - // expected-error@-2 {{closure passed to parameter of type 'Bool?' that does not accept a closure}} + // expected-error@-2 {{cannot convert value of type 'Bool?' to expected argument type '(() -> ())?'}} // expected-error@-3 {{cannot convert value of type 'Void' to expected condition type 'Bool'}} // expected-error@-4 {{'if' may only be used as expression in return, throw, or as the source of an assignment}} // expected-error@-5 {{'if' must have an unconditional 'else' to be used as expression}} @@ -1337,3 +1336,33 @@ func rdar143338891() { } } } + +do { + struct V { + init(value: @autoclosure @escaping () -> any Hashable) { } + init(other: @autoclosure @escaping () -> String) { } + } + + let _ = V(value: { [Int]() }) // expected-error {{add () to forward '@autoclosure' parameter}} {{31-31=()}} + let _ = V(other: { [Int]() }) // expected-error {{cannot convert value of type '[Int]' to closure result type 'String'}} +} + +// https://github.com/swiftlang/swift/issues/81770 +do { + func test(_: Int) {} + func test(_: Int = 42, _: (Int) -> Void) {} + + test { + if let _ = $0.missing { // expected-error {{value of type 'Int' has no member 'missing'}} + } + } + + test { + if let _ = (($0.missing)) { // expected-error {{value of type 'Int' has no member 'missing'}} + } + } + + test { // expected-error {{invalid conversion from throwing function of type '(Int) throws -> Void' to non-throwing function type '(Int) -> Void'}} + try $0.missing // expected-error {{value of type 'Int' has no member 'missing'}} + } +} diff --git a/test/Constraints/fixes.swift b/test/Constraints/fixes.swift index 66674920156f3..117c21d98c376 100644 --- a/test/Constraints/fixes.swift +++ b/test/Constraints/fixes.swift @@ -158,16 +158,14 @@ struct Q { let s: String? } let q = Q(s: nil) -let a: Int? = q.s.utf8 // expected-error{{value of optional type 'String?' must be unwrapped to refer to member 'utf8' of wrapped base type 'String'}} -// expected-error@-1 {{cannot convert value of type 'String.UTF8View?' to specified type 'Int?'}} -// expected-note@-2{{chain the optional using '?'}}{{18-18=?}} -let b: Int = q.s.utf8 // expected-error{{value of optional type 'String?' must be unwrapped to refer to member 'utf8' of wrapped base type 'String'}} +let a: Int? = q.s.utf8 // expected-error{{value of optional type 'String?' must be unwrapped to refer to member 'utf8' of wrapped base type 'String'}} expected-note {{chain the optional using '?'}}{{18-18=?}} +// expected-error@-1 {{cannot assign value of type 'String.UTF8View?' to type 'Int?'}} +// expected-note@-2 {{arguments to generic parameter 'Wrapped' ('String.UTF8View' and 'Int') are expected to be equal}} +let b: Int = q.s.utf8 // expected-error{{value of optional type 'String?' must be unwrapped to refer to member 'utf8' of wrapped base type 'String'}} expected-note {{chain the optional using '?'}}{{17-17=?}} expected-note {{force-unwrap using '!'}}{{17-17=!}} // expected-error@-1 {{cannot convert value of type 'String.UTF8View' to specified type 'Int'}} -// expected-note@-2{{chain the optional using '?'}}{{17-17=?}} -// expected-note@-3{{force-unwrap using '!'}}{{17-17=!}} -let d: Int! = q.s.utf8 // expected-error{{value of optional type 'String?' must be unwrapped to refer to member 'utf8' of wrapped base type 'String'}} -// expected-error@-1 {{cannot convert value of type 'String.UTF8View?' to specified type 'Int?'}} -// expected-note@-2{{chain the optional using '?'}}{{18-18=?}} +let d: Int! = q.s.utf8 // expected-error{{value of optional type 'String?' must be unwrapped to refer to member 'utf8' of wrapped base type 'String'}} expected-note {{chain the optional using '?'}}{{18-18=?}} +// expected-error@-1 {{cannot assign value of type 'String.UTF8View?' to type 'Int?'}} +// expected-note@-2 {{arguments to generic parameter 'Wrapped' ('String.UTF8View' and 'Int') are expected to be equal}} let c = q.s.utf8 // expected-error{{value of optional type 'String?' must be unwrapped to refer to member 'utf8' of wrapped base type 'String'}} // expected-note@-1{{chain the optional using '?' to access member 'utf8' only for non-'nil' base values}}{{12-12=?}} // expected-note@-2{{force-unwrap using '!' to abort execution if the optional value contains 'nil'}}{{12-12=!}} diff --git a/test/Constraints/if_expr.swift b/test/Constraints/if_expr.swift index 81307396b3075..301a02145a2ff 100644 --- a/test/Constraints/if_expr.swift +++ b/test/Constraints/if_expr.swift @@ -705,6 +705,7 @@ func builderInClosure() { func testInvalidOptionalChainingInIfContext() { let v63796 = 1 if v63796? {} // expected-error{{cannot use optional chaining on non-optional value of type 'Int'}} + // expected-error@-1 {{type 'Int' cannot be used as a boolean; test for '!= 0' instead}} } // https://github.com/swiftlang/swift/issues/79395 diff --git a/test/Constraints/invalid_implicit_conversions.swift b/test/Constraints/invalid_implicit_conversions.swift index 14606c8e1c26e..26699dad73cd9 100644 --- a/test/Constraints/invalid_implicit_conversions.swift +++ b/test/Constraints/invalid_implicit_conversions.swift @@ -29,7 +29,7 @@ func test( takesAutoclosure(rawPtr, a) // expected-error {{cannot perform pointer conversion of value of type '[Int]' to autoclosure result type 'UnsafeRawPointer'}} takesAutoclosure(ptr, a) // expected-error {{cannot perform pointer conversion of value of type '[Int]' to autoclosure result type 'UnsafePointer'}} - takesAutoclosure(optPtr, b) // expected-error {{cannot perform pointer conversion of value of type '[Int]?' to autoclosure result type 'UnsafePointer?'}} + takesAutoclosure(optPtr, b) // expected-error {{conflicting arguments to generic parameter 'T' ('UnsafePointer?' vs. '[Int]?')}} takesAutoclosure(rawPtr, s) // expected-error {{cannot perform pointer conversion of value of type 'String' to autoclosure result type 'UnsafeRawPointer'}} takesAutoclosure(ptrI8, s) // expected-error {{cannot perform pointer conversion of value of type 'String' to autoclosure result type 'UnsafePointer'}} diff --git a/test/Constraints/issue-81023.swift b/test/Constraints/issue-81023.swift new file mode 100644 index 0000000000000..a9b302ac1a1f5 --- /dev/null +++ b/test/Constraints/issue-81023.swift @@ -0,0 +1,30 @@ +// RUN: %target-typecheck-verify-swift + +// https://github.com/swiftlang/swift/issues/81023 + +protocol MyPublisher { + associatedtype Output + associatedtype Failure: Error + func eraseToAnyPublisher() -> MyAnyPublisher +} + +extension MyPublisher { + func eraseToAnyPublisher() -> MyAnyPublisher { + fatalError() + } +} + +struct MyAnyPublisher: MyPublisher {} +struct MyJust: MyPublisher { + typealias Failure = Never + init(_ value: Output) {} +} + +extension MyPublisher where Output == (any Collection)? { // expected-note {{where 'Self.Output' = '[Int]?'}} + func mapCount() -> MyAnyPublisher { fatalError() } +} + +func test(myPub: MyAnyPublisher<[Int]?, Never>) { + myPub.mapCount() + // expected-error@-1 {{referencing instance method 'mapCount()' on 'MyPublisher' requires the types '[Int]?' and '(any Collection)?' be equivalent}} +} diff --git a/test/Constraints/iuo.swift b/test/Constraints/iuo.swift index 8ff330deb4626..c8ff7d25ff8b9 100644 --- a/test/Constraints/iuo.swift +++ b/test/Constraints/iuo.swift @@ -216,7 +216,8 @@ let _ = (returnsIUO as () -> Int)() // expected-error {{cannot convert value of // Make sure we only permit an IUO unwrap on the first application. func returnsIUOFn() -> (() -> Int?)! { nil } let _: (() -> Int?)? = returnsIUOFn() -let _: (() -> Int)? = returnsIUOFn() // expected-error {{cannot convert value of type '(() -> Int?)?' to specified type '(() -> Int)?'}} +let _: (() -> Int)? = returnsIUOFn() // expected-error {{cannot assign value of type '(() -> Int?)?' to type '(() -> Int)?'}} +// expected-note@-1 {{arguments to generic parameter 'Wrapped' ('() -> Int?' and '() -> Int') are expected to be equal}} let _: () -> Int? = returnsIUOFn() let _: () -> Int = returnsIUOFn() // expected-error {{cannot convert value of type '(() -> Int?)?' to specified type '() -> Int'}} let _: Int? = returnsIUOFn()() diff --git a/test/Constraints/keypath.swift b/test/Constraints/keypath.swift index b662bd15b8ff7..a0627f847e2e3 100644 --- a/test/Constraints/keypath.swift +++ b/test/Constraints/keypath.swift @@ -34,7 +34,7 @@ class Demo { let some = Some(keyPath: \Demo.here) // expected-error@-1 {{cannot convert value of type 'KeyPath Void)?>' to expected argument type 'KeyPath Void)?>'}} -// expected-note@-2 {{arguments to generic parameter 'Value' ('(() -> Void)?' and '((V) -> Void)?') are expected to be equal}} +// expected-note@-2 {{arguments to generic parameter 'Wrapped' ('() -> Void' and '(V) -> Void') are expected to be equal}} // expected-error@-3 {{generic parameter 'V' could not be inferred}} // expected-note@-4 {{explicitly specify the generic arguments to fix this issue}} diff --git a/test/Constraints/operator.swift b/test/Constraints/operator.swift index 8e1360dcc448a..a54ff50a4770d 100644 --- a/test/Constraints/operator.swift +++ b/test/Constraints/operator.swift @@ -278,6 +278,7 @@ func rdar60727310() { func myAssertion(_ a: T, _ op: ((T,T)->Bool), _ b: T) {} var e: Error? = nil myAssertion(e, ==, nil) // expected-error {{cannot convert value of type '(any Error)?' to expected argument type '(any (~Copyable & ~Escapable).Type)?'}} + // expected-note@-1 {{arguments to generic parameter 'Wrapped' ('any Error' and 'any (~Copyable & ~Escapable).Type') are expected to be equal}} } // https://github.com/apple/swift/issues/54877 diff --git a/test/Constraints/optional.swift b/test/Constraints/optional.swift index b080fd665370d..35dad2e8d99c7 100644 --- a/test/Constraints/optional.swift +++ b/test/Constraints/optional.swift @@ -435,7 +435,7 @@ func test_force_unwrap_not_being_too_eager() { // rdar://problem/57097401 func invalidOptionalChaining(a: Any) { a == "="? // expected-error {{cannot use optional chaining on non-optional value of type 'String'}} - // expected-error@-1 {{binary operator '==' cannot be applied to operands of type 'Any' and 'String?'}} + // expected-error@-1 {{cannot convert value of type 'Any' to expected argument type 'String'}} } /// https://github.com/apple/swift/issues/54739 @@ -595,3 +595,26 @@ do { test(x!) // expected-error {{no exact matches in call to local function 'test'}} // expected-error@-1 {{cannot force unwrap value of non-optional type 'Double'}} } + +func testExtraQuestionMark(action: () -> Void, v: Int) { + struct Test { + init(action: () -> Void) {} + } + + Test(action: action?) + // expected-error@-1 {{cannot use optional chaining on non-optional value of type '() -> Void'}} + Test(action: v?) + // expected-error@-1 {{cannot convert value of type 'Int' to expected argument type '() -> Void'}} + // expected-error@-2 {{cannot use optional chaining on non-optional value of type 'Int'}} +} + +func testPassingOptionalChainAsWrongArgument() { + class Test { + func fn(_ asdType: String?) { + } + } + + func test(test: Test, arr: [Int]?) { + test.fn(arr?.first) // expected-error {{cannot convert value of type 'Int?' to expected argument type 'String?'}} + } +} diff --git a/test/Constraints/protocols.swift b/test/Constraints/protocols.swift index 2e1197c5b9b9f..92d5b898aa7e5 100644 --- a/test/Constraints/protocols.swift +++ b/test/Constraints/protocols.swift @@ -577,3 +577,11 @@ do { isFooableError(overloaded()) // Ok } + +do { + func takesFooables(_: [any Fooable]) {} + + func test(v: String) { + takesFooables([v]) // expected-error {{cannot convert value of type 'String' to expected element type 'any Fooable'}} + } +} diff --git a/test/Constraints/rdar44770297.swift b/test/Constraints/rdar44770297.swift index 0853b150b353a..77dc228fc2551 100644 --- a/test/Constraints/rdar44770297.swift +++ b/test/Constraints/rdar44770297.swift @@ -10,6 +10,3 @@ func foo(_: () throws -> T) -> T.A? { // expected-note {{where 'T' = 'Neve let _ = foo() {fatalError()} & nil // expected-error@-1 {{global function 'foo' requires that 'Never' conform to 'P'}} -// expected-error@-2 {{value of optional type 'Never.A?' must be unwrapped to a value of type 'Never.A'}} -// expected-note@-3 {{coalesce using '??' to provide a default when the optional value contains 'nil'}} -// expected-note@-4 {{force-unwrap using '!' to abort execution if the optional value contains 'nil'}} diff --git a/test/Constraints/subscript.swift b/test/Constraints/subscript.swift index 31b4cc9a4097f..4959791a0d65d 100644 --- a/test/Constraints/subscript.swift +++ b/test/Constraints/subscript.swift @@ -113,6 +113,7 @@ let squares = [ 1, 2, 3 ].reduce([:]) { (dict, n) in func r23670252(_ dictionary: [String : AnyObject], someObject: AnyObject) { let color : String? color = dictionary["color"] // expected-error {{cannot assign value of type 'AnyObject?' to type 'String?'}} + // expected-note@-1 {{arguments to generic parameter 'Wrapped' ('AnyObject' and 'String') are expected to be equal}} _ = color } diff --git a/test/Constraints/swift_to_c_pointer_conversions.swift.gyb b/test/Constraints/swift_to_c_pointer_conversions.swift.gyb index f689bba38da8e..3e60aff908834 100644 --- a/test/Constraints/swift_to_c_pointer_conversions.swift.gyb +++ b/test/Constraints/swift_to_c_pointer_conversions.swift.gyb @@ -59,7 +59,7 @@ func test_${TestPtrSize}_bit_ptrs(sptr: UnsafePointer, % end % for pointer in ['osptr', 'ouptr']: - opt_char_ptr_func(${pointer}) // expected-error {{}} + opt_char_ptr_func(${pointer}) // expected-error {{}} expected-note {{arguments to generic parameter 'Wrapped'}} const_opt_char_ptr_func(${pointer}) // Ok unsigned_opt_char_ptr_func(${pointer}) // expected-error {{}} const_opt_unsigned_char_ptr_func(${pointer}) // Ok @@ -96,13 +96,13 @@ func test_${TestPtrSize}_bit_ptrs(sptr: UnsafePointer, % for pointer in ['osptr', 'ouptr']: % for Size in ['16', '32', '64']: % if Size == TestPtrSize: - opt_int_${TestPtrSize}_ptr_func(${pointer}) // expected-error {{}} - opt_uint_${TestPtrSize}_ptr_func(${pointer}) // expected-error {{}} + opt_int_${TestPtrSize}_ptr_func(${pointer}) // expected-error {{}} expected-note {{arguments to generic parameter 'Wrapped'}} + opt_uint_${TestPtrSize}_ptr_func(${pointer}) // expected-error {{}} expected-note {{arguments to generic parameter 'Wrapped'}} const_opt_int_${TestPtrSize}_ptr_func(${pointer}) // Ok const_opt_uint_${TestPtrSize}_ptr_func(${pointer}) // Ok % else: - opt_int_${Size}_ptr_func(${pointer}) // expected-error {{}} - opt_uint_${Size}_ptr_func(${pointer}) // expected-error {{}} + opt_int_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{arguments to generic parameter 'Wrapped'}} + opt_uint_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{arguments to generic parameter 'Wrapped'}} const_opt_int_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}} const_opt_uint_${Size}_ptr_func(${pointer}) // expected-error {{}} expected-note {{}} % end @@ -215,7 +215,7 @@ func test_raw_ptr_value_to_optional_promotion( func test_raw_ptr_optional_to_optional_conversion( riptr: UnsafeRawPointer?, rmptr: UnsafeMutableRawPointer?) { - opt_char_ptr_func(riptr) // expected-error {{}} + opt_char_ptr_func(riptr) // expected-error {{}} expected-note {{arguments to generic parameter 'Wrapped'}} const_opt_char_ptr_func(riptr) // Ok opt_char_ptr_func(rmptr) // Ok @@ -226,11 +226,11 @@ func test_raw_ptr_optional_to_optional_conversion( % for Ptr in ['riptr', 'rmptr']: % for Size in ['16', '32', '64']: - opt_int_${Size}_ptr_func(${Ptr}) // expected-error {{}} - opt_uint_${Size}_ptr_func(${Ptr}) // expected-error {{}} + opt_int_${Size}_ptr_func(${Ptr}) // expected-error {{}} expected-note {{arguments to generic parameter 'Wrapped'}} + opt_uint_${Size}_ptr_func(${Ptr}) // expected-error {{}} expected-note {{arguments to generic parameter 'Wrapped'}} - const_opt_int_${Size}_ptr_func(${Ptr}) // expected-error {{}} - const_opt_uint_${Size}_ptr_func(${Ptr}) // expected-error {{}} + const_opt_int_${Size}_ptr_func(${Ptr}) // expected-error {{}} expected-note {{arguments to generic parameter 'Wrapped'}} + const_opt_uint_${Size}_ptr_func(${Ptr}) // expected-error {{}} expected-note {{arguments to generic parameter 'Wrapped'}} % end % end } diff --git a/test/Constraints/tuple.swift b/test/Constraints/tuple.swift index 72c353aecdc33..51187eed1be35 100644 --- a/test/Constraints/tuple.swift +++ b/test/Constraints/tuple.swift @@ -340,6 +340,7 @@ var optionalTuple3: (UInt64, Int)? = (bignum, 1) // expected-error {{cannot conv optionalTuple = (bignum, 1) // expected-error {{cannot assign value of type '(Int64, Int)' to type '(Int, Int)'}} // Optional to Optional optionalTuple = optionalTuple2 // expected-error {{cannot assign value of type '(Int64, Int)?' to type '(Int, Int)?'}} +// expected-note@-1 {{arguments to generic parameter 'Wrapped' ('(Int64, Int)' and '(Int, Int)') are expected to be equal}} func testTupleLabelMismatchFuncConversion(fn1: @escaping ((x: Int, y: Int)) -> Void, fn2: @escaping () -> (x: Int, Int)) { diff --git a/test/Constraints/tuple_arguments.swift b/test/Constraints/tuple_arguments.swift index a24c78a443458..9f4498169cef1 100644 --- a/test/Constraints/tuple_arguments.swift +++ b/test/Constraints/tuple_arguments.swift @@ -1660,6 +1660,7 @@ do { func foo(_: (() -> Void)?) {} func bar() -> ((()) -> Void)? { return nil } foo(bar()) // expected-error {{cannot convert value of type '((()) -> Void)?' to expected argument type '(() -> Void)?'}} + // expected-note@-1 {{arguments to generic parameter 'Wrapped' ('(()) -> Void' and '() -> Void') are expected to be equal}} } // https://github.com/apple/swift/issues/49059 @@ -1699,6 +1700,7 @@ do { func log() -> ((T) -> Void)? { return nil } f(a: log() as ((()) -> Void)?) // expected-error {{cannot convert value of type '((()) -> Void)?' to expected argument type '(() -> Void)?'}} + // expected-note@-1 {{arguments to generic parameter 'Wrapped' ('(()) -> Void' and '() -> Void') are expected to be equal}} func logNoOptional() -> (T) -> Void { } f(a: logNoOptional() as ((()) -> Void)) // expected-error {{cannot convert value of type '(()) -> Void' to expected argument type '() -> Void'}} diff --git a/test/Parse/try.swift b/test/Parse/try.swift index b79f36f8150e9..8b2df406d437d 100644 --- a/test/Parse/try.swift +++ b/test/Parse/try.swift @@ -237,12 +237,14 @@ struct ThingProducer { } let optProducer: ThingProducer? = ThingProducer() -let _: Int? = try? optProducer?.produceInt() // expected-error {{value of optional type 'Int??' not unwrapped; did you mean to use 'try!' or chain with '?'?}} +let _: Int? = try? optProducer?.produceInt() // expected-error {{cannot assign value of type 'Int??' to type 'Int?'}} +// expected-note@-1 {{arguments to generic parameter 'Wrapped' ('Int?' and 'Int') are expected to be equal}} let _: Int = try? optProducer?.produceInt() // expected-error {{cannot convert value of type 'Int??' to specified type 'Int'}} let _: String = try? optProducer?.produceInt() // expected-error {{cannot convert value of type 'Int??' to specified type 'String'}} let _: Int?? = try? optProducer?.produceInt() // good -let _: Int? = try? optProducer?.produceIntNoThrowing() // expected-error {{value of optional type 'Int??' not unwrapped; did you mean to use 'try!' or chain with '?'?}} +let _: Int? = try? optProducer?.produceIntNoThrowing() // expected-error {{cannot assign value of type 'Int??' to type 'Int?'}} +// expected-note@-1 {{arguments to generic parameter 'Wrapped' ('Int?' and 'Int') are expected to be equal}} let _: Int?? = try? optProducer?.produceIntNoThrowing() // expected-warning {{no calls to throwing functions occur within 'try' expression}} let _: Int? = (try? optProducer?.produceAny()) as? Int // good diff --git a/test/Sema/clang_fn_type_mismatch.swift b/test/Sema/clang_fn_type_mismatch.swift index 4f5f3fd0c4883..796fc17f1bef5 100644 --- a/test/Sema/clang_fn_type_mismatch.swift +++ b/test/Sema/clang_fn_type_mismatch.swift @@ -12,10 +12,12 @@ let _ : @convention(c) (Int) -> Int = f1! // expected-error@-1{{cannot convert value of type '@convention(c, cType: "size_t (*)(size_t)") (Int) -> Int' to specified type '@convention(c) (Int) -> Int'}} let _ : (@convention(c) (Int) -> Int)? = f1 -// expected-error@-1{{cannot convert value of type '(@convention(c, cType: "size_t (*)(size_t)") (Int) -> Int)?' to specified type '(@convention(c) (Int) -> Int)?'}} +// expected-error@-1{{cannot assign value of type '(@convention(c, cType: "size_t (*)(size_t)") (Int) -> Int)?' to type '(@convention(c) (Int) -> Int)?'}} +// expected-note@-2 {{arguments to generic parameter 'Wrapped' ('@convention(c, cType: "size_t (*)(size_t)") (Int) -> Int' and '@convention(c) (Int) -> Int') are expected to be equal}} let _ : (@convention(c, cType: "void *(*)(void *)") (Int) -> Int)? = f1 -// expected-error@-1{{cannot convert value of type '(@convention(c, cType: "size_t (*)(size_t)") (Int) -> Int)?' to specified type '(@convention(c, cType: "void *(*)(void *)") (Int) -> Int)?'}} +// expected-error@-1{{cannot assign value of type '(@convention(c, cType: "size_t (*)(size_t)") (Int) -> Int)?' to type '(@convention(c, cType: "void *(*)(void *)") (Int) -> Int)?'}} +// expected-note@-2 {{arguments to generic parameter 'Wrapped' ('@convention(c, cType: "size_t (*)(size_t)") (Int) -> Int' and '@convention(c, cType: "void *(*)(void *)") (Int) -> Int') are expected to be equal}} // Converting from @convention(c) -> @convention(swift) works diff --git a/test/Serialization/Recovery/typedefs.swift b/test/Serialization/Recovery/typedefs.swift index 15715a967736c..6d6ef13e7de02 100644 --- a/test/Serialization/Recovery/typedefs.swift +++ b/test/Serialization/Recovery/typedefs.swift @@ -45,11 +45,13 @@ public func testVTableBuilding(user: User) { #if VERIFY let _: String = useAssoc(ImportedType.self) // expected-error {{cannot convert value of type 'Int32?' to specified type 'String'}} -let _: Bool? = useAssoc(ImportedType.self) // expected-error {{cannot convert value of type 'Int32?' to specified type 'Bool?'}} +let _: Bool? = useAssoc(ImportedType.self) // expected-error {{cannot assign value of type 'Int32?' to type 'Bool?'}} +// expected-note@-1 {{arguments to generic parameter 'Wrapped' ('Int32' and 'Bool') are expected to be equal}} let _: Int32? = useAssoc(ImportedType.self) let _: String = useAssoc(AnotherType.self) // expected-error {{cannot convert value of type 'AnotherType.Assoc?' (aka 'Optional') to specified type 'String'}} -let _: Bool? = useAssoc(AnotherType.self) // expected-error {{cannot convert value of type 'AnotherType.Assoc?' (aka 'Optional') to specified type 'Bool?'}} +let _: Bool? = useAssoc(AnotherType.self) // expected-error {{cannot assign value of type 'AnotherType.Assoc?' (aka 'Optional') to type 'Bool?'}} +// expected-note@-1 {{arguments to generic parameter 'Wrapped' ('AnotherType.Assoc' (aka 'Int32') and 'Bool') are expected to be equal}} let _: Int32? = useAssoc(AnotherType.self) let _ = wrapped // expected-error {{cannot find 'wrapped' in scope}} diff --git a/test/decl/class/circular_inheritance_2.swift b/test/decl/class/circular_inheritance_2.swift index d1f7bb3e54732..7f977a6a1cb1d 100644 --- a/test/decl/class/circular_inheritance_2.swift +++ b/test/decl/class/circular_inheritance_2.swift @@ -4,5 +4,6 @@ class Foo {} class Bar: Bar {} // expected-error{{'Bar' inherits from itself}} func foo(_ o: AnyObject) -> Foo? { - return o as? Bar // expected-error{{cannot convert return expression of type 'Bar?' to return type 'Foo?'}} + return o as? Bar // expected-error{{cannot convert return expression of type 'Bar?' to return type 'Foo?'}} + // expected-note@-1 {{arguments to generic parameter 'Wrapped' ('Bar' and 'Foo') are expected to be equal}} } diff --git a/test/decl/func/typed_throws.swift b/test/decl/func/typed_throws.swift index c453ed0d27f61..d753b454c1d54 100644 --- a/test/decl/func/typed_throws.swift +++ b/test/decl/func/typed_throws.swift @@ -192,7 +192,7 @@ enum G_E { func testArrMap(arr: [String]) { _ = mapArray(arr, body: G_E.tuple) - // expected-error@-1{{cannot convert value of type '((x: Int, y: Int)) -> G_E' to expected argument type '(String) -> G_E'}} + // expected-error@-1{{conflicting arguments to generic parameter 'T' ('String' vs. '(x: Int, y: Int)')}} } // Shadowing of typed-throws Result.get() addresses a source compatibility diff --git a/test/expr/cast/optional.swift b/test/expr/cast/optional.swift index 9bd5063e7b0c3..825f3481d1b53 100644 --- a/test/expr/cast/optional.swift +++ b/test/expr/cast/optional.swift @@ -45,5 +45,6 @@ func implicitCastOfLiteralToOptional() { // https://github.com/apple/swift/issues/46093 func castUnrelatedOptionalTypes(x: Int?) { let _ = x as String? // expected-error {{cannot convert value}} + // expected-note@-1 {{arguments to generic parameter 'Wrapped' ('Int' and 'String') are expected to be equal}} let _ = x as? String? // no-warning } diff --git a/test/expr/conversions/RawRepresentable_fixits.swift b/test/expr/conversions/RawRepresentable_fixits.swift index 84ff6fd566bb5..5298666f9b510 100644 --- a/test/expr/conversions/RawRepresentable_fixits.swift +++ b/test/expr/conversions/RawRepresentable_fixits.swift @@ -85,22 +85,28 @@ do { // FIXME: No fix-it. Could be 'int_o.map { Mask(rawValue: UInt64($0)) }'. let _: Mask? = int_o - // expected-error@-1:20 {{cannot convert value of type 'Int?' to specified type 'Mask?'}}{{none}} + // expected-error@-1:20 {{cannot assign value of type 'Int?' to type 'Mask?'}}{{none}} + // expected-note@-2 {{arguments to generic parameter 'Wrapped' ('Int' and 'Mask') are expected to be equal}} takeMask_o(int_o) // expected-error@-1:16 {{cannot convert value of type 'Int?' to expected argument type 'Mask?'}}{{none}} + // expected-note@-2 {{arguments to generic parameter 'Wrapped' ('Int' and 'Mask') are expected to be equal}} // FIXME: No fix-it. Could be 'uint64_o.map(Mask(rawValue:))' or 'uint64_o.map { Mask(rawValue: $0) }'. let _: Mask? = uint64_o - // expected-error@-1:20 {{cannot convert value of type 'UInt64?' to specified type 'Mask?'}}{{none}} + // expected-error@-1:20 {{cannot assign value of type 'UInt64?' to type 'Mask?'}}{{none}} + // expected-note@-2 {{arguments to generic parameter 'Wrapped' ('UInt64' and 'Mask') are expected to be equal}} takeMask_o(uint64_o) // expected-error@-1:16 {{cannot convert value of type 'UInt64?' to expected argument type 'Mask?'}}{{none}} + // expected-note@-2 {{arguments to generic parameter 'Wrapped' ('UInt64' and 'Mask') are expected to be equal}} // FIXME: No fix-it. Could be '(anything as? Int).map { Mask(rawValue: UInt64($0)) }'. let anything: Any let _: Mask? = anything as? Int - // expected-error@-1:29 {{cannot convert value of type 'Int?' to specified type 'Mask?'}}{{none}} + // expected-error@-1:29 {{cannot assign value of type 'Int?' to type 'Mask?'}}{{none}} + // expected-note@-2 {{arguments to generic parameter 'Wrapped' ('Int' and 'Mask') are expected to be equal}} takeMask_o(anything as? Int) // expected-error@-1:25 {{cannot convert value of type 'Int?' to expected argument type 'Mask?'}}{{none}} + // expected-note@-2 {{arguments to generic parameter 'Wrapped' ('Int' and 'Mask') are expected to be equal}} } // Try a nested OptionSet. @@ -162,15 +168,18 @@ do { // FIXME: No fix-it. Could be 'mask_o.map { Int($0.rawValue) }'. let _: Int? = mask_o - // expected-error@-1:19 {{cannot convert value of type 'Mask?' to specified type 'Int?'}}{{none}} + // expected-error@-1:19 {{cannot assign value of type 'Mask?' to type 'Int?'}}{{none}} + // expected-note@-2 {{arguments to generic parameter 'Wrapped' ('Mask' and 'Int') are expected to be equal}} takeInt_o(mask_o) // expected-error@-1:15 {{cannot convert value of type 'Mask?' to expected argument type 'Int?'}}{{none}} + // expected-note@-2 {{arguments to generic parameter 'Wrapped' ('Mask' and 'Int') are expected to be equal}} // FIXME: No fix-it. Could be 'mask_o?.rawValue' or 'mask_o.map { $0.rawValue }'. let _: UInt64? = mask_o // expected-error@-1:22 {{cannot convert value of type 'Mask?' to specified type 'UInt64?'}}{{28-28=?.rawValue}} takeUInt64_o(mask_o) // expected-error@-1:18 {{cannot convert value of type 'Mask?' to expected argument type 'UInt64?'}}{{none}} + // expected-note@-2 {{arguments to generic parameter 'Wrapped' ('Mask' and 'UInt64') are expected to be equal}} } } @@ -202,7 +211,7 @@ do { takeAnyObject(iuo) // expected-error@-1:19 {{argument type 'ClassWrapper?' expected to be an instance of a class or class-constrained type}}{{none}} takeAnyObjectOpt(iuo) - // expected-error@-1:22 {{argument type 'ClassWrapper' expected to be an instance of a class or class-constrained type}}{{none}} + // expected-error@-1:22 {{argument type 'ClassWrapper?' expected to be an instance of a class or class-constrained type}}{{none}} let _: ClassWrapper = subClass // expected-error@-1:27 {{cannot convert value of type 'SubClass' to specified type 'ClassWrapper'}}{{27-27=ClassWrapper(rawValue: }} {{35-35=) ?? <#default value#>}} diff --git a/test/expr/expressions.swift b/test/expr/expressions.swift index 1e90eaad80b38..b30d808a56264 100644 --- a/test/expr/expressions.swift +++ b/test/expr/expressions.swift @@ -794,7 +794,7 @@ func testNilCoalescePrecedence(cond: Bool, a: Int?, r: ClosedRange?) { // ?? should have lower precedence than range and arithmetic operators. let r1 = r ?? (0...42) // ok - let r2 = (r ?? 0)...42 // not ok: expected-error {{binary operator '??' cannot be applied to operands of type 'ClosedRange?' and 'Int'}} + let r2 = (r ?? 0)...42 // not ok: expected-error {{cannot convert value of type 'ClosedRange?' to expected argument type 'Int?'}} let r3 = r ?? 0...42 // parses as the first one, not the second. diff --git a/test/stdlib/UnsafePointerDiagnostics.swift b/test/stdlib/UnsafePointerDiagnostics.swift index 0a3cbf8ba9800..eb843b30622ed 100644 --- a/test/stdlib/UnsafePointerDiagnostics.swift +++ b/test/stdlib/UnsafePointerDiagnostics.swift @@ -78,7 +78,7 @@ func unsafePointerConversionAvailability( _ = UnsafeMutablePointer(rp) // expected-error {{cannot convert value of type 'UnsafeRawPointer' to expected argument type 'UnsafeMutablePointer'}} _ = UnsafeMutablePointer(mrp) // expected-error {{cannot convert value of type 'UnsafeMutableRawPointer' to expected argument type 'UnsafeMutablePointer'}} - // Two candidates here: OpaquePointer? and UnsafeMutablePointer? + // This is ambiguous because we have failable and non-failable initializers that accept the same argument type. _ = UnsafeMutablePointer(orp) // expected-error {{no exact matches in call to initializer}} // Two candidates here: OpaquePointer? and UnsafeMutablePointer? _ = UnsafeMutablePointer(omrp) // expected-error {{no exact matches in call to initializer}} @@ -87,7 +87,6 @@ func unsafePointerConversionAvailability( _ = UnsafePointer(mrp) // expected-error {{cannot convert value of type 'UnsafeMutableRawPointer' to expected argument type 'UnsafePointer'}} // Two candidates here: OpaquePointer? and UnsafeMutablePointer? _ = UnsafePointer(orp) // expected-error {{no exact matches in call to initializer}} - // Two candidates here: OpaquePointer? and UnsafeMutablePointer? _ = UnsafePointer(omrp) // expected-error {{no exact matches in call to initializer}} _ = UnsafePointer(ups) // expected-error {{cannot convert value of type 'UnsafePointer' to expected argument type 'UnsafePointer'}} @@ -126,6 +125,7 @@ func unsafeRawBufferPointerConversions( _ = UnsafeMutableRawBufferPointer(start: omrp, count: 1) _ = UnsafeRawBufferPointer(start: omrp, count: 1) _ = UnsafeMutableRawBufferPointer(start: orp, count: 1) // expected-error {{cannot convert value of type 'UnsafeRawPointer?' to expected argument type 'UnsafeMutableRawPointer?'}} + // expected-note@-1 {{arguments to generic parameter 'Wrapped' ('UnsafeRawPointer' and 'UnsafeMutableRawPointer') are expected to be equal}} _ = UnsafeRawBufferPointer(start: orp, count: 1) } diff --git a/test/stmt/if_while_var.swift b/test/stmt/if_while_var.swift index ea5a9e347231e..3b620819ca44d 100644 --- a/test/stmt/if_while_var.swift +++ b/test/stmt/if_while_var.swift @@ -64,7 +64,8 @@ if case let x? = nonOptionalEnum() { _ = x } // expected-error{{'?' pattern cann if let x { _ = x } // expected-error{{cannot find 'x' in scope}} if let optional: String { _ = optional } -if let optional: Int { _ = optional } // expected-error{{cannot convert value of type 'String?' to specified type 'Int?'}} +if let optional: Int { _ = optional } // expected-error{{cannot assign value of type 'String?' to type 'Int?'}} +// expected-note@-1 {{arguments to generic parameter 'Wrapped' ('String' and 'Int') are expected to be equal}} class B {} // expected-note * {{did you mean 'B'?}} class D : B {}// expected-note * {{did you mean 'D'?}} diff --git a/validation-test/compiler_crashers_2_fixed/issue-54093.swift b/validation-test/compiler_crashers_2_fixed/issue-54093.swift index c0d59f21127aa..55c766ead83b8 100644 --- a/validation-test/compiler_crashers_2_fixed/issue-54093.swift +++ b/validation-test/compiler_crashers_2_fixed/issue-54093.swift @@ -13,11 +13,12 @@ class Test1 { } @propertyWrapper -struct Wrapper2 { // expected-note {{property wrapper type 'Wrapper2' declared here}} +struct Wrapper2 { var wrappedValue: Int?? } class Test2 { @Wrapper2 var user: Int? - // expected-error@-1 {{property type 'Int?' does not match that of the 'wrappedValue' property of its wrapper type 'Wrapper2'}} + // expected-error@-1 {{property type 'Int?' does not match 'wrappedValue' type 'Int??'}} + // expected-note@-2 {{arguments to generic parameter 'Wrapped' ('Int' and 'Int?') are expected to be equal}} }