Skip to content

[ConstraintSystem] Fix not warn about some redundant coercions #27881

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

Closed
Closed
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
7 changes: 7 additions & 0 deletions lib/Sema/CSFix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,13 @@ bool RemoveUnnecessaryCoercion::attempt(ConstraintSystem &cs, Type fromType,
if (!isa<ImplicitlyUnwrappedOptionalTypeRepr>(toTypeRepr) &&
(isa<DeclRefExpr>(expr->getSubExpr()) ||
isa<CoerceExpr>(expr->getSubExpr()))) {

// If coerced type is not known upfront let's not warn
// because the type could be inferred from coercion.
auto coercedType = cs.getType(expr->getSubExpr());
if (coercedType->is<TypeVariableType>())
return false;

auto *fix = new (cs.getAllocator()) RemoveUnnecessaryCoercion(
cs, fromType, toType, cs.getConstraintLocator(locator));

Expand Down
4 changes: 2 additions & 2 deletions test/ClangImporter/Security_test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import Security

_ = kSecClass as CFString // expected-warning {{redundant cast to 'CFString' has no effect}} {{15-27=}}
_ = kSecClassGenericPassword as CFString // expected-warning {{redundant cast to 'CFString' has no effect}} {{30-42=}}
_ = kSecClass as CFString
_ = kSecClassGenericPassword as CFString
_ = kSecClassGenericPassword as CFDictionary // expected-error {{'CFString?' is not convertible to 'CFDictionary'}} {{30-32=as!}}

func testIntegration() {
Expand Down
2 changes: 1 addition & 1 deletion test/Constraints/casts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ var c2f2: C2<[Float]>? = b as! C3


// <rdar://problem/15633178>
var f: (Float) -> Float = { $0 as Float } // expected-warning {{redundant cast to 'Float' has no effect}} {{32-41=}}
var f: (Float) -> Float = { $0 as Float }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't merge my PR because I think this case is supposed to warn, $0 is a float based on contextual type, it can't be anything else...

var f2: (B) -> Bool = { $0 is D }

func metatype_casts<T, U>(_ b: B.Type, t:T.Type, u: U.Type) {
Expand Down
4 changes: 4 additions & 0 deletions test/expr/cast/as_coerce.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,7 @@ typealias Double2 = Double
let sr11295ta1: Double1 = 1.0
_ = sr11295ta1 as Double2 // expected-warning {{redundant cast from 'Double1' (aka 'Double') to 'Double2' (aka 'Double') has no effect}} {{16-27=}}
_ = sr11295ta1 as Double1 // expected-warning {{redundant cast to 'Double1' (aka 'Double') has no effect}} {{16-27=}}

// rdar://problem/56608085
var f56608085: (Float) -> Float = { $0 as Float } // Ok
var f56608085_1 = { $0 as Float } // Ok