Skip to content

Commit d92b5bf

Browse files
Fix implementation
1 parent fc1da4b commit d92b5bf

File tree

3 files changed

+21
-19
lines changed

3 files changed

+21
-19
lines changed

lib/Sema/CSGen.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2659,9 +2659,7 @@ namespace {
26592659
CS.setType(expr->getCastTypeLoc(), toType);
26602660

26612661
auto fromType = CS.getType(expr->getSubExpr());
2662-
2663-
auto pathElt = LocatorPathElt(ConstraintLocator::TypeCoercion);
2664-
auto locator = CS.getConstraintLocator(expr, pathElt);
2662+
auto locator = CS.getConstraintLocator(expr);
26652663

26662664
// Add a conversion constraint for the direct conversion between
26672665
// types.

lib/Sema/CSSimplify.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2812,14 +2812,15 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
28122812
}
28132813
}
28142814

2815+
// If we are trying to perform coercion to the same type emit a warning.
28152816
if (locator.getBaseLocator()->isTypeCoercion()) {
2816-
auto expr = dyn_cast<CoerceExpr>(locator.getBaseLocator()->getAnchor());
2817-
if (!shouldSuppressDiagnostics()) {
2818-
// If we are trying to perform coercion to the same type emit a warning.
2819-
if (type1->getCanonicalType()->isEqual(type2->getCanonicalType())) {
2820-
TC.diagnose(expr->getLoc(), diag::unecessary_same_type_coercion, type2)
2821-
.fixItRemove(SourceRange(expr->getLoc(),
2822-
expr->getCastTypeLoc().getSourceRange().End));
2817+
if (auto expr = dyn_cast<CoerceExpr>(locator.getBaseLocator()->getAnchor())) {
2818+
if (!shouldSuppressDiagnostics()) {
2819+
if (type1->getCanonicalType()->isEqual(type2->getCanonicalType())) {
2820+
TC.diagnose(expr->getLoc(), diag::unecessary_same_type_coercion, type2)
2821+
.fixItRemove(SourceRange(expr->getLoc(),
2822+
expr->getCastTypeLoc().getSourceRange().End));
2823+
}
28232824
}
28242825
}
28252826
}
@@ -7521,9 +7522,12 @@ void ConstraintSystem::addExplicitConversionConstraint(
75217522
auto locatorPtr = getConstraintLocator(locator);
75227523

75237524
// Coercion (the common case).
7525+
auto coercionPath = LocatorPathElt(ConstraintLocator::TypeCoercion);
7526+
auto coerceLocator = getConstraintLocator(locator.getBaseLocator(),
7527+
coercionPath);
75247528
Constraint *coerceConstraint =
75257529
Constraint::create(*this, ConstraintKind::Conversion,
7526-
fromType, toType, locatorPtr);
7530+
fromType, toType, coerceLocator);
75277531
coerceConstraint->setFavored();
75287532
constraints.push_back(coerceConstraint);
75297533

test/expr/cast/as_coerce.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,18 +137,18 @@ _ = sr6022_1 as! Any // expected-warning {{forced cast from '() -> ()' to 'Any'
137137
_ = sr6022_1 as? Any // expected-warning {{conditional cast from '() -> ()' to 'Any' always succeeds}}
138138

139139
// SR-11295
140-
let a = "Hello"
141-
_ = a as String // expected-warning {{casting expression to 'String' doesn't change the type}} {{7-17=}}
140+
let sr11295a = "Hello"
141+
_ = sr11295a as String // expected-warning {{casting expression to 'String' doesn't change the type}} {{7-17=}}
142142

143-
let b = 1
144-
_ = b as Int // expected-warning {{casting expression to 'Int' doesn't change the type}} {{7-14=}}
143+
let sr11295b = 1
144+
_ = sr11295b as Int // expected-warning {{casting expression to 'Int' doesn't change the type}} {{7-14=}}
145145

146146
typealias Type = String
147147

148-
let c: Type = "Hello Typealias"
149-
_ = c as String // expected-warning {{casting expression to 'String' doesn't change the type}} {{7-17=}}
148+
let sr11295c: Type = "Hello Typealias"
149+
_ = sr11295c as String // expected-warning {{casting expression to 'String' doesn't change the type}} {{7-17=}}
150150

151-
let d = "Hello Typealias"
152-
_ = d as Type // expected-warning {{casting expression to 'Type' (aka 'String') doesn't change the type}} {{7-15=}}
151+
let sr11295d = "Hello Typealias"
152+
_ = sr11295d as Type // expected-warning {{casting expression to 'Type' (aka 'String') doesn't change the type}} {{7-15=}}
153153

154154
_ = "Hello" as String // expected-warning {{casting expression to 'String' doesn't change the type}} {{13-23=}}

0 commit comments

Comments
 (0)