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
[SR-2022] Better fixit for type checking in switch statements #44631
Comments
The reported issue still occurs on Swift master as of this morning: $ cat ~/GitHub/tmp/ParserExamples/BasketCase.swift
protocol IntOrString {}
extension Int: IntOrString {}
extension String: IntOrString {}
func foo(bar: IntOrString) {
switch bar {
case let intValue as? Int: print("Hello, I'm an Int: \(intValue)")
case let stringValue as? String: print("Hello, I'm a String: \(stringValue)")
default: print("Some other value...")
}
}
$ swift -frontend -parse ~/tmp/AsQuestionMark.swift
/home/modocache/tmp/AsQuestionMark.swift:3:14: error: pattern variable binding cannot appear in an expression
case let intValue as? Int: print("Hello, I'm an Int: \(intValue)")
^
/home/modocache/tmp/AsQuestionMark.swift:3:23: warning: cast from '<<error type>>' to unrelated type 'Int' always fails
case let intValue as? Int: print("Hello, I'm an Int: \(intValue)")
~~~~~~~~ ^ ~~~
/home/modocache/tmp/AsQuestionMark.swift:3:14: error: pattern variable binding cannot appear in an expression
case let intValue as? Int: print("Hello, I'm an Int: \(intValue)")
^
/home/modocache/tmp/AsQuestionMark.swift:3:23: warning: cast from '<<error type>>' to unrelated type 'Int' always fails
case let intValue as? Int: print("Hello, I'm an Int: \(intValue)")
~~~~~~~~ ^ ~~~
/home/modocache/tmp/AsQuestionMark.swift:3:23: error: expression pattern of type 'Int?' cannot match values of type 'Int'
case let intValue as? Int: print("Hello, I'm an Int: \(intValue)")
~~~~~~~~~^~~~~~~
/home/modocache/tmp/AsQuestionMark.swift:4:14: error: pattern variable binding cannot appear in an expression
case let stringValue as? String: print("Hello, I'm a String: \(stringValue)")
^
/home/modocache/tmp/AsQuestionMark.swift:4:26: warning: cast from '<<error type>>' to unrelated type 'String' always fails
case let stringValue as? String: print("Hello, I'm a String: \(stringValue)")
~~~~~~~~~~~ ^ ~~~~~~
/home/modocache/tmp/AsQuestionMark.swift:4:14: error: pattern variable binding cannot appear in an expression
case let stringValue as? String: print("Hello, I'm a String: \(stringValue)")
^
/home/modocache/tmp/AsQuestionMark.swift:4:26: warning: cast from '<<error type>>' to unrelated type 'String' always fails
case let stringValue as? String: print("Hello, I'm a String: \(stringValue)")
~~~~~~~~~~~ ^ ~~~~~~
/home/modocache/tmp/AsQuestionMark.swift:4:26: error: expression pattern of type 'String?' cannot match values of type 'Int'
case let stringValue as? String: print("Hello, I'm a String: \(stringValue)")
~~~~~~~~~~~~^~~~~~~~~~ $ cat ~/GitHub/tmp/ParserExamples/Case.swift
protocol IntOrString {}
extension Int: IntOrString {}
extension String: IntOrString {}
func foo(bar: IntOrString) {
switch bar {
case let intValue as Int: print("Hello, I'm an Int: \(intValue)")
case let stringValue as String: print("Hello, I'm a String: \(stringValue)")
default: print("Some other value...")
}
}
$ ../build/Ninja-ReleaseAssert/swift-linux-x86_64/bin/swift -frontend -parse ~/GitHub/tmp/ParserExamples/Case.swift The output is pretty confusing. I suspect "pattern variable binding cannot appear in an expression" doesn't make a ton of sense to an end user. The fixit @karwa describes would be very helpful. |
I'd like to try my hand at adding a fix-it here. 🙂 |
On second thought, at a first glance it looks like this logic might be in Sema. I don't know a lot about those yet, so I'll drop this for now. Maybe sometime in late October. Sorry for the noise! 🙂 |
Comment by Christopher Ian Stern (JIRA) While perhaps this should be caught by the parser, the parser doesn't seem to have the context to do it. Either a bunch of parser methods would need addl. arguments or class Parser needs another InThisKindOfStructure variable. Sema only needs 20some lines of code to detect and give a clear diagnostic. |
Resetting assignee on all Starter Bugs not modified since 2018. |
Comment by Christopher Ian Stern (JIRA) Conflicts have developed between master and PR-20456, which aims to resolve this issue. Looks like the code that was to be patched in lib/Sema/TypeCheckStmt.cpp moved to a new function `visitSwitchStmt(SwitchStmt *S)`. |
Oops, looks like we missed that PR at the time. Sorry about that. |
Additional Detail from JIRA
md5: 9b53adc1e41e34c46ad044ffe4bfbac9
Issue Description:
Gives you an error on the casting lines saying "Invalid pattern". It would be better if it told you to make the casts non-conditional, I.e.:
The text was updated successfully, but these errors were encountered: