remove the Failure::IsNotOptional failure mode, as CSDiags does a better job

of providing contextual diagnostics (e.g. producing the warning in 
Constraints/dynamic_lookup.swift).  This drops a specific diagnostic about
force casting the result of as! which was added in the Swift 1.2 timeframe
to explain the change in cast semantics.  Now that as! has been around for
a long time, it is more confusing than helpful.



Swift SVN r31887
This commit is contained in:
Chris Lattner
2015-09-11 04:40:13 +00:00
parent ebe67f30ea
commit f8c6e46e03
8 changed files with 15 additions and 59 deletions

View File

@@ -1685,10 +1685,6 @@ ERROR(binding_explicit_downcast,sema_tce,none,
"operand of postfix '?' is a forced downcast to type %0; use 'as?' to "
"perform a conditional downcast", (Type))
ERROR(forcing_explicit_downcast,sema_tce,none,
"extraneous postfix '!'; forced downcast already produces a non-optional "
"value of type %0", (Type))
WARNING(inject_forced_downcast,sema_tce,none,
"treating a forced downcast to %0 as optional will never produce 'nil'",
(Type))

View File

@@ -41,10 +41,6 @@ void Failure::dump(SourceManager *sm, raw_ostream &out) const {
out << getFirstType().getString() << "is not bridged to Objective-C";
break;
case IsNotOptional:
out << getFirstType().getString() << "is not an optional type";
break;
case IsForbiddenLValue:
out << "disallowed l-value binding of " << getFirstType().getString()
<< " and " << getSecondType().getString();
@@ -717,36 +713,6 @@ static bool diagnoseFailure(ConstraintSystem &cs, Failure &failure,
return false;
}
case Failure::IsNotOptional: {
if (auto force = dyn_cast_or_null<ForceValueExpr>(anchor)) {
// If there was an 'as' cast in the subexpression, note it.
if (auto *cast = findForcedDowncast(tc.Context, force->getSubExpr())) {
tc.diagnose(force->getLoc(), diag::forcing_explicit_downcast,
failure.getFirstType())
.highlight(cast->getLoc())
.fixItRemove(force->getLoc());
return true;
}
tc.diagnose(loc, diag::invalid_force_unwrap,
failure.getFirstType())
.highlight(force->getSourceRange())
.fixItRemove(force->getExclaimLoc());
return true;
}
if (auto bind = dyn_cast_or_null<BindOptionalExpr>(anchor)) {
tc.diagnose(loc, diag::invalid_optional_chain,
failure.getFirstType())
.highlight(bind->getSourceRange())
.fixItRemove(bind->getQuestionLoc());
return true;
}
return false;
}
case Failure::NoPublicInitializers: {
tc.diagnose(loc, diag::no_accessible_initializers, failure.getFirstType())
.highlight(range);

View File

@@ -2570,16 +2570,12 @@ ConstraintSystem::simplifyOptionalObjectConstraint(const Constraint &constraint)
// If the base type is not optional, the constraint fails.
Type objectTy = optTy->getAnyOptionalObjectType();
if (!objectTy) {
recordFailure(constraint.getLocator(), Failure::IsNotOptional,
optTy);
if (!objectTy)
return SolutionKind::Error;
}
// The object type is an lvalue if the optional was.
if (optLValueTy->is<LValueType>()) {
if (optLValueTy->is<LValueType>())
objectTy = LValueType::get(objectTy);
}
// Equate it to the other type in the constraint.
addConstraint(ConstraintKind::Bind, objectTy, constraint.getSecondType(),

View File

@@ -363,8 +363,6 @@ public:
enum FailureKind {
/// \brief The type is not bridged to an Objective-C type.
IsNotBridgedToObjectiveC,
/// \brief The type is not an optional type.
IsNotOptional,
/// \brief The type is not allowed to be an l-value.
IsForbiddenLValue,
/// Out-of-order arguments.
@@ -441,7 +439,6 @@ public:
getSecondType());
case IsNotBridgedToObjectiveC:
case IsNotOptional:
case MissingArgument:
case NoPublicInitializers:
return Profile(id, locator, kind, resolvedOverloadSets, getFirstType(),

View File

@@ -205,7 +205,8 @@ obj.dynamicType.foo!(obj)(5) // expected-error{{instance member 'foo' cannot be
// Checked casts to AnyObject
var p: P = Y()
var obj3 : AnyObject = (p as! AnyObject)! // expected-error{{extraneous postfix '!'}} {{41-42=}}
// expected-warning @+1 {{forced cast from 'P' to 'AnyObject' always succeeds; did you mean to use 'as'?}}
var obj3 : AnyObject = (p as! AnyObject)! // expected-error{{cannot force unwrap value of non-optional type 'AnyObject'}} {{41-42=}}
// Implicit force of an implicitly unwrapped optional
let uopt : AnyObject! = nil

View File

@@ -62,4 +62,4 @@ class C {}
class D: C {}
let c = C()
let d = (c as! D)! // expected-error{{forced downcast already produces a non-optional value}} {{18-19=}}
let d = (c as! D)! // expected-error{{cannot force unwrap value of non-optional type 'D'}} {{18-19=}}

View File

@@ -55,7 +55,7 @@ class Sub : Super {
}
convenience init(forceNonfail: Int) {
self.init(nonfail: forceNonfail)! // expected-error{{cannot force unwrap value of non-optional type 'Sub'}} {{37-38=}}
self.init(nonfail: forceNonfail)! // expected-error{{cannot force unwrap value of non-optional type '()'}} {{37-38=}}
}
init(nonfail2: Int) { // okay, traps on nil

View File

@@ -13,19 +13,19 @@
import Foundation
func testDowncastObjectToArray(obj: AnyObject, objImplicit: AnyObject!) {
var nsstrArr1 = (obj as! [NSString])! // expected-error{{extraneous postfix '!'; forced downcast already produces a non-optional value of type '[NSString]'}}{{39-40=}}
var strArr1 = (obj as! [String])! // expected-error{{extraneous postfix '!'; forced downcast already produces a non-optional value of type '[String]'}}{{35-36=}}
var nsstrArr1 = (obj as! [NSString])! // expected-error{{cannot force unwrap value of non-optional type '[NSString]'}}{{39-40=}}
var strArr1 = (obj as! [String])! // expected-error{{cannot force unwrap value of non-optional type '[String]'}}{{35-36=}}
var nsstrArr2 = (objImplicit as! [NSString])! // expected-error{{extraneous postfix '!'; forced downcast already produces a non-optional value of type '[NSString]'}}{{47-48=}}
var strArr2 = (objImplicit as! [String])! // expected-error{{extraneous postfix '!'; forced downcast already produces a non-optional value of type '[String]'}}{{43-44=}}
var nsstrArr2 = (objImplicit as! [NSString])! // expected-error{{cannot force unwrap value of non-optional type '[NSString]'}}{{47-48=}}
var strArr2 = (objImplicit as! [String])! // expected-error{{cannot force unwrap value of non-optional type '[String]'}}{{43-44=}}
}
func testArrayDowncast(arr: [AnyObject], arrImplicit: [AnyObject]!) {
var nsstrArr1 = (arr as! [NSString])! // expected-error{{extraneous postfix '!'; forced downcast already produces a non-optional value of type '[NSString]'}} {{39-40=}}
var strArr1 = (arr as! [String])! // expected-error{{extraneous postfix '!'; forced downcast already produces a non-optional value of type '[String]'}} {{35-36=}}
var nsstrArr1 = (arr as! [NSString])! // expected-error{{cannot force unwrap value of non-optional type '[NSString]'}} {{39-40=}}
var strArr1 = (arr as! [String])! // expected-error{{cannot force unwrap value of non-optional type '[String]'}} {{35-36=}}
var nsstrArr2 = (arrImplicit as! [NSString])! // expected-error{{extraneous postfix '!'; forced downcast already produces a non-optional value of type '[NSString]'}} {{47-48=}}
var strArr2 = (arrImplicit as! [String])! // expected-error{{extraneous postfix '!'; forced downcast already produces a non-optional value of type '[String]'}} {{43-44=}}
var nsstrArr2 = (arrImplicit as! [NSString])! // expected-error{{cannot force unwrap value of non-optional type '[NSString]'}} {{47-48=}}
var strArr2 = (arrImplicit as! [String])! // expected-error{{cannot force unwrap value of non-optional type '[String]'}} {{43-44=}}
}
func testDowncastNSArrayToArray(nsarray: NSArray) {