about summary refs log tree commit diff
path: root/src/librustc_errors
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2016-09-07 18:40:31 -0700
committerEsteban Küber <esteban@kuber.com.ar>2016-09-15 20:06:29 -0700
commit68e8624d05bc9b291fc3d945aaf5c1cb24bf015f (patch)
tree4ab82df7b25b87e929bdd75af2b4c515a2499bf7 /src/librustc_errors
parentc87ba3f1222ba20d491e8ed76a04977283280742 (diff)
downloadrust-68e8624d05bc9b291fc3d945aaf5c1cb24bf015f.tar.gz
rust-68e8624d05bc9b291fc3d945aaf5c1cb24bf015f.zip
Specify when type parameter shadows primitive type
When a type parameter shadows a primitive type, the error message
was non obvious. For example, given the file `file.rs`:

```rust
trait Parser<T> {
    fn parse(text: &str) -> Option<T>;
}

impl<bool> Parser<bool> for bool {
    fn parse(text: &str) -> Option<bool> {
        Some(true)
    }
}

fn main() {
    println!("{}", bool::parse("ok").unwrap_or(false));
}
```

The output was:

```bash
% rustc file.rs
error[E0308]: mismatched types
 --> file.rs:7:14
  |
7 |         Some(true)
  |              ^^^^ expected type parameter, found bool
  |
  = note: expected type `bool`
  = note:    found type `bool`

error: aborting due to previous error
```

We now show extra information about the type:

```bash
% rustc file.rs
error[E0308]: mismatched types
 --> file.rs:7:14
  |
7 |         Some(true)
  |              ^^^^ expected type parameter, found bool
  |
  = note: expected type `bool` (type parameter)
  = note:    found type `bool` (bool)

error: aborting due to previous error
```

Fixes #35030
Diffstat (limited to 'src/librustc_errors')
-rw-r--r--src/librustc_errors/lib.rs17
1 files changed, 14 insertions, 3 deletions
diff --git a/src/librustc_errors/lib.rs b/src/librustc_errors/lib.rs
index d82d7dbe70f..d2f3eea85f2 100644
--- a/src/librustc_errors/lib.rs
+++ b/src/librustc_errors/lib.rs
@@ -274,9 +274,20 @@ impl<'a> DiagnosticBuilder<'a> {
                                found: &fmt::Display)
                                -> &mut DiagnosticBuilder<'a>
     {
+        self.note_expected_found_extra(label, expected, found, &"", &"")
+    }
+
+    pub fn note_expected_found_extra(&mut self,
+                                     label: &fmt::Display,
+                                     expected: &fmt::Display,
+                                     found: &fmt::Display,
+                                     expected_extra: &fmt::Display,
+                                     found_extra: &fmt::Display)
+                                     -> &mut DiagnosticBuilder<'a>
+    {
         // For now, just attach these as notes
-        self.note(&format!("expected {} `{}`", label, expected));
-        self.note(&format!("   found {} `{}`", label, found));
+        self.note(&format!("expected {} `{}`{}", label, expected, expected_extra));
+        self.note(&format!("   found {} `{}`{}", label, found, found_extra));
         self
     }
 
@@ -764,4 +775,4 @@ pub fn expect<T, M>(diag: &Handler, opt: Option<T>, msg: M) -> T where
         Some(t) => t,
         None => diag.bug(&msg()),
     }
-}
\ No newline at end of file
+}