<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 30, 2016, at 2:07 PM, Jacob Bandes-Storch via swift-dev <<a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">This is a pretty great bug: <a href="https://bugs.swift.org/browse/SR-3483" class="">https://bugs.swift.org/browse/SR-3483</a><div class=""><div class=""><br class=""></div><div class=""><div class=""><font face="monospace, monospace" class=""> let x: Double? = 1</font></div><div class=""><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><font face="monospace, monospace" class=""> </font><span style="font-family:monospace,monospace" class="">// error: ambiguous reference to member '+'</span></div><div class=""><font face="monospace, monospace" class=""> let sum = x! + x! + x!</font></div><div class=""><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><font face="monospace, monospace" class=""> </font><span style="font-family:monospace,monospace" class="">// error: value of optional type 'Double?' not unwrapped; did you mean to use '!' or '?'?</span></div><div class=""><font face="monospace, monospace" class=""> let sum: Double = x! + x! + x!</font></div></div><div class=""><br class=""></div><div class="">I've been poking around and I think the problem might be in LinkedExprAnalyzer. </div></div></div></div></blockquote><div><br class=""></div><div>Yeah I think you’re onto something there. Just looking at the output of the constraint solver and where bindings are happening in matchTypes(), it looks like the constraint optimizer is trying to force the result type of both adds to ‘Double?’ rather than ‘Double’, so we only ever try to solve for that type.</div><div><br class=""></div><div>I haven’t looked at the constraint optimizer code in a while, so I don’t have a lot of insight into the best fix, but it’s clearly the problematic piece here.</div><div><br class=""></div><div>Mark</div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class=""><div class="">This class collects type info about elements in a chain of binary operators to speed up constraint solving. It does so in this case by grabbing the DeclRefExpr's type: <a href="https://github.com/apple/swift/tree/474096b9cbd6ff7ac998d7cea41d629512e25570#L230-L239" class="">https://github.com/apple/swift/tree/474096b9cbd6ff7ac998d7cea41d629512e25570#L230-L239</a></div><div class=""><br class=""></div><div class="">However, since this is an ASTWalker, (I think) it automatically traverses into the ForceValueExpr without remembering that the type it finds inside (from the DeclRefExpr) should have one level of optionality removed when added to the constraint system.</div><div class=""><br class=""></div><div class="">This theory sort of makes sense to me, but it doesn't explain why the simpler "<font face="monospace, monospace" class="">let sum = x! + x!</font>" typechecks correctly, because that goes through the same code path.</div><div class=""><br class=""></div><div class="">Am I correct that the LinkedExprAnalyzer probably needs to make sure it doesn't keep the Optional when adding the type of a ForceValueExpr? Why wouldn't this also cause problems for a single binop application?</div><div class=""><br class=""></div><div class="">Would it be more appropriate for LinkedExprAnalyzer to be an ASTVisitor (ExprVisitor) so that instead of just saying "yes, continue traversing downwards" (by returning {true, expr}), its ForceValueExpr case could recursively call visit() and then getAnyOptionalObjectType on the result?</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Semi-eptly,</div><div class=""><div class=""><div class="gmail_signature"><div dir="ltr" class=""><div class="">Jacob<br class=""></div></div></div></div>
</div></div></div>
_______________________________________________<br class="">swift-dev mailing list<br class=""><a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-dev<br class=""></div></blockquote></div><br class=""></div></body></html>