New Diagnostic Architecture Overview
swift.org
swift.org
Past the breaking point. SwiftUI brought several new language features just to make it possible.
type of expression is ambiguous without more context
on a straightforward Text() expression halfway up the file). The only way to find the actual source of the error is to undo every change until the error disappears, and coupled with sluggish compilation times this becomes a real pain. I hope they address this issue as part of their improvements.I guess SwiftUI exacerbates the problem since expressions are necessarily very deeply nested.
It seems to be a problematic diagnostic they have particularly focused on resolving.
The article describes the mental model needed to understand the operation of the swift type error diagnostic system with a high level of clarity.
Thinking through how this system works provides a super valuable learning experience for a wide audience of readers.
fn main() {
let x = "a" + "b";
}
which is invalid Rust due to reasons, but the compiler emits the following output[1]: error[E0369]: binary operation `+` cannot be applied to type `&str`
--> src/main.rs:2:17
|
2 | let x = "a" + "b";
| --- ^ --- &str
| | |
| | `+` cannot be used to concatenate two `&str` strings
| &str
help: `to_owned()` can be used to create an owned `String` from a string reference. String concatenation appends the string on the right to the string on the left and may require reallocation. This requires ownership of the string on the left
|
2 | let x = "a".to_owned() + "b";
| ^^^^^^^^^^^^^^
Is it perfect? No, it is verbose and you could make the case that the language should be extended to accept the code as is. Because we have design constraints that don't let us accept the code, we've extended the compiler to support the construct in order to provide the fixed code, while simultaneously allowing the language to conform to its constraints. The hard part of this is that now your compiler is as complex as it would have to be to support the superset language. Also, the less negative space there's in the language, namely, the more expressive it is, the less you can apply this strategyI am reminded of a recent tweet: "We spent decades trying to invent a sufficiently smart compiler when we should have been inventing a sufficiently empathetic one"[2]
Edit: if you have to Google to understand what the compiler is saying, I consider that a bug. File tickets for confusing diagnostics!
[1]: https://play.rust-lang.org/?version=nightly&mode=debug&editi...
[2]: https://twitter.com/mostlysafe/status/1176170363266449410
I this it's a great idea and with new diagnostic architecture described in the blog post it would be relatively easy to archive.
error: binary operator '+' cannot be applied to operands of type 'String' and 'Int'
_ = "a" + 1
~~~ ^ ~
note: overloads for '+' exist with these partially matching parameter lists: (Int, Int), (String, String)_ = "a" + 1
^
note: candidate expects value of type 'String' for parameter #2 @inlinable public static func + (lhs: String, rhs: String) -> String
^
note: candidate expects value of type 'Int' for parameter #1 public static func + (lhs: Int, rhs: Int) -> Int
^