rust-lang/rust — pull request #19870 by mdinger (2014-12-15 09:15:14+00:00)
Title: Multi-line errors

This is a continuation of #19203 which I apparently broke by force pushing after it was closed. I'm attempting to add multi-line errors where they are largely beneficial - to help differentiate different types in compiler messages. As before, this is still a simple fix.

#### Testcase:
```rust
struct S;

fn test() -> Option {
let s: S;

s
}

fn test2() -> Option {
Ok(7) // Should be Some(7)
}

impl Iterator for S {
fn next(&mut self) -> Result { Ok(7) }
}

fn main(){
test();
test2();
}
```

---

#### Single-line playpen errors:
```cmd
:6:5: 6:6 error: mismatched types: expected `core::option::Option`, found `S` (expected enum core::option::Option, found struct S)
:6 s
^
:10:5: 10:10 error: mismatched types: expected `core::option::Option`, found `core::result::Result<_, _>` (expected enum core::option::Option, found enum core::result::Result)
:10 Ok(7) // Should be Some(7)
^~~~~
:14:5: 14:55 error: method `next` has an incompatible type for trait: expected enum core::option::Option, found enum core::result::Result [E0053]
:14 fn next(&mut self) -> Result { Ok(7) }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to 3 previous errors
playpen: application terminated with error code 101
```

---

#### Multi-line errors:
```cmd
$ ./rustc test.rs
test.rs:6:5: 6:6 error: mismatched types:
expected `core::option::Option`,
found `S`
(expected enum `core::option::Option`,
found struct `S`)
test.rs:6 s
^
test.rs:10:5: 10:10 error: mismatched types:
expected `core::option::Option`,
found `core::result::Result<_, _>`
(expected enum `core::option::Option`,
found enum `core::result::Result`)
test.rs:10 Ok(7) // Should be Some(7)
^~~~~
test.rs:14:5: 14:55 error: method `next` has an incompatible type for trait:
expected enum `core::option::Option`,
found enum `core::result::Result` [E0053]
test.rs:14 fn next(&mut self) -> Result { Ok(7) }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to 3 previous errors
```

---

#### Positive notes
* Vim worked fine with it: https://github.com/rust-lang/rust/pull/19203#issuecomment-66861668
* `make check` didn't find any errors
* Fixed *backtick* placement suggested by @p1start at https://github.com/rust-lang/rust/pull/19203#issuecomment-64062052

#### Negative notes
* Didn't check Emacs support but also wasn't provided a testcase...
* Needs to be tested with macro errors but I don't have a good testcase yet
* I would like to move the `E[0053]` earlier (see https://github.com/rust-lang/rust/issues/19464#issuecomment-65334301) but I don't know how
* It might be better to indent the types slightly like so (but I don't know how):
```cmd
test.rs:6:5: 6:6 error: mismatched types:
expected `core::option::Option`,
found `S`
(expected enum `core::option::Option`,
found struct `S`)
test.rs:6 s
```

#### Other
* I thought that compiler flags or something else (environment variables maybe) might be required because of comments against it but now that seems too much of a burden for users and for too little gain.
* There was concern that it will make large quantities of errors difficult to distinguish but I don't find that an issue. They both look awful and multi-line errors makes the types easier to understand.

---

#### Single lined spew:
```cmd
$ rustc test2.rs
test2.rs:161:9: 170:10 error: method `next` has an incompatible type for trait: expected enum core::option::Option, found enum core::result::Result [E0053]
test2.rs:161 fn next(&mut self) -> Result<&'a str, int> {
test2.rs:162 self.curr = self.next;
test2.rs:163
test2.rs:164 if let (Some(open), Some(close)) = Parens::find_parens(self.all, self.next) {
test2.rs:165 self.next = if self.all.char_at(self.next) == '(' { close }
test2.rs:166 else { open }
...
test2.rs:164:21: 164:31 error: mismatched types: expected `core::result::Result`, found `core::option::Option<_>` (expected enum core::result::Result, found enum core::option::Option)
test2.rs:164 if let (Some(open), Some(close)) = Parens::find_parens(self.all, self.next) {
^~~~~~~~~~
test2.rs:164:33: 164:44 error: mismatched types: expected `core::result::Result`, found `core::option::Option<_>` (expected enum core::result::Result, found enum core::option::Option)
test2.rs:164 if let (Some(open), Some(close)) = Parens::find_parens(self.all, self.next) {
^~~~~~~~~~~
test2.rs:169:40: 169:76 error: mismatched types: expected `core::result::Result<&'a str, int>`, found `core::option::Option<&str>` (expected enum core::result::Result, found enum core::option::Option)
test2.rs:169 if self.curr != self.len { Some(self.all[self.curr..self.next]) } else { None }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test2.rs:169:86: 169:90 error: mismatched types: expected `core::result::Result<&'a str, int>`, found `core::option::Option<_>` (expected enum core::result::Result, found enum core::option::Option)
test2.rs:169 if self.curr != self.len { Some(self.all[self.curr..self.next]) } else { None }
^~~~
test2.rs:205:14: 205:18 error: mismatched types: expected `core::result::Result`, found `core::option::Option` (expected enum core::result::Result, found enum core::option::Option)
test2.rs:205 (open, close)
^~~~
test2.rs:205:20: 205:25 error: mismatched types: expected `core::result::Result`, found `core::option::Option` (expected enum core::result::Result, found enum core::option::Option)
test2.rs:205 (open, close)
^~~~~
test2.rs:210:21: 210:31 error: mismatched types: expected `core::result::Result`, found `core::option::Option<_>` (expected enum core::result::Result, found enum core::option::Option)
test2.rs:210 if let (Some(open), _) = Parens::find_parens(self.all, 0) {
^~~~~~~~~~
test2.rs:210:13: 212:28 error: mismatched types: expected `core::option::Option<&'a int>`, found `core::option::Option<&str>` (expected int, found str)
test2.rs:210 if let (Some(open), _) = Parens::find_parens(self.all, 0) {
test2.rs:211 Some(self.all[0..open])
test2.rs:212 } else { None }
test2.rs:299:48: 299:58 error: mismatched types: expected `Box`, found `collections::vec::Vec<_>` (expected box, found struct collections::vec::Vec)
test2.rs:299 pub fn new() -> Entity { Entity::Group(Vec::new()) }
^~~~~~~~~~
test2.rs:359:51: 359:58 error: type `&mut Box` does not implement any method in scope named `push`
test2.rs:359 Entity::Group(ref mut vec) => vec.push(e),
^~~~~~~
test2.rs:366:51: 366:85 error: type `&mut Box` does not implement any method in scope named `push`
test2.rs:366 Entity::Group(ref mut vec) => vec.push(Entity::Inner(s.to_string())),
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to 12 previous errors
```

---

#### Multi-line spew:

```cmd
$ ./rustc test2.rs
test2.rs:161:9: 170:10 error: method `next` has an incompatible type for trait:
expected enum `core::option::Option`,
found enum `core::result::Result` [E0053]
test2.rs:161 fn next(&mut self) -> Result<&'a str, int> {
test2.rs:162 self.curr = self.next;
test2.rs:163
test2.rs:164 if let (Some(open), Some(close)) = Parens::find_parens(self.all, self.next) {
test2.rs:165 self.next = if self.all.char_at(self.next) == '(' { close }
test2.rs:166 else { open }
...
test2.rs:164:21: 164:31 error: mismatched types:
expected `core::result::Result`,
found `core::option::Option<_>`
(expected enum `core::result::Result`,
found enum `core::option::Option`)
test2.rs:164 if let (Some(open), Some(close)) = Parens::find_parens(self.all, self.next) {
^~~~~~~~~~
test2.rs:164:33: 164:44 error: mismatched types:
expected `core::result::Result`,
found `core::option::Option<_>`
(expected enum `core::result::Result`,
found enum `core::option::Option`)
test2.rs:164 if let (Some(open), Some(close)) = Parens::find_parens(self.all, self.next) {
^~~~~~~~~~~
test2.rs:169:40: 169:76 error: mismatched types:
expected `core::result::Result<&'a str, int>`,
found `core::option::Option<&str>`
(expected enum `core::result::Result`,
found enum `core::option::Option`)
test2.rs:169 if self.curr != self.len { Some(self.all[self.curr..self.next]) } else { None }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test2.rs:169:86: 169:90 error: mismatched types:
expected `core::result::Result<&'a str, int>`,
found `core::option::Option<_>`
(expected enum `core::result::Result`,
found enum `core::option::Option`)
test2.rs:169 if self.curr != self.len { Some(self.all[self.curr..self.next]) } else { None }
^~~~
test2.rs:205:14: 205:18 error: mismatched types:
expected `core::result::Result`,
found `core::option::Option`
(expected enum `core::result::Result`,
found enum `core::option::Option`)
test2.rs:205 (open, close)
^~~~
test2.rs:205:20: 205:25 error: mismatched types:
expected `core::result::Result`,
found `core::option::Option`
(expected enum `core::result::Result`,
found enum `core::option::Option`)
test2.rs:205 (open, close)
^~~~~
test2.rs:210:21: 210:31 error: mismatched types:
expected `core::result::Result`,
found `core::option::Option<_>`
(expected enum `core::result::Result`,
found enum `core::option::Option`)
test2.rs:210 if let (Some(open), _) = Parens::find_parens(self.all, 0) {
^~~~~~~~~~
test2.rs:210:13: 212:28 error: mismatched types:
expected `core::option::Option<&'a int>`,
found `core::option::Option<&str>`
(expected int,
found str)
test2.rs:210 if let (Some(open), _) = Parens::find_parens(self.all, 0) {
test2.rs:211 Some(self.all[0..open])
test2.rs:212 } else { None }
test2.rs:229:57: 229:96 error: the trait `core::ops::Fn<(char,), bool>` is not implemented for the type `|char| -> bool`
test2.rs:229 .map(|s| s.trim_chars(|c: char| c.is_whitespace()))
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test2.rs:238:46: 239:75 error: type `core::str::CharSplits<'_, |char| -> bool>` does not implement any method in scope named `filter_map`
test2.rs:238 .filter_map(|s| if !s.is_empty() { Some(s.trim_chars('\'')) }
test2.rs:239 else { None })
test2.rs:237:46: 237:91 error: the trait `core::ops::Fn<(char,), bool>` is not implemented for the type `|char| -> bool`
test2.rs:237 let vec: Vec<&str> = value[].split(|c: char| matches!(c, '(' | ')' | ','))
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test2.rs:238:65: 238:77 error: the type of this value must be known in this context
test2.rs:238 .filter_map(|s| if !s.is_empty() { Some(s.trim_chars('\'')) }
^~~~~~~~~~~~
test2.rs:299:48: 299:58 error: mismatched types:
expected `Box`,
found `collections::vec::Vec<_>`
(expected box,
found struct `collections::vec::Vec`)
test2.rs:299 pub fn new() -> Entity { Entity::Group(Vec::new()) }
^~~~~~~~~~
test2.rs:321:36: 322:65 error: type `core::str::CharSplits<'_, |char| -> bool>` does not implement any method in scope named `filter_map`
test2.rs:321 .filter_map(|s| if !s.is_empty() { Some(s.trim_chars('\'')) }
test2.rs:322 else { None })
test2.rs:320:36: 320:81 error: the trait `core::ops::Fn<(char,), bool>` is not implemented for the type `|char| -> bool`
test2.rs:320 let vec: Vec<&str> = s.split(|c: char| matches!(c, '(' | ')' | ','))
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test2.rs:321:55: 321:67 error: the type of this value must be known in this context
test2.rs:321 .filter_map(|s| if !s.is_empty() { Some(s.trim_chars('\'')) }
^~~~~~~~~~~~
test2.rs:359:51: 359:58 error: type `&mut Box` does not implement any method in scope named `push`
test2.rs:359 Entity::Group(ref mut vec) => vec.push(e),
^~~~~~~
test2.rs:366:51: 366:85 error: type `&mut Box` does not implement any method in scope named `push`
test2.rs:366 Entity::Group(ref mut vec) => vec.push(Entity::Inner(s.to_string())),
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to 24 previous errors
```

Closes #18946 #19464
cc @P1start @jakub- @tomjakubowski @kballard @chris-morgan